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(dynamodb): imported tables always grant permissions for indexes #20682

Merged
merged 5 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ If you intend to use the `tableStreamArn` (including indirectly, for example by
`@aws-cdk/aws-lambda-event-source.DynamoEventSource` on the imported table), you *must* use the
`Table.fromTableAttributes` method and the `tableStreamArn` property *must* be populated.

In order to grant permissions to indexes on imported tables you can either set `grantIndexPermissions` to `true`, or you can provide the indexes via the `globalIndexes` or `localIndexes` properties. This will enable `grant*` methods to also grant permissions to *all* table indexes.

## Keys

When a table is defined, you must define it's schema using the `partitionKey`
Expand Down
12 changes: 11 additions & 1 deletion packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,15 @@ export interface TableAttributes {
* @default - no local indexes
*/
readonly localIndexes?: string[];

/**
* If set to true, grant methods always grant permissions for all indexes.
* If false is provided, grant methods grant the permissions
* only when {@link globalIndexes} or {@link localIndexes} is specified.
*
* @default - false
*/
readonly grantIndexPermissions?: boolean;
}

abstract class TableBase extends Resource implements ITable {
Expand Down Expand Up @@ -1078,7 +1087,8 @@ export class Table extends TableBase {
public readonly tableArn: string;
public readonly tableStreamArn?: string;
public readonly encryptionKey?: kms.IKey;
protected readonly hasIndex = (attrs.globalIndexes ?? []).length > 0 ||
protected readonly hasIndex = (attrs.grantIndexPermissions ?? false) ||
(attrs.globalIndexes ?? []).length > 0 ||
(attrs.localIndexes ?? []).length > 0;

constructor(_tableArn: string, tableName: string, tableStreamArn?: string) {
Expand Down
58 changes: 58 additions & 0 deletions packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,64 @@ describe('import', () => {
},
});
});

test('creates the index permissions if grantIndexPermissions is provided', () => {
const stack = new Stack();

const table = Table.fromTableAttributes(stack, 'ImportedTable', {
tableName: 'MyTableName',
grantIndexPermissions: true,
});

const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.AnyPrincipal(),
});

table.grantReadData(role);

Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: [
'dynamodb:BatchGetItem',
'dynamodb:GetRecords',
'dynamodb:GetShardIterator',
'dynamodb:Query',
'dynamodb:GetItem',
'dynamodb:Scan',
'dynamodb:ConditionCheckItem',
'dynamodb:DescribeTable',
],
Resource: [
{
'Fn::Join': ['', [
'arn:',
{ Ref: 'AWS::Partition' },
':dynamodb:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':table/MyTableName',
]],
},
{
'Fn::Join': ['', [
'arn:',
{ Ref: 'AWS::Partition' },
':dynamodb:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':table/MyTableName/index/*',
]],
},
],
},
],
},
});
});
});
});

Expand Down