Skip to content

Commit 9d8f69e

Browse files
authored
feat(rds): use user-defined security group for db user rotation function (#23087)
closes #23086 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 69e6978 commit 9d8f69e

11 files changed

+1269
-42
lines changed

Diff for: packages/@aws-cdk/aws-rds/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,11 @@ When the master password is generated and stored in AWS Secrets Manager, it can
359359
import * as cdk from '@aws-cdk/core';
360360

361361
declare const instance: rds.DatabaseInstance;
362+
declare const mySecurityGroup: ec2.SecurityGroup;
362363
instance.addRotationSingleUser({
363364
automaticallyAfter: cdk.Duration.days(7), // defaults to 30 days
364365
excludeCharacters: '!@#$%^&*', // defaults to the set " %+~`#$&*()|[]{}:;<>?!'/@\"\\"
366+
securityGroup: mySecurityGroup, // defaults to an auto-created security group
365367
});
366368
```
367369

Diff for: packages/@aws-cdk/aws-rds/lib/props.ts

+7
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,13 @@ export interface CommonRotationUserOptions {
492492
* @default https://secretsmanager.<region>.amazonaws.com
493493
*/
494494
readonly endpoint?: ec2.IInterfaceVpcEndpoint;
495+
496+
/**
497+
* The security group for the Lambda rotation function
498+
*
499+
* @default - a new security group is created
500+
*/
501+
readonly securityGroup?: ec2.ISecurityGroup;
495502
}
496503

497504
/**

Diff for: packages/@aws-cdk/aws-rds/test/cluster.test.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ describe('cluster', () => {
954954
});
955955
});
956956

957-
test('addRotationSingleUser() with custom automaticallyAfter, excludeCharacters and vpcSubnets', () => {
957+
test('addRotationSingleUser() with custom automaticallyAfter, excludeCharacters, vpcSubnets and securityGroup', () => {
958958
// GIVEN
959959
const stack = new cdk.Stack();
960960
const vpcWithIsolated = ec2.Vpc.fromVpcAttributes(stack, 'Vpc', {
@@ -967,6 +967,9 @@ describe('cluster', () => {
967967
isolatedSubnetIds: ['isolated-subnet-id-1', 'isolated-subnet-id-2'],
968968
isolatedSubnetNames: ['isolated-subnet-name-1', 'isolated-subnet-name-2'],
969969
});
970+
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', {
971+
vpc: vpcWithIsolated,
972+
});
970973

971974
// WHEN
972975
// DB in isolated subnet (no internet connectivity)
@@ -984,6 +987,7 @@ describe('cluster', () => {
984987
automaticallyAfter: cdk.Duration.days(15),
985988
excludeCharacters: '°_@',
986989
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
990+
securityGroup,
987991
});
988992

989993
// THEN
@@ -1005,11 +1009,17 @@ describe('cluster', () => {
10051009
},
10061010
vpcSubnetIds: 'private-subnet-id-1,private-subnet-id-2',
10071011
excludeCharacters: '°_@',
1012+
vpcSecurityGroupIds: {
1013+
'Fn::GetAtt': [
1014+
stack.getLogicalId(securityGroup.node.defaultChild as ec2.CfnSecurityGroup),
1015+
'GroupId',
1016+
],
1017+
},
10081018
},
10091019
});
10101020
});
10111021

1012-
test('addRotationMultiUser() with custom automaticallyAfter, excludeCharacters and vpcSubnets', () => {
1022+
test('addRotationMultiUser() with custom automaticallyAfter, excludeCharacters, vpcSubnets and securityGroup', () => {
10131023
// GIVEN
10141024
const stack = new cdk.Stack();
10151025
const vpcWithIsolated = ec2.Vpc.fromVpcAttributes(stack, 'Vpc', {
@@ -1022,6 +1032,9 @@ describe('cluster', () => {
10221032
isolatedSubnetIds: ['isolated-subnet-id-1', 'isolated-subnet-id-2'],
10231033
isolatedSubnetNames: ['isolated-subnet-name-1', 'isolated-subnet-name-2'],
10241034
});
1035+
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', {
1036+
vpc: vpcWithIsolated,
1037+
});
10251038
const userSecret = new DatabaseSecret(stack, 'UserSecret', { username: 'user' });
10261039

10271040
// WHEN
@@ -1041,6 +1054,7 @@ describe('cluster', () => {
10411054
automaticallyAfter: cdk.Duration.days(15),
10421055
excludeCharacters: '°_@',
10431056
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
1057+
securityGroup,
10441058
});
10451059

10461060
// THEN
@@ -1062,6 +1076,12 @@ describe('cluster', () => {
10621076
},
10631077
vpcSubnetIds: 'private-subnet-id-1,private-subnet-id-2',
10641078
excludeCharacters: '°_@',
1079+
vpcSecurityGroupIds: {
1080+
'Fn::GetAtt': [
1081+
stack.getLogicalId(securityGroup.node.defaultChild as ec2.CfnSecurityGroup),
1082+
'GroupId',
1083+
],
1084+
},
10651085
},
10661086
});
10671087
});

Diff for: packages/@aws-cdk/aws-rds/test/instance.test.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ describe('instance', () => {
823823
});
824824
});
825825

826-
test('addRotationSingleUser() with custom automaticallyAfter, excludeCharacters and vpcSubnets', () => {
826+
test('addRotationSingleUser() with custom automaticallyAfter, excludeCharacters, vpcSubnets and securityGroup', () => {
827827
// GIVEN
828828
const vpcWithIsolated = ec2.Vpc.fromVpcAttributes(stack, 'Vpc', {
829829
vpcId: 'vpc-id',
@@ -835,6 +835,9 @@ describe('instance', () => {
835835
isolatedSubnetIds: ['isolated-subnet-id-1', 'isolated-subnet-id-2'],
836836
isolatedSubnetNames: ['isolated-subnet-name-1', 'isolated-subnet-name-2'],
837837
});
838+
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', {
839+
vpc: vpcWithIsolated,
840+
});
838841

839842
// WHEN
840843
// DB in isolated subnet (no internet connectivity)
@@ -849,6 +852,7 @@ describe('instance', () => {
849852
automaticallyAfter: cdk.Duration.days(15),
850853
excludeCharacters: '°_@',
851854
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
855+
securityGroup,
852856
});
853857

854858
// THEN
@@ -870,11 +874,17 @@ describe('instance', () => {
870874
},
871875
vpcSubnetIds: 'private-subnet-id-1,private-subnet-id-2',
872876
excludeCharacters: '°_@',
877+
vpcSecurityGroupIds: {
878+
'Fn::GetAtt': [
879+
stack.getLogicalId(securityGroup.node.defaultChild as ec2.CfnSecurityGroup),
880+
'GroupId',
881+
],
882+
},
873883
},
874884
});
875885
});
876886

877-
test('addRotationMultiUser() with custom automaticallyAfter, excludeCharacters and vpcSubnets', () => {
887+
test('addRotationMultiUser() with custom automaticallyAfter, excludeCharacters, vpcSubnets and securityGroup', () => {
878888
// GIVEN
879889
const vpcWithIsolated = ec2.Vpc.fromVpcAttributes(stack, 'Vpc', {
880890
vpcId: 'vpc-id',
@@ -886,6 +896,9 @@ describe('instance', () => {
886896
isolatedSubnetIds: ['isolated-subnet-id-1', 'isolated-subnet-id-2'],
887897
isolatedSubnetNames: ['isolated-subnet-name-1', 'isolated-subnet-name-2'],
888898
});
899+
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', {
900+
vpc: vpcWithIsolated,
901+
});
889902
const userSecret = new rds.DatabaseSecret(stack, 'UserSecret', { username: 'user' });
890903

891904
// WHEN
@@ -902,6 +915,7 @@ describe('instance', () => {
902915
automaticallyAfter: cdk.Duration.days(15),
903916
excludeCharacters: '°_@',
904917
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
918+
securityGroup,
905919
});
906920

907921
// THEN
@@ -923,6 +937,12 @@ describe('instance', () => {
923937
},
924938
vpcSubnetIds: 'private-subnet-id-1,private-subnet-id-2',
925939
excludeCharacters: '°_@',
940+
vpcSecurityGroupIds: {
941+
'Fn::GetAtt': [
942+
stack.getLogicalId(securityGroup.node.defaultChild as ec2.CfnSecurityGroup),
943+
'GroupId',
944+
],
945+
},
926946
},
927947
});
928948
});

Diff for: packages/@aws-cdk/aws-rds/test/integ.cluster-rotation.lit.js.snapshot/aws-cdk-rds-cluster-rotation.assets.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "20.0.0",
2+
"version": "21.0.0",
33
"files": {
4-
"b5d2442773f45a1a63e67895776106093341355451336b5073275c46364c618e": {
4+
"dfc2f8e8aa2f2f42357312f7f92524a12cb383c762b91eaecbbefb8ad8400f82": {
55
"source": {
66
"path": "aws-cdk-rds-cluster-rotation.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "b5d2442773f45a1a63e67895776106093341355451336b5073275c46364c618e.json",
12+
"objectKey": "dfc2f8e8aa2f2f42357312f7f92524a12cb383c762b91eaecbbefb8ad8400f82.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

0 commit comments

Comments
 (0)