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

CodeCommitSourceAction tries to add a policy to an imported role #3025

Closed
kadishmal opened this issue Jun 24, 2019 · 13 comments · Fixed by #3716
Closed

CodeCommitSourceAction tries to add a policy to an imported role #3025

kadishmal opened this issue Jun 24, 2019 · 13 comments · Fixed by #3716
Assignees
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline @aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. effort/small Small work item – less than a day of effort p0

Comments

@kadishmal
Copy link

In an attempt to build a cross account codepipeline, I have used CodeCommitSourceAction as follows:

    // Tools account
    const actionProps: any = {};

    // Since `CodeCommitSourceAction` doesn't accept a role (in TypeScript) even though the
    // abstract `Action` does, have to pass the role via an object of `any` type.
    actionProps.role = Role.fromRoleArn(this, 'code-commit-role', codeCommitRoleArn);
    
    const sourceAction = new CodeCommitSourceAction({
      actionName: 'CodeCommit',
      repository: codeCommitRepository,
      output: sourceOutput,
      ...actionProps
    });

When using Role.fromRoleArn I assume that the role exists, potentially in a different account (it is in a different Dev account). However CodeCommitSourceAction tries to create a policy for it according to its source code https://github.com/awslabs/aws-cdk/blob/master/packages/%40aws-cdk/aws-codepipeline-actions/lib/codecommit/source-action.ts#L95-L105 which results in the following resource in the template:

  codecommitrolePolicyC2DD4708:
    Type: AWS::IAM::Policy
    Properties:
      PolicyDocument:
        Statement:
          - Action:
              - codecommit:GetBranch
              - codecommit:GetCommit
              - codecommit:UploadArchive
              - codecommit:GetUploadArchiveStatus
              - codecommit:CancelUploadArchive
            Effect: Allow
            Resource: arn:aws:codecommit:us-west-2:123456789012:repository
        Version: "2012-10-17"
      PolicyName: codecommitrolePolicyC2DD4708
      Roles:
        - ToolsAcctCodePipelineCodeCommitRole
    Metadata:
      aws:cdk:path: ToolsCodePipelineStack/code-commit-role/Policy/Resource

This fails because ToolsAcctCodePipelineCodeCommitRole, obviously, doesn't exist in this Tools account. It is in the Dev account.

I think CodeCommitSourceAction should be able to distinguish if the role is imported or not and add only if the role is auto created by the same account.

@skinny85
Copy link
Contributor

Thanks for reporting @kadishmal .

@rix0rrr is this a consequence of the recent change to add Policies to imported Roles?

@skinny85 skinny85 added the bug This issue is a bug. label Jun 24, 2019
@NGL321 NGL321 added @aws-cdk/aws-codecommit Related to AWS CodeCommit @aws-cdk/aws-iam Related to AWS Identity and Access Management labels Jun 24, 2019
@skinny85 skinny85 added @aws-cdk/aws-codepipeline Related to AWS CodePipeline and removed @aws-cdk/aws-codecommit Related to AWS CodeCommit labels Jun 26, 2019
@cbrothersnow
Copy link

cbrothersnow commented Jul 12, 2019

I have the same. Policies are being added to the imported roles.

I created roles and pipeline with sceptre that work for sceptre. When I modify the infrastructure build to use cdk deploy, I get this error "AccessDenied: Cross-account pass role is not allowed ", so something in the cdk deploy is looking for a missing policy in the cross-account imported role, or you've hard-coded that error in the cdk deploy? idk

I'm using this: cdk deploy --path-metadata=false --role-arn=$rolearn --app "node bin/$env/event-guardduty.js" --output=templates
where $rolearn is the arn of the role in the other account. Is that the wrong way to do this?

@skinny85
Copy link
Contributor

I don't think the two are related @cbrothersnow .

"AccessDenied: Cross-account pass role is not allowed "

That's an IAM error, not a CDK error. We definitely don't do that validation in the CDK.

I'm afraid what you're trying to do can't be achieved in IAM (unless I'm missing something).

@cbrothersnow
Copy link

cbrothersnow commented Jul 12, 2019 via email

@shashanktomar
Copy link

shashanktomar commented Jul 16, 2019

We are facing exactly the same issue while writing a cross-account pipeline. We are facing it in CloudFormationCreateReplaceChangeSetAction. Is there any workaround this issue before it is fixed?

@GisaldjoPurbollari
Copy link

Facing the same issue when importing ecs execution role. Although the role has the permissions, the template produced by cdk attempts to attach a policy with all the ecs permissions (as well as log stream permissions, because I use them elsewhere on the template). If you're importing a role, you should have the option of not adding new permissions to said role. Highly applicable to accounts that don't have permissions to create/modify roles.

