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

feat(iam): apply permissions boundary to a Stage #22913

Merged
merged 10 commits into from
Nov 18, 2022
Merged

feat(iam): apply permissions boundary to a Stage #22913

merged 10 commits into from
Nov 18, 2022

Conversation

corymhall
Copy link
Contributor

@corymhall corymhall commented Nov 14, 2022

For the implementation, there are two components to "applying" the permissions boundary.

The first is "applying" the permissions boundary to a construct scope. For this I've used context since context has built in resolution up the tree. For example, you could theoretically do something like the below.

// apply at the app scope
const app = new App({
  context: {
    [PERMISSIONS_BOUNDARY_CONTEXT_KEY]: {
      name: 'app-level-pb',
    },
  },
});

// apply different value at this stage
const prodStage = new Stage(app, 'Stage', {
  permissionsBoundary: PermissionsBoundary.fromName('stage-pb'),
});

const stack = new Stack(prodStage, 'Stack', {
  permissionsBoundary: PermissionsBoundary.fromName('stack-level-pb'),
});

While the above is possible, the most likely scenario is that you would apply a permission boundary at the App, Stage or Stack level. Stages/Stacks correspond to AWS environments (account + region) and so they would most likely also map to bootstrap environments. Because of this I've added parameters a new permissionsBoundary property to both the Stage & Stack props to make it easier to configure.

The second aspect to "applying" the boundary is actually attaching the permissions boundary to each IAM Role & User that is created. For this I add an Aspect to every Stack that has the permissions boundary context. This aspect will attach the permissions boundary to the Role or User.

closes #22745


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

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

This PR should be not be merged until #22792 which adds support for
creating a default permissions boundary as part of CDK bootstrap.

Once that is merged we will be creating a default permissions boundary
managed policy with a standard naming convention. That allows us to
easily attach this default permissions boundary to applications at
either the `Stage` or `Stack` level.

For the implementation, there are two components to "applying" the
permissions boundary.

The first is "applying" the permissions boundary to a construct scope.
For this I've used context since context has built in resolution up the
tree. For example, you could theoretically do something like the below.

```ts
// apply at the app scope
const app = new App({
  context: {
    [PERMISSIONS_BOUNDARY_CONTEXT_KEY]: {
      name: 'app-level-pb',
    },
  },
});

// apply different value at this stage
const prodStage = new Stage(app, 'Stage', {
  permissionsBoundary: PermissionsBoundary.default(),
});

const stack = new Stack(prodStage, 'Stack', {
  permissionsBoundary: PermissionsBoundary.fromName('stack-level-pb'),
});

class MyConstruct extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    const pb = PermissionsBoundary.fromName('construct-level-pb');
    pb.bind(this);
  }
}
```

While the above is possible, the most likely scenario is that you would
apply a permission boundary at the `Stage` or `Stack` level. Stages/Stacks correspond to
AWS environments (account + region) and so they would most likely also
map to bootstrap environments. Because of this I've added parameters a
new `permissionsBoundary` property to both the `Stage` & `Stack` props
to make it easier to configure.

The second aspect to "applying" the boundary is actually attaching the
permissions boundary to each IAM Role & User that is created. For this I
add an Aspect by default to every `Stack`. This aspect will look for the
`context` that was applied earlier for whether or not it should attach a
permissions boundary to the Role or User.

closes #22745
@gitpod-io
Copy link

gitpod-io bot commented Nov 14, 2022

@github-actions github-actions bot added the p2 label Nov 14, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team November 14, 2022 19:58
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 14, 2022
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

@aws-cdk-automation aws-cdk-automation dismissed their stale review November 14, 2022 20:20

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

@corymhall corymhall added the pr/do-not-merge This PR should not be merged at this time. label Nov 14, 2022
packages/@aws-cdk/aws-iam/README.md Outdated Show resolved Hide resolved
Comment on lines 432 to 433
This will apply the default permissions boundary created as part of CDK
bootstrap to all IAM Roles that are created within this `Stage`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note, not feedback] I've seen a lot of confusion stemming from dev experience to pipeline/ prod expectation. Should we have a bigger paragraph here or full docs on how to do development vs how to deploy?
Or it gets to be informational excess?

packages/@aws-cdk/core/lib/stack.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/lib/permissions-boundary.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/lib/permissions-boundary.ts Outdated Show resolved Hide resolved
@corymhall corymhall removed the pr/do-not-merge This PR should not be merged at this time. label Nov 17, 2022
Comment on lines +480 to +483
(arn.includes('${Qualifier}')
|| arn.includes('${AWS::AccountId}')
|| arn.includes('${AWS::Region}')
|| arn.includes('${AWS::Partition}'))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[neither here nor there] Easier to check than for ^\$\{[a-zA-Z0-9]+\}?

@mergify
Copy link
Contributor

mergify bot commented Nov 18, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 93859ad
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Nov 18, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit ba4896a into aws:main Nov 18, 2022
mergify bot pushed a commit that referenced this pull request Nov 23, 2022
#22744

Users can now specify in the CDK CLI a [(permissions boundary) policy](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html) to be applied on the Execution Role and all subsequent IAM users and roles of their app.

If you want to try out the feature, a good starting point is having the`--example-permissions-boundary`(or `--epb`) parameter for the `cdk botstrap`:
```
cdk boostrap --epb
```
This achieves a couple of things: a new policy will be created (if not already present) in the account being bootstrapped (`cdk-${qualifier}-permissions-boundary`) and it will be referenced in the bootstrap template. In order for the bootstrap to be successful, the credentials use must include `iam:getPolicy` and `iam:createPolicy` permissions.
This works pairs with #22913, as permissions boundary needs propagation.
You can inspect the policy via the console, retrieve it via aws cli or sdk and you can copy the structure to use on your own from `packages/aws-cdk/lib/api/bootstrap/bootstrap-template.yaml`: Resources.CdkBoostrapPermissionsBoundaryPolicy

At this point you can edit the policy, add restrictions and see what scope would match your requirements.

For non-dev work, the suggestion is to use `--custom-permissions-boundary` (or `--cpb`):
```
cdk bootstrap --cpb "custom-policy-name"
```
The policy must be created and accessible for the credentials used to perform the bootstrap.

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add context parameter and PermissionBoundary aspect
3 participants