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): preserveClientIp for NetworkTargetGroup #14589

Merged
merged 3 commits into from
May 10, 2021
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 @@ -239,6 +239,7 @@ export class NetworkListener extends BaseListener implements INetworkListener {
port: props.port,
protocol: props.protocol ?? this.protocol,
proxyProtocolV2: props.proxyProtocolV2,
preserveClientIp: props.preserveClientIp,
targetGroupName: props.targetGroupName,
targets: props.targets,
vpc: this.loadBalancer.vpc,
Expand Down Expand Up @@ -333,6 +334,14 @@ export interface AddNetworkTargetsProps {
*/
readonly proxyProtocolV2?: boolean;

/**
* Indicates whether client IP preservation is enabled.
*
* @default false if the target group type is IP address and the
* target group protocol is TCP or TLS. Otherwise, true.
*/
readonly preserveClientIp?: boolean;

/**
* Health check configuration
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export interface NetworkTargetGroupProps extends BaseTargetGroupProps {
*/
readonly proxyProtocolV2?: boolean;

/**
* Indicates whether client IP preservation is enabled.
*
* @default false if the target group type is IP address and the
* target group protocol is TCP or TLS. Otherwise, true.
*/
readonly preserveClientIp?: boolean;

/**
* The targets to add to this target group.
*
Expand Down Expand Up @@ -82,6 +90,10 @@ export class NetworkTargetGroup extends TargetGroupBase implements INetworkTarge
this.setAttribute('proxy_protocol_v2.enabled', props.proxyProtocolV2 ? 'true' : 'false');
}

if (props.preserveClientIp != null) {
njlynch marked this conversation as resolved.
Show resolved Hide resolved
this.setAttribute('preserve_client_ip.enabled', props.preserveClientIp ? 'true' : 'false');
}

this.addTarget(...(props.targets || []));
}

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

test('Enable preserve_client_ip attribute for target group', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');

// WHEN
new elbv2.NetworkTargetGroup(stack, 'Group', {
vpc,
port: 80,
preserveClientIp: true,
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'preserve_client_ip.enabled',
Value: 'true',
},
],
});
});

test('Disable proxy protocol v2 for attribute target group', () => {
// GIVEN
const stack = new cdk.Stack();
Expand All @@ -51,6 +74,29 @@ describe('tests', () => {
});
});

test('Disable preserve_client_ip attribute for target group', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');

// WHEN
new elbv2.NetworkTargetGroup(stack, 'Group', {
vpc,
port: 80,
preserveClientIp: false,
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'preserve_client_ip.enabled',
Value: 'false',
},
],
});
});

test('Configure protocols for target group', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');
Expand Down