@cbrothersnow
Copy link

I finally got this working, and it's not at all an issue with CDK. Sceptre must do some "magic" when it does a sceptre deploy with the kms keys. The problem was just my misunderstanding of how to create a cross account pipeline. The first thing I did was remove imported roles and let CDK do it's thing. Then I added cross account stuff to the roles. The final piece for me was realizing I needed to put the custom encryption key in the artifact bucket, as well as in the CodeBuild project phase. Figuring it out little by little.

@nija-at
Copy link
Contributor

nija-at commented Jul 30, 2019

I'm having the same problem described here.

I am using CDK to set up a CodePipeline where its deployment stage deploys the application (a CF template) to another AWS account.

To achieve this, I’m importing an existing IAM role that’s in the deployment account using Role.fromRoleARN() and then setting the role and the deploymentRole properties of CloudFormationCreateUpdateStackActionProps construct to the imported IAM role.

The CDK synthesised template specifies a policy and attaches it to the imported role, hence failing the CloudFormation deploy.

@sriram-mv
Copy link

sriram-mv commented Jul 30, 2019

same exact issue here, I'm importing a role using the arn. This role is a cross account role, but CDK seems to be adding a policy to this and also assumes that the cross account role is present in the current account. The only way I have gotten to this to work is to let cdk synthesize the template. Manually the remove the cross account policy. and then deploy using aws cloudformation deploy.

This is on cdk 1.1. Note that similar code works well on cdk v0.34

@skinny85 skinny85 self-assigned this Aug 12, 2019
@skinny85 skinny85 added p0 effort/small Small work item – less than a day of effort labels Aug 12, 2019
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Aug 19, 2019
…ame account as the owning stack

We also allow adding policies to the imported role if the owning stack does not have an account specified.

Fixes aws#3025
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Aug 19, 2019
…ame account as the owning stack

We also allow adding policies to the imported role if the owning stack does not have an account specified.

Fixes aws#3025
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Aug 30, 2019
…ame account as the owning stack

We also allow adding policies to the imported role if the owning stack does not have an account specified,
or if the ARN used for importing was dynamic.

Fixes aws#3025
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Aug 30, 2019
…ame account as the owning stack

We also allow adding policies to the imported role if the owning stack does not have an account specified,
or if the ARN used for importing was dynamic.

Fixes aws#3025
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Aug 31, 2019
…ame account as the owning stack

We also allow adding policies to the imported role if the owning stack does not have an account specified,
or if the ARN used for importing was dynamic.

Fixes aws#3025
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Sep 4, 2019
…ame account as the owning stack

We also allow adding policies to the imported role if the owning stack does not have an account specified,
or if the ARN used for importing was dynamic.

Fixes aws#3025
skinny85 added a commit to skinny85/aws-cdk that referenced this issue Sep 5, 2019
We allow attaching policies to the imported role if the account passed in the ARN
matches the account of the stack the policy belongs to,
or if either one of those accounts was not specified (i.e., is env-agnostic),
or if the ARN used for importing was dynamic.

Fixes aws#2985
Fixes aws#3025
@mergify mergify bot closed this as completed in #3716 Sep 6, 2019
mergify bot pushed a commit that referenced this issue Sep 6, 2019
#3716)

We allow attaching policies to the imported role if the account passed in the ARN
matches the account of the stack the policy belongs to,
or if either one of those accounts was not specified (i.e., is env-agnostic),
or if the ARN used for importing was dynamic.

Fixes #2985
Fixes #3025
@kiranch2004
Copy link

I got the same issue, and I am switching back to serverless framework... I will probably use this framework once its more matured.

@skinny85
Copy link
Contributor

Sorry to the poor experience @kiranch2004 . If you need some help diagnosing the issue, let me know.

@skinny85
Copy link
Contributor

@TamasBartosMentor when importing the Roles, you should pass the mutable parameter as false to the last argument of the call to fromRoleArn: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iam.Role.html#static-from-wbr-role-wbr-arnscope-id-rolearn-options . For example, in TypeScript:

const myRole = iam.Role.fromRoleArn(this, 'MyRole', 'arn:aws:iam::123456789012:role/MyRole', {
  mutable: false,
});

@dkhainas
Copy link

I'm wondering why is the default behaviour to mutate the role if it's ARN from another account. It's failing the CloudFormation deployment. I'm trying to achieve https://github.com/awslabs/aws-refarch-cross-account-pipeline/blob/master/ToolsAcct/code-pipeline.yaml, and failed until I added mutable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline @aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. effort/small Small work item – less than a day of effort p0
Projects
None yet
10 participants