Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMS: keys refactoring #2720

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241111133703-101a74fd5b4e
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241112162134-960efd8ff98a
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.21.0
golang.org/x/sync v0.1.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241104181956-db479a6d384d h1:6nr8FpvqTw30NPORd7XIKKUW0EtYEKzWbxEO5mF/00g=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241104181956-db479a6d384d/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241111133703-101a74fd5b4e h1:/rdKqoWltx2CwxKQQ4hPxuxX6ip2JQ8lAazWTvtji3k=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241111133703-101a74fd5b4e/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241112162134-960efd8ff98a h1:MasOCdy6QVtflzeAnkx7rhR1LcpHsQIkygAJLWCHyX0=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241112162134-960efd8ff98a/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func testAccCheckKmsV1KeyDestroy(s *terraform.State) error {
if rs.Type != "opentelekomcloud_kms_key_v1" {
continue
}
v, err := keys.Get(client, rs.Primary.ID).ExtractKeyInfo()
v, err := keys.Get(client, rs.Primary.ID)
if err != nil {
return err
}
Expand All @@ -85,7 +85,7 @@ func testAccCheckKmsV1KeyExists(n string, key *keys.Key) resource.TestCheckFunc
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud KMSv1 client: %s", err)
}
found, err := keys.Get(client, rs.Primary.ID).ExtractKeyInfo()
found, err := keys.Get(client, rs.Primary.ID)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func dataSourceKmsDataKeyV1Read(_ context.Context, d *schema.ResourceData, meta
return fmterr.Errorf("error creating OpenTelekomCloud kms key client: %s", err)
}

