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): missing permissions for SecretsManager environment variables #12121

Merged
merged 6 commits into from Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 28 additions & 6 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Expand Up @@ -790,7 +790,7 @@ export class Project extends ProjectBase {
this.projectName = this.getResourceNameAttribute(resource.ref);

this.addToRolePolicy(this.createLoggingPermission());
this.addParameterStorePermission(props);
this.addEnvVariablesPermissions(props.environmentVariables);
// add permissions to create and use test report groups
// with names starting with the project's name,
// unless the customer explicitly opts out of it
Expand Down Expand Up @@ -922,12 +922,13 @@ export class Project extends ProjectBase {
});
}

private addParameterStorePermission(props: ProjectProps) {
if (!props.environmentVariables) {
return;
}
private addEnvVariablesPermissions(environmentVariables: { [name: string]: BuildEnvironmentVariable } | undefined): void {
this.addParameterStorePermissions(environmentVariables);
this.addSecretsManagerPermissions(environmentVariables);
}

const resources = Object.values(props.environmentVariables)
private addParameterStorePermissions(environmentVariables: { [name: string]: BuildEnvironmentVariable } | undefined): void {
const resources = Object.values(environmentVariables || {})
.filter(envVariable => envVariable.type === BuildEnvironmentVariableType.PARAMETER_STORE)
.map(envVariable =>
// If the parameter name starts with / the resource name is not separated with a double '/'
Expand All @@ -951,6 +952,27 @@ export class Project extends ProjectBase {
}));
}

private addSecretsManagerPermissions(environmentVariables: { [name: string]: BuildEnvironmentVariable } | undefined): void {
const resources = Object.values(environmentVariables || {})
.filter(envVariable => envVariable.type === BuildEnvironmentVariableType.SECRETS_MANAGER)
.map(envVariable => Stack.of(this).formatArn({
service: 'secretsmanager',
resource: 'secret',
// we don't know the exact ARN of the Secret just from its name, but we can get close
resourceName: `${envVariable.value}-??????`,
sep: ':',
}));

if (resources.length === 0) {
return;
}

this.addToRolePolicy(new iam.PolicyStatement({
actions: ['secretsmanager:GetSecretValue'],
resources,
}));
}

private renderEnvironment(
env: BuildEnvironment = {},
projectVars: { [name: string]: BuildEnvironmentVariable } = {}): CfnProject.EnvironmentProperty {
Expand Down
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Expand Up @@ -889,5 +889,44 @@ export = {

test.done();
},

iliapolo marked this conversation as resolved.
Show resolved Hide resolved

iliapolo marked this conversation as resolved.
Show resolved Hide resolved
"grants the Project's Role read permissions to the SecretsManager environment variables"(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new codebuild.PipelineProject(stack, 'Project', {
environmentVariables: {
'ENV_VAR1': {
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER,
value: 'my-secret',
},
},
});

// THEN
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': arrayWith({
'Action': 'secretsmanager:GetSecretValue',
'Effect': 'Allow',
'Resource': {
'Fn::Join': ['', [
'arn:',
{ Ref: 'AWS::Partition' },
':secretsmanager:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':secret:my-secret-??????',
]],
},
}),
},
}));

test.done();
},
},
};