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

r/aws_rds_cluster: Fix crash when attempting to update master_password #30379

Merged
merged 10 commits into from
Mar 31, 2023
7 changes: 7 additions & 0 deletions .changelog/30379.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_rds_cluster: Fix crash when updating `master_password`
```

```release-note:bug
resource/aws_db_instance: Fix crash when updating `password`
```
4 changes: 2 additions & 2 deletions internal/service/rds/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,12 +1279,12 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, meta int
input.ManageMasterUserPassword = aws.Bool(d.Get("manage_master_user_password").(bool))
}
if d.HasChange("master_password") {
if v, ok := d.GetOk("master_password"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
if v, ok := d.GetOk("master_password"); ok {
input.MasterUserPassword = aws.String(v.(string))
}
}
if d.HasChange("master_user_secret_kms_key_id") {
if v, ok := d.GetOk("master_user_secret_kms_key_id"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
if v, ok := d.GetOk("master_user_secret_kms_key_id"); ok {
input.MasterUserSecretKmsKeyId = aws.String(v.(string))
}
}
Expand Down
47 changes: 46 additions & 1 deletion internal/service/rds/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,36 @@ func TestAccRDSCluster_enableHTTPEndpoint(t *testing.T) {
})
}

func TestAccRDSCluster_password(t *testing.T) {
ctx := acctest.Context(t)
var dbCluster rds.DBCluster
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, rds.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_password(rName, "valid-password-1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "master_password", "valid-password-1"),
),
},
{
Config: testAccClusterConfig_password(rName, "valid-password-2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &dbCluster),
resource.TestCheckResourceAttr(resourceName, "master_password", "valid-password-2"),
),
},
},
})
}

func testAccCheckClusterDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
return testAccCheckClusterDestroyWithProvider(ctx)(s, acctest.Provider)
Expand Down Expand Up @@ -2470,7 +2500,8 @@ resource "aws_rds_cluster" "test" {
database_name = "test"
manage_master_user_password = true
master_username = "tfacctest"
db_cluster_parameter_group_name = "default.aurora5.6"
engine = "aurora-mysql"
db_cluster_parameter_group_name = "default.aurora-mysql5.7"
skip_final_snapshot = true
}
`, rName)
Expand Down Expand Up @@ -4359,3 +4390,17 @@ resource "aws_rds_cluster" "test" {
}
`, rName, enableHttpEndpoint)
}

func testAccClusterConfig_password(rName, password string) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "test" {
cluster_identifier = %[1]q
database_name = "test"
master_username = "tfacctest"
master_password = %[2]q
engine = "aurora-mysql"
db_cluster_parameter_group_name = "default.aurora-mysql5.7"
skip_final_snapshot = true
}
`, rName, password)
}
7 changes: 5 additions & 2 deletions internal/service/rds/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2092,8 +2092,11 @@ func dbInstancePopulateModify(input *rds_sdkv2.ModifyDBInstanceInput, d *schema.
}

if d.HasChange("master_user_secret_kms_key_id") {
if v, ok := d.GetOk("master_user_secret_kms_key_id"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
needsModify = true
if v, ok := d.GetOk("master_user_secret_kms_key_id"); ok {
input.MasterUserSecretKmsKeyId = aws.String(v.(string))
// InvalidParameterValue: A ManageMasterUserPassword value is required when MasterUserSecretKmsKeyId is specified.
input.ManageMasterUserPassword = aws.Bool(d.Get("manage_master_user_password").(bool))
}
}

Expand Down Expand Up @@ -2139,7 +2142,7 @@ func dbInstancePopulateModify(input *rds_sdkv2.ModifyDBInstanceInput, d *schema.
if d.HasChange("password") {
needsModify = true
// With ManageMasterUserPassword set to true, the password is no longer needed, so we omit it from the API call.
if v, ok := d.GetOk("password"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil {
if v, ok := d.GetOk("password"); ok {
input.MasterUserPassword = aws.String(v.(string))
}
}
Expand Down
11 changes: 9 additions & 2 deletions internal/service/rds/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,10 +971,10 @@ func TestAccRDSInstance_password(t *testing.T) {
ExpectError: regexp.MustCompile(`MasterUserPassword is not a valid password because it is shorter than 8 characters`),
},
{
Config: testAccInstanceConfig_password(rName, "valid-password"),
Config: testAccInstanceConfig_password(rName, "valid-password-1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "password", "valid-password"),
resource.TestCheckResourceAttr(resourceName, "password", "valid-password-1"),
),
},
{
Expand All @@ -988,6 +988,13 @@ func TestAccRDSInstance_password(t *testing.T) {
"skip_final_snapshot",
},
},
{
Config: testAccInstanceConfig_password(rName, "valid-password-2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "password", "valid-password-2"),
),
},
},
})
}
Expand Down