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(cli): diff now uses the lookup Role for new-style synthesis #18277

Merged
merged 9 commits into from
Jan 10, 2022
Merged

feat(cli): diff now uses the lookup Role for new-style synthesis #18277

merged 9 commits into from
Jan 10, 2022

Conversation

corymhall
Copy link
Contributor

@corymhall corymhall commented Jan 5, 2022

This PR exposes information on the bootstrap lookup role on the
CloudFormation stack artifact. This enables the CLI to assume the lookup
role during cli operations in order to lookup information in the stack
account.

Along with the ARN of the lookup role, this also exposes a
requiresBootstrapStackVersion property which is set to 8 (the
version the lookup role was given ReadOnlyAccess), and the
bootstrapStackVersionSsmParameter which is needed to lookup the
bootstrap version if a user has renamed the bootstrap stack.

This allows us to first check whether the lookupRole exists and has the
correct permissions prior to using it.

This also updates the diff capability in the CLI (run as part of cdk diff or cdk deploy)
to use this new functionality. It now will try to assume the lookupRole and if it doesn't exist or
if the bootstrap stack version is not valid, then it will fallback to using the deployRole (what it uses
currently).

This PR also updates the forEnvironment function to return whether or not it is returning the
default credentials. This allows the calling function to decide whether or not it actually wants
to use the default credentials.


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

stack artifact

This PR exposes information on the bootstrap lookup role on the
CloudFormation stack artifact. This enables the CLI to assume the lookup
role during cli operations in order to lookup information in the stack
account.

Along with the ARN of the lookup role, this also exposes a
`requiresBootstrapStackVersion` property which is set to `8` (the
version the lookup role was given ReadOnlyAccess), and the
`bootstrapStackVersionSsmParameter` which is needed to lookup the
bootstrap version if a user has renamed the bootstrap stack.

This allows us to first check whether the lookupRole exists and has the
correct permissions prior to using it.
@gitpod-io
Copy link

gitpod-io bot commented Jan 5, 2022

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Can you also add the changes to the synthesizer that will emit the new values, and the changes to the CLI that will consume the new values?

@rix0rrr
Copy link
Contributor

rix0rrr commented Jan 5, 2022

the changes to the CLI that will consume the new values?

At least for cdk diff for example

@skinny85
Copy link
Contributor

skinny85 commented Jan 5, 2022

the changes to the CLI that will consume the new values?

At least for cdk diff for example

Yes, +1000 this!

Let's re-shape this PR to, instead of a chore:, be a feat(cli): diff now uses the lookup Role for new-style synthesis.

This will allow for a beautiful transition into Calvin's PR for supporting diff in Nested Stacks (#18207).

@corymhall
Copy link
Contributor Author

corymhall commented Jan 5, 2022

the changes to the CLI that will consume the new values?

At least for cdk diff for example

I'm actually not sure that the approach that I've currently implemented will work. I am providing the requiresBootstrapStackVersion so that on the consuming side we can check whether or not we can use the lookup role. This creates a chicken/egg issue because we need to first lookup the bootstrap version (with the lookup role) in order to determine whether we can use the lookup role.

The only approach I can think of is to:

  1. Try to assume the lookup role and if it fails fall back to something else (deploy-role, default credentials, etc)
  2. If it can be assumed then lookup the bootstrap version to verify we have a version where the lookup role has the correct permissions.

Alternatively this feels like a big enough change that we could just bump the required bootstrap version in the CLI to 8. @rix0rrr let me know what you think.

@rix0rrr
Copy link
Contributor

rix0rrr commented Jan 6, 2022

I am really loath to bump the required bootstrap version. We've had extremely negative feedback to that in the past.

I think your solution is the best one. The most backwards-compatible algorithm I can think of is:

  • Try to assume the role (if present).
    • If it fails, print a warning that we're going to be using current credentials, include instructions on bootstrapping, and proceed anyway.
    • If it succeeds, use the role we just assumed to do a post-hoc validation of the bootstrap version, same way we do it for deploy: check SSM parameter value by a parameter name that's in the assembly.
      • In this case we don't have to do the fallback to looking up the default stack name, we should always have an SSM parameter name (it was just there in the previous assembly because of backwards compat concerns).
      • It's probably fine to make the requiredBootstrapVersion optional, I guess I can see situations in which people might not want to have this check at all.

For cdk diff, the "most" backwards compatible would be to, instead of falling back to current credentials, is to fall back to the deploy-role instead.

For cdk watch that is not necessary.

…mplate

also updated forEnvironment to return whether or not it is returning default
credentials.
@corymhall corymhall changed the title chore(cloud-assembly-schema): expose lookupRole on the cloudformation stack artifact feat(cli): diff now uses the lookup Role for new-style synthesis Jan 6, 2022
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Jan 6, 2022
@corymhall corymhall requested a review from rix0rrr January 6, 2022 18:15
@skinny85 skinny85 added the pr-linter/exempt-readme The PR linter will not require README changes label Jan 6, 2022
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Thanks for the awesome work! Got some style niggles, but this is almost there

