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 all commits
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 @@ -300,4 +300,27 @@ export abstract class BaseLoadBalancer extends Resource {
public removeAttribute(key: string) {
this.setAttribute(key, undefined);
}

protected validate(): string[] {
const ret = super.validate();

// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-loadbalancer.html#cfn-elasticloadbalancingv2-loadbalancer-name
const loadBalancerName = this.physicalName;
if (!Token.isUnresolved(loadBalancerName) && loadBalancerName !== undefined) {
if (loadBalancerName.length > 32) {
ret.push(`Load balancer name: "${loadBalancerName}" can have a maximum of 32 characters.`);
}
if (loadBalancerName.startsWith('internal-')) {
ret.push(`Load balancer name: "${loadBalancerName}" must not begin with "internal-".`);
}
if (loadBalancerName.startsWith('-') || loadBalancerName.endsWith('-')) {
ret.push(`Load balancer name: "${loadBalancerName}" must not begin or end with a hyphen.`);
}
if (!/^[0-9a-z-]+$/i.test(loadBalancerName)) {
ret.push(`Load balancer name: "${loadBalancerName}" must contain only alphanumeric characters or hyphens.`);
}
}

return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ export abstract class TargetGroupBase extends CoreConstruct implements ITargetGr
ret.push("'vpc' is required for a non-Lambda TargetGroup");
}

// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-targetgroup.html#cfn-elasticloadbalancingv2-targetgroup-name
const targetGroupName = this.resource.name;
if (!cdk.Token.isUnresolved(targetGroupName) && targetGroupName !== undefined) {
if (targetGroupName.length > 32) {
ret.push(`Target group name: "${targetGroupName}" can have a maximum of 32 characters.`);
}
if (targetGroupName.startsWith('-') || targetGroupName.endsWith('-')) {
ret.push(`Target group name: "${targetGroupName}" must not begin or end with a hyphen.`);
}
if (!/^[0-9a-z-]+$/i.test(targetGroupName)) {
ret.push(`Target group name: "${targetGroupName}" must contain only alphanumeric characters or hyphens.`);
}
}

return ret;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,96 @@ describe('tests', () => {
});
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Load balancer name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" can have a maximum of 32 characters.');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Load balancer name: "internal-myLoadBalancer" must not begin with "internal-".');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Load balancer name: "-myLoadBalancer" must not begin or end with a hyphen.');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Load balancer name: "myLoadBalancer-" must not begin or end with a hyphen.');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Load balancer name: "my load balancer" must contain only alphanumeric characters or hyphens.');
});

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,82 @@ describe('tests', () => {
}).toThrow(/Health check interval '5' not supported. Must be one of the following values '10,30'./);
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Target group name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" can have a maximum of 32 characters.');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Target group name: "-myTargetGroup" must not begin or end with a hyphen.');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Target group name: "myTargetGroup-" must not begin or end with a hyphen.');
});

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

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

// THEN
expect(() => {
app.synth();
}).toThrow('Target group name: "my target group" must contain only alphanumeric characters or hyphens.');
});

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