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

fix(codebuild): Secret env variable from another account fails on Key decryption #14226

Merged
merged 4 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 25 additions & 2 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { ArnComponents, Aws, Duration, IResource, Lazy, Names, PhysicalName, Resource, SecretValue, Stack, Token, Tokenization } from '@aws-cdk/core';
import { ArnComponents, Aws, Duration, IResource, Lazy, Names, PhysicalName, Resource, SecretValue, Stack, Token, TokenComparison, Tokenization } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { IArtifacts } from './artifacts';
import { BuildSpec } from './build-spec';
Expand Down Expand Up @@ -721,6 +721,7 @@ export class Project extends ProjectBase {
const ret = new Array<CfnProject.EnvironmentVariableProperty>();
const ssmIamResources = new Array<string>();
const secretsManagerIamResources = new Array<string>();
const kmsIamResources = new Set<string>();

for (const [name, envVariable] of Object.entries(environmentVariables)) {
const envVariableValue = envVariable.value?.toString();
Expand Down Expand Up @@ -790,7 +791,7 @@ export class Project extends ProjectBase {
// If we were given just a name, it must be partial, as CodeBuild doesn't support providing full names.
// In this case, we need to accommodate for the generated suffix in the IAM resource name
: `${secretName}-??????`;
secretsManagerIamResources.push(Stack.of(principal).formatArn({
secretsManagerIamResources.push(stack.formatArn({
service: 'secretsmanager',
resource: 'secret',
resourceName: secretIamResourceName,
Expand All @@ -800,6 +801,22 @@ export class Project extends ProjectBase {
account: parsedArn?.account,
region: parsedArn?.region,
}));
// if secret comes from another account, SecretsManager will need to access
// KMS on the other account as well to be able to get the secret
if (parsedArn && parsedArn.account && Token.compareStrings(parsedArn.account, stack.account) === TokenComparison.DIFFERENT) {
kmsIamResources.add(stack.formatArn({
service: 'kms',
resource: 'key',
// We do not know the ID of the key, but since this is a cross-account access,
// the key policies have to allow this access, so a wildcard is safe here
resourceName: '*',
Kruspe marked this conversation as resolved.
Show resolved Hide resolved
sep: '/',
// if we were given an ARN, we need to use the provided partition/account/region
partition: parsedArn.partition,
account: parsedArn.account,
region: parsedArn.region,
}));
}
}
}
}
Expand All @@ -817,6 +834,12 @@ export class Project extends ProjectBase {
resources: secretsManagerIamResources,
}));
}
if (kmsIamResources.size !== 0) {
principal?.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['kms:Decrypt'],
resources: Array.from(kmsIamResources),
}));
}

return ret;
}
Expand Down
88 changes: 88 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,17 @@ export = {
},
}));

// THEN
expect(stack).to(not(haveResourceLike('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': arrayWith({
'Action': 'kms:Decrypt',
'Effect': 'Allow',
'Resource': 'arn:aws:kms:us-west-2:123456789012:key/*',
}),
},
})));

test.done();
},

Expand Down Expand Up @@ -1098,6 +1109,83 @@ export = {
},
}));

// THEN
expect(stack).to(not(haveResourceLike('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': arrayWith({
'Action': 'kms:Decrypt',
'Effect': 'Allow',
'Resource': 'arn:aws:kms:us-west-2:123456789012:key/*',
}),
},
})));

test.done();
},

"when provided as a verbatim partial secret ARN from another account, adds permission to decrypt keys in the Secret's account"(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'ProjectStack', {
env: { account: '123456789012' },
});

// WHEN
new codebuild.PipelineProject(stack, 'Project', {
environmentVariables: {
'ENV_VAR1': {
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER,
value: 'arn:aws:secretsmanager:us-west-2:901234567890:secret:mysecret',
},
},
});

// THEN
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': arrayWith({
'Action': 'kms:Decrypt',
'Effect': 'Allow',
'Resource': 'arn:aws:kms:us-west-2:901234567890:key/*',
}),
},
}));

test.done();
},

'when two secrets from another account are provided as verbatim partial secret ARNs, adds only one permission for decrypting'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'ProjectStack', {
env: { account: '123456789012' },
});

// WHEN
new codebuild.PipelineProject(stack, 'Project', {
environmentVariables: {
'ENV_VAR1': {
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER,
value: 'arn:aws:secretsmanager:us-west-2:901234567890:secret:mysecret',
},
'ENV_VAR2': {
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER,
value: 'arn:aws:secretsmanager:us-west-2:901234567890:secret:other-secret',
},
},
});

// THEN
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': arrayWith({
'Action': 'kms:Decrypt',
'Effect': 'Allow',
'Resource': 'arn:aws:kms:us-west-2:901234567890:key/*',
}),
},
}));

test.done();
},

Expand Down