aws-cdk: (aws-cdk): IAM role created by CDK Bootstrap is not following AWS best practices

What is the problem?

Security Hub best practices suggest that IAM roles should not be given encrypt/decrypt permissions to all KMS keys (source).

The deployment role created by CDK bootstrap does not meet this criteria.

Reproduction Steps

Run cdk bootstrap and check the role policy on the deployment role (cdk-hnb659fds-deploy-role-787743944430-eu-west-1).

What did you expect to happen?

The bootstrap process should generate roles which conform to AWS best practices.

What actually happened?

The CDK bootstrap creates an IAM policy which contains a statement which looks like the following:

        {
            "Condition": {
                "StringEquals": {
                    "kms:ViaService": "s3.eu-west-1.amazonaws.com"
                }
            },
            "Action": [
                "kms:Decrypt",
                "kms:DescribeKey",
                "kms:Encrypt",
                "kms:ReEncrypt*",
                "kms:GenerateDataKey*"
            ],
            "Resource": "*",
            "Effect": "Allow",
            "Sid": "PipelineCrossAccountArtifactsKey"
        },

This is therefore not following the best practice control guidelines within Security Hub which are proposed by AWS.

CDK CLI Version

2.15.0 (build 151055e)

Framework Version

No response

Node.js Version

16.13.1

OS

Ubuntu

Language

Typescript, Python, .NET, Java, Go

Language Version

No response

Other information

Based on the template file (packages/aws-cdk/lib/api/bootstrap/bootstrap-template.yaml) I believe that this can be resolved by changing the PipelineCrossAccountArtifactsKey statement in the DeploymentActionRole to use a single KMS resource instead of *.

For example:

              - Sid: PipelineCrossAccountArtifactsKey
                # Use keys only for the purposes of reading encrypted files from S3.
                Effect: Allow
                Action:
                  - kms:Decrypt
                  - kms:DescribeKey
                  - kms:Encrypt
                  - kms:ReEncrypt*
                  - kms:GenerateDataKey*
                Resource: "*"
                Condition:
                  StringEquals:
                    kms:ViaService:
                      Fn::Sub: s3.${AWS::Region}.amazonaws.com

could be updated to:

              - Sid: PipelineCrossAccountArtifactsKey
                # Use keys only for the purposes of reading encrypted files from S3.
                Effect: Allow
                Action:
                  - kms:Decrypt
                  - kms:DescribeKey
                  - kms:Encrypt
                  - kms:ReEncrypt*
                  - kms:GenerateDataKey*
                Resource: "arn:aws:kms:*:*:alias/SOME_ALIAS_NAME"
                Condition:
                  StringEquals:
                    kms:ViaService:
                      Fn::Sub: s3.${AWS::Region}.amazonaws.com

However, I do not know enough about the internal working of the CDK pipelines to understand if this could use an alias. Perhaps there’s another way to achieve this?

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 13
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

The Condition makes it so that the role does not have permissions to all keys. It only has permissions to keys that protect data in S3 buckets that the role is allowed to read, and that the KMS key itself allows permission for using its key policy.

Security Hub’s finding seems a bit overeager here.

You’re right. Security Hub is often a little over jealous in it’s assessments and it often misses additional information like the conditions. However, I think currently that key could be used to encrypt data in any S3 bucket within the account. It wouldn’t be hard for someone to use it incorrectly on another bucket.

Definitely not a huge security risk but it’s not ideal having every account using the CDK being non-compliant with the foundational checks in Security Hub.