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(cli): can't bootstrap environment not in app #7510

Merged
merged 4 commits into from
Apr 24, 2020

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Apr 22, 2020

Another commit split off from #6366 to make that other one more manageable to review :).

Commit Message

fix(cli): can't bootstrap environment not in app

It used to be that if we had an --app argument, we would always glob
arguments to cdk bootstrap <ENV> through the environments of stacks
in the app.

This makes it super hard/annoying to run cdk bootstrap aws://1234/us-somewhere in a CI/CD project; you have to add a stack
there first and compile before you're allowed to do that, which is
kinda silly.

Change behavior to only glob environment from the environments in the
app if it looks like the user is supplying a glob (if it contains *).

If the user is supplying a concrete environment name, just use it
directly.

Also in this commit:

  • refactor: lots of places where we were passing around a pair
    of (account, region). Replace those by passing a cxapi.Environment
    instead (most of the changes in this PR).
  • refactor: the old and new bootstrapping experience had a lot of
    copy/pasta between them. Refactored to make them share code.
  • (prerelease) feat: add a version check to the bootstrapping operation,
    so that users won't accidentally downgrade a bootstrap stack to an older
    version (This happened to 2 devs already, and is an easy mistake to
    make. Protect against it.)

End Commit Message


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

It used to be that if we had an `--app` argument, we would always glob
arguments to `cdk bootstrap <ENV>` through the environments of stacks
in the app.

This makes it super hard/annoying to run `cdk bootstrap
aws://1234/us-somewhere` in a CI/CD project; you have to add a stack
there first and compile before you're allowed to do that, which is
kinda silly.

Change behavior to only glob environment from the environments in the
app if it looks like the user is supplying a glob (if it contains `*`).

If the user is supplying a concrete environment name, just use it
directly.

Also in this commit:

- refactor: lots of places where we were passing around a pair
  of `(account, region)`. Replace those by passing a `cxapi.Environment`
  instead (most of the changes in this PR).
- refactor: the old and new bootstrapping experience had a lot of
  copy/pasta between them. Refactored to make them share code.
- (prerelease) feat: add a version check to the bootstrapping operation,
  so that users won't accidentally downgrade a bootstrap stack to an older
  version (This happened to 2 devs already, and is an easy mistake to
  make. Protect against it.)
@rix0rrr rix0rrr requested a review from a team April 22, 2020 11:56
@rix0rrr rix0rrr self-assigned this Apr 22, 2020
@mergify mergify bot added contribution/core This is a PR that came from AWS. labels Apr 22, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: b1422bf
  • 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

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 0dc5313
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@shivlaks shivlaks added the pr/do-not-merge This PR should not be merged at this time. label Apr 24, 2020
Copy link
Contributor

@shivlaks shivlaks left a comment

Choose a reason for hiding this comment

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

provisional - can you also update the title... the double negatives got me for a second

