Skip to content

Commit

Permalink
feat(dynamodb): allow providing indexes when importing a Table
Browse files Browse the repository at this point in the history
For imported Tables, the grant() methods skipped adding permissions for indexes,
as there was no way of providing the indexes on import.
This change adds globalIndexes and localIndexes properties to the TableAttributes interface,
so you can now provide indexes when calling Table.fromTableAttributes().

Fixes aws#6392
  • Loading branch information
skinny85 committed May 27, 2020
1 parent c2b9e75 commit 8a6173d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
34 changes: 27 additions & 7 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ export interface ITable extends IResource {
export interface TableAttributes {
/**
* The ARN of the dynamodb table.
* One of this, or {@link tabeName}, is required.
* One of this, or {@link tableName}, is required.
*
* @default - no table arn
*/
readonly tableArn?: string;

/**
* The table name of the dynamodb table.
* One of this, or {@link tabeArn}, is required.
* One of this, or {@link tableArn}, is required.
*
* @default - no table name
*/
Expand All @@ -439,6 +439,28 @@ export interface TableAttributes {
* @default - no key
*/
readonly encryptionKey?: kms.IKey;

/**
* The name of the global indexes set for this Table.
* Note that you need to set either this property,
* or {@link localIndexes},
* if you want methods like grantReadData()
* to grant permissions for indexes as well as the table itself.
*
* @default - no global indexes
*/
readonly globalIndexes?: string[];

/**
* The name of the local indexes set for this Table.
* Note that you need to set either this property,
* or {@link globalIndexes},
* if you want methods like grantReadData()
* to grant permissions for indexes as well as the table itself.
*
* @default - no local indexes
*/
readonly localIndexes?: string[];
}

abstract class TableBase extends Resource implements ITable {
Expand Down Expand Up @@ -682,7 +704,7 @@ abstract class TableBase extends Resource implements ITable {
private combinedGrant(
grantee: iam.IGrantable,
opts: {keyActions?: string[], tableActions?: string[], streamActions?: string[]},
) {
): iam.Grant {
if (opts.tableActions) {
const resources = [this.tableArn,
Lazy.stringValue({ produce: () => this.hasIndex ? `${this.tableArn}/index/*` : Aws.NO_VALUE }),
Expand Down Expand Up @@ -773,6 +795,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 ||
(attrs.localIndexes ?? []).length > 0;

constructor(_tableArn: string, tableName: string, tableStreamArn?: string) {
super(scope, id);
Expand All @@ -781,10 +805,6 @@ export class Table extends TableBase {
this.tableStreamArn = tableStreamArn;
this.encryptionKey = attrs.encryptionKey;
}

protected get hasIndex(): boolean {
return false;
}
}

let name: string;
Expand Down
57 changes: 57 additions & 0 deletions packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,63 @@ describe('import', () => {
Roles: [stack.resolve(role.roleName)],
});
});

test('creates the correct index grant if indexes have been provided when importing', () => {
const stack = new Stack();

const table = Table.fromTableAttributes(stack, 'ImportedTable', {
tableName: 'MyTableName',
globalIndexes: ['global'],
localIndexes: ['local'],
});

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

table.grantReadData(role);

expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: [
'dynamodb:BatchGetItem',
'dynamodb:GetRecords',
'dynamodb:GetShardIterator',
'dynamodb:Query',
'dynamodb:GetItem',
'dynamodb:Scan',
],
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

0 comments on commit 8a6173d

Please sign in to comment.