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(amplify): Add support for custom headers in the App #17102

Merged
merged 6 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
"@aws-cdk/assertions/string-width/**",
"@aws-cdk/assertions/table",
"@aws-cdk/assertions/table/**",
"@aws-cdk/aws-amplify/yaml",
"@aws-cdk/aws-amplify/yaml/**",
"@aws-cdk/aws-amplify-alpha/yaml",
"@aws-cdk/aws-amplify-alpha/yaml/**",
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved
"@aws-cdk/aws-codebuild/yaml",
"@aws-cdk/aws-codebuild/yaml/**",
"@aws-cdk/aws-codepipeline-actions/case",
Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-amplify/NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
AWS Cloud Development Kit (AWS CDK)
Copyright 2018-2021 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 <eemeli@gmail.com>

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.

----------------
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-amplify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,32 @@ const amplifyApp = new amplify.App(this, 'MyApp', {
autoBranchDeletion: true, // Automatically disconnect a branch when you delete a branch from your repository
});
```

## Adding custom headers

Use the `customHeaders` prop to configure custom headers for an Amplify app:

```ts
const amplifyApp = new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: cdk.SecretValue.secretsManager('my-github-token')
}),
customHeaders: [
{
pattern: '*.json',
headers: [
{ key: 'custom-header-name-1', value: 'custom-header-value-1' },
{ key: 'custom-header-name-2', value: 'custom-header-value-2' },
],
},
{
pattern: '/path/*',
headers: [
{ key: 'custom-header-name-1', value: 'custom-header-value-2' },
],
},
],
});
```
47 changes: 47 additions & 0 deletions packages/@aws-cdk/aws-amplify/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -118,6 +119,16 @@ export interface AppProps {
*/
readonly buildSpec?: codebuild.BuildSpec;


/**
* The custom HTTP headers for an Amplify app.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/custom-headers.html
*
* @default - no custom headers
*/
readonly customHeaders?: CustomHeader[];
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved

/**
* Custom rewrite/redirect rules for the application
*
Expand Down Expand Up @@ -238,6 +249,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
name: props.appName || this.node.id,
oauthToken: sourceCodeProviderOptions?.oauthToken?.toString(),
repository: sourceCodeProviderOptions?.repository,
customHeaders: props.customHeaders ? renderCustomHeaders(props.customHeaders) : undefined,
});

this.appId = app.attrAppId;
Expand Down Expand Up @@ -486,3 +498,38 @@ export class CustomRule {
this.condition = options.condition;
}
}

/**
* Custom header of an Amplify App.
*/
export interface CustomHeader {
/**
* The pattern used to match files to be applied with this header.
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly pattern: string;

/**
* The array of custom header options to be applied.
*/
readonly headers: CustomHeaderOptions[];
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Options for any custom header for an Amplify App.
*/
export interface CustomHeaderOptions {
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved
/**
* The name of the custom header.
*/
readonly key: string;

/**
* The value of the custom header.
*/
readonly value: string;
}

function renderCustomHeaders(customHeaders: CustomHeader[]): string {
const customHeadersObject = { customHeaders };
return YAML.stringify(customHeadersObject);
}
9 changes: 7 additions & 2 deletions packages/@aws-cdk/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"@aws-cdk/cdk-integ-tools": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^26.0.24"
"@types/jest": "^26.0.24",
"@types/yaml": "1.9.6"
},
"dependencies": {
"@aws-cdk/aws-codebuild": "0.0.0",
Expand All @@ -88,8 +89,12 @@
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-secretsmanager": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.3.69"
"constructs": "^3.3.69",
"yaml": "1.10.2"
},
"bundledDependencies": [
"yaml"
],
"peerDependencies": {
"@aws-cdk/aws-codebuild": "0.0.0",
"@aws-cdk/aws-codecommit": "0.0.0",
Expand Down
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-amplify/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,34 @@ test('with auto branch deletion', () => {
EnableBranchAutoDeletion: true,
});
});

test('with custom headers', () => {
// WHEN
new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: 'aws',
repository: 'aws-cdk',
oauthToken: SecretValue.plainText('secret'),
}),
customHeaders: [
{
pattern: '*.json',
headers: [
{ key: 'custom-header-name-1', value: 'custom-header-value-1' },
{ key: 'custom-header-name-2', value: 'custom-header-value-2' },
],
},
{
pattern: '/path/*',
headers: [
{ key: 'custom-header-name-1', value: 'custom-header-value-2' },
],
},
],
});

// 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',
});
});
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-amplify/test/integ.app.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"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",
"CustomRules": [
{
"Source": "/source",
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-amplify/test/integ.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ class TestStack extends Stack {
const amplifyApp = new amplify.App(this, 'App', {
basicAuth: amplify.BasicAuth.fromGeneratedPassword('aws'),
autoBranchCreation: {},
customHeaders: [
{
pattern: '*.json',
headers: [
{ key: 'custom-header-name-1', value: 'custom-header-value-1' },
{ key: 'custom-header-name-2', value: 'custom-header-value-2' },
],
},
{
pattern: '/path/*',
headers: [
{ key: 'custom-header-name-1', value: 'custom-header-value-2' },
],
},
],
});

amplifyApp.addCustomRule({
Expand Down