Skip to content

Commit 4d003a5

Browse files
authored
feat(dynamodb): imported tables always grant permissions for indexes (#20682)
When we use imported tables, grant methods don't grant permissions for indexes unless local indexes or global secondary indexes are specified. The information for indexes is used only for grant permissions now. Users either keep track of index information of the imported tables or specify random index (e.g. `*`) as a workaround to obtain the permissions. This PR let imported tables grant permissions for indexes without providing indexes. close #13703 ---- ### 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 * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] 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 bb5b730 commit 4d003a5

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

packages/@aws-cdk/aws-dynamodb/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ If you intend to use the `tableStreamArn` (including indirectly, for example by
3636
`@aws-cdk/aws-lambda-event-source.DynamoEventSource` on the imported table), you *must* use the
3737
`Table.fromTableAttributes` method and the `tableStreamArn` property *must* be populated.
3838

39+
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.
40+
3941
## Keys
4042

4143
When a table is defined, you must define it's schema using the `partitionKey`

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,15 @@ export interface TableAttributes {
599599
* @default - no local indexes
600600
*/
601601
readonly localIndexes?: string[];
602+
603+
/**
604+
* If set to true, grant methods always grant permissions for all indexes.
605+
* If false is provided, grant methods grant the permissions
606+
* only when {@link globalIndexes} or {@link localIndexes} is specified.
607+
*
608+
* @default - false
609+
*/
610+
readonly grantIndexPermissions?: boolean;
602611
}
603612

604613
abstract class TableBase extends Resource implements ITable {
@@ -1078,7 +1087,8 @@ export class Table extends TableBase {
10781087
public readonly tableArn: string;
10791088
public readonly tableStreamArn?: string;
10801089
public readonly encryptionKey?: kms.IKey;
1081-
protected readonly hasIndex = (attrs.globalIndexes ?? []).length > 0 ||
1090+
protected readonly hasIndex = (attrs.grantIndexPermissions ?? false) ||
1091+
(attrs.globalIndexes ?? []).length > 0 ||
10821092
(attrs.localIndexes ?? []).length > 0;
10831093

10841094
constructor(_tableArn: string, tableName: string, tableStreamArn?: string) {

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

+58
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,64 @@ describe('import', () => {
25082508
},
25092509
});
25102510
});
2511+
2512+
test('creates the index permissions if grantIndexPermissions is provided', () => {
2513+
const stack = new Stack();
2514+
2515+
const table = Table.fromTableAttributes(stack, 'ImportedTable', {
2516+
tableName: 'MyTableName',
2517+
grantIndexPermissions: true,
2518+
});
2519+
2520+
const role = new iam.Role(stack, 'Role', {
2521+
assumedBy: new iam.AnyPrincipal(),
2522+
});
2523+
2524+
table.grantReadData(role);
2525+
2526+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
2527+
PolicyDocument: {
2528+
Statement: [
2529+
{
2530+
Action: [
2531+
'dynamodb:BatchGetItem',
2532+
'dynamodb:GetRecords',
2533+
'dynamodb:GetShardIterator',
2534+
'dynamodb:Query',
2535+
'dynamodb:GetItem',
2536+
'dynamodb:Scan',
2537+
'dynamodb:ConditionCheckItem',
2538+
'dynamodb:DescribeTable',
2539+
],
2540+
Resource: [
2541+
{
2542+
'Fn::Join': ['', [
2543+
'arn:',
2544+
{ Ref: 'AWS::Partition' },
2545+
':dynamodb:',
2546+
{ Ref: 'AWS::Region' },
2547+
':',
2548+
{ Ref: 'AWS::AccountId' },
2549+
':table/MyTableName',
2550+
]],
2551+
},
2552+
{
2553+
'Fn::Join': ['', [
2554+
'arn:',
2555+
{ Ref: 'AWS::Partition' },
2556+
':dynamodb:',
2557+
{ Ref: 'AWS::Region' },
2558+
':',
2559+
{ Ref: 'AWS::AccountId' },
2560+
':table/MyTableName/index/*',
2561+
]],
2562+
},
2563+
],
2564+
},
2565+
],
2566+
},
2567+
});
2568+
});
25112569
});
25122570
});
25132571

0 commit comments

Comments
 (0)