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(secretsmanager): SecretRotation for secret imported by name has incorrect permissions #18567

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class RotationSchedule extends Resource {
'secretsmanager:PutSecretValue',
'secretsmanager:UpdateSecretVersionStage',
],
resources: [props.secret.secretArn],
resources: [props.secret.secretFullArn ? props.secret.secretFullArn : `${props.secret.secretArn}-??????`],
}),
);
props.rotationLambda.addToRolePolicy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,57 @@ test('assign permissions for rotation schedule with a rotation Lambda', () => {
});
});

test('grants correct permissions for secret imported by name', () => {
// GIVEN
const secret = secretsmanager.Secret.fromSecretNameV2(stack, 'Secret', 'mySecretName');
const rotationLambda = new lambda.Function(stack, 'Lambda', {
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.fromInline('export.handler = event => event;'),
handler: 'index.handler',
});

// WHEN
new secretsmanager.RotationSchedule(stack, 'RotationSchedule', {
secret,
rotationLambda,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: Match.arrayWith([
{
Action: [
'secretsmanager:DescribeSecret',
'secretsmanager:GetSecretValue',
'secretsmanager:PutSecretValue',
'secretsmanager:UpdateSecretVersionStage',
],
Effect: 'Allow',
Resource: {
'Fn::Join': ['', [
'arn:',
{ Ref: 'AWS::Partition' },
':secretsmanager:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':secret:mySecretName-??????',
]],
},
},
]),
Version: '2012-10-17',
},
PolicyName: 'LambdaServiceRoleDefaultPolicyDAE46E21',
Roles: [
{
Ref: 'LambdaServiceRoleA8ED4D3B',
},
],
});
});

test('assign kms permissions for rotation schedule with a rotation Lambda', () => {
// GIVEN
const encryptionKey = new kms.Key(stack, 'Key');
Expand Down