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] Force delete subresources during kmip_adapter destroy, avoid casting panics #5565

Merged
merged 2 commits into from
Aug 29, 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 @@ -23,7 +23,7 @@ require (
github.com/IBM/ibm-cos-sdk-go-config/v2 v2.1.0
github.com/IBM/ibm-hpcs-tke-sdk v0.0.0-20211109141421-a4b61b05f7d1
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta
github.com/IBM/keyprotect-go-client v0.14.0
github.com/IBM/keyprotect-go-client v0.15.1
github.com/IBM/logs-go-sdk v0.3.0
github.com/IBM/networking-go-sdk v0.49.0
github.com/IBM/logs-router-go-sdk v1.0.3
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ github.com/IBM/ibm-hpcs-tke-sdk v0.0.0-20211109141421-a4b61b05f7d1/go.mod h1:M2J
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta h1:P1fdIfKsD9xvJQ5MHIEztPS9yfNf9x+VDTamaYcmqcs=
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta/go.mod h1:MLVNHMYoKsvovJZ4v1gQCpIYtRDHTtoIHK6XztDZGsU=
github.com/IBM/keyprotect-go-client v0.5.1/go.mod h1:5TwDM/4FRJq1ZOlwQL1xFahLWQ3TveR88VmL1u3njyI=
github.com/IBM/keyprotect-go-client v0.14.0 h1:GqgK3BdczA/w7+B1RxEPLya0w9S/ZXi5YWKAxdW8vHQ=
github.com/IBM/keyprotect-go-client v0.14.0/go.mod h1:cAt714Vnwnd03mmkBHHSJlDNRVthdRmJB6RePd4/B8Q=
github.com/IBM/keyprotect-go-client v0.15.1 h1:m4qzqF5zOumRxKZ8s7vtK7A/UV/D278L8xpRG+WgT0s=
github.com/IBM/keyprotect-go-client v0.15.1/go.mod h1:asXtHwL/4uCHA221Vd/7SkXEi2pcRHDzPyyksc1DthE=
github.com/IBM/logs-go-sdk v0.3.0 h1:FHzTCCMyp9DvQGXgkppzcOPywC4ggt7x8xu0MR5h8xI=
github.com/IBM/logs-go-sdk v0.3.0/go.mod h1:yv/GCXC4/p+MZEeXl4xjZAOMvDAVRwu61WyHZFKFXQM=
github.com/IBM/logs-router-go-sdk v1.0.3 h1:VO64OpANNouxS/0kvUeBpENKWxYx3TYnoNzW8OycMb0=
Expand Down Expand Up @@ -1604,6 +1604,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down Expand Up @@ -2035,6 +2036,7 @@ golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
Expand Down
4 changes: 2 additions & 2 deletions ibm/service/kms/data_source_ibm_kms_kmip_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func DataSourceIBMKMSKMIPObject() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The id of the KMIP adapter that contains the cert",
Description: "The id of the KMIP adapter that contains the kmip object",
ForceNew: true,
ExactlyOneOf: []string{"adapter_id", "adapter_name"},
}
baseMap["adapter_name"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the KMIP adapter that contains the cert",
Description: "The name of the KMIP adapter that contains the kmip object",
ForceNew: true,
ExactlyOneOf: []string{"adapter_id", "adapter_name"},
}
Expand Down
16 changes: 9 additions & 7 deletions ibm/service/kms/resource_ibm_kms_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ func resourceIBMKmsKeyExists(d *schema.ResourceData, meta interface{}) (bool, er

_, err = kpAPI.GetKey(context.Background(), keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return false, nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 {
return false, nil
}
}
return false, err
}
Expand Down Expand Up @@ -455,10 +456,11 @@ func populateSchemaData(d *schema.ResourceData, meta interface{}) (*kp.Client, e
ctx := context.Background()
key, err := kpAPI.GetKey(ctx, keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil, nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil, nil
}
}
return nil, fmt.Errorf("[ERROR] Get Key failed with error while reading Key: %s", err)
} else if key.State == 5 { //Refers to Deleted state of the Key
Expand Down
19 changes: 10 additions & 9 deletions ibm/service/kms/resource_ibm_kms_key_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ func resourceIBMKmsKeyAliasRead(d *schema.ResourceData, meta interface{}) error
}
key, err := kpAPI.GetKey(context.Background(), keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
}
}
return fmt.Errorf("[ERROR] Get Key failed with error while reading policies: %s", err)
} else if key.State == 5 { //Refers to Deleted state of the Key
Expand Down Expand Up @@ -129,12 +130,12 @@ func resourceIBMKmsKeyAliasDelete(d *schema.ResourceData, meta interface{}) erro
}
err1 := kpAPI.DeleteKeyAlias(context.Background(), id[0], keyid)
if err1 != nil {
kpError := err1.(*kp.Error)
if kpError.StatusCode == 404 {
return nil
} else {
return fmt.Errorf(" failed to Destroy alias with error: %s", err1)
if kpError, ok := err1.(*kp.Error); ok {
if kpError.StatusCode == 404 {
return nil
}
}
return fmt.Errorf(" failed to Destroy alias with error: %s", err1)
}
return nil
}
9 changes: 5 additions & 4 deletions ibm/service/kms/resource_ibm_kms_key_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ func resourceIBMKmsKeyPolicyRead(context context.Context, d *schema.ResourceData
}
key, err := kpAPI.GetKey(context, keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
}
}
return diag.Errorf("Get Key failed with error while reading policies: %s", err)
} else if key.State == 5 { //Refers to Deleted state of the Key
Expand Down
18 changes: 9 additions & 9 deletions ibm/service/kms/resource_ibm_kms_key_rings.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ func resourceIBMKmsKeyRingRead(d *schema.ResourceData, meta interface{}) error {
}
_, err = kpAPI.GetKeyRings(context.Background())
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
}
}
return fmt.Errorf("[ERROR] Get Key Rings failed with error: %s", err)
}
Expand All @@ -151,11 +152,10 @@ func resourceIBMKmsKeyRingDelete(d *schema.ResourceData, meta interface{}) error

