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

Add support for RequestHeaderModifier for HTTPRouteRule objects #717

Merged
merged 1 commit into from
Jun 22, 2023

Conversation

ciarams87
Copy link
Member

@ciarams87 ciarams87 commented Jun 6, 2023

Proposed changes

Problem: As an App Dev I will want to manipulate headers at ingress. Either to add correlation IDs, provenance data, strip unnecessary headers, or yet another use case not foreseen or delineated here.

Solution:

Header manipulation is supported through the HTTPRouteFilterType == RequestHeaderModifier. This PR extends the current filter logic to support the RequestHeaderModifier filter. This RequestHeaderModifier filter maps to the proxy_set_header NGINX directive.

There are three possible actions - set, add, and remove. Set redefines any header value, add appends to a given header value if present, and remove strips the header from the request. All three are configured using the same proxy_set_header NGINX directive.

If the action is set, we simply configure the directive with the given value in the HTTPRoute spec.
If the action is remove, we configure the directive with the value set to an empty string.
If the action is add, we create a mapping for the $http_header_name variable appending a comma at the end of any client provided headers (if present) and prepend this to the given value in the HTTPRoute spec.
See https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header.

This commit also validates the headers and values and rejects anything that might cause NGINX to fail to reload or any malicious value. We also reject redefining the Host header.

Testing: Unit-testing (100% coverage), e2e testing using the supplied example and variations of same (example uses an echo server to verify the correct request headers are indeed being set, added, and removed)

Closes #480

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

@github-actions github-actions bot added documentation Improvements or additions to documentation enhancement New feature or request labels Jun 6, 2023
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 5 times, most recently from f4d73c4 to 9d69a14 Compare June 8, 2023 17:59
@ciarams87 ciarams87 marked this pull request as ready for review June 8, 2023 18:00
@ciarams87 ciarams87 requested a review from a team as a code owner June 8, 2023 18:00
examples/http-header-filter/README.md Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers_template.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
internal/state/graph/httproute.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 2 times, most recently from 261e236 to 1ea40fe Compare June 9, 2023 10:44
Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

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

hi @ciarams87 Please see my feedback.

I didn't comment on the code yet, just the approach, so that we agree on the requirements and the approach first.

UPDATED:
I updated the review comments - the map approach is reasonable, no need to consider njs. (I incorrectly tested some map NGINX config in older version of NGINX. new versions of NGINX - starting from 1.23.0 - combine all indentical header lines

Changes with nginx 1.23.0                                        21 Jun 2022

    *) Change in internal API: now header lines are represented as linked
       lists.

    *) Change: now nginx combines arbitrary header lines with identical
       names when sending to FastCGI, SCGI, and uwsgi backends, in the
       $r->header_in() method of the ngx_http_perl_module, and during lookup
       of the "$http_...", "$sent_http_...", "$sent_trailer_...",
       "$upstream_http_...", and "$upstream_trailer_..." variables.

examples/http-header-filter/cafe.yaml Outdated Show resolved Hide resolved
internal/nginx/config/validation/http_filters.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Show resolved Hide resolved
docs/gateway-api-compatibility.md Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
internal/state/graph/httproute.go Outdated Show resolved Hide resolved
internal/state/graph/httproute_test.go Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers_template.go Outdated Show resolved Hide resolved
internal/nginx/config/servers_template.go Outdated Show resolved Hide resolved
Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

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

additional feedback, considering the fact that the approach with using maps works well.

internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
examples/http-header-filter/README.md Outdated Show resolved Hide resolved
examples/http-header-filter/README.md Outdated Show resolved Hide resolved
examples/http-header-filter/cafe-route.yaml Outdated Show resolved Hide resolved
internal/nginx/config/http/config.go Outdated Show resolved Hide resolved
internal/nginx/config/http/config.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 2 times, most recently from b562f93 to 99bd512 Compare June 12, 2023 21:26
internal/nginx/config/http/config.go Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 3 times, most recently from 355c004 to eec590d Compare June 13, 2023 15:00
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 3 times, most recently from 61c201a to f2f20ef Compare June 13, 2023 15:50
Copy link
Contributor

@kate-osborn kate-osborn left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few small suggestions.

internal/nginx/config/maps_test.go Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names_test.go Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
internal/state/dataplane/configuration.go Outdated Show resolved Hide resolved
internal/state/graph/httproute.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common_test.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names_test.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/http_filters.go Outdated Show resolved Hide resolved
internal/nginx/config/http/config.go Outdated Show resolved Hide resolved
internal/nginx/config/maps_test.go Outdated Show resolved Hide resolved
internal/nginx/config/maps_test.go Outdated Show resolved Hide resolved
internal/nginx/config/servers_template.go Outdated Show resolved Hide resolved
internal/nginx/config/servers_test.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 2 times, most recently from 3cdf684 to 4d10b9d Compare June 15, 2023 12:13
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 2 times, most recently from 33d9bc1 to ecb282e Compare June 15, 2023 13:04
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/servers.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names.go Show resolved Hide resolved
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 4 times, most recently from 046a20c to 1d5188a Compare June 20, 2023 15:39
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
@ciarams87 ciarams87 force-pushed the feat/http-header-filter branch 3 times, most recently from cac212f to 69e51a8 Compare June 21, 2023 13:09
internal/nginx/config/servers_template.go Outdated Show resolved Hide resolved
internal/nginx/config/validation/common.go Outdated Show resolved Hide resolved
examples/http-header-filter/echo-route.yaml Outdated Show resolved Hide resolved
internal/nginx/config/variable_names.go Show resolved Hide resolved
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
internal/nginx/config/variable_names.go Outdated Show resolved Hide resolved
Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

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

👍

@ciarams87 ciarams87 merged commit a710e0f into nginxinc:main Jun 22, 2023
15 checks passed
@ciarams87 ciarams87 deleted the feat/http-header-filter branch June 22, 2023 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Core API: RequestHeaderModifier
4 participants