req := &keys.DataEncryptOpts{
req := keys.DataEncryptOpts{
KeyID: d.Get("key_id").(string),
EncryptionContext: d.Get("encryption_context").(string),
DatakeyLength: d.Get("datakey_length").(string),
}
log.Printf("[DEBUG] KMS get data key for key: %s", d.Get("key_id").(string))
v, err := keys.DataEncryptGet(KmsDataKeyV1Client, req).ExtractDataKey()
v, err := keys.DataEncryptGet(KmsDataKeyV1Client, req)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func dataSourceKmsKeyV1Read(_ context.Context, d *schema.ResourceData, meta inte
nextMarker := ""
var allKeys []keys.Key
for isListKey {
req := &keys.ListOpts{
req := keys.ListOpts{
KeyState: d.Get("key_state").(string),
Marker: nextMarker,
}

v, err := keys.List(client, req).ExtractListKey()
v, err := keys.List(client, req)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,34 +134,31 @@ func resourceKmsKeyV1Create(ctx context.Context, d *schema.ResourceData, meta in
return fmterr.Errorf("error validating KMS key: %s", err)
}

createOpts := &keys.CreateOpts{
createOpts := keys.CreateOpts{
KeyAlias: d.Get("key_alias").(string),
KeyDescription: d.Get("key_description").(string),
Realm: d.Get("realm").(string),
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
key, err := keys.Create(client, createOpts).ExtractKeyInfo()
key, err := keys.Create(client, createOpts)
if err != nil {
return fmterr.Errorf("error creating OpenTelekomCloud key: %s", err)
}
log.Printf("[INFO] Key ID: %s", key.KeyID)

if d.Get("allow_cancel_deletion").(bool) {
keyGet, err := keys.Get(client, key.KeyID).ExtractKeyInfo()
keyGet, err := keys.Get(client, key.KeyID)
if err != nil {
return diag.FromErr(err)
}
if keyGet.KeyState == PendingDeletionState {
cancelDeleteOpts := keys.CancelDeleteOpts{
KeyID: key.KeyID,
}
_, err = keys.CancelDelete(client, cancelDeleteOpts).Extract()
_, err = keys.CancelDelete(client, key.KeyID)
if err != nil {
return fmterr.Errorf("error disabling deletion of key: %s", err)
}

key, err := keys.EnableKey(client, key.KeyID).ExtractKeyInfo()
key, err := keys.EnableKey(client, key.KeyID)
if err != nil {
return fmterr.Errorf("error enabling key: %s", err)
}
Expand Down Expand Up @@ -190,7 +187,7 @@ func resourceKmsKeyV1Create(ctx context.Context, d *schema.ResourceData, meta in
}

if !d.Get("is_enabled").(bool) {
disableKey, err := keys.DisableKey(client, key.KeyID).ExtractKeyInfo()
disableKey, err := keys.DisableKey(client, key.KeyID)
if err != nil {
return fmterr.Errorf("error disabling key: %s", err)
}
Expand All @@ -202,26 +199,26 @@ func resourceKmsKeyV1Create(ctx context.Context, d *schema.ResourceData, meta in

// enable rotation and change interval if necessary
if _, ok := d.GetOk("rotation_enabled"); ok {
rotationOpts := &keys.RotationOpts{
rotationOpts := keys.RotationOpts{
KeyID: key.KeyID,
}

keyRotation, err := keys.GetKeyRotationStatus(client, rotationOpts).ExtractResult()
keyRotation, err := keys.GetKeyRotationStatus(client, rotationOpts)
if err != nil {
return fmterr.Errorf("failed to fetch KMS key rotation status: %s", err)
}
if !keyRotation.Enabled {
err := keys.EnableKeyRotation(client, rotationOpts).ExtractErr()
err := keys.EnableKeyRotation(client, key.KeyID)
if err != nil {
return fmterr.Errorf("failed to enable KMS key rotation: %s", err)
}

if i, ok := d.GetOk("rotation_interval"); ok {
rotationOpts := &keys.RotationOpts{
rotationOpts := keys.RotationOpts{
KeyID: key.KeyID,
Interval: i.(int),
}
err := keys.UpdateKeyRotationInterval(client, rotationOpts).ExtractErr()
err := keys.UpdateKeyRotationInterval(client, rotationOpts)
if err != nil {
return fmterr.Errorf("failed to change KMS key rotation interval: %s", err)
}
Expand Down Expand Up @@ -253,7 +250,7 @@ func resourceKmsKeyV1Read(ctx context.Context, d *schema.ResourceData, meta inte
return fmterr.Errorf(errCreationClient, err)
}

key, err := keys.Get(client, d.Id()).ExtractKeyInfo()
key, err := keys.Get(client, d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -293,10 +290,10 @@ func resourceKmsKeyV1Read(ctx context.Context, d *schema.ResourceData, meta inte
}

// save rotation status
rotationOpts := &keys.RotationOpts{
rotationOpts := keys.RotationOpts{
KeyID: key.KeyID,
}
r, err := keys.GetKeyRotationStatus(client, rotationOpts).ExtractResult()
r, err := keys.GetKeyRotationStatus(client, rotationOpts)
if err == nil {
_ = d.Set("rotation_enabled", r.Enabled)
_ = d.Set("rotation_interval", r.Interval)
Expand All @@ -322,7 +319,7 @@ func resourceKmsKeyV1Update(ctx context.Context, d *schema.ResourceData, meta in
KeyID: d.Id(),
KeyAlias: d.Get("key_alias").(string),
}
_, err = keys.UpdateAlias(client, updateAliasOpts).ExtractKeyInfo()
_, err = keys.UpdateAlias(client, updateAliasOpts)
if err != nil {
return fmterr.Errorf("error updating OpenTelekomCloud key: %s", err)
}
Expand All @@ -333,20 +330,20 @@ func resourceKmsKeyV1Update(ctx context.Context, d *schema.ResourceData, meta in
KeyID: d.Id(),
KeyDescription: d.Get("key_description").(string),
}
_, err = keys.UpdateDes(client, updateDesOpts).ExtractKeyInfo()
_, err = keys.UpdateDes(client, updateDesOpts)
if err != nil {
return fmterr.Errorf("error updating OpenTelekomCloud key: %s", err)
}
}

if d.HasChange("is_enabled") {
key, err := keys.Get(client, d.Id()).ExtractKeyInfo()
key, err := keys.Get(client, d.Id())
if err != nil {
return fmterr.Errorf("describeKey got an error: %s", err)
}

if d.Get("is_enabled").(bool) && key.KeyState == DisabledState {
key, err := keys.EnableKey(client, d.Id()).ExtractKeyInfo()
key, err := keys.EnableKey(client, d.Id())
if err != nil {
return fmterr.Errorf("error enabling key: %s", err)
}
Expand All @@ -356,7 +353,7 @@ func resourceKmsKeyV1Update(ctx context.Context, d *schema.ResourceData, meta in
}

if !d.Get("is_enabled").(bool) && key.KeyState == EnabledState {
key, err := keys.DisableKey(client, d.Id()).ExtractKeyInfo()
key, err := keys.DisableKey(client, d.Id())
if err != nil {
return fmterr.Errorf("error disabling key: %s", err)
}
Expand All @@ -376,13 +373,10 @@ func resourceKmsKeyV1Update(ctx context.Context, d *schema.ResourceData, meta in
_, rotationEnabled := d.GetOk("rotation_enabled")
if d.HasChange("rotation_enabled") {
var rotationErr error
rotationOpts := &keys.RotationOpts{
KeyID: d.Id(),
}
if rotationEnabled {
rotationErr = keys.EnableKeyRotation(client, rotationOpts).ExtractErr()
rotationErr = keys.EnableKeyRotation(client, d.Id())
} else {
rotationErr = keys.DisableKeyRotation(client, rotationOpts).ExtractErr()
rotationErr = keys.DisableKeyRotation(client, d.Id())
}

if rotationErr != nil {
Expand All @@ -391,11 +385,11 @@ func resourceKmsKeyV1Update(ctx context.Context, d *schema.ResourceData, meta in
}

if rotationEnabled && d.HasChange("rotation_interval") {
intervalOpts := &keys.RotationOpts{
intervalOpts := keys.RotationOpts{
KeyID: d.Id(),
Interval: d.Get("rotation_interval").(int),
}
err := keys.UpdateKeyRotationInterval(client, intervalOpts).ExtractErr()
err := keys.UpdateKeyRotationInterval(client, intervalOpts)
if err != nil {
return fmterr.Errorf("failed to change key rotation interval: %s", err)
}
Expand All @@ -414,12 +408,12 @@ func resourceKmsKeyV1Delete(ctx context.Context, d *schema.ResourceData, meta in
return fmterr.Errorf(errCreationClient, err)
}

key, err := keys.Get(client, d.Id()).ExtractKeyInfo()
key, err := keys.Get(client, d.Id())
if err != nil {
return common.CheckDeletedDiag(d, err, "key")
}

deleteOpts := &keys.DeleteOpts{
deleteOpts := keys.DeleteOpts{
KeyID: d.Id(),
}
if v, ok := d.GetOk("pending_days"); ok {
Expand All @@ -430,26 +424,26 @@ func resourceKmsKeyV1Delete(ctx context.Context, d *schema.ResourceData, meta in
// in a pending deletion state from when the instance was terminated.
// If this is true, just move on. It'll eventually delete.
if key.KeyState != PendingDeletionState {
rotationOpts := &keys.RotationOpts{
rotationOpts := keys.RotationOpts{
KeyID: d.Id(),
}
keyRotation, err := keys.GetKeyRotationStatus(client, rotationOpts).ExtractResult()
keyRotation, err := keys.GetKeyRotationStatus(client, rotationOpts)
if err != nil {
return fmterr.Errorf("failed to fetch KMS key rotation status: %s", err)
}
if keyRotation.Enabled {
err := keys.DisableKeyRotation(client, rotationOpts).ExtractErr()
err := keys.DisableKeyRotation(client, d.Id())
if err != nil {
return fmterr.Errorf("failed to disable KMS key rotation: %s", err)
}
}

key, err = keys.Delete(client, deleteOpts).Extract()
keyDel, err := keys.Delete(client, deleteOpts)
if err != nil {
return diag.FromErr(err)
}

if key.KeyState != PendingDeletionState {
if keyDel.KeyState != PendingDeletionState {
return fmterr.Errorf("failed to delete key")
}
}
Expand All @@ -461,7 +455,7 @@ func resourceKmsKeyV1Delete(ctx context.Context, d *schema.ResourceData, meta in

func keyV1StateRefreshFunc(client *golangsdk.ServiceClient, keyID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
v, err := keys.Get(client, keyID).ExtractKeyInfo()
v, err := keys.Get(client, keyID)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return v, PendingDeletionState, nil
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/kms_key_refactor-0b21ecebef3e4f0b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
other:
- |
**[KMS]** Refactoring of KMS keys (`#2720 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2720>`_)