packages/aws-cdk/lib/api/aws-auth/sdk-provider.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/cloudformation-deployments.ts Outdated Show resolved Hide resolved
/**
* Read a version from an SSM parameter, cached
*/
protected async versionFromSsmParameter(sdk: ISDK, parameterName: string): Promise<number> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this code copy/pasted? Can we get rid of the duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to remove the duplication I had to make this method a public method on ToolkitInfo. Let me know what you think.

packages/aws-cdk/lib/api/cloudformation-deployments.ts Outdated Show resolved Hide resolved
Comment on lines 319 to 320
if (!stack.lookupRole) {
throw new Error(`The stack ${stack.displayName} does not have the lookupRole configured`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this worth an error? What is it checking?

  • Is it checking that the caller of prepareSdkWithLookupRoleFor should not have called this function if there's not a lookup role?
    • Is it worth requiring every caller of this function to add an additional if ? Can we not just use the "current credentials" and simplify the call path for all callers?
  • Or is it checking that all stacks have a lookupRole ?
    • What about old versions of the cloud assembly, or legacy-synthesized stacks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

packages/aws-cdk/lib/api/cloudformation-deployments.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/cloudformation-deployments.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/cloudformation-deployments.ts Outdated Show resolved Hide resolved
@@ -453,6 +459,11 @@ export class DefaultStackSynthesizer extends StackSynthesizer {
requiresBootstrapStackVersion: MIN_BOOTSTRAP_STACK_VERSION,
bootstrapStackVersionSsmParameter: this.bootstrapStackVersionSsmParameter,
additionalDependencies: [artifactId],
lookupRole: this.lookupRoleArn ? {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably useful if people can disable this. I can imagine environments in which the lookup role has been neutered, or people prefer using current credentials to access their stacks.

Add a property to switch this off?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add a disableLookupRole property.

/**
* Try to use the bootstrap lookupRole. There are two scenarios that are handled here
* 1. The lookup role may not exist (it was added in bootstrap stack version 7)
* 2. The lookup role may not have the correct permissions (ReadOnlyAccess was added in
Copy link
Contributor

Choose a reason for hiding this comment

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

Sheesh, fortunately at version 7 it already has the required SSM parameter permissions... 😅

@corymhall corymhall requested a review from rix0rrr January 7, 2022 16:25
@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Jan 10, 2022
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Conditionally shipped, I'd like to see a minor tweak to the user-facing flag

Comment on lines 107 to 112
/**
* Whether or not to disable use of the lookup role
*
* @default - false
*/
readonly disableLookupRole?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, I hate to use flags that are named after a negative. In frail human heads it becomes very hard to reason about (at least in mine 😅).

Can we turn this into something like this (adding some explanation of what the flag does as well):

Suggested change
/**
* Whether or not to disable use of the lookup role
*
* @default - false
*/
readonly disableLookupRole?: boolean;
/**
* Use the bootstrapped lookup role for (read-only) stack operations
*
* Use the lookup role when performing a `cdk diff`. If set to `false`, the
* `deploy role` credentials will be used to perform a `cdk diff.`
*
* Requires bootstrap stack version 8.
*
* @default true
*/
readonly useLookupRoleForStackOperations?: boolean;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@corymhall corymhall removed the pr/do-not-merge This PR should not be merged at this time. label Jan 10, 2022
@mergify
Copy link
Contributor

mergify bot commented Jan 10, 2022

Thank you for contributing! Your pull request will be updated from master 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: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 3c97283
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@mergify mergify bot merged commit 2256680 into aws:master Jan 10, 2022
@mergify
Copy link
Contributor

mergify bot commented Jan 10, 2022

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

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
…#18277)

This PR exposes information on the bootstrap lookup role on the
CloudFormation stack artifact. This enables the CLI to assume the lookup
role during cli operations in order to lookup information in the stack
account.

Along with the ARN of the lookup role, this also exposes a
`requiresBootstrapStackVersion` property which is set to `8` (the
version the lookup role was given ReadOnlyAccess), and the
`bootstrapStackVersionSsmParameter` which is needed to lookup the
bootstrap version if a user has renamed the bootstrap stack.

This allows us to first check whether the lookupRole exists and has the
correct permissions prior to using it.

This also updates the `diff` capability in the CLI (run as part of `cdk diff` or `cdk deploy`)
to use this new functionality. It now will try to assume the lookupRole and if it doesn't exist or
if the bootstrap stack version is not valid, then it will fallback to using the deployRole (what it uses
currently).

This PR also updates the `forEnvironment` function to return whether or not it is returning the
default credentials. This allows the calling function to decide whether or not it actually wants
to use the default credentials.


----

*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
@aws-cdk/cloud-assembly-schema contribution/core This is a PR that came from AWS. package/tools Related to AWS CDK Tools or CLI pr-linter/exempt-readme The PR linter will not require README changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants