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): allow use of custom role for pipeline #21299

Merged
merged 4 commits into from
Aug 1, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/@aws-cdk/pipelines/lib/codepipeline/codepipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ export interface CodePipelineProps {
* @default - true (Use the same support stack for all pipelines in App)
*/
readonly reuseCrossRegionSupportStacks?: boolean;

/**
* The IAM role to be assumed by this Pipeline
*
* @default - A new role is created
*/
readonly role?: iam.IRole;
}

/**
Expand Down Expand Up @@ -362,6 +369,9 @@ export class CodePipeline extends PipelineBase {
if (this.props.reuseCrossRegionSupportStacks !== undefined) {
throw new Error('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\'');
}
if (this.props.role !== undefined) {
throw new Error('Cannot set \'role\' if an existing CodePipeline is given using \'codePipeline\'');
}

this._pipeline = this.props.codePipeline;
} else {
Expand All @@ -372,6 +382,7 @@ export class CodePipeline extends PipelineBase {
// This is necessary to make self-mutation work (deployments are guaranteed
// to happen only after the builds of the latest pipeline definition).
restartExecutionOnUpdate: true,
role: this.props.role,
});
}

Expand Down
65 changes: 65 additions & 0 deletions packages/@aws-cdk/pipelines/test/codepipeline/codepipeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Template, Annotations, Match } from '@aws-cdk/assertions';
import * as ccommit from '@aws-cdk/aws-codecommit';
import { Pipeline } from '@aws-cdk/aws-codepipeline';
import * as iam from '@aws-cdk/aws-iam';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cdkp from '../../lib';
import { CodePipeline } from '../../lib';
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp } from '../testhelpers';

let app: TestApp;
Expand Down Expand Up @@ -88,6 +91,15 @@ describe('Providing codePipeline parameter and prop(s) of codePipeline parameter
reuseCrossRegionSupportStacks: true,
}).create()).toThrowError('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\'');
});
test('Providing codePipeline parameter and role parameter should throw error', () => {
const stack = new Stack(app, 'Stack');

expect(() => new CodePipelinePropsCheckTest(stack, 'CodePipeline', {
role: new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'),
}),
}).create()).toThrowError('Cannot set \'role\' if an existing CodePipeline is given using \'codePipeline\'');
});
});

test('Policy sizes do not exceed the maximum size', () => {
Expand Down Expand Up @@ -180,6 +192,51 @@ test('CodeBuild action role has the right AssumeRolePolicyDocument', () => {
});
});

test('CodePipeline supports use of existing role', () => {
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
const repo = new ccommit.Repository(pipelineStack, 'Repo', {
repositoryName: 'MyRepo',
});
const cdkInput = cdkp.CodePipelineSource.codeCommit(
repo,
'main',
);

new CodePipeline(pipelineStack, 'Pipeline', {
synth: new cdkp.ShellStep('Synth', {
input: cdkInput,
installCommands: ['npm ci'],
commands: [
'npm run build',
'npx cdk synth',
],
}),
role: new iam.Role(pipelineStack, 'CustomRole', {
assumedBy: new iam.ServicePrincipal('codepipeline.amazonaws.com'),
roleName: 'MyCustomPipelineRole',
}),
});

const template = Template.fromStack(pipelineStack);
template.hasResourceProperties('AWS::IAM::Role', {
AssumeRolePolicyDocument: {
Statement: [
{
Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: {
Service: 'codepipeline.amazonaws.com',
},
},
],
},
RoleName: 'MyCustomPipelineRole',
});
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
RoleArn: { 'Fn::GetAtt': ['CustomRole6D8E6809', 'Arn'] },
});
});

interface ReuseCodePipelineStackProps extends cdk.StackProps {
reuseCrossRegionSupportStacks?: boolean;
}
Expand Down Expand Up @@ -241,6 +298,7 @@ interface CodePipelineStackProps extends cdk.StackProps {
pipelineName?: string;
crossAccountKeys?: boolean;
reuseCrossRegionSupportStacks?: boolean;
role?: iam.IRole;
}

class CodePipelinePropsCheckTest extends cdk.Stack {
Expand Down Expand Up @@ -271,5 +329,12 @@ class CodePipelinePropsCheckTest extends cdk.Stack {
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
}).buildPipeline();
}
if (this.cProps.role !== undefined) {
new cdkp.CodePipeline(this, 'CodePipeline4', {
role: this.cProps.role,
codePipeline: new Pipeline(this, 'Pipline4'),
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
}).buildPipeline();
}
}
}