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

feat(pipelines): add PolicyStatements to CodeBuild project role #9527

Merged
Show file tree
Hide file tree
Changes from 9 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
35 changes: 35 additions & 0 deletions packages/@aws-cdk/pipelines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,41 @@ const validationAction = new ShellScriptAction({
});
```

#### Add Additional permissions to the CodeBuild Project Role for building and synthing

You can customize the role permissions used by the CodeBuild project so it has access to
the needed resources. eg: Adding CodeArtifact repo permissions so we pull npm packages
from the CA repo instead of NPM.

```ts
class MyPipelineStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
...
const pipeline = new CdkPipeline(this, 'Pipeline', {
...
synthAction: SimpleSynthAction.standardNpmSynth({
sourceArtifact,
cloudAssemblyArtifact,

// Use this to customize and a permissions required for the build
// and synth
rolePolicyStatements: [
new PolicyStatement({
actions: ['codeartifact:*', 'sts:GetServiceBearerToken'],
resources: ['arn:codeartifact:repo:arn'],
}),
],

// Then you can login to codeartifact repository
// and npm will now pull packages from your repository
// Note the codeartifact login command requires more params to work.
buildCommand: 'aws codeartifact login --tool npm && npm run build',
}),
});
}
}
```

## CDK Environment Bootstrapping

An *environment* is an *(account, region)* pair where you want to deploy a
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/pipelines/lib/synths/simple-synth-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';
import * as events from '@aws-cdk/aws-events';
import { PolicyStatement } from '@aws-cdk/aws-iam';
import { Construct } from '@aws-cdk/core';
import { cloudAssemblyBuildSpecDir } from '../private/construct-internals';
import { copyEnvironmentVariables, filterEmpty } from './_util';
Expand Down Expand Up @@ -77,6 +78,15 @@ export interface SimpleSynthOptions {
* @default - No additional artifacts generated
*/
readonly additionalArtifacts?: AdditionalArtifact[];

/**
* Policy statements to add to role used during the synth
*
* Can be used to add acces to a CodeArtifact repository etc.
*
* @default - No policy statements added to CodeBuild Project Role
*/
readonly rolePolicyStatements?: PolicyStatement[];
}

/**
Expand Down Expand Up @@ -235,6 +245,12 @@ export class SimpleSynthAction implements codepipeline.IAction {
},
});

if (this.props.rolePolicyStatements !== undefined) {
this.props.rolePolicyStatements.forEach(policyStatement => {
project.addToRolePolicy(policyStatement);
});
}

this._action = new codepipeline_actions.CodeBuildAction({
actionName: this.actionProperties.actionName,
input: this.props.sourceArtifact,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { arrayWith, deepObjectLike } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import { PolicyStatement } from '@aws-cdk/aws-iam';
import { Stack } from '@aws-cdk/core';
import * as cdkp from '../lib';
import { PIPELINE_ENV, TestApp, TestGitHubNpmPipeline } from './testutil';

let app: TestApp;
let pipelineStack: Stack;
let sourceArtifact: codepipeline.Artifact;
let cloudAssemblyArtifact: codepipeline.Artifact;

beforeEach(() => {
app = new TestApp({ outdir: 'testcdk.out' });
pipelineStack = new Stack(app, 'PipelineStackPolicy', { env: PIPELINE_ENV });
sourceArtifact = new codepipeline.Artifact();
cloudAssemblyArtifact = new codepipeline.Artifact('CloudAsm');
});

afterEach(() => {
app.cleanup();
});

test('Build project includes codeartifact policy statements for role', () => {
// WHEN
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {
sourceArtifact,
cloudAssemblyArtifact,
synthAction: cdkp.SimpleSynthAction.standardNpmSynth({
sourceArtifact,
cloudAssemblyArtifact,
rolePolicyStatements: [
new PolicyStatement({
actions: ['codeartifact:*', 'sts:GetServiceBearerToken'],
resources: ['arn:my:arn'],
}),
],
}),
});

// THEN
expect(pipelineStack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: arrayWith(deepObjectLike({
Action: [
'codeartifact:*',
'sts:GetServiceBearerToken',
],
Resource: 'arn:my:arn',
})),
},
});
});