Skip to content

Commit

Permalink
fix(elbv2): consider default protocol when validating redirectHTTP (#…
Browse files Browse the repository at this point in the history
…10100)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
hoegertn committed Sep 2, 2020
1 parent aa2068f commit 9e4c6d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,13 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
if (props.certificate !== undefined && props.protocol !== undefined && props.protocol !== ApplicationProtocol.HTTPS) {
throw new Error('The HTTPS protocol must be used when a certificate is given');
}
if (props.protocol !== ApplicationProtocol.HTTPS && props.redirectHTTP === true) {
throw new Error('The HTTPS protocol must be used when redirecting HTTP traffic');
}
const protocol = props.protocol !== undefined ? props.protocol :
(props.certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP);

if (protocol !== ApplicationProtocol.HTTPS && props.redirectHTTP === true) {
throw new Error('The HTTPS protocol must be used when redirecting HTTP traffic');
}

const targetProps = {
port: 80,
};
Expand Down
43 changes: 43 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,49 @@ export = {
test.done();
},

'errors when setting both HTTP protocol and redirectHTTP'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

// THEN
test.throws(() => {
new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
},
protocol: ApplicationProtocol.HTTP,
redirectHTTP: true,
});
});

test.done();
},

'does not throw errors when not setting HTTPS protocol but certificate for redirectHTTP'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
const zone = new PublicHostedZone(stack, 'HostedZone', { zoneName: 'example.com' });

// THEN
new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
},
domainName: 'api.example.com',
domainZone: zone,
redirectHTTP: true,
certificate: Certificate.fromCertificateArn(stack, 'Cert', 'helloworld'),
});

test.done();
},

'errors when setting HTTPS protocol but not domain name'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 9e4c6d2

Please sign in to comment.