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(elbv2): add name validation for target group and load balancer names #19385

Merged
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ export abstract class BaseLoadBalancer extends Resource {

this.vpc = baseProps.vpc;

if (!Token.isUnresolved(baseProps.loadBalancerName) && baseProps.loadBalancerName !== undefined && (baseProps.loadBalancerName.length > 32 || baseProps.loadBalancerName.startsWith('internal-') || !/^[0-9a-z]+[0-9a-z-]*[0-9a-z]+$/i.test(baseProps.loadBalancerName))) {
throw new Error(`Load balancer name: "${baseProps.loadBalancerName}" is not allowed. Load balancer name can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, must not begin or end with a hyphen, and must not begin with "internal-".`);
}

const resource = new CfnLoadBalancer(this, 'Resource', {
name: this.physicalName,
subnets: subnetIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ export abstract class TargetGroupBase extends CoreConstruct implements ITargetGr
this.setAttribute('deregistration_delay.timeout_seconds', baseProps.deregistrationDelay.toSeconds().toString());
}

if (!cdk.Token.isUnresolved(baseProps.targetGroupName) && baseProps.targetGroupName !== undefined && (baseProps.targetGroupName.length > 32 || !/^[0-9a-z]+[0-9a-z-]*[0-9a-z]+$/i.test(baseProps.targetGroupName))) {
throw new Error(`Target group name: "${baseProps.targetGroupName}" is not allowed. Target group name can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen.`);
}

this.healthCheck = baseProps.healthCheck || {};
this.vpc = baseProps.vpc;
this.targetType = baseProps.targetType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,76 @@ describe('tests', () => {
});
});

test('loadBalancerName unallowed: starts with "internal-"', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkLoadBalancer(stack, 'NLB', {
loadBalancerName: 'internal-myLoadBalancer',
vpc,
}),
).toThrow();
});

test('loadBalancerName unallowed: more than 32 characters', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkLoadBalancer(stack, 'NLB', {
loadBalancerName: 'a'.repeat(33),
vpc,
}),
).toThrow();
});

test('loadBalancerName unallowed: unallowed characters', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkLoadBalancer(stack, 'NLB', {
loadBalancerName: 'my load balancer',
vpc,
}),
).toThrow();
});

test('loadBalancerName unallowed: ends with hyphen', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkLoadBalancer(stack, 'NLB', {
loadBalancerName: 'myLoadBalancer-',
vpc,
}),
).toThrow();
});

test('loadBalancerName unallowed: starts with hyphen', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkLoadBalancer(stack, 'NLB', {
loadBalancerName: '-myLoadBalancer',
vpc,
}),
).toThrow();
});

test('imported network load balancer with no vpc specified throws error when calling addTargets', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,66 @@ describe('tests', () => {
}).toThrow(/Health check interval '5' not supported. Must be one of the following values '10,30'./);
});

test('loadBalancerName unallowed: more than 32 characters', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkTargetGroup(stack, 'Group', {
vpc,
port: 80,
targetGroupName: 'a'.repeat(33),
}),
).toThrow();
});

test('loadBalancerName unallowed: unallowed characters', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkTargetGroup(stack, 'Group', {
vpc,
port: 80,
targetGroupName: 'my target group',
}),
).toThrow();
});

test('loadBalancerName unallowed: ends with hyphen', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkTargetGroup(stack, 'Group', {
vpc,
port: 80,
targetGroupName: 'myTargetGroup-',
}),
).toThrow();
});

test('loadBalancerName unallowed: starts with hyphen', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

// WHEN/THEN
expect(() =>
new elbv2.NetworkTargetGroup(stack, 'Group', {
vpc,
port: 80,
targetGroupName: '-myTargetGroup',
}),
).toThrow();
});

test.each([elbv2.Protocol.UDP, elbv2.Protocol.TCP_UDP, elbv2.Protocol.TLS])(
'Throws validation error, when `healthCheck` has `protocol` set to %s',
(protocol) => {
Expand Down