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

fix(rds): fixed the IAM policy that grantConnect() generates for DatabaseInstanceReadReplica #31068

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

Conversation

ashishdhingra
Copy link
Contributor

Issue # (if applicable)

Closes #31061.

Reason for this change

Calling grantConnect() on an instance of DatabaseInstanceReadReplica generates an incorrect policy that uses the full ARN of the instance instead of the instanceResourceId value. It should have created policy with correct resource format arn:aws:rds-db:region:account-id:dbuser:DbiResourceId/db-user-name per Creating and using an IAM policy for IAM database access.

Description of changes

Fixed the IAM policy that grantConnect() generates for DatabaseInstanceReadReplica. The change correctly sets the value of instanceResourceId to replica instance attrDbiResourceId. The value of instanceResourceId is used to generate IAM policy.

Description of how you validated changes

  • Added new unit test.
  • Updated existing integration test.

Checklist


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

@aws-cdk-automation aws-cdk-automation requested a review from a team August 9, 2024 00:58
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p2 labels Aug 9, 2024
@ashishdhingra ashishdhingra force-pushed the DatabaseInstanceReadReplica-GrantRead-Fix branch from e821569 to 4b8b0d5 Compare August 9, 2024 01:00
@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 9, 2024
@@ -1366,7 +1366,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements
this.instanceIdentifier = instance.ref;
this.dbInstanceEndpointAddress = instance.attrEndpointAddress;
this.dbInstanceEndpointPort = instance.attrEndpointPort;
this.instanceResourceId = instance.attrDbInstanceArn;
Copy link
Contributor

Choose a reason for hiding this comment

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

I am afraid that this can be a breaking change to customers. I know that we assigned a wrong value, but may be there are some customers that use this property to get the DB Instance Arn and use it some how. So, my recommendation is to deprecate this property, and to add a new property for the instance resource id, and use it in grant method, and also check where else we use the current property.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@moelasmar Thanks for your review comment. Would creating a feature flag (example #29794) be the recommended approach instead of adding a new property? Please advise.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the feature flag here will not fix the problem, as the flag default value will be false (to avoid breaking customers), so the policy will still referring to the wrong replica instance id. So, I do not prefer feature flag, it add a lot of confusion on us when we write code, so we can use this property in similar cases assuming that we are using the correct property, but because the feature flag is disabled, this may cause more issues for customers.

I prefer to go with deprecation, and adding new property.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@moelasmar Thanks for your review comment. I have added a commit to:

  • Deprecate instanceResourceId property and add new instanceResourceIdV2 property in DatabaseInstanceReadReplica.
  • Added a copy of grantConnect() in DatabaseInstanceReadReplica that makes use of instanceResourceIdV2.

Please review.

Thanks,
Ashish

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @ashishdhingra for resolving my comment, and sorry for late response.

I actually, did a deeper check, and I found that I missed that the property instanceResourceId is actually exist in the Interface IDatabaseInstance so, adding a new property will actually break the idea of that interface, and also deprecating this property is not a good idea.

So, I think it is better to make this change under feature flag, and to enable the feature flag by default, and the customers who want to keep the current behaviour, they can disable the flag.

Copy link
Contributor

Choose a reason for hiding this comment

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

I pushed some updates to this change to add the feature flag. Could you please take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks good reviewing at high level. Needs review from the maintainer.

Copy link
Contributor

@moelasmar moelasmar left a comment

Choose a reason for hiding this comment

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

left one comment

@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 9, 2024
@amazon-codecatalyst amazon-codecatalyst bot force-pushed the DatabaseInstanceReadReplica-GrantRead-Fix branch from 4b8b0d5 to ad7c2de Compare August 15, 2024 23:01
@ashishdhingra ashishdhingra force-pushed the DatabaseInstanceReadReplica-GrantRead-Fix branch from ad7c2de to 113ab2a Compare August 15, 2024 23:02
@mergify mergify bot dismissed moelasmar’s stale review August 15, 2024 23:02

Pull request has been modified.

…ceResourceIdV2 property in DatabaseInstanceReadReplica to use in grantConnect().
@amazon-codecatalyst amazon-codecatalyst bot force-pushed the DatabaseInstanceReadReplica-GrantRead-Fix branch from ced2113 to 4d0239a Compare August 18, 2024 02:48
@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 18, 2024
@ashishdhingra ashishdhingra added pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Aug 21, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 21, 2024
…InstanceReadReplica-GrantRead-Fix

# Conflicts:
#	packages/@aws-cdk/cx-api/FEATURE_FLAGS.md
#	packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md
#	packages/aws-cdk-lib/cx-api/README.md
#	packages/aws-cdk-lib/cx-api/lib/features.ts
#	packages/aws-cdk-lib/cx-api/test/features.test.ts
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@paulhcsun paulhcsun self-assigned this Sep 11, 2024
Copy link
Contributor

@paulhcsun paulhcsun left a comment

Choose a reason for hiding this comment

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

Changes look good, just a couple comments about how the feature flag is defined and the default value being true.

@@ -73,6 +73,7 @@ Flags come in three types:
| [@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault](#aws-cdkcustom-resourceslogapiresponsedatapropertytruedefault) | When enabled, the custom resource used for `AwsCustomResource` will configure the `logApiResponseData` property as true by default | 2.145.0 | (fix) |
| [@aws-cdk/aws-s3:keepNotificationInImportedBucket](#aws-cdkaws-s3keepnotificationinimportedbucket) | When enabled, Adding notifications to a bucket in the current stack will not remove notification from imported stack. | 2.155.0 | (fix) |
| [@aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask](#aws-cdkaws-stepfunctions-tasksusenews3uriparametersforbedrockinvokemodeltask) | When enabled, use new props for S3 URI field in task definition of state machine for bedrock invoke model. | V2NEXT | (fix) |
| [@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId](#aws-rds-setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId) | When enabled, the value of property `instanceResourceId` in construct `DatabaseInstanceReadReplica` will be set to the correct value which is `DbiResourceId` instead of currently `DbInstanceArn` | V2NEXT | (fix) |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
| [@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId](#aws-rds-setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId) | When enabled, the value of property `instanceResourceId` in construct `DatabaseInstanceReadReplica` will be set to the correct value which is `DbiResourceId` instead of currently `DbInstanceArn` | V2NEXT | (fix) |
| [@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId](#aws-rds-setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId) | When enabled, the value of property `instanceResourceId` in construct `DatabaseInstanceReadReplica` will be set to the correct value which is `DbiResourceId` instead of currently `DbInstanceArn` | V2NEXT | (fix) |

Comment on lines +1393 to +1395
| ----- |---------|-------------|
| (not in v1) | | |
| V2NEXT | `true` | `true` |
Copy link
Contributor

Choose a reason for hiding this comment

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

The spacing on some of these seem off, was it manually generated or auto generated by running the build?

Copy link
Contributor

Choose a reason for hiding this comment

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

it seems while I merged the conflicts I messed up these files, let me rerun the build and see if it will fix it

| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| V2NEXT | `true` | `true` |
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want this to be true by default? Would that not break customers?

Copy link
Contributor

Choose a reason for hiding this comment

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

my idea was which behaviour should I keep, shall I keep the wrong behaviour, or should I fix it. I chose to fix the wrong behaviour with an option to let people keep the current one. But, honestly you have a point, it is a breaking change

Copy link
Contributor

Choose a reason for hiding this comment

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

I will change it to false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-rds): grantConnect generates incorrect policy for DatabaseInstanceReadReplica
4 participants