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(region-info): ssm service principal is wrong in majority of regions #17984

Merged
merged 27 commits into from
Dec 15, 2021

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Dec 13, 2021

The SSM service principal format depends on the region. Older regions have a "global" service principal (ssm.amazonaws.com), while newer regions have only regional service principals (ssm.ap-east-1.amazonaws.com).

A number of things have been changed to address this:

  • Add the notion of a "region order" into the region-info library. This allows us to express things like "does this region predate or postdate the change of some convention", and allows us to express that certain regions are after SSM introduced this change.
  • For region-agnostic stacks, it is no longer possible to supply a single value for the template that will suffice in all regions, as the format itself will have changed (neither "ssm.amazonaws.com" nor "ssm.$REGION.amazonaws.com" will work in all regions). That means we must always introduce a lookup map for region-agnostic stacks. Add stack.regionalFact() to generate lookup maps from facts in case it is necessary.
    • Detect if all map values are just an instantiation of a token pattern, and return the simplification if possible (e.g.: if the lookup values are service.us-east-1.amazonaws.com, service.us-east-2.amazonaws.com, etc, then simplify to service.$REGION.$URL_SUFFIX and avoid emitting a lookup).
    • Simplify existing usage sites of RegionInfo.regionMap() in Lambda and CodeBuild to use the new stack.regionalFact().
  • Because lookup maps would always include information for all regions, including GovCloud regions, and those are only rarely necessary: add the infrastructure for users to restrict what partitions they want to include information for, by means of a context flag. Defaults to all regions if not specified (so we don't break old templates), but for new projects restricts itself to ['aws', 'aws-cn']. Set to just ['aws'] for integration tests so we don't break all of our snapshot tests.

Fixes #16188, fixes #17646.


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

@rix0rrr rix0rrr requested review from njlynch and a team December 13, 2021 10:41
@rix0rrr rix0rrr self-assigned this Dec 13, 2021
@gitpod-io
Copy link

gitpod-io bot commented Dec 13, 2021

@rix0rrr rix0rrr changed the title fix(region-info): ssm service principal is wrong fix(region-info): ssm service principal is wrong in some regions Dec 13, 2021
@github-actions github-actions bot added the @aws-cdk/region-info Related to AWS Region information label Dec 13, 2021
@rix0rrr rix0rrr changed the title fix(region-info): ssm service principal is wrong in some regions fix(region-info): ssm service principal is wrong in old regions Dec 13, 2021
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Dec 13, 2021
@rix0rrr rix0rrr changed the title fix(region-info): ssm service principal is wrong in old regions fix(region-info): ssm service principal is wrong in majority of regions Dec 13, 2021
Copy link
Contributor

@njlynch njlynch left a comment

Choose a reason for hiding this comment

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

Looks good, except it's not 100% clear the Lambda insights stuff is 1:1 compatible.

Comment on lines 108 to 121
* To get around this limitation, this is the mapping structure:
* LambdaInsightsVersions10980 // for version 1.0.98.0
* - us-east-1
* -- {'arn': 'arn1'},
* - us-east-2
* -- {'arn': 'arn2'}
* LambdaInsightsVersions10890 // a separate mapping version 1.0.89.0
* - us-east-1
* -- {'arn': 'arn3'},
* - us-east-2
* -- {'arn': 'arn4'}
*/

const mapName = DEFAULT_MAPPING_PREFIX + insightsVersion.split('.').join('');
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not seeing where this was replicated/replaced. If I'm reading the above comment correctly, that means the new mappings won't actually deploy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They will deploy, the mapping name will just be different.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, ok. FWIW, I checked out this branch locally and ran the aws-lambda tests, and got several failures (looks related to the rename).

packages/@aws-cdk/core/lib/stack.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/cx-api/lib/features.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/region-info/lib/aws-entities.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/region-info/lib/default.ts Outdated Show resolved Hide resolved
@rix0rrr rix0rrr requested a review from njlynch December 13, 2021 14:19
@skinny85 skinny85 removed their assignment Dec 13, 2021
@rix0rrr rix0rrr requested a review from a team December 14, 2021 15:19
Copy link
Contributor

@njlynch njlynch left a comment

Choose a reason for hiding this comment

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

One comment about a potential breaking change; everything else is peachy.

this.accountExpression = account ?? core.Fn.findInMap(mappingName, core.Aws.REGION, 'repositoryAccount');
this.imageId = `${this.accountExpression}.dkr.ecr.${core.Aws.REGION}.${core.Aws.URL_SUFFIX}/${repositoryName}:${tag}`;
private constructor(private readonly repositoryName: string, private readonly tag: string, private readonly account: string | undefined) {
this.imageId = core.Lazy.string({
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a potentially breaking change, no?

const myImage = codebuild.LinuxGpuBuildImage.DLC_TENSORFLOW_1_14_0;
createCBJob(myImage);

// ...
function createCBJob(myImage: codebuild.IBindableBuildImage) {
  const jobId = myImage.imageId.includes('pytorch-training') ? 'pytorchjob' : 'tensorflowjob';
  new codebuild.Project(this, jobId, { ... });
}

Obviously, a fabricated example, but can we really convert a string from a concrete to Lazy, tokenized value in a stable module and guarantee we won't break customers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sigh. I suppose not, and it's a good catch. But based on those kinds of arguments I don't think we can't make any changes at all because anybody anywhere might have taken a dependency on anything.

I want to say how big is the chance people have actually done that... and I think not too big?

@rix0rrr rix0rrr requested review from njlynch and a team December 14, 2021 16:09
@mergify
Copy link
Contributor

mergify bot commented Dec 15, 2021

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: 18c1b60
  • 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 77144f5 into master Dec 15, 2021
@mergify mergify bot deleted the huijbers/ssm-service-principal branch December 15, 2021 11:50
@mergify
Copy link
Contributor

mergify bot commented Dec 15, 2021

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 bot pushed a commit that referenced this pull request Dec 22, 2021
The #17984 (big kudos to @rix0rrr for that) introduced a fix for the SSM service principal format which depends on the region. However, due to a typo in that PR some of regions still don't have correct SSM service principal. 

Currently the SSM service principal for the following regions incorrectly include region, while according to the [issue #16188](#16188) it should be only added to all regions since `ap-east-1`. 

```
cn-north-1
us-iso-east-1
eu-central-1
ap-northeast-2
ap-south-1
us-east-2
ca-central-1
eu-west-2
us-isob-east-1
cn-northwest-1
eu-west-3
ap-northeast-3
us-gov-east-1
eu-north-1
```

It works like that because by accident `RULE_SSM_PRINCIPALS_ARE_REGIONAL` has the same  value as `RULE_S3_WEBSITE_REGIONAL_SUBDOMAIN`. This causes incorrect results returned by the `aws-entities/before` function.

This PR fixes that issue.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
akash1810 added a commit to guardian/cdk that referenced this pull request Dec 22, 2021
This brings in two fixes which is reflected in the snapshot changes.

See:
  - aws/aws-cdk#17984
  - aws/aws-cdk#18011

BREAKING CHANGE: Update aws-cdk libraries to 1.137.0
akash1810 added a commit to guardian/cdk that referenced this pull request Dec 23, 2021
This brings in two fixes which is reflected in the snapshot changes.

See:
  - aws/aws-cdk#17984
  - aws/aws-cdk#18011

BREAKING CHANGE: Update aws-cdk libraries to 1.137.0
mergify bot pushed a commit that referenced this pull request Feb 11, 2022
In #17984, mappings were altered so that non-alphanumeric values were replaced with `_`. However, the names in the name-value pairs must be fully alphanumeric according to the [docs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html). The result was potentially invalid mappings generated at `cdk synth` that would fail at `cdk deploy`. One such example is the mappings generated for `lambda-insights` in #18789.

In this PR, the replacement value is updated from `_` to `x`. The mapping is not surfaced anywhere other than the template, so we just need a value that satisfies cloudformation. Thus we're okay with the slight loss of readability. In addition, `CfnMapping` is updated to validate the names in the name-value pair and ensure that it is alphanumeric.

Fixes #18789.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
…ns (aws#17984)

The SSM service principal format depends on the region. Older regions have a "global" service principal (`ssm.amazonaws.com`), while newer regions have only regional service principals (`ssm.ap-east-1.amazonaws.com`).

A number of things have been changed to address this:

* Add the notion of a "region order" into the `region-info` library. This allows us to express things like "does this region predate or postdate the change of some convention", and allows us to express that certain regions are *after* SSM introduced this change.
* For region-agnostic stacks, it is no longer possible to supply a single value for the template that will suffice in all regions, as the *format itself* will have changed (neither `"ssm.amazonaws.com"` nor `"ssm.$REGION.amazonaws.com"` will work in all regions). That means we must always introduce a lookup map for region-agnostic stacks. Add `stack.regionalFact()` to generate lookup maps from facts in case it is necessary.
  * Detect if all map values are just an instantiation of a token pattern, and return the simplification if possible (e.g.: if the lookup values are `service.us-east-1.amazonaws.com`, `service.us-east-2.amazonaws.com`, etc, then simplify to `service.$REGION.$URL_SUFFIX` and avoid emitting a lookup).   
  * Simplify existing usage sites of `RegionInfo.regionMap()` in Lambda and CodeBuild to use the new `stack.regionalFact()`.
* Because lookup maps would always include information for all regions, including GovCloud regions, and those are only rarely necessary: add the infrastructure for users to restrict what partitions they want to include information for, by means of a context flag. Defaults to all regions if not specified (so we don't break old templates), but for new projects restricts itself to `['aws', 'aws-cn']`. Set to just `['aws']` for integration tests so we don't break all of our snapshot tests.

Fixes aws#16188, fixes aws#17646.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
The aws#17984 (big kudos to @rix0rrr for that) introduced a fix for the SSM service principal format which depends on the region. However, due to a typo in that PR some of regions still don't have correct SSM service principal. 

Currently the SSM service principal for the following regions incorrectly include region, while according to the [issue aws#16188](aws#16188) it should be only added to all regions since `ap-east-1`. 

```
cn-north-1
us-iso-east-1
eu-central-1
ap-northeast-2
ap-south-1
us-east-2
ca-central-1
eu-west-2
us-isob-east-1
cn-northwest-1
eu-west-3
ap-northeast-3
us-gov-east-1
eu-north-1
```

It works like that because by accident `RULE_SSM_PRINCIPALS_ARE_REGIONAL` has the same  value as `RULE_S3_WEBSITE_REGIONAL_SUBDOMAIN`. This causes incorrect results returned by the `aws-entities/before` function.

This PR fixes that issue.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
In aws#17984, mappings were altered so that non-alphanumeric values were replaced with `_`. However, the names in the name-value pairs must be fully alphanumeric according to the [docs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html). The result was potentially invalid mappings generated at `cdk synth` that would fail at `cdk deploy`. One such example is the mappings generated for `lambda-insights` in aws#18789.

In this PR, the replacement value is updated from `_` to `x`. The mapping is not surfaced anywhere other than the template, so we just need a value that satisfies cloudformation. Thus we're okay with the slight loss of readability. In addition, `CfnMapping` is updated to validate the names in the name-value pair and ensure that it is alphanumeric.

Fixes aws#18789.

----

*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/region-info Related to AWS Region information contribution/core This is a PR that came from AWS.
Projects
None yet
4 participants