-
Notifications
You must be signed in to change notification settings - Fork 4k
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
fix(dynamodb): addtoresourcepolicy fix for table (v1) construct #31516
Changes from 2 commits
49bfdb1
57dab53
057ab1e
4668ce7
3fb88ac
99284b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -524,7 +524,7 @@ export interface TableAttributes { | |
readonly grantIndexPermissions?: boolean; | ||
} | ||
|
||
export abstract class TableBase extends Resource implements ITable, iam.IResourceWithPolicy { | ||
export abstract class TableBase extends Resource implements ITable { | ||
/** | ||
* @attribute | ||
*/ | ||
|
@@ -564,7 +564,7 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc | |
* @param actions The set of actions to allow (i.e. "dynamodb:PutItem", "dynamodb:GetItem", ...) | ||
*/ | ||
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant { | ||
return iam.Grant.addToPrincipalOrResource({ | ||
return iam.Grant.addToPrincipal({ | ||
grantee, | ||
actions, | ||
resourceArns: [ | ||
|
@@ -575,7 +575,7 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc | |
produce: () => this.hasIndex ? `${arn}/index/*` : Aws.NO_VALUE, | ||
})), | ||
], | ||
resource: this, | ||
scope: this, | ||
}); | ||
} | ||
/** | ||
|
@@ -691,23 +691,6 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc | |
return this.combinedGrant(grantee, { keyActions, tableActions: ['dynamodb:*'] }); | ||
} | ||
|
||
/** | ||
* Adds a statement to the resource policy associated with this file system. | ||
* A resource policy will be automatically created upon the first call to `addToResourcePolicy`. | ||
* | ||
* Note that this does not work with imported file systems. | ||
* | ||
* @param statement The policy statement to add | ||
*/ | ||
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult { | ||
this.resourcePolicy = this.resourcePolicy ?? new iam.PolicyDocument({ statements: [] }); | ||
this.resourcePolicy.addStatements(statement); | ||
return { | ||
statementAdded: true, | ||
policyDependable: this, | ||
}; | ||
} | ||
|
||
/** | ||
* Return the given named metric for this Table | ||
* | ||
|
@@ -958,11 +941,11 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc | |
produce: () => this.hasIndex ? `${arn}/index/*` : Aws.NO_VALUE, | ||
})), | ||
]; | ||
const ret = iam.Grant.addToPrincipalOrResource({ | ||
const ret = iam.Grant.addToPrincipal({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, this would break Table v1 for us. |
||
grantee, | ||
actions: opts.tableActions, | ||
resourceArns: resources, | ||
resource: this, | ||
scope: this, | ||
}); | ||
return ret; | ||
} | ||
|
@@ -971,11 +954,11 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc | |
throw new Error(`DynamoDB Streams must be enabled on the table ${this.node.path}`); | ||
} | ||
const resources = [this.tableStreamArn]; | ||
const ret = iam.Grant.addToPrincipalOrResource({ | ||
const ret = iam.Grant.addToPrincipal({ | ||
grantee, | ||
actions: opts.streamActions, | ||
resourceArns: resources, | ||
resource: this, | ||
scope: this, | ||
}); | ||
return ret; | ||
} | ||
|
@@ -1732,6 +1715,29 @@ export class Table extends TableBase { | |
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Adds a statement to the resource policy associated with this file system. | ||
* A resource policy will be automatically created upon the first call to `addToResourcePolicy`. | ||
* | ||
* Note that this does not work with imported file systems. | ||
* | ||
* @param statement The policy statement to add | ||
*/ | ||
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult { | ||
|
||
this.resourcePolicy = this.resourcePolicy ?? new iam.PolicyDocument({ statements: [] }); | ||
this.resourcePolicy.addStatements(statement); | ||
|
||
this.table.resourcePolicy = { | ||
policyDocument: this.resourcePolicy, | ||
}; | ||
|
||
return { | ||
statementAdded: true, | ||
policyDependable: this, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this need to become
addToPrincipal
? We are for example already using the resource policy to grant cross-account access to a table directly. We build the policy in CDK from external accountArnPrincipal
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because addToPrincipalOrResource never adds to the resource, as IAM is missing methods. So I'm putting it back to how it was before I changed it until I have time to implement the missing methods in IAM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand what methods are missing. Looking at
Grant
, it seems to be handling this already, if given theresource
in the call: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-iam/lib/grant.ts#L145-L157. It's just thatTable.addToResourcePolicy
is not functional, but instead adds statements to a policy instance not bound with the internalCfnTable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LeeroyHannigan I want to check if I follow your line of thoughts here.
I see the
iam.Grant.addToPrincipalOrResource
is implemented here. It basically does 2 things, calladdToPrincipal
and a bit further down on line 151, it callsoptions.resource.addToResourcePolicy
, which means theTable.addToResourcePolicy
. Since the method is reportedly a no-op (from #30793), you are saying your change here fromaddToPrincipalOrResource
toaddToPrincipal
will not change any actual behaviour.I agree with that. But if I understand @everilae correctly, even though
Table.addToResourcePolicy
was a no-op, there may be CDK users who are currently relying on whatiam.Grant.addToPrincipalOrResource
returns (it returns the resource policy statement whereasiam.Grant.addToPrincipal
returns principal policy statement.). This allows CDK users to use this method to build the resource policy instead of creating their own.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
We rely on the "...OrResource" part, or would like to, once all this is fixed. If the call is replaced with just "addToPrincipal", then attempts at granting to external accounts would not work.