Skip to content

Commit ca065bb

Browse files
authored
fix(elasticloadbalancingv2): add validation on application listeners for certificates on HTTP protocol (#34233)
### Issue # (if applicable) ### Reason for this change ElasticLoadBalancerV2 throw a 400 error if you try to append a certificate to a listener on port 80 (or protocol HTTP). This PR brings this same validation to CDK ### Description of changes Added a new check for the application protocol and the length of certificates, and if there is any certificate, throw a validation error. Also, added a test for this case. ### Describe any new or updated permissions being added ### Description of how you validated changes ### Checklist My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent ef5edf5 commit ca065bb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

packages/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-listener.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ListenerCondition } from './conditions';
88
import { ITrustStore } from './trust-store';
99
import * as ec2 from '../../../aws-ec2';
1010
import * as cxschema from '../../../cloud-assembly-schema';
11-
import { Duration, FeatureFlags, Lazy, Resource, Token } from '../../../core';
11+
import { Annotations, Duration, FeatureFlags, Lazy, Resource, Token } from '../../../core';
1212
import { ValidationError } from '../../../core/lib/errors';
1313
import { addConstructMetadata, MethodMetadata } from '../../../core/lib/metadata-resource';
1414
import { propertyInjectable } from '../../../core/lib/prop-injectable';
@@ -293,6 +293,10 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
293293
// Enhanced CDK Analytics Telemetry
294294
addConstructMetadata(this, props);
295295

296+
if (protocol === ApplicationProtocol.HTTP && props.certificates?.length) {
297+
Annotations.of(this).addWarningV2('@aws-cdk/aws-elasticloadbalancingv2:httpListenerWithCertificates', 'Certificates cannot be specified for HTTP listeners. Use HTTPS instead.');
298+
}
299+
296300
this.loadBalancer = props.loadBalancer;
297301
this.protocol = protocol;
298302
this.port = port;

packages/aws-cdk-lib/aws-elasticloadbalancingv2/test/alb/listener.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describeDeprecated, testDeprecated } from '@aws-cdk/cdk-build-tools';
22
import * as constructs from 'constructs';
3-
import { Match, Template } from '../../../assertions';
3+
import { Annotations, Match, Template } from '../../../assertions';
44
import * as acm from '../../../aws-certificatemanager';
55
import { Metric } from '../../../aws-cloudwatch';
66
import * as ec2 from '../../../aws-ec2';
@@ -257,6 +257,21 @@ describe('tests', () => {
257257
});
258258
});
259259

260+
test('HTTP listener cannot have a certificate', () => {
261+
// GIVEN
262+
const stack = new cdk.Stack();
263+
const vpc = new ec2.Vpc(stack, 'Stack');
264+
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
265+
266+
const listener = lb.addListener('Listener', {
267+
port: 80,
268+
certificates: [elbv2.ListenerCertificate.fromArn('cert1')],
269+
defaultTargetGroups: [new elbv2.ApplicationTargetGroup(stack, 'Group', { vpc, port: 80 })],
270+
});
271+
272+
Annotations.fromStack(stack).hasWarning('/'+listener.node.path, Match.stringLikeRegexp('Certificates cannot be specified for HTTP listeners. Use HTTPS instead.'));
273+
});
274+
260275
test('Can configure targetType on TargetGroups', () => {
261276
// GIVEN
262277
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)