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-actions): CodeCommit source action fails when it's cross-account #14260

Merged
merged 2 commits into from
Apr 21, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as codecommit from '@aws-cdk/aws-codecommit';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as targets from '@aws-cdk/aws-events-targets';
import * as iam from '@aws-cdk/aws-iam';
import { Names, Token } from '@aws-cdk/core';
import { Names, Stack, Token, TokenComparison } from '@aws-cdk/core';
import { Action } from '../action';
import { sourceArtifactBounds } from '../common';

Expand Down Expand Up @@ -171,6 +171,11 @@ export class CodeCommitSourceAction extends Action {
// the Action will write the contents of the Git repository to the Bucket,
// so its Role needs write permissions to the Pipeline Bucket
options.bucket.grantReadWrite(options.role);
// when this action is cross-account,
// the Role needs the s3:PutObjectAcl permission for some not yet fully understood reason
if (Token.compareStrings(this.props.repository.env.account, Stack.of(stage.pipeline).account) === TokenComparison.DIFFERENT) {
options.bucket.grantPutAcl(options.role);
}

// https://docs.aws.amazon.com/codecommit/latest/userguide/auth-and-access-control-permissions-reference.html#aa-acp
options.role.addToPrincipalPolicy(new iam.PolicyStatement({
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-codepipeline-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-events-targets": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-s3": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
Expand All @@ -110,6 +111,7 @@
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-events-targets": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-s3": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as iam from '@aws-cdk/aws-iam';
import { Stack, Lazy } from '@aws-cdk/core';
import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import { Stack, Lazy, App } from '@aws-cdk/core';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as cpactions from '../../lib';

Expand Down Expand Up @@ -429,6 +431,60 @@ nodeunitShim({

test.done();
},

'grants explicit s3:PutObjectAcl permissions when the Actions is cross-account'(test: Test) {
const app = new App();

const repoStack = new Stack(app, 'RepoStack', {
env: { account: '123', region: 'us-east-1' },
});
const repoFomAnotherAccount = codecommit.Repository.fromRepositoryName(repoStack, 'Repo', 'my-repo');

const pipelineStack = new Stack(app, 'PipelineStack', {
env: { account: '456', region: 'us-east-1' },
});
new codepipeline.Pipeline(pipelineStack, 'Pipeline', {
artifactBucket: s3.Bucket.fromBucketAttributes(pipelineStack, 'PipelineBucket', {
bucketName: 'pipeline-bucket',
encryptionKey: kms.Key.fromKeyArn(pipelineStack, 'PipelineKey',
'arn:aws:kms:us-east-1:456:key/my-key'),
}),
stages: [
{
stageName: 'Source',
actions: [new cpactions.CodeCommitSourceAction({
actionName: 'Source',
output: new codepipeline.Artifact(),
repository: repoFomAnotherAccount,
})],
},
{
stageName: 'Approve',
actions: [new cpactions.ManualApprovalAction({
actionName: 'Approve',
})],
},
],
});

expect(repoStack).to(haveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: arrayWith({
'Action': 's3:PutObjectAcl',
'Effect': 'Allow',
'Resource': {
'Fn::Join': ['', [
'arn:',
{ 'Ref': 'AWS::Partition' },
':s3:::pipeline-bucket/*',
]],
},
}),
},
}));

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

Expand Down