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: Allow access on all indexes of a table in GuDynamoDBPolicy #2024

Merged
merged 5 commits into from
Sep 18, 2023
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
96 changes: 66 additions & 30 deletions src/constructs/iam/policies/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,40 @@ describe("The GuDynamoDBReadPolicy construct", () => {
"dynamodb:GetRecords",
],
Effect: "Allow",
Resource: {
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/MyTable",
Resource: [
{
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/MyTable",
],
],
],
},
},
{
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/MyTable/index/*",
],
],
},
],
},
],
},
Expand All @@ -57,22 +75,40 @@ describe("The GuDynamoDBWritePolicy construct", () => {
{
Action: ["dynamodb:BatchWriteItem", "dynamodb:PutItem", "dynamodb:DeleteItem", "dynamodb:UpdateItem"],
Effect: "Allow",
Resource: {
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/MyTable",
Resource: [
{
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/MyTable",
],
],
],
},
},
{
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/MyTable/index/*",
],
],
},
],
},
],
},
Expand Down
5 changes: 4 additions & 1 deletion src/constructs/iam/policies/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ abstract class GuDynamoDBPolicy extends GuAllowPolicy {
super(scope, id, {
...props,
actions: props.actions.map((action) => `dynamodb:${action}`),
resources: [`arn:aws:dynamodb:${scope.region}:${scope.account}:table/${props.tableName}`],
resources: [
`arn:aws:dynamodb:${scope.region}:${scope.account}:table/${props.tableName}`,
`arn:aws:dynamodb:${scope.region}:${scope.account}:table/${props.tableName}/index/*`,
Copy link

@adamnfish adamnfish Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this will work for the actions people are using in practice? There aren't many actions that support indexes as a resource, just the basics like Scan:
https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazondynamodb.html

If people have added other action props, this might break their stacks if CloudFormation then rejects the index resource for that action?

Copy link
Contributor Author

@kenoir kenoir Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I've tested that the CloudFormation that gets generated will successfully apply by deploying https://github.com/guardian/crosswordv2/pull/38 to our CODE environment (CF updates successfully and the expected behaviour can be observed with regard to permissions).

I think this is similar to the case where wildcard resources can be applied to sets of actions. See the AmazonDynamoDBReadOnlyAccess managed policy attached.

Screenshot 2023-09-18 at 10 42 29

The documentation that I can find is not explicit on this point, but does state:

If you specify a resource type in a statement with an action that does not support that resource type, then the statement doesn't allow access.

So I believe we're safe from disrupting existing stacks here. I will add a comment describing this to the change as it does seem like a potential point of confusion.

],
});
}
}
Expand Down
48 changes: 33 additions & 15 deletions src/patterns/ec2-app/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,22 +406,40 @@ describe("the GuEC2App pattern", function () {
{
Action: ["dynamodb:BatchWriteItem", "dynamodb:PutItem", "dynamodb:DeleteItem", "dynamodb:UpdateItem"],
Effect: "Allow",
Resource: {
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/my-dynamo-table",
Resource: [
{
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/my-dynamo-table",
],
],
],
},
},
{
"Fn::Join": [
"",
[
"arn:aws:dynamodb:",
{
Ref: "AWS::Region",
},
":",
{
Ref: "AWS::AccountId",
},
":table/my-dynamo-table/index/*",
],
],
},
],
},
],
},
Expand Down