err = kpAPI.DeleteKeyRing(context.Background(), id[0], kp.WithForce(true))
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return nil
} else {
return fmt.Errorf(" failed to Destroy key ring with error: %s", err)
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 {
return nil
}
}
}
return nil
Expand Down
15 changes: 11 additions & 4 deletions ibm/service/kms/resource_ibm_kms_kmip_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,14 @@ func resourceIBMKmsKMIPAdapterDelete(d *schema.ResourceData, meta interface{}) e
}

for _, object := range objects.Objects {
err = kpAPI.DeleteKMIPObject(ctx, adapterID, object.ID)
err = kpAPI.DeleteKMIPObject(ctx, adapterID, object.ID, kp.WithForce(true))
if err != nil {
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 || kpError.StatusCode == 410 {
// if the kmip object is already deleted, do not error out
continue
}
}
return fmt.Errorf("[ERROR] Failed to delete KMIP object associated with adapter (%s): %s",
adapterID,
err,
Expand All @@ -194,9 +200,10 @@ func resourceIBMKmsKMIPAdapterExists(d *schema.ResourceData, meta interface{}) (
ctx := context.Background()
_, err = kpAPI.GetKMIPAdapter(ctx, adapterID)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return false, nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 {
return false, nil
}
}
return false, wrapError(err, "Error checking adapter existence")
}
Expand Down
7 changes: 4 additions & 3 deletions ibm/service/kms/resource_ibm_kms_kmip_client_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ func resourceIBMKmsKMIPClientCertExists(d *schema.ResourceData, meta interface{}
ctx := context.Background()
_, err = kpAPI.GetKMIPClientCertificate(ctx, adapterID, certID)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return false, nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 {
return false, nil
}
}
return false, wrapError(err, "Error checking KMIP Client Certificate existence")
}
Expand Down
7 changes: 4 additions & 3 deletions ibm/service/kms/resource_ibm_kp_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ func resourceIBMKeyExists(d *schema.ResourceData, meta interface{}) (bool, error
// keyid := d.Id()
_, err = api.GetKey(context.Background(), keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return false, nil
if kpError, ok := err.(*kp.Error); ok {
if kpError.StatusCode == 404 {
return false, nil
}
}
return false, err
}
Expand Down
Loading