Skip to content

Commit

Permalink
feat(aws-cloudfront): support minimum security protocol (#12231)
Browse files Browse the repository at this point in the history
Closes [#12199](#12199)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
hassanazharkhan authored Jan 4, 2021
1 parent 3513edb commit 40976d9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ new cloudfront.Distribution(this, 'myDist', {
});
```

However, you can customize the minimum protocol version for the certificate while creating the distribution using `minimumProtocolVersion` property.

```ts
new cloudfront.Distribution(this, 'myDist', {
defaultBehavior: { origin: new origins.S3Origin(myBucket) },
domainNames: ['www.example.com'],
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2016
});
```

### Multiple Behaviors & Origins

Each distribution has a default behavior which applies to all requests to that distribution; additional behaviors may be specified for a
Expand Down
19 changes: 15 additions & 4 deletions packages/@aws-cdk/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ export interface DistributionProps {
* @default - No custom error responses.
*/
readonly errorResponses?: ErrorResponse[];

/**
* The minimum version of the SSL protocol that you want CloudFront to use for HTTPS connections.
*
* CloudFront serves your objects only to browsers or devices that support at
* least the SSL version that you specify.
*
* @default SecurityPolicyProtocol.TLS_V1_2_2019
*/
readonly minimumProtocolVersion?: SecurityPolicyProtocol;
}

/**
Expand Down Expand Up @@ -284,7 +294,7 @@ export class Distribution extends Resource implements IDistribution {
logging: this.renderLogging(props),
priceClass: props.priceClass ?? undefined,
restrictions: this.renderRestrictions(props.geoRestriction),
viewerCertificate: this.certificate ? this.renderViewerCertificate(this.certificate) : undefined,
viewerCertificate: this.certificate ? this.renderViewerCertificate(this.certificate, props.minimumProtocolVersion) : undefined,
webAclId: props.webAclId,
},
});
Expand Down Expand Up @@ -427,11 +437,12 @@ export class Distribution extends Resource implements IDistribution {
} : undefined;
}

private renderViewerCertificate(certificate: acm.ICertificate): CfnDistribution.ViewerCertificateProperty {
private renderViewerCertificate(certificate: acm.ICertificate,
minimumProtocolVersion: SecurityPolicyProtocol = SecurityPolicyProtocol.TLS_V1_2_2019) : CfnDistribution.ViewerCertificateProperty {
return {
acmCertificateArn: certificate.certificateArn,
sslSupportMethod: SSLMethod.SNI,
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2_2019,
minimumProtocolVersion: minimumProtocolVersion,
};
}
}
Expand Down Expand Up @@ -600,7 +611,7 @@ export enum LambdaEdgeEventType {
VIEWER_REQUEST = 'viewer-request',

/**
* The viewer-response specifies the outgoing reponse
* The viewer-response specifies the outgoing response
*/
VIEWER_RESPONSE = 'viewer-response',
}
Expand Down
25 changes: 23 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as acm from '@aws-cdk/aws-certificatemanager';
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from '@aws-cdk/aws-s3';
import { App, Duration, Stack } from '@aws-cdk/core';
import { CfnDistribution, Distribution, GeoRestriction, HttpVersion, IOrigin, LambdaEdgeEventType, PriceClass } from '../lib';
import { CfnDistribution, Distribution, GeoRestriction, HttpVersion, IOrigin, LambdaEdgeEventType, PriceClass, SecurityPolicyProtocol } from '../lib';
import { defaultOrigin, defaultOriginGroup } from './test-origin';

let app: App;
Expand Down Expand Up @@ -314,6 +314,27 @@ describe('certificates', () => {
},
});
});

test('adding a certificate with non default security policy protocol', () => {
const certificate = acm.Certificate.fromCertificateArn(stack, 'Cert', 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012');
new Distribution(stack, 'Dist', {
defaultBehavior: { origin: defaultOrigin() },
domainNames: ['www.example.com'],
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2016,
certificate: certificate,
});

expect(stack).toHaveResourceLike('AWS::CloudFront::Distribution', {
DistributionConfig: {
ViewerCertificate: {
AcmCertificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012',
SslSupportMethod: 'sni-only',
MinimumProtocolVersion: 'TLSv1_2016',
},
},
});
});

});

describe('custom error responses', () => {
Expand Down Expand Up @@ -615,7 +636,7 @@ describe('with Lambda@Edge functions', () => {

test('with incompatible env vars', () => {
const envLambdaFunction = new lambda.Function(stack, 'EnvFunction', {
runtime: lambda.Runtime.NODEJS,
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromInline('whateverwithenv'),
handler: 'index.handler',
environment: {
Expand Down

0 comments on commit 40976d9

Please sign in to comment.