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(codepipeline): an action's role imported in a different stack adds a dependency to the CodePipeline stack #6458

Merged
merged 2 commits into from
Feb 26, 2020
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
163 changes: 163 additions & 0 deletions packages/@aws-cdk/aws-codepipeline-actions/test/test.pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,5 +959,168 @@ export = {

test.done();
},

'adds a dependency on the Stack containing a new action Role'(test: Test) {
const region = 'us-west-2';
const pipelineAccount = '123456789012';
const buildAccount = '901234567890';
const app = new App();

const buildStack = new Stack(app, 'BuildStack', {
env: { account: buildAccount, region },
});
const actionRolePhysicalName = 'ProjectRolePhysicalName';
const actionRoleInOtherAccount = new iam.Role(buildStack, 'ProjectRole', {
assumedBy: new iam.AccountPrincipal(pipelineAccount),
roleName: actionRolePhysicalName,
});
const projectPhysicalName = 'ProjectPhysicalName';
const project = codebuild.Project.fromProjectName(buildStack, 'Project',
projectPhysicalName);

const pipelineStack = new Stack(app, 'PipelineStack', {
env: { account: pipelineAccount, region },
});
const bucket = new s3.Bucket(pipelineStack, 'ArtifactBucket', {
bucketName: 'source-bucket',
encryption: s3.BucketEncryption.KMS,
});
const sourceOutput = new codepipeline.Artifact();
new codepipeline.Pipeline(pipelineStack, 'Pipeline', {
artifactBucket: bucket,
stages: [
{
stageName: 'Source',
actions: [
new cpactions.S3SourceAction({
actionName: 'S3',
bucket,
bucketKey: 'path/to/file.zip',
output: sourceOutput,
}),
],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'CodeBuild',
project,
input: sourceOutput,
role: actionRoleInOtherAccount,
}),
],
},
],
});

expect(pipelineStack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
"Stages": [
{
"Name": "Source",
},
{
"Name": "Build",
"Actions": [
{
"Name": "CodeBuild",
"Configuration": {
"ProjectName": projectPhysicalName,
},
"RoleArn": {
"Fn::Join": ["", [
"arn:",
{ "Ref": "AWS::Partition" },
`:iam::${buildAccount}:role/${actionRolePhysicalName}`,
]],
},
},
],
},
],
}));

test.equal(pipelineStack.dependencies.length, 1);

test.done();
},

'does not add a dependency on the Stack containing an imported action Role'(test: Test) {
const region = 'us-west-2';
const pipelineAccount = '123456789012';
const buildAccount = '901234567890';
const app = new App();

const buildStack = new Stack(app, 'BuildStack', {
env: { account: buildAccount, region },
});
const actionRolePhysicalName = 'ProjectRolePhysicalName';
const actionRoleInOtherAccount = iam.Role.fromRoleArn(buildStack, 'ProjectRole',
`arn:aws:iam::${buildAccount}:role/${actionRolePhysicalName}`);
const projectPhysicalName = 'ProjectPhysicalName';
const project = new codebuild.PipelineProject(buildStack, 'Project', {
projectName: projectPhysicalName,
});

const pipelineStack = new Stack(app, 'PipelineStack', {
env: { account: pipelineAccount, region },
});
const bucket = new s3.Bucket(pipelineStack, 'ArtifactBucket', {
bucketName: 'source-bucket',
encryption: s3.BucketEncryption.KMS,
});
const sourceOutput = new codepipeline.Artifact();
new codepipeline.Pipeline(pipelineStack, 'Pipeline', {
artifactBucket: bucket,
stages: [
{
stageName: 'Source',
actions: [
new cpactions.S3SourceAction({
actionName: 'S3',
bucket,
bucketKey: 'path/to/file.zip',
output: sourceOutput,
}),
],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'CodeBuild',
project,
input: sourceOutput,
role: actionRoleInOtherAccount,
}),
],
},
],
});

expect(pipelineStack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
"Stages": [
{
"Name": "Source",
},
{
"Name": "Build",
"Actions": [
{
"Name": "CodeBuild",
"Configuration": {
"ProjectName": projectPhysicalName,
},
"RoleArn": `arn:aws:iam::${buildAccount}:role/${actionRolePhysicalName}`,
},
],
},
],
}));

test.equal(pipelineStack.dependencies.length, 0);

test.done();
},
},
};
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,14 @@ export class Pipeline extends PipelineBase {
if (action.actionProperties.role) {
if (this.isAwsOwned(action)) {
// the role has to be deployed before the pipeline
const roleStack = Stack.of(action.actionProperties.role);
pipelineStack.addDependency(roleStack);
// (our magical cross-stack dependencies will not work,
// because the role might be from a different environment),
// but _only_ if it's a new Role -
// an imported Role should not add the dependency
if (action.actionProperties.role instanceof iam.Role) {
const roleStack = Stack.of(action.actionProperties.role);
pipelineStack.addDependency(roleStack);
}

return action.actionProperties.role;
} else {
Expand Down