@@ -56,7 +56,8 @@ async function parseCommandLineArguments() {
.option('tags', { type: 'array', alias: 't', desc: 'Tags to add for the stack (KEY=VALUE)', nargs: 1, requiresArg: true, default: [] })
.option('execute', {type: 'boolean', desc: 'Whether to execute ChangeSet (--no-execute will NOT execute the ChangeSet)', default: true})
.option('trust', { type: 'array', desc: 'The (space-separated) list of AWS account IDs that should be trusted to perform deployments into this environment', default: [], hidden: true })
.option('cloudformation-execution-policies', { type: 'array', desc: 'The (space-separated) list of Managed Policy ARNs that should be attached to the role performing deployments into this environment. Required if --trust was passed', default: [], hidden: true }),
.option('cloudformation-execution-policies', { type: 'array', desc: 'The (space-separated) list of Managed Policy ARNs that should be attached to the role performing deployments into this environment. Required if --trust was passed', default: [], hidden: true })
.option('force', { alias: 'f', type: 'boolean', desc: 'Always bootstrap even if it would downgrade template version', default: false }),
Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense to protect against it, but what's the use case behind forcing a bootstrap and the implication of using a downgraded template version?

when would it be used?

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 don't want to prevent people from doing what they're trying to do, in case we get it wrong (for whatever reason).

Just feels like any protection should have an 'override' feature'.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough

Comment on lines 113 to 114
* The `region` and `accountId` parameters are interpreted as in `resolveEnvironment()` (which is to
* say, `undefined` doesn't do what you expect).
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment needs updating

Comment on lines 153 to 154
* `undefined` actually means `undefined`, and is NOT changed to default values! Only the magic values UNKNOWN_REGION
* and UNKNOWN_ACCOUNT will be replaced with looked-up values!
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Comment on lines -175 to -176
public async resolveEnvironmentObject(env: cxapi.Environment) {
return this.resolveEnvironment(env.account, env.region);
Copy link
Contributor

Choose a reason for hiding this comment

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

there was nothing using this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was one place, but this was the method that resolveEnvironment should have been all along.

Comment on lines -160 to -162
if (!region) {
throw new Error('AWS region must be configured either when you configure your CDK stack or through the environment');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

was this dead code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep

const newVersion = bootstrapVersionFromTemplate(template);
const currentBootstrapStack = await ToolkitInfo.lookup(resolvedEnvironment, sdk, toolkitStackName);
if (currentBootstrapStack && newVersion < currentBootstrapStack.version && !options.force) {
throw new Error(`Refusing to downgrade existing bootstrap stack from version '${currentBootstrapStack.version}' to version '${newVersion}'. Use --force to force.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new Error(`Refusing to downgrade existing bootstrap stack from version '${currentBootstrapStack.version}' to version '${newVersion}'. Use --force to force.`);
throw new Error(`Downgrade not allowed. The existing bootstrap stack version '${currentBootstrapStack.version}' is higher than version '${newVersion}'. Use --force to force.`);

maybe it's just the word "refusing" i don't like

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 modeled this after tar's "Cowardly refusing to create an empty archive" :).

I'll rephrase.

@@ -29,6 +29,7 @@ export class ToolkitInfo {
sdk, environment,
bucketName: requireOutput(BUCKET_NAME_OUTPUT),
bucketEndpoint: requireOutput(BUCKET_DOMAIN_NAME_OUTPUT),
version: parseInt(outputs[BOOTSTRAP_VERSION_OUTPUT] ?? '0', 10),
Copy link
Contributor

Choose a reason for hiding this comment

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

is the bootstrap version always going to be bumped up by numbers? (i.e. as opposed to 1.1.0 vs 1.2.0 would both parse to the same int)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep

// Partition into globs and non-globs (this will mutate environmentSpecs).
const globSpecs = partition(environmentSpecs, looksLikeGlob);
if (globSpecs.length > 0 && !this.props.cloudExecutable.hasApp) {
throw new Error(`Don't know how to bootstrap environment '${globSpecs}'. Run in app directory or specify an environment name like \'aws://123456789012/us-east-1\'.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new Error(`Don't know how to bootstrap environment '${globSpecs}'. Run in app directory or specify an environment name like \'aws://123456789012/us-east-1\'.`);
throw new Error(`Unable to bootstrap environment '${globSpecs}'. Run in app directory or specify an environment name like \'aws://123456789012/us-east-1\'.`);

I don't think "don't know how to" adds as much as the guidance that follows after it.

@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Apr 24, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 1ab47ba
  • 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 Apr 24, 2020

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: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 7c347aa
  • 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 Apr 24, 2020

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).

@mergify mergify bot merged commit 9566cca into master Apr 24, 2020
@rix0rrr rix0rrr deleted the huijbers/bootstrappin-across-the-universe branch April 29, 2020 08:59
shivlaks added a commit that referenced this pull request May 14, 2020
…gument

If a user supplied the `--app` argument, we select the environments from the
app. However, we did not have a check that precludes this selection when the
argument is not provided. We recently refactored this area in #7510

Closes #7906
mergify bot pushed a commit that referenced this pull request May 14, 2020
…gument (#7970)

If a user supplied the `--app` argument, we select the environments from the
app. However, we did not have a check that precludes this selection when the
argument is not provided. We recently refactored this area in #7510

Closes #7906
karupanerura pushed a commit to karupanerura/aws-cdk that referenced this pull request May 22, 2020
…gument (aws#7970)

If a user supplied the `--app` argument, we select the environments from the
app. However, we did not have a check that precludes this selection when the
argument is not provided. We recently refactored this area in aws#7510

Closes aws#7906
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.

3 participants