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

feat(rds): add support for manageMasterUserPassword in L2 construct #30997

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

epoctic
Copy link

@epoctic epoctic commented Aug 1, 2024

Issue # (if applicable)

Closes #29239

Reason for this change

In order to make the properties in the manageMasterUserPassword of the L1 Construct CfnDBCluster configurable in the L2 Construct.

Even when using a CFN override to set manageMasterUserPassword, the L2 cluster construct would create an additional unused secret. This PR adds native support for the manageMasterUserPassword CloudFormation attribute and eliminates the extra secret that the workaround caused.

Description of changes

To ensure existing clusters wouldn't have a new cfn property added and set to false, I had to use a few conditional statements. I also wanted to make sure that the created secret supported a customer defined username and kms key.

Description of how you validated changes

I deployed a database cluster using the new code and logged into it using the managed password with custom username. I also manually triggered rotations on the secret and ensured that the newly generated secret also worked for logging into the database.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Aug 1, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team August 1, 2024 01:29
@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch from 7ba289f to de886c9 Compare August 1, 2024 01:36
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Aug 1, 2024
Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 Left some comments for adjustments.
Also, can you please add unit test coverage?

packages/aws-cdk-lib/aws-rds/lib/cluster.ts Outdated Show resolved Hide resolved
cluster = new CfnDBCluster(this, 'Resource', {
...this.newCfnProps,
// Admin
// Don't set password if RDS is managing credentials.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From docs.

Can't manage the master user password with AWS Secrets Manager if MasterUserPassword is specified.

We should add validation instead of simply ignoring the specified password.

Valid for Cluster Type: Aurora DB clusters and Multi-AZ DB clusters

Can we validate that the RDS cluster is Multi-AZ if manageMasterUserPassword: true?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the L2 DatabaseCluster construct support non-Aurora engines? The DatabaseClusterEngine appears to only support Aurora, so I don't think that the multi-AZ check is required. I'm also sure that manageMasterUserPassword works on a RDS cluster with a single instance when the db engine is Aurora based.

Copy link
Contributor

@a-bigelow a-bigelow Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non Aurora IClusterEngine variants are supported by the construct (you need to implement them yourself), but the mysql and postgres engines are both "Multi-AZ DB clusters". As far as I can tell, the only way to deploy those two engines not as "Multi-AZ DB clusters" is to use DatabaseInstance instead of this construct.

Copy link
Author

@epoctic epoctic Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification! Based on that information I believe that a specific set of checks for a multi-AZ cluster wouldn't be necessary.

packages/aws-cdk-lib/aws-rds/README.md Outdated Show resolved Hide resolved
packages/aws-cdk-lib/aws-rds/README.md Show resolved Hide resolved
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Aug 1, 2024
@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch from 0bd1d1d to 3909d7e Compare August 5, 2024 22:19
@epoctic epoctic changed the title feat(rds): add support for cluster managed master password in L2 cons… feat(rds): add support for manageMasterUserPassword in L2 construct Aug 5, 2024
@epoctic
Copy link
Author

epoctic commented Aug 6, 2024

Thanks 👍 Left some comments for adjustments. Also, can you please add unit test coverage?

Done, sorry about the wait!

@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch from 58f8a79 to 5f89799 Compare August 6, 2024 17:58
masterUserPassword: credentials.password?.unsafeUnwrap(),
});
let cluster: CfnDBCluster;
if (!props.manageMasterUserPassword) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the if statement given the above validation?

      const credentials = renderCredentials(this, props.engine, props.credentials);
      const secret = credentials.secret;

      const cluster = new CfnDBCluster(this, 'Resource', {
        ...this.newCfnProps,
        // Admin
        masterUsername: credentials.username,
        masterUserPassword: credentials.password?.unsafeUnwrap(),
        // Define kms key the RDS Managed Secret will use.
        ...props.manageMasterUserPassword && credentials.encryptionKey && {
          masterUserSecret: {
            kmsKeyId: credentials.encryptionKey.keyId,
          },
        },
      });

      this.clusterIdentifier = cluster.ref;
      this.clusterResourceIdentifier = cluster.attrDbClusterResourceId;

      if (secret) {
        this.secret = secret.attach(this);
      }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot unfortunately. The only way renderCredentials doesn't create a new secret is if the credentials prop is passed the output from a Credentials.[method] call with a SecretsManager Secret object, or a password that's an instance of SecretValue. Since we disallow password and secret from being passed as props to credentials, the renderCredential function would always create a new secret. Not desirable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for clarifying 👍
We could remove a bit of duplication by moving

      this.clusterIdentifier = cluster.ref;
      this.clusterResourceIdentifier = cluster.attrDbClusterResourceId;

after the if statement block.
Also, note the other comment on unit tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll get that changed, thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for clarifying 👍 We could remove a bit of duplication by moving

      this.clusterIdentifier = cluster.ref;
      this.clusterResourceIdentifier = cluster.attrDbClusterResourceId;

after the if statement block. Also, note the other comment on unit tests.

I just tested this change, and it can't be done. A secret is created in one of the if blocks, and the secret needs those values to be set, so the lines must be duplicated no matter what.


});

test('throw error for setting `manageMasterUserPassword` to true while `credentials` props excludeCharacters is defined', () => {
Copy link
Contributor

@lpizzinidev lpizzinidev Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could unify these test cases:

test.each([
  ['excludeCharacters', { excludeCharacters: '1234' }],
  ['secret', { secret: new sm.Secret(stack, 'secret') }],
])('throw error for setting `manageMasterUserPassword` to true while `credentials.%s` is defined', (_, credentials) => { ... }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could unify these test cases:

test.each([
  ['excludeCharacters', { excludeCharacters: '1234' }],
  ['secret', { secret: new sm.Secret(stack, 'secret') }],
])('throw error for setting `manageMasterUserPassword` to true while `credentials.%s` is defined', (_, credentials) => { ... }

Sorry about the wait! It took a bit to work through how I could define an external stack and still keep resources unique.

@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch 2 times, most recently from c47961e to 02cebb7 Compare August 13, 2024 20:20
@epoctic
Copy link
Author

epoctic commented Aug 13, 2024

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 02cebb7
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

This failure doesn't seem to be related to code I've committed.

@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch from 02cebb7 to 744e065 Compare August 14, 2024 00:38
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 744e065
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

masterUsername: props.credentials?.username ?? Credentials.fromUsername(props.engine.defaultUsername ?? 'admin').username,
// Define kms key the RDS Managed Secret will use.
...props.credentials?.encryptionKey !== undefined && {
masterUserSecret: {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this.secret be set by CfnDBCluster's masterUserSecret.secretArn? I don't see a direct way to access the created secret for use within CDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AWS::RDS::DBCluster Password management with Amazon Aurora and AWS Secrets Manager
5 participants