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(cloudfront): verify acm certificate is in us-east-1 #3485

Closed
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ export class CloudFrontWebDistribution extends cdk.Construct implements IDistrib
const sslSupportMethod = props.aliasConfiguration.sslMethod || SSLMethod.SNI;
const acmCertificateArn = props.aliasConfiguration.acmCertRef;

if (acmCertificateArn) {
const {region} = cdk.Arn.parse(acmCertificateArn);

if (!cdk.Token.isUnresolved(region) && region !== 'us-east-1') {
throw new Error(`acmCertificateArn must be in the 'us-east-1' region, got '${region}'`);
}
}

distributionConfig = {
...distributionConfig,
aliases: props.aliasConfiguration.names,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"AcmCertificateArn": "testACM",
"AcmCertificateArn": "arn:aws:acm:us-east-1:1234567890:certificate/testACM",
"MinimumProtocolVersion": "TLSv1",
"SslSupportMethod": "sni-only"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ new cloudfront.CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
}
],
aliasConfiguration: {
acmCertRef: 'testACM',
acmCertRef: 'arn:aws:acm:us-east-1:1234567890:certificate/testACM',
names: ['test.test.com'],
sslMethod: cloudfront.SSLMethod.SNI,
securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1
Expand Down
59 changes: 59 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/test/test.basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from '@aws-cdk/assert';
import certificatemanager = require('@aws-cdk/aws-certificatemanager');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/core');
import { Test } from 'nodeunit';
Expand Down Expand Up @@ -325,4 +326,62 @@ export = {
test.done();
},

'throws if certificate is not in us-east-1 - fromCertificateArn'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');
const certificate = certificatemanager.Certificate.fromCertificateArn(
stack, 'EuCertificate', 'arn:aws:acm:eu-west-1:1234567890:certificate/testACM'
);

// WHEN
const toThrow = () => {
new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
originConfigs: [
{
s3OriginSource: {s3BucketSource: sourceBucket},
behaviors: [{isDefaultBehavior: true}]
}
],
aliasConfiguration: {
names: ['www.example.com'],
acmCertRef: certificate.certificateArn
}
});
};

// THEN
test.throws(() => toThrow(), /acmCertificateArn must be in the 'us-east-1' region, got 'eu-west-1'/);
test.done();
},

'throws if certificate arn is invalid - constructor'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'RegionStack', {env: {region: 'eu-west-3'}});
const sourceBucket = new s3.Bucket(stack, 'Bucket');
const certificate = new certificatemanager.Certificate(stack, 'TestCertificate', {
eladb marked this conversation as resolved.
Show resolved Hide resolved
domainName: 'www.example.com',
});

// WHEN
const toThrow = () => {
new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
originConfigs: [
{
s3OriginSource: {s3BucketSource: sourceBucket},
behaviors: [{isDefaultBehavior: true}]
}
],
aliasConfiguration: {
names: ['www.example.com'],
acmCertRef: certificate.certificateArn
}
});
};

// THEN
test.throws(() => toThrow(), /acmCertificateArn must be in the 'us-east-1' region, got 'eu-west-3'/);
test.done();
},
};