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(codebuild): cannot use immutable roles for Project #5608

Merged
merged 9 commits into from
Jan 6, 2020

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Jan 2, 2020

Commit Message

fix(codebuild): cannot use immutable roles for Project

Immutably imported Roles could not be used for CodeBuild
Projects, because they would create a policy but be unable
to attach it to the Role. That leaves an unattached Policy,
which is invalid.

Fix this by making Policy objects only render to an AWS::IAM::Policy
resource if they actually have any effect. It is perfectly allowed to
create new unattached Policy objects, or have empty Policy objects.
Only if and when they actually need to mutate the policy of an IAM
identity will they render themselves to the CloudFormation template.
Being able to abstract away these kinds of concerns is exactly the value
of a higher-level programming model.

To allow for the rare cases where an empty Policy object would be
considered a programming error, we still have the flag mustCreate
which triggers the legacy behavior of alwyas creating the
AWS::IAM::Policy resource which must have a statement and be
attached to an identity.

Fixes #1408.


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

Immutably imported `Role`s could not be used for CodeBuild
`Project`s, because they would create a policy but be unable
to attach it to the Role. That leaves an unattached Policy,
which is invalid.

Fix this by making `Policy` objects only render to an `AWS::IAM::Policy`
resource if they actually have any effect. It is perfectly allowed to
create new unattached Policy objects, or have empty Policy objects.
Only if and when they actually need to mutate the policy of an IAM
identity will they render themselves to the CloudFormation template.
Being able to abstract away these kinds of concerns is exactly the value
of a higher-level programming model.

To allow for the rare cases where an empty Policy object would be
considered a programming error, we still have the flag `mustCreate`
which triggers the legacy behavior of alwyas creating the
`AWS::IAM::Policy` resource which must have a statement and be
attached to an identity.
@rix0rrr rix0rrr requested review from skinny85 and eladb January 2, 2020 11:27
@rix0rrr rix0rrr self-assigned this Jan 2, 2020
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 2, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

eladb
eladb previously requested changes Jan 2, 2020
Comment on lines 229 to 231
private get shouldExist() {
return this.mustCreate || this.referenceTaken || (!this.document.isEmpty && this.isAttached);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

since this is temporal (i.e. only relevant after the tree has been fully initialized) and used only in one place, I think this code should just be part of prepare to avoid accidentally using it in the future from the wrong context.

*
* It is not an error if a child with the given name does not exist.
*/
public removeChild(childName: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rename to tryRemoveChild to indicate it won't throw, and return a boolean indicating if the child was actually removed or not.

I am a bit worried by potential abuse of this API. Maybe we can make it a bit safer and only allow this during "prepare" somehow? What do you think? Can you please also mark this as @experimental?

This also resolves #1408

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not too worried about misuse. The methods on node are pretty much "plumbing sticking out" anyway, not sure they're part of people's day-to-day operations with the API.

I would hope that people venturing there will be aware they're messing with internals which can do what they want, or have unintentional effects they themselves will be responsible for cleaning up. Not sure that limiting to prepare() is necessary.

@rix0rrr rix0rrr requested a review from eladb January 2, 2020 13:12
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

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

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

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

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

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

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

*
* @default false
*/
readonly mustCreate?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't love this name... I proposed changing it to lazy?: boolean in the previous PR, but I'm also open to other suggestions 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

forceResource?

mustExist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel "lazy" is usually used to communicate something else.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just force?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was considering force, slightly on the fence but I also don't have a better alternative. I can do force.

Copy link

Choose a reason for hiding this comment

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

forceReify? forceRender?

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

protected validate(): string[] {
const result = new Array<string>();

// validate that the policy document is not empty
if (this.document.isEmpty) {
result.push('Policy is empty. You must add statements to the policy');
if (this.force) {
result.push('Policy created with mustCreate=true is empty. You must add statements to the policy');
Copy link
Contributor

Choose a reason for hiding this comment

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

References old property name.

result.push('Policy created with mustCreate=true is empty. You must add statements to the policy');
}
if (!this.force && this.referenceTaken) {
result.push('Policy name has been read of empty policy. You must add statements to the policy so it can exist.');
Copy link
Contributor

Choose a reason for hiding this comment

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

result.push(`Policy created with mustCreate=true must be attached to at least one principal: user, group or role`);
}
if (!this.force && this.referenceTaken) {
result.push('Policy name has been read of unattached policy. Attach to at least one principal: user, group or role.');
Copy link
Contributor

@skinny85 skinny85 Jan 3, 2020

Choose a reason for hiding this comment

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

See here: #5569 (comment)

result.push(`Policy must be attached to at least one principal: user, group or role`);
if (!this.isAttached) {
if (this.force) {
result.push(`Policy created with mustCreate=true must be attached to at least one principal: user, group or role`);
Copy link
Contributor

Choose a reason for hiding this comment

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

References old property name.

@rix0rrr rix0rrr requested a review from skinny85 January 6, 2020 08:53
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

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

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • 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 Jan 6, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • 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 Jan 6, 2020

Thank you for contributing! Your pull request is now being automatically merged.

@mergify mergify bot merged commit 6103180 into master Jan 6, 2020
@mergify mergify bot deleted the huijbers/lazy-policy branch January 6, 2020 16:41
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escape hatch: be able to delete an entire resource from the subtree
4 participants