Skip to content

Add request header filter support for gRPC #1909

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

Merged
merged 8 commits into from
May 16, 2024
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
Prev Previous commit
Next Next commit
Add base gRPC headers
  • Loading branch information
ciarams87 committed May 16, 2024
commit a65d8b9c8b505606f390a5f08725e410c0a92e14
25 changes: 19 additions & 6 deletions internal/mode/static/nginx/config/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@ var baseHeaders = []http.Header{
Name: "X-Forwarded-For",
Value: "$proxy_add_x_forwarded_for",
},
{
}

// httpBaseHeaders contains the constant headers set in each HTTP server block
var httpBaseHeaders = append(baseHeaders,
http.Header{
Name: "Upgrade",
Value: "$http_upgrade",
},
{
http.Header{
Name: "Connection",
Value: "$connection_upgrade",
},
}
})

// grpcBaseHeaders contains the constant headers set in each gRPC server block
var grpcBaseHeaders = append(baseHeaders,
http.Header{
Name: "Authority",
Value: "$gw_api_compliant_host",
})

func executeServers(conf dataplane.Configuration) []executeResult {
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers)
Expand Down Expand Up @@ -558,8 +568,11 @@ func createMatchLocation(path string) http.Location {
func generateProxySetHeaders(filters *dataplane.HTTPFilters, grpc bool) []http.Header {
var headers []http.Header
if !grpc {
headers = make([]http.Header, len(baseHeaders))
copy(headers, baseHeaders)
headers = make([]http.Header, len(httpBaseHeaders))
copy(headers, httpBaseHeaders)
} else {
headers = make([]http.Header, len(grpcBaseHeaders))
copy(headers, grpcBaseHeaders)
}

if filters != nil && filters.RequestURLRewrite != nil && filters.RequestURLRewrite.Hostname != nil {
Expand Down
105 changes: 74 additions & 31 deletions internal/mode/static/nginx/config/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,19 +649,19 @@ func TestCreateServers(t *testing.T) {
{
Path: "@rule0-route0",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "@rule0-route1",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "@rule0-route2",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Expand All @@ -671,7 +671,7 @@ func TestCreateServers(t *testing.T) {
{
Path: "@rule1-route0",
ProxyPass: "http://$test__route1_rule1$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Expand All @@ -681,19 +681,19 @@ func TestCreateServers(t *testing.T) {
{
Path: "/path-only/",
ProxyPass: "http://invalid-backend-ref$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "= /path-only",
ProxyPass: "http://invalid-backend-ref$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "/backend-tls-policy/",
ProxyPass: "https://test_btp_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ProxySSLVerify: &http.ProxySSLVerify{
Name: "test-btp.example.com",
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
Expand All @@ -702,7 +702,7 @@ func TestCreateServers(t *testing.T) {
{
Path: "= /backend-tls-policy",
ProxyPass: "https://test_btp_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ProxySSLVerify: &http.ProxySSLVerify{
Name: "test-btp.example.com",
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
Expand Down Expand Up @@ -809,13 +809,13 @@ func TestCreateServers(t *testing.T) {
{
Path: "= /exact",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "@rule12-route0",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Expand Down Expand Up @@ -898,7 +898,7 @@ func TestCreateServers(t *testing.T) {
Path: "= /grpc/method",
ProxyPass: "grpc://test_foo_80",
GRPC: true,
ProxySetHeaders: nil,
ProxySetHeaders: grpcBaseHeaders,
},
{
Path: "= /grpc-with-backend-tls-policy/method",
Expand All @@ -908,7 +908,7 @@ func TestCreateServers(t *testing.T) {
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
},
GRPC: true,
ProxySetHeaders: nil,
ProxySetHeaders: grpcBaseHeaders,
},
}
}
Expand Down Expand Up @@ -1028,13 +1028,13 @@ func TestCreateServersConflicts(t *testing.T) {
{
Path: "/coffee/",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "= /coffee",
ProxyPass: "http://test_bar_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
createDefaultRootLocation(),
Expand Down Expand Up @@ -1068,13 +1068,13 @@ func TestCreateServersConflicts(t *testing.T) {
{
Path: "= /coffee",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "/coffee/",
ProxyPass: "http://test_bar_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
createDefaultRootLocation(),
Expand Down Expand Up @@ -1118,13 +1118,13 @@ func TestCreateServersConflicts(t *testing.T) {
{
Path: "/coffee/",
ProxyPass: "http://test_bar_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "= /coffee",
ProxyPass: "http://test_baz_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
createDefaultRootLocation(),
Expand Down Expand Up @@ -1243,13 +1243,13 @@ func TestCreateLocationsRootPath(t *testing.T) {
{
Path: "/path-1",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "/path-2",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Expand All @@ -1268,17 +1268,18 @@ func TestCreateLocationsRootPath(t *testing.T) {
{
Path: "/path-1",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
},
{
Path: "/path-2",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
},
{
Path: "/grpc",
ProxyPass: "grpc://test_foo_80",
GRPC: true,
Path: "/grpc",
ProxyPass: "grpc://test_foo_80",
GRPC: true,
ProxySetHeaders: grpcBaseHeaders,
},
{
Path: "/",
Expand All @@ -1295,19 +1296,19 @@ func TestCreateLocationsRootPath(t *testing.T) {
{
Path: "/path-1",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "/path-2",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
{
Path: "/",
ProxyPass: "http://test_foo_80$request_uri",
ProxySetHeaders: baseHeaders,
ProxySetHeaders: httpBaseHeaders,
ResponseHeaders: http.ResponseHeaders{},
},
},
Expand Down Expand Up @@ -2027,9 +2028,51 @@ func TestGenerateProxySetHeaders(t *testing.T) {
},
},
{
msg: "grpc",
expectedHeaders: nil,
GRPC: true,
msg: "header filter with gRPC",
GRPC: true,
filters: &dataplane.HTTPFilters{
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
Add: []dataplane.HTTPHeader{
{
Name: "Authorization",
Value: "my-auth",
},
},
Set: []dataplane.HTTPHeader{
{
Name: "Accept-Encoding",
Value: "gzip",
},
},
Remove: []string{"my-header"},
},
},
expectedHeaders: []http.Header{
{
Name: "Authorization",
Value: "${authorization_header_var}my-auth",
},
{
Name: "Accept-Encoding",
Value: "gzip",
},
{
Name: "my-header",
Value: "",
},
{
Name: "Host",
Value: "$gw_api_compliant_host",
},
{
Name: "X-Forwarded-For",
Value: "$proxy_add_x_forwarded_for",
},
{
Name: "Authority",
Value: "$gw_api_compliant_host",
},
},
},
}

Expand Down