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 7 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
packages/aws-cdk-lib/aws-rds/lib/cluster.ts Show resolved Hide resolved
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
packages/aws-cdk-lib/aws-rds/lib/cluster.ts Show resolved Hide resolved

});

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
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.

Copy link
Author

Choose a reason for hiding this comment

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

There wouldn't be one unfortunately. I'm not sure Cfn has a way to reference it either. The AWS SDK does allow you to get the secret's ARN by describing the cluster.

Choose a reason for hiding this comment

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

Not sure if it helps, but this is how I was getting access to the secret so that I could include it in the task definition for my ECS tasks that are created by the same stack.

((CfnDBCluster)cfnCluster.Node.DefaultChild).AttrMasterUserSecretSecretArn

Copy link
Author

Choose a reason for hiding this comment

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

@antonfelich Thanks for the comment! After a bit of testing I was able to use a similar technique to create an imported secret that's accessible from the cluster's secret prop. Should be a great addition!

Line 1169 in cluster.ts:
this.secret = secretsmanager.Secret.fromSecretCompleteArn(this, 'DbClusterSecret', cluster.attrMasterUserSecretSecretArn)

@aws-cdk-automation
Copy link
Collaborator

This PR has been in the MERGE CONFLICTS state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch from 744e065 to 7b0ecee Compare September 30, 2024 19:43
@antonfelich
Copy link

Any idea when this might be merged in? I've been using the CfnDBCluster ManageMasterUserPassword work around, but suddenly today it's decided not to work any more. The unused secret seems to be attaching to the cluster first, and then when the real secret is trying to attach it blows up because there already is one. CloudFormation just gives a Internal Failure (RequestToken: 6e94374a-4094-6627-e639-c8c95a165caf) which isn't particularly helpful unfortunately.

@epoctic
Copy link
Author

epoctic commented Oct 2, 2024

Any idea when this might be merged in? I've been using the CfnDBCluster ManageMasterUserPassword work around, but suddenly today it's decided not to work any more. The unused secret seems to be attaching to the cluster first, and then when the real secret is trying to attach it blows up because there already is one. CloudFormation just gives a Internal Failure (RequestToken: 6e94374a-4094-6627-e639-c8c95a165caf) which isn't particularly helpful unfortunately.

@antonfelich I'm still waiting on a community approval, please feel free to review it!

@epoctic epoctic requested a review from Hawxy October 2, 2024 15:02
Copy link

@antonfelich antonfelich left a comment

Choose a reason for hiding this comment

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

Looks good to me, keen to see this out in the wild!

if (
props.manageMasterUserPassword &&
(
props.credentials?.excludeCharacters ||

Choose a reason for hiding this comment

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

It might already be taken care of further up, but ManageMasterUserPassword and MasterUserPassword are mutually exclusive, so would be good to throw if they accidentally set both.

When I was manually setting ManageMasterUserPassword I had to intentionally remove the MasterUserPassword attribute.

        var cfnCluster = (CfnDBCluster)dbCluster.Node.DefaultChild;
        cfnCluster.AddPropertyOverride("ManageMasterUserPassword", true);
        cfnCluster.AddPropertyDeletionOverride("MasterUserPassword");

Choose a reason for hiding this comment

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

Sorry almost forgot, because one of my clusters already existed and I was migrating it from manual password to RDS managed, I also had to blank out the username.

cfnCluster.AddPropertyDeletionOverride("MasterUsername");

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: {

Choose a reason for hiding this comment

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

Not sure if it helps, but this is how I was getting access to the secret so that I could include it in the task definition for my ECS tasks that are created by the same stack.

((CfnDBCluster)cfnCluster.Node.DefaultChild).AttrMasterUserSecretSecretArn

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

2 similar comments
@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch 6 times, most recently from e1d6ca7 to c646a07 Compare October 10, 2024 23:34
@epoctic epoctic force-pushed the epoctic/support-rds-managed-cluster-secret branch 2 times, most recently from d137d34 to d589245 Compare October 11, 2024 14:15
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

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
6 participants