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

[pipelines] add custom PolicyStatement to the ShellScriptAction #9600

Closed
2 tasks
am29d opened this issue Aug 11, 2020 · 8 comments · Fixed by #10149
Closed
2 tasks

[pipelines] add custom PolicyStatement to the ShellScriptAction #9600

am29d opened this issue Aug 11, 2020 · 8 comments · Fixed by #10149
Assignees
Labels
@aws-cdk/pipelines CDK Pipelines library effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md needs-triage This issue or PR still needs to be triaged. p1

Comments

@am29d
Copy link

am29d commented Aug 11, 2020

Hi,

I have used the new pipeline constructs recently and added a custom stage with a ShellScriptAction and custom sam cli commands such as sam package and sam publish. The issue I have encountered is that it is not easy to add custom PolicyStatement to the role, that is associated with the stage. Given the nature of a ShellScriptAction to be generic step in a pipeline running bash commands, it would be great to pass a specific IAM PolicyStatement to a corresponding action role.

I have found a way to do that, but this is not an easy task, this is how it resolved now:

const publishStageNode = this.node?.tryFindChild('Pipeline')?.node.tryFindChild('Pipeline')?.node.tryFindChild('publishStageNode')
publishStageNode?.node.tryFindChild('publishAction')?.node.children?.forEach(item => {
    if (item instanceof PipelineProject) {
        item.addToRolePolicy(allowCreateLayerSererlessRepoPolicy);
        item.addToRolePolicy(allowUploadToS3Policy);
     }
})

As you can see this is not an easy way to fetch the stage and the PipelineProject construct that is nested within the tree. Furthermore, I have to loop through the children, because in some cases there is a Role and a PipelineProject construct.

Proposed Solution

it would be great to add a policy statement directly to an action i.e.:

const policyStatement = new iam.PolicyStatement({...});

const action = new ShellScriptAction({
    actionName: 'mySpecialShellScriptAction',
    commands: [_some_bash_commands_here_]
});

action.addToRolePolicy(policyStatement);

Other

Pinging @webdog as per request.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@am29d am29d added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 11, 2020
@github-actions github-actions bot added the @aws-cdk/pipelines CDK Pipelines library label Aug 11, 2020
@am29d am29d changed the title [pipelines] [pipelines] add custom PolicyStatement to the ShellScriptAction Aug 11, 2020
@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p1 labels Aug 12, 2020
@rix0rrr rix0rrr added this to the [CDK Pipelines] Soon milestone Aug 12, 2020
@Chriscbr
Copy link
Contributor

@am29d I have a question, is it possible to use the project property (like below) to add the desired policy statement? Or does this project not refer to the right stage / is this not doable because of the order in which objects are created/binded together, or something else?

myAction.project.addToRolePolicy(myPolicyStatement);

Even if this was a solution though, I could still see the value in adding an extra method for adding the role since it makes the mental model a bit simpler. (I'm also a bit new to CodePipelines so forgive me if I'm misunderstanding the issue 😅)

@am29d
Copy link
Author

am29d commented Aug 26, 2020

Hi @Chriscbr, you are absolutely correct! The project object can be accessed via the action, but I would have never found it. As you said, from the mental model I would expect to use the method from the action. Important catch, make sure to add the action to the project first, and then add the policy, otherwise you will end up with an error that the stage does not have any actions:

const buildStage = pipeline.addStage('BuildStage');
const shellScriptAction = new ShellScriptAction({
  actionName: "shellScriptAction",
  commands: [
    "echo foo"
  ],
  additionalArtifacts: [sourceArtifact],
  runOrder: buildStage.nextSequentialRunOrder()
});

buildStage.addActions(shellScriptAction);

shellScriptAction.project.addToRolePolicy(new PolicyStatement({
  effect: Effect.ALLOW,
  actions: [
      "s3:PutObject"
  ],
  resources: ["*"]
}));

I have also tested it with multiple actions and PolicyStatements, the least privilege principle is working as expected and each step has its own permissions, because they are in a separate CodeBuild project. Maybe it is worth to update the docs to show how custom permissions can be added to the custom stage?

Thanks a lot for the hint, @Chriscbr !

@brianfoody
Copy link

@am29d @Chriscbr do you know how to do this for cross-account deployments?

I have a build account where the pipeline runs and deploys the stack to a staging account. I want to assume a role in the staging account to execute the acceptance tests on but I'm not sure how to do this with CDK pipelines.

rix0rrr added a commit that referenced this issue Sep 3, 2020
Allow more control over the IAM permissions for the execution role
of a `ShellScriptAction`. Statements can be added at construction
time, the object can also be used as a Grantable.

Fixes #9600.
@mergify mergify bot closed this as completed in #10149 Sep 4, 2020
mergify bot pushed a commit that referenced this issue Sep 4, 2020
Allow more control over the IAM permissions for the execution role
of a `ShellScriptAction`. Statements can be added at construction
time, the object can also be used as a Grantable.

Fixes #9600.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@michaelfecher
Copy link

@am29d @Chriscbr do you know how to do this for cross-account deployments?

I have a build account where the pipeline runs and deploys the stack to a staging account. I want to assume a role in the staging account to execute the acceptance tests on but I'm not sure how to do this with CDK pipelines.

Did you find that out in the meantime? I'm having the same issue

@brianfoody
Copy link

@michaelfecher no circled back to it a couple of times when I've had a spare 15 mins but haven't cracked it yet.

@brianfoody
Copy link

Got it working by creating a role in the testing account and assuming it in my tests from the build account.

Screenshot from 2020-10-14 17-24-50 (1)
Screenshot from 2020-10-14 17-26-48 (1)

@am29d
Copy link
Author

am29d commented Nov 4, 2020

@michaelfecher I have just stumbled upon this nice workshop that guides you through a cross account deployment with CDK, take a look: https://github.com/aws-samples/aws-cross-account-cicd-pipeline

@pfried
Copy link

pfried commented Jan 2, 2023

@brianfoody This is actually the solution to many cross-account pipeline actions (e.g. this one). In my case deploying a webapp to S3 and creating a cloudformation invalidation. IMHO this "pattern" should be referenced in the pipelines docs, so thanks for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/pipelines CDK Pipelines library effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md needs-triage This issue or PR still needs to be triaged. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants