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: grant decrypt for the KMS key ViaService [CLK-222798] #105

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/aurora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class Aurora extends Construct {

constructor(scope: Construct, id: Namer, props: AuroraProps) {
super(scope, id.pascal);
const stack = Stack.of(this);

if (!props.skipAddRotationMultiUser && !props.skipProxy) {
Annotations.of(this).addWarning(
Expand All @@ -274,6 +275,21 @@ export class Aurora extends Construct {
const schemas = props.schemas ?? ['public'];

const encryptionKey = (this.kmsKey = props.kmsKey);
encryptionKey.addToResourcePolicy(
new aws_iam.PolicyStatement({
principals: [new aws_iam.AnyPrincipal()],
actions: ['kms:Encrypt', 'kms:Decrypt', 'kms:ReEncrypt', 'kms:CreateGrant', 'kms:DescribeKey'],
sid: 'AllowSecretsManagerAccess',
resources: ['*'],
conditions: {
StringEquals: {
'kms:ViaService': `secretsmanager.${stack.region}.amazonaws.com`,
'kms:CallerAccount': stack.account,
},
},
}),
);

const instanceType =
props.instanceType || aws_ec2.InstanceType.of(aws_ec2.InstanceClass.T4G, aws_ec2.InstanceSize.MEDIUM);
if (instanceType.architecture !== aws_ec2.InstanceArchitecture.ARM_64) {
Expand Down
37 changes: 37 additions & 0 deletions test/aurora.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,43 @@ describe('Aurora', () => {
it('defaults to no prefix on secret names', () => {
template.hasResourceProperties('AWS::SecretsManager::Secret', { Name: 'TestManager' });
});
it('ABAC grants the resourcePolicy of the KMS key', () => {
const policyStatements = new Capture(Match.arrayWith([]));
template.hasResourceProperties('AWS::KMS::Key', {
KeyPolicy: {
Statement: policyStatements,
},
});
const abacGrant = policyStatements.asArray().find((s) => s.Sid === 'AllowSecretsManagerAccess');
expect(abacGrant).toEqual({
Sid: 'AllowSecretsManagerAccess',
Action: ['kms:Encrypt', 'kms:Decrypt', 'kms:ReEncrypt', 'kms:CreateGrant', 'kms:DescribeKey'],
Condition: {
StringEquals: {
'kms:CallerAccount': {
Ref: 'AWS::AccountId',
},
'kms:ViaService': {
'Fn::Join': [
'',
[
'secretsmanager.',
{
Ref: 'AWS::Region',
},
'.amazonaws.com',
],
],
},
},
},
Effect: 'Allow',
Principal: {
AWS: '*',
},
Resource: '*',
});
});
// it('outputs ProxyEndpoint', () => {
// template.hasOutput('ProxyEndpoint', {});
// });
Expand Down
Loading