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): grant the Project's Role permissions to the KMS Key i… #2715

Merged
merged 1 commit into from
Jun 3, 2019
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
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ export interface CommonProjectProps {
*/
readonly allowAllOutbound?: boolean;
}

export interface ProjectProps extends CommonProjectProps {
/**
* The source of the build.
Expand Down Expand Up @@ -711,6 +712,10 @@ export class Project extends ProjectBase {
this.projectName = resource.projectName;

this.addToRolePolicy(this.createLoggingPermission());

if (props.encryptionKey) {
props.encryptionKey.grantEncryptDecrypt(this);
}
}

public get securityGroups(): ec2.ISecurityGroup[] {
Expand Down
43 changes: 42 additions & 1 deletion packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import codecommit = require('@aws-cdk/aws-codecommit');
import ec2 = require('@aws-cdk/aws-ec2');
import kms = require('@aws-cdk/aws-kms');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
Expand Down Expand Up @@ -714,7 +715,47 @@ export = {
})
, /Configure 'allowAllOutbound' directly on the supplied SecurityGroup/);
test.done();
}
},

'with a KMS Key adds decrypt permissions to the CodeBuild Role'(test: Test) {
const stack = new cdk.Stack();

const key = new kms.Key(stack, 'MyKey');

new codebuild.PipelineProject(stack, 'MyProject', {
encryptionKey: key,
});

expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
"PolicyDocument": {
"Statement": [
{}, // CloudWatch logs
{
"Action": [
"kms:Decrypt",
"kms:Encrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
],
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"MyKey6AB29FA6",
"Arn",
],
},
},
],
},
"Roles": [
{
"Ref": "MyProjectRole9BBE5233",
},
],
}));

test.done();
},
},

'using timeout and path in S3 artifacts sets it correctly'(test: Test) {
Expand Down
33 changes: 33 additions & 0 deletions packages/decdk/test/__snapshots__/synth.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,21 @@ Object {
},
],
},
Object {
"Action": Array [
"kms:Decrypt",
"kms:Encrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
],
"Effect": "Allow",
"Resource": Object {
"Fn::GetAtt": Array [
"Key961B73FD",
"Arn",
],
},
},
Object {
"Action": Array [
"s3:GetObject*",
Expand Down Expand Up @@ -1889,6 +1904,24 @@ Object {
},
"Resource": "*",
},
Object {
"Action": Array [
"kms:Decrypt",
"kms:Encrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
],
"Effect": "Allow",
"Principal": Object {
"AWS": Object {
"Fn::GetAtt": Array [
"BuildProjectRoleAA92C755",
"Arn",
],
},
},
"Resource": "*",
},
],
"Version": "2012-10-17",
},
Expand Down