|
1 | 1 | import { cloudformation as applicationautoscaling } from '@aws-cdk/aws-applicationautoscaling';
|
2 |
| -import { PolicyStatement, PolicyStatementEffect, Role, ServicePrincipal } from '@aws-cdk/aws-iam'; |
| 2 | +import iam = require('@aws-cdk/aws-iam'); |
3 | 3 | import { Construct, TagManager, Tags } from '@aws-cdk/cdk';
|
4 | 4 | import { cloudformation as dynamodb } from './dynamodb.generated';
|
5 | 5 |
|
6 | 6 | const HASH_KEY_TYPE = 'HASH';
|
7 | 7 | const RANGE_KEY_TYPE = 'RANGE';
|
8 | 8 |
|
| 9 | +const READ_DATA_ACTIONS = [ |
| 10 | + 'dynamodb:BatchGetItem', |
| 11 | + 'dynamodb:GetRecords', |
| 12 | + 'dynamodb:GetShardIterator', |
| 13 | + 'dynamodb:Query', |
| 14 | + 'dynamodb:GetItem', |
| 15 | + 'dynamodb:Scan' |
| 16 | +]; |
| 17 | + |
| 18 | +const WRITE_DATA_ACTIONS = [ |
| 19 | + 'dynamodb:BatchWriteItem', |
| 20 | + 'dynamodb:PutItem', |
| 21 | + 'dynamodb:UpdateItem', |
| 22 | + 'dynamodb:DeleteItem' |
| 23 | +]; |
| 24 | + |
9 | 25 | export interface Attribute {
|
10 | 26 | /**
|
11 | 27 | * The name of an attribute.
|
@@ -314,6 +330,57 @@ export class Table extends Construct {
|
314 | 330 | this.writeScalingPolicyResource = this.buildAutoScaling(this.writeScalingPolicyResource, 'Write', props);
|
315 | 331 | }
|
316 | 332 |
|
| 333 | + /** |
| 334 | + * Adds an IAM policy statement associated with this table to an IAM |
| 335 | + * principal's policy. |
| 336 | + * @param principal The principal (no-op if undefined) |
| 337 | + * @param actions The set of actions to allow (i.e. "dynamodb:PutItem", "dynamodb:GetItem", ...) |
| 338 | + */ |
| 339 | + public grant(principal?: iam.IPrincipal, ...actions: string[]) { |
| 340 | + if (!principal) { |
| 341 | + return; |
| 342 | + } |
| 343 | + principal.addToPolicy(new iam.PolicyStatement() |
| 344 | + .addResource(this.tableArn) |
| 345 | + .addActions(...actions)); |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * Permits an IAM principal all data read operations from this table: |
| 350 | + * BatchGetItem, GetRecords, GetShardIterator, Query, GetItem, Scan. |
| 351 | + * @param principal The principal to grant access to |
| 352 | + */ |
| 353 | + public grantReadData(principal?: iam.IPrincipal) { |
| 354 | + this.grant(principal, ...READ_DATA_ACTIONS); |
| 355 | + } |
| 356 | + |
| 357 | + /** |
| 358 | + * Permits an IAM principal all data write operations to this table: |
| 359 | + * BatchWriteItem, PutItem, UpdateItem, DeleteItem. |
| 360 | + * @param principal The principal to grant access to |
| 361 | + */ |
| 362 | + public grantWriteData(principal?: iam.IPrincipal) { |
| 363 | + this.grant(principal, ...WRITE_DATA_ACTIONS); |
| 364 | + } |
| 365 | + |
| 366 | + /** |
| 367 | + * Permits an IAM principal to all data read/write operations to this table. |
| 368 | + * BatchGetItem, GetRecords, GetShardIterator, Query, GetItem, Scan, |
| 369 | + * BatchWriteItem, PutItem, UpdateItem, DeleteItem |
| 370 | + * @param principal The principal to grant access to |
| 371 | + */ |
| 372 | + public grantReadWriteData(principal?: iam.IPrincipal) { |
| 373 | + this.grant(principal, ...READ_DATA_ACTIONS, ...WRITE_DATA_ACTIONS); |
| 374 | + } |
| 375 | + |
| 376 | + /** |
| 377 | + * Permits all DynamoDB operations ("dynamodb:*") to an IAM principal. |
| 378 | + * @param principal The principal to grant access to |
| 379 | + */ |
| 380 | + public grantFullAccess(principal?: iam.IPrincipal) { |
| 381 | + this.grant(principal, 'dynamodb:*'); |
| 382 | + } |
| 383 | + |
317 | 384 | /**
|
318 | 385 | * Validate the table construct.
|
319 | 386 | *
|
@@ -443,21 +510,21 @@ export class Table extends Construct {
|
443 | 510 | }
|
444 | 511 |
|
445 | 512 | private buildAutoScalingRole(roleResourceName: string) {
|
446 |
| - const autoScalingRole = new Role(this, roleResourceName, { |
447 |
| - assumedBy: new ServicePrincipal('application-autoscaling.amazonaws.com') |
| 513 | + const autoScalingRole = new iam.Role(this, roleResourceName, { |
| 514 | + assumedBy: new iam.ServicePrincipal('application-autoscaling.amazonaws.com') |
448 | 515 | });
|
449 |
| - autoScalingRole.addToPolicy(new PolicyStatement(PolicyStatementEffect.Allow) |
| 516 | + autoScalingRole.addToPolicy(new iam.PolicyStatement(iam.PolicyStatementEffect.Allow) |
450 | 517 | .addActions("dynamodb:DescribeTable", "dynamodb:UpdateTable")
|
451 | 518 | .addResource(this.tableArn));
|
452 |
| - autoScalingRole.addToPolicy(new PolicyStatement(PolicyStatementEffect.Allow) |
| 519 | + autoScalingRole.addToPolicy(new iam.PolicyStatement(iam.PolicyStatementEffect.Allow) |
453 | 520 | .addActions("cloudwatch:PutMetricAlarm", "cloudwatch:DescribeAlarms", "cloudwatch:GetMetricStatistics",
|
454 | 521 | "cloudwatch:SetAlarmState", "cloudwatch:DeleteAlarms")
|
455 | 522 | .addAllResources());
|
456 | 523 | return autoScalingRole;
|
457 | 524 | }
|
458 | 525 |
|
459 | 526 | private buildScalableTargetResourceProps(scalableDimension: string,
|
460 |
| - scalingRole: Role, |
| 527 | + scalingRole: iam.Role, |
461 | 528 | props: AutoScalingProps) {
|
462 | 529 | return {
|
463 | 530 | maxCapacity: props.maxCapacity,
|
|
0 commit comments