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

(aws-dynamodb): Granting permissions with tables any dynamodb.grant* methods will provide wildcard permissions to KMS #23991

Open
heikkima opened this issue Feb 3, 2023 · 7 comments
Assignees
Labels
@aws-cdk/aws-dynamodb Related to Amazon DynamoDB bug This issue is a bug. p2

Comments

@heikkima
Copy link

heikkima commented Feb 3, 2023

Describe the bug

Granting permissions with Table's grantReadWriteData() or grantReadData() methods (possibly other grant* methods as well) will grant kms permissions to all kms keys within the AWS account.

Expected Behavior

Expecting Table grant* methods to grant minimal permissions to the specific KMS key that the dynamoDb table is encrypted with.

Current Behavior

Table's grantReadData() method grants KMS permissions to all KMS keys in the AWS account. Does not matter if KMS key is created within the same CDK application or with Key.fromLookup()

The following statement is appended to the IAM role's inline policy

        {
            "Action": [
                "kms:Decrypt",
                "kms:DescribeKey"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }

Reproduction Steps

const encryptionKey = new Key(this, 'MyKey', {
  enableKeyRotation: true,
})

const table = new Table(this, 'MyTable', {
  partitionKey: { name: 'id', type: AttributeType.STRING },
  tableName: 'my-table',
  encryption: TableEncryption.CUSTOMER_MANAGED,
  encryptionKey
})

table.grantReadData(iamRole)

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.63.0 (build 7f4e35e)

Framework Version

No response

Node.js Version

v18.12.1

OS

macOS 13.0.1

Language

Typescript

Language Version

4.9.5

Other information

No response

@heikkima heikkima added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 3, 2023
@github-actions github-actions bot added the @aws-cdk/aws-dynamodb Related to Amazon DynamoDB label Feb 3, 2023
@khushail
Copy link
Contributor

khushail commented Feb 4, 2023

Hi @heikkima , thanks for reaching out. We tried to reproduce the issue as you described but we are not able to. The policy we generated contains the access to KMS key only and not the one to all resources. Is there any other information would you like to provide to replicate the issue?

@khushail khushail added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 4, 2023
@khushail khushail self-assigned this Feb 4, 2023
@github-actions
Copy link

github-actions bot commented Feb 6, 2023

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Feb 6, 2023
@heikkima
Copy link
Author

heikkima commented Feb 6, 2023

Further testing indicates that this happens if the IAM role is provided as a stack prop to the template (the IAM role is created in another CDK template but within the same CDK application).

I tried creating the IAM role in the same template as the KMS key and DynamoDB table and this works as expected:

    const encryptionKey = new Key(this, 'MyKey', {
      enableKeyRotation: true,
    })
    
    const table = new Table(this, 'MyTable', {
      partitionKey: { name: 'id', type: AttributeType.STRING },
      tableName: 'my-table',
      encryption: TableEncryption.CUSTOMER_MANAGED,
      encryptionKey
    })

    const iamRole = new Role(this, 'Role', {
      permissionsBoundary: ManagedPolicy.fromManagedPolicyName(this, 'boundary-policy', 'default-permission-boundary'),
      assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
      description: 'Example role...',
      path: '/app/',
      roleName: 'my-role'
    })
    
    table.grantReadData(iamRole)

But if I import the IAM role from another stack it does the reported behaviour:

    const { iamRole } = props

    const encryptionKey = new Key(this, 'MyKey', {
      enableKeyRotation: true,
    })
    
    const table = new Table(this, 'MyTable', {
      partitionKey: { name: 'id', type: AttributeType.STRING },
      tableName: 'my-table',
      encryption: TableEncryption.CUSTOMER_MANAGED,
      encryptionKey
    })
    
    table.grantReadData(iamRole)

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Feb 6, 2023
@heikkima
Copy link
Author

heikkima commented Feb 8, 2023

I discovered that the issue is most likely in the stack's environment configurations.

In my CDK project I have two stacks. First stack which I create IAM roles and the second where I create dynamodb table.
In the project I use an existing KMS key (created outside of the CDK project) to encrypt the dynamodb table, so I use Key.fromLookup() function to import it inside the dynamodb stack. For Key.fromLookup() function to work I need to configure env: { account: '2383838383', region: 'eu-west-1' } in the stack's instance options. The IAM stack instance is configured: env: { region: 'eu-west-1' }

If I modify IAM stack's instance options to env: { account: '2383838383', region: 'eu-west-1' } then the correct KMS key arn is added to the IAM role's inline policy, otherwise * is used

@khushail
Copy link
Contributor

khushail commented Feb 8, 2023

Hi @heikkima , I tried to repro the issue creating mentioned stacks but could not succeed with multiple configurations. Could you please share the full code(including stacks and multiple configs) and provide more insights on this ?

@khushail khushail added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 8, 2023
@heikkima
Copy link
Author

heikkima commented Feb 9, 2023

@khushail I created a public repository where you can find the whole source code to replicate this issue
https:/heikkima/aws-cdk-issue-23991

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 9, 2023
@khushail khushail added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Feb 10, 2023
@khushail
Copy link
Contributor

Thanks @heikkima for sharing the repo. I was able to reproduce the issue and could confirm the issue as stated by you.

Although this seems valid, I am marking this issue as p2, which means that we are unable to work on this immediately. We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization.

@khushail khushail removed the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Feb 10, 2023
@khushail khushail removed their assignment Mar 1, 2023
@pahud pahud added p1 and removed p2 labels Feb 29, 2024
@kirtishrinkhala kirtishrinkhala self-assigned this Apr 24, 2024
@pahud pahud added p2 and removed p1 labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-dynamodb Related to Amazon DynamoDB bug This issue is a bug. p2
Projects
None yet
Development

No branches or pull requests

4 participants