Skip to content

Commit

Permalink
fix(amplify): custom headers break with tokens (#20395)
Browse files Browse the repository at this point in the history
`YAML.stringify` generates YAML with a fixed line length, splitting
long strings. This can split the token string value on multiple lines
making it unresolvable:

`${Token[AWS.URLSuf\\\n          fix.2]}`

This can be the case with `Content-Security-Policy` headers with lots of
directives and referencing API endpoints in the `connect-src` for
example.

Get rid of `YAML.stringify` and generate this "simple" YAML string
"manually".


----

### All Submissions:

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

### Adding new Unconventional Dependencies:

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

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] 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*
  • Loading branch information
jogold authored May 18, 2022
1 parent 33b983c commit 765f441
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 45 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@
"@aws-cdk/assertions-alpha/fs-extra/**",
"@aws-cdk/assertions/fs-extra",
"@aws-cdk/assertions/fs-extra/**",
"@aws-cdk/aws-amplify-alpha/yaml",
"@aws-cdk/aws-amplify-alpha/yaml/**",
"@aws-cdk/aws-iot-actions-alpha/case",
"@aws-cdk/aws-iot-actions-alpha/case/**",
"@aws-cdk/aws-amplify/yaml",
"@aws-cdk/aws-amplify/yaml/**",
"@aws-cdk/aws-codebuild/yaml",
"@aws-cdk/aws-codebuild/yaml/**",
"@aws-cdk/aws-codepipeline-actions/case",
Expand Down
21 changes: 0 additions & 21 deletions packages/@aws-cdk/aws-amplify/NOTICE
Original file line number Diff line number Diff line change
@@ -1,23 +1,2 @@
AWS Cloud Development Kit (AWS CDK)
Copyright 2018-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.

-------------------------------------------------------------------------------

The AWS CDK includes the following third-party software/licensing:

** yaml - https://www.npmjs.com/package/yaml
Copyright 2018 Eemeli Aro <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.

----------------
20 changes: 13 additions & 7 deletions packages/@aws-cdk/aws-amplify/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as codebuild from '@aws-cdk/aws-codebuild';
import * as iam from '@aws-cdk/aws-iam';
import { IResource, Lazy, Resource, SecretValue } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as YAML from 'yaml';
import { CfnApp } from './amplify.generated';
import { BasicAuth } from './basic-auth';
import { Branch, BranchOptions } from './branch';
Expand Down Expand Up @@ -515,11 +514,18 @@ export interface CustomResponseHeader {
}

function renderCustomResponseHeaders(customHeaders: CustomResponseHeader[]): string {
const modifiedHeaders = customHeaders.map(customHeader => ({
...customHeader,
headers: Object.entries(customHeader.headers).map(([key, value]) => ({ key, value })),
}));
const yaml = [
'customHeaders:',
];

for (const customHeader of customHeaders) {
yaml.push(` - pattern: "${customHeader.pattern}"`);
yaml.push(' headers:');
for (const [key, value] of Object.entries(customHeader.headers)) {
yaml.push(` - key: "${key}"`);
yaml.push(` value: "${value}"`);
}
}

const customHeadersObject = { customHeaders: modifiedHeaders };
return YAML.stringify(customHeadersObject);
return `${yaml.join('\n')}\n`;
}
7 changes: 1 addition & 6 deletions packages/@aws-cdk/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"@aws-cdk/cfn2ts": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^27.5.0",
"@types/yaml": "1.9.6",
"aws-sdk": "^2.848.0"
},
"dependencies": {
Expand All @@ -101,12 +100,8 @@
"@aws-cdk/aws-secretsmanager": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/custom-resources": "0.0.0",
"constructs": "^3.3.69",
"yaml": "1.10.2"
"constructs": "^3.3.69"
},
"bundledDependencies": [
"yaml"
],
"peerDependencies": {
"@aws-cdk/aws-codebuild": "0.0.0",
"@aws-cdk/aws-codecommit": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,18 @@
},
"Username": "aws"
},
"CustomHeaders": "customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: custom-header-name-1\n value: custom-header-value-1\n - key: custom-header-name-2\n value: custom-header-value-2\n - pattern: /path/*\n headers:\n - key: custom-header-name-1\n value: custom-header-value-2\n",
"CustomHeaders": {
"Fn::Join": [
"",
[
"customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-1\"\n - key: \"custom-header-name-2\"\n value: \"custom-header-value-2\"\n - pattern: \"/path/*\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-2\"\n - key: \"x-aws-url-suffix\"\n value: \"this-is-the-suffix-",
{
"Ref": "AWS::URLSuffix"
},
"\"\n"
]
]
},
"CustomRules": [
{
"Source": "/source",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"17.0.0"}
{"version":"19.0.0"}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "18.0.0",
"version": "19.0.0",
"testCases": {
"aws-amplify/test/integ.app": {
"integ.app": {
"stacks": [
"cdk-amplify-app"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "17.0.0",
"version": "19.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
Expand Down
13 changes: 12 additions & 1 deletion packages/@aws-cdk/aws-amplify/test/app.integ.snapshot/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,18 @@
]
}
},
"customHeaders": "customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: custom-header-name-1\n value: custom-header-value-1\n - key: custom-header-name-2\n value: custom-header-value-2\n - pattern: /path/*\n headers:\n - key: custom-header-name-1\n value: custom-header-value-2\n",
"customHeaders": {
"Fn::Join": [
"",
[
"customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-1\"\n - key: \"custom-header-name-2\"\n value: \"custom-header-value-2\"\n - pattern: \"/path/*\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-2\"\n - key: \"x-aws-url-suffix\"\n value: \"this-is-the-suffix-",
{
"Ref": "AWS::URLSuffix"
},
"\"\n"
]
]
},
"customRules": [
{
"source": "/source",
Expand Down
19 changes: 18 additions & 1 deletion packages/@aws-cdk/aws-amplify/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,28 @@ test('with custom headers', () => {
'custom-header-name-1': 'custom-header-value-2',
},
},
{
pattern: '/with-tokens/*',
headers: {
'x-custom': `${'hello'.repeat(10)}${Stack.of(stack).urlSuffix} `,
},
},
],
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Amplify::App', {
CustomHeaders: 'customHeaders:\n - pattern: "*.json"\n headers:\n - key: custom-header-name-1\n value: custom-header-value-1\n - key: custom-header-name-2\n value: custom-header-value-2\n - pattern: /path/*\n headers:\n - key: custom-header-name-1\n value: custom-header-value-2\n',
CustomHeaders: {
'Fn::Join': [
'',
[
'customHeaders:\n - pattern: "*.json"\n headers:\n - key: "custom-header-name-1"\n value: "custom-header-value-1"\n - key: "custom-header-name-2"\n value: "custom-header-value-2"\n - pattern: "/path/*"\n headers:\n - key: "custom-header-name-1"\n value: "custom-header-value-2"\n - pattern: "/with-tokens/*"\n headers:\n - key: "x-custom"\n value: "hellohellohellohellohellohellohellohellohellohello',
{
Ref: 'AWS::URLSuffix',
},
' "\n',
],
],
},
});
});
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-amplify/test/integ.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TestStack extends Stack {
pattern: '/path/*',
headers: {
'custom-header-name-1': 'custom-header-value-2',
'x-aws-url-suffix': `this-is-the-suffix-${Stack.of(this).urlSuffix}`,
},
},
],
Expand Down

0 comments on commit 765f441

Please sign in to comment.