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

Using imported code build project for CodeBuildAction results in "resource imported without a role" #4613

Closed
Stompf opened this issue Oct 21, 2019 · 2 comments · Fixed by #4637
Assignees
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline bug This issue is a bug.

Comments

@Stompf
Copy link

Stompf commented Oct 21, 2019

I'm trying to use an imported code build project in a CodeBuildAction imported from @aws-cdk/aws-codepipeline-actions but it results in an error: "Cannot get policy fragment of Stack/CodeBuild, resource imported without a role". My use case is that I want to use a code build in a different AWS account in my code pipeline with a cross account role.

Reproduction Steps

I have a cross account role like this:

const deployRole = iam.Role.fromRoleArn(
        stack,
        `DeployRole`,
        `arn:aws:iam::${accountId}:role/DeployRole`,
        {
            mutable: false,
        },
    );

this role is used for my CodeBuildAction in my pipeline:

const codeBuildProject = codebuild.PipelineProject.fromProjectName(
        stack,
        `CodeBuild`,
        'codeBuildProjectNameInAnotherAccount',
);

const buildAction = new codepipeline_actions.CodeBuildAction({
        actionName: 'CodeBuild',
        project: codeBuildProject,
        input: artifact,
        role: deployRole,
});

pipeline.addStage({
        stageName: `Build`,
        actions: [buildAction],
    });

Now when running cdk synth i get the error.

Error Log

> cdk synth > template.yml

/node_modules/@aws-cdk/aws-iam/lib/unknown-principal.js:22
        throw new Error(`Cannot get policy fragment of ${this.resource.node.path}, resource imported without a role`);
        ^

Error: Cannot get policy fragment of Stack/CodeBuild, resource imported without a role
    at UnknownPrincipal.get policyFragment [as policyFragment] (/node_modules/@aws-cdk/aws-iam/lib/unknown-principal.js:22:15)
    at PolicyStatement.addPrincipals (/node_modules/@aws-cdk/aws-iam/lib/policy-statement.js:64:40)
    at new PolicyStatement (/node_modules/@aws-cdk/aws-iam/lib/policy-statement.js:27:14)
    at Function.addToPrincipalAndResource (/node_modules/@aws-cdk/aws-iam/lib/grant.js:68:27)
    at Import.grant (/node_modules/@aws-cdk/aws-kms/lib/key.js:63:26)
    at Import.grant (/node_modules/@aws-cdk/aws-s3/lib/bucket.js:247:32)
    at Import.grantRead (/node_modules/@aws-cdk/aws-s3/lib/bucket.js:140:21)
    at CodeBuildAction.bound (/node_modules/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.js:59:28)
    at CodeBuildAction.bind (/node_modules/@aws-cdk/aws-codepipeline-actions/lib/action.js:23:21)
    at Pipeline._attachActionToPipeline (/node_modules/@aws-cdk/aws-codepipeline/lib/pipeline.js:199:41)
Subprocess exited with error 1

Environment

  • CLI Version : 1.13.1
  • Framework Version: 1.13.1
  • OS : Ubuntu 18.04 LTS
  • Language : TypeScript

Other

It looks like the problem is it tries to attach bucket policies to the UnknownPrincipal. If I uncomment options.bucket.grantRead(this.props.project); in /node_modules/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.js the cloud formation is generated with no problems and after deploying it the pipeline works. Is there a way to get around this issue without chaining the source files?


This is 🐛 Bug Report

@Stompf Stompf added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 21, 2019
@SomayaB SomayaB added the @aws-cdk/aws-codebuild Related to AWS CodeBuild label Oct 21, 2019
@skinny85
Copy link
Contributor

Hey @Stompf ,

thanks for opening the issue. I was able to reproduce it locally, unfortunately the problem (in my opinion) is in the guts of the IAM library, and how it deals with unknown principals. It won't be an easy fix, I'm afraid :(.

I'm not even sure how to unblock you here... can you find out what Role does the CodeBuild project you're importing use? If we had a method like Project.fromProjectAttributes(), we could allow you to pass that (imported) Role when importing the Project, and that should solve it...

Sorry I don't have better news here :(.

Thanks,
Adam

@skinny85
Copy link
Contributor

Actually, scratch that. I think I might have an idea for an easy fix. Stay tuned.

skinny85 added a commit to skinny85/aws-cdk that referenced this issue Oct 22, 2019
Because of the way the IAM library was used in the CodeBuild CodePipeline action,
it was not possible to provide an imported project to the action
(it would result in an error being thrown from the IAM library for the KMS key permissions).

Fixes aws#4613
@skinny85 skinny85 added @aws-cdk/aws-codepipeline Related to AWS CodePipeline and removed needs-triage This issue or PR still needs to be triaged. @aws-cdk/aws-codebuild Related to AWS CodeBuild labels Oct 22, 2019
@SomayaB SomayaB added the in-progress This issue is being actively worked on. label Oct 22, 2019
@mergify mergify bot closed this as completed in #4637 Oct 23, 2019
mergify bot pushed a commit that referenced this issue Oct 23, 2019
…cts (#4637)

Because of the way the IAM library was used in the CodeBuild CodePipeline action,
it was not possible to provide an imported project to the action
(it would result in an error being thrown from the IAM library for the KMS key permissions).

Fixes #4613
@peterwoodworth peterwoodworth removed the in-progress This issue is being actively worked on. label Jun 28, 2021
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 bug This issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants