Skip to content

feat: Support Client IP Detection using Custom Header#2566

Merged
arkodg merged 3 commits intoenvoyproxy:mainfrom
davidalger:algerdev/custom-ip-detection-custom-header
Feb 16, 2024
Merged

feat: Support Client IP Detection using Custom Header#2566
arkodg merged 3 commits intoenvoyproxy:mainfrom
davidalger:algerdev/custom-ip-detection-custom-header

Conversation

@davidalger
Copy link
Copy Markdown
Contributor

@davidalger davidalger commented Feb 5, 2024

What type of PR is this?

feat: Support Client IP Detection using Custom Header

What this PR does / why we need it:

Similar to #2535 this extends ClientTrafficPolicy to support using a custom header rather than XFF headers to perform Client IP Detection. Where envoy proxies reside behind a CDN or a cloud providers global LBs, this is useful as often the client IP is provided to the backend by the downstream CDN/LB proxy when handling the request.

Two examples:

  • GCP backend services used to route traffic from a global LB to envoy proxies configured with "customRequestHeaders":["x-client-ip-address:{client_ip_address}"] in which case the x-client-ip-address may be used to determine the address in a more favorable manner than counting number of hops in the XFF header.
  • Cloudflare CDN configured with the True-Client-IP header enabled to include the end user’s IP address on all requests to envoy proxies.

Proposed configuration support on ClientTrafficPolicy:

  clientIPDetection:
    customHeader:
        name: X-Client-IP-Address
        failClosed: true

Signed-off-by: David Alger <davidmalger@gmail.com>
@davidalger davidalger requested a review from a team as a code owner February 5, 2024 23:12
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 5, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (861c3cd) 64.49% compared to head (3da570c) 63.96%.
Report is 3 commits behind head on main.

Files Patch % Lines
internal/ir/zz_generated.deepcopy.go 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2566      +/-   ##
==========================================
- Coverage   64.49%   63.96%   -0.53%     
==========================================
  Files         116      117       +1     
  Lines       17790    18098     +308     
==========================================
+ Hits        11473    11576     +103     
- Misses       5575     5769     +194     
- Partials      742      753      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: David Alger <davidmalger@gmail.com>
Signed-off-by: David Alger <davidmalger@gmail.com>
var useRemoteAddress = true
var originalIPDetectionExtensions = originalIPDetectionExtensions(irListener.ClientIPDetection)
if originalIPDetectionExtensions != nil {
useRemoteAddress = false
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have to be false ?
this knobs helps the next upstream with middleware client ip info, unsure how its tied to downstream client ip info which is being used to determine true client ip for logging, acl, ratelimiting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have to be false? this knobs helps the next upstream with middleware client ip info

It has to be false. Both top-level xff_num_trusted_hops and use_remote_address are mutually exclusive with original_ip_detection_extensions. There's a note in the docs about it, and if you try setting it to true while also configuring original_ip_detection_extensions envoy will reject the config:

[2024-02-07 22:30:29.118][1][warning][config] [source/extensions/config_subscription/grpc/grpc_subscription_impl.cc:138] gRPC config for type.googleapis.com/envoy.config.listener.v3.Listener rejected: Error adding/updating listener(s) default/eg/http: Original IP detection extensions and use_remote_address may not be mixed   

And yes, this does mean when customHeader is configured the X-Forwarded-For header will not have the envoy proxy IP appended to it since use_remote_address is false and it's assumed you aren't relying on the XFF headers when using alternate forms of client IP detection.

unsure how its tied to downstream client ip info which is being used to determine true client ip for logging, acl, ratelimiting

The value of the named header will be present in log entries as the downstream_remote_address, so I would assume that it would still be used for ACLs and rate limits.

AllowExtensionToSetAddressAsTrusted: true is set on the extension which at minimum allows the address to also be used to determine if the address is "internal" or not based on the CIDR range.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for explaining this

Copy link
Copy Markdown
Contributor

@arkodg arkodg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks !

@arkodg arkodg requested review from a team February 7, 2024 23:03
@davidalger
Copy link
Copy Markdown
Contributor Author

@arkodg anything required from me to get this one across the finish line?

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Feb 13, 2024

@arkodg anything required from me to get this one across the finish line?

waiting for another review from a maintainer before merging this one, cc @zhaohuabing @zirain


// ClientIPDetectionSettings provides configuration for determining the original client IP address for requests.
//
// +kubebuilder:validation:XValidation:rule="!(has(self.xForwardedFor) && has(self.customHeader))",message="customHeader cannot be used in conjunction with xForwardedFor"
Copy link
Copy Markdown
Member

@zhaohuabing zhaohuabing Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of 'xForwordFor' and 'customHeader' must be specified? Or default to 'xForwadFor' ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. The top-level xff_num_trusted_hops knob in Envoy is mutually exclusive with original_ip_detection_extensions. Even if Envoy supported both simultaneously though, I'm not sure it would make sense to have two methods of downstream client IP detection configured at the same time as it would muddy where the logged downstream IP address came from.

When customHeader is specified, any XFF headers are essentially ignored.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of 'xForwordFor' and 'customHeader' must be specified?

Can we also add a CEL validation for that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhaohuabing I think I misunderstood your question at first. They are mutually exclusive, but neither one is required. When both are omitted, they are simply not configured and the default behavior is to use the remote address which is correct when Envoy is directly exposed to clients rather than residing behind a CDN or cloud-provider LB. Not sure any additional CEL validation is needed here.

@arkodg arkodg requested review from a team and zhaohuabing February 15, 2024 03:45
@zirain
Copy link
Copy Markdown
Member

zirain commented Feb 15, 2024

/retest

@arkodg arkodg merged commit 1775624 into envoyproxy:main Feb 16, 2024
arkodg pushed a commit to arkodg/gateway that referenced this pull request Feb 16, 2024
Between envoyproxy#2566

&

envoyproxy#2585

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
@arkodg arkodg mentioned this pull request Feb 16, 2024
arkodg added a commit that referenced this pull request Feb 16, 2024
Fix merge race

Between #2566

&

#2585

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
@davidalger davidalger deleted the algerdev/custom-ip-detection-custom-header branch February 23, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants