Skip to content

Commit 77f1e0b

Browse files
fix(dynamodb): Table.grantWriteData() doesn't include enough KMS permissions (#19102)
This fix adds the additional KMS actions `KEY_READ_ACTIONS` during calls to `grantWriteData`. This is required when using Tables are using CMKs during write operations such as put_item and batch_write_item. Fixes #10010 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5b764cc commit 77f1e0b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/@aws-cdk/aws-dynamodb/lib/table.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,8 @@ abstract class TableBase extends Resource implements ITable {
732732
* @param grantee The principal to grant access to
733733
*/
734734
public grantWriteData(grantee: iam.IGrantable): iam.Grant {
735-
return this.combinedGrant(grantee, { keyActions: perms.KEY_WRITE_ACTIONS, tableActions: perms.WRITE_DATA_ACTIONS });
735+
const keyActions = perms.KEY_READ_ACTIONS.concat(perms.KEY_WRITE_ACTIONS);
736+
return this.combinedGrant(grantee, { keyActions, tableActions: perms.WRITE_DATA_ACTIONS });
736737
}
737738

738739
/**

packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,38 @@ test('if an encryption key is included, encrypt/decrypt permissions are added to
720720
});
721721
});
722722

723+
test('if an encryption key is included, encrypt/decrypt permissions are added to the principal for grantWriteData', () => {
724+
const stack = new Stack();
725+
const table = new Table(stack, 'Table A', {
726+
tableName: TABLE_NAME,
727+
partitionKey: TABLE_PARTITION_KEY,
728+
encryption: TableEncryption.CUSTOMER_MANAGED,
729+
});
730+
const user = new iam.User(stack, 'MyUser');
731+
table.grantWriteData(user);
732+
733+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
734+
PolicyDocument: {
735+
Statement: Match.arrayWith([{
736+
Action: [
737+
'kms:Decrypt',
738+
'kms:DescribeKey',
739+
'kms:Encrypt',
740+
'kms:ReEncrypt*',
741+
'kms:GenerateDataKey*',
742+
],
743+
Effect: 'Allow',
744+
Resource: {
745+
'Fn::GetAtt': [
746+
'TableAKey07CC09EC',
747+
'Arn',
748+
],
749+
},
750+
}]),
751+
},
752+
});
753+
});
754+
723755
test('when specifying STANDARD_INFREQUENT_ACCESS table class', () => {
724756
const stack = new Stack();
725757
new Table(stack, CONSTRUCT_NAME, {

0 commit comments

Comments
 (0)