Skip to content

Commit 2e3a07e

Browse files
authored
Add request header filter support for gRPC (#1909)
* Add request header filter support for gRPC & Add base gRPC headers
1 parent bc01fdf commit 2e3a07e

File tree

9 files changed

+876
-663
lines changed

9 files changed

+876
-663
lines changed

internal/mode/static/nginx/config/servers.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const (
2020
rootPath = "/"
2121
)
2222

23-
// baseHeaders contains the constant headers set in each server block
24-
var baseHeaders = []http.Header{
23+
// httpBaseHeaders contains the constant headers set in each HTTP server block
24+
var httpBaseHeaders = []http.Header{
2525
{
2626
Name: "Host",
2727
Value: "$gw_api_compliant_host",
@@ -40,6 +40,22 @@ var baseHeaders = []http.Header{
4040
},
4141
}
4242

43+
// grpcBaseHeaders contains the constant headers set in each gRPC server block
44+
var grpcBaseHeaders = []http.Header{
45+
{
46+
Name: "Host",
47+
Value: "$gw_api_compliant_host",
48+
},
49+
{
50+
Name: "X-Forwarded-For",
51+
Value: "$proxy_add_x_forwarded_for",
52+
},
53+
{
54+
Name: "Authority",
55+
Value: "$gw_api_compliant_host",
56+
},
57+
}
58+
4359
func executeServers(conf dataplane.Configuration) []executeResult {
4460
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers)
4561

@@ -558,8 +574,11 @@ func createMatchLocation(path string) http.Location {
558574
func generateProxySetHeaders(filters *dataplane.HTTPFilters, grpc bool) []http.Header {
559575
var headers []http.Header
560576
if !grpc {
561-
headers = make([]http.Header, len(baseHeaders))
562-
copy(headers, baseHeaders)
577+
headers = make([]http.Header, len(httpBaseHeaders))
578+
copy(headers, httpBaseHeaders)
579+
} else {
580+
headers = make([]http.Header, len(grpcBaseHeaders))
581+
copy(headers, grpcBaseHeaders)
563582
}
564583

565584
if filters != nil && filters.RequestURLRewrite != nil && filters.RequestURLRewrite.Hostname != nil {

internal/mode/static/nginx/config/servers_test.go

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -649,19 +649,19 @@ func TestCreateServers(t *testing.T) {
649649
{
650650
Path: "@rule0-route0",
651651
ProxyPass: "http://test_foo_80$request_uri",
652-
ProxySetHeaders: baseHeaders,
652+
ProxySetHeaders: httpBaseHeaders,
653653
ResponseHeaders: http.ResponseHeaders{},
654654
},
655655
{
656656
Path: "@rule0-route1",
657657
ProxyPass: "http://test_foo_80$request_uri",
658-
ProxySetHeaders: baseHeaders,
658+
ProxySetHeaders: httpBaseHeaders,
659659
ResponseHeaders: http.ResponseHeaders{},
660660
},
661661
{
662662
Path: "@rule0-route2",
663663
ProxyPass: "http://test_foo_80$request_uri",
664-
ProxySetHeaders: baseHeaders,
664+
ProxySetHeaders: httpBaseHeaders,
665665
ResponseHeaders: http.ResponseHeaders{},
666666
},
667667
{
@@ -671,7 +671,7 @@ func TestCreateServers(t *testing.T) {
671671
{
672672
Path: "@rule1-route0",
673673
ProxyPass: "http://$test__route1_rule1$request_uri",
674-
ProxySetHeaders: baseHeaders,
674+
ProxySetHeaders: httpBaseHeaders,
675675
ResponseHeaders: http.ResponseHeaders{},
676676
},
677677
{
@@ -681,19 +681,19 @@ func TestCreateServers(t *testing.T) {
681681
{
682682
Path: "/path-only/",
683683
ProxyPass: "http://invalid-backend-ref$request_uri",
684-
ProxySetHeaders: baseHeaders,
684+
ProxySetHeaders: httpBaseHeaders,
685685
ResponseHeaders: http.ResponseHeaders{},
686686
},
687687
{
688688
Path: "= /path-only",
689689
ProxyPass: "http://invalid-backend-ref$request_uri",
690-
ProxySetHeaders: baseHeaders,
690+
ProxySetHeaders: httpBaseHeaders,
691691
ResponseHeaders: http.ResponseHeaders{},
692692
},
693693
{
694694
Path: "/backend-tls-policy/",
695695
ProxyPass: "https://test_btp_80$request_uri",
696-
ProxySetHeaders: baseHeaders,
696+
ProxySetHeaders: httpBaseHeaders,
697697
ProxySSLVerify: &http.ProxySSLVerify{
698698
Name: "test-btp.example.com",
699699
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -702,7 +702,7 @@ func TestCreateServers(t *testing.T) {
702702
{
703703
Path: "= /backend-tls-policy",
704704
ProxyPass: "https://test_btp_80$request_uri",
705-
ProxySetHeaders: baseHeaders,
705+
ProxySetHeaders: httpBaseHeaders,
706706
ProxySSLVerify: &http.ProxySSLVerify{
707707
Name: "test-btp.example.com",
708708
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -809,13 +809,13 @@ func TestCreateServers(t *testing.T) {
809809
{
810810
Path: "= /exact",
811811
ProxyPass: "http://test_foo_80$request_uri",
812-
ProxySetHeaders: baseHeaders,
812+
ProxySetHeaders: httpBaseHeaders,
813813
ResponseHeaders: http.ResponseHeaders{},
814814
},
815815
{
816816
Path: "@rule12-route0",
817817
ProxyPass: "http://test_foo_80$request_uri",
818-
ProxySetHeaders: baseHeaders,
818+
ProxySetHeaders: httpBaseHeaders,
819819
ResponseHeaders: http.ResponseHeaders{},
820820
},
821821
{
@@ -898,7 +898,7 @@ func TestCreateServers(t *testing.T) {
898898
Path: "= /grpc/method",
899899
ProxyPass: "grpc://test_foo_80",
900900
GRPC: true,
901-
ProxySetHeaders: nil,
901+
ProxySetHeaders: grpcBaseHeaders,
902902
},
903903
{
904904
Path: "= /grpc-with-backend-tls-policy/method",
@@ -908,7 +908,7 @@ func TestCreateServers(t *testing.T) {
908908
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
909909
},
910910
GRPC: true,
911-
ProxySetHeaders: nil,
911+
ProxySetHeaders: grpcBaseHeaders,
912912
},
913913
}
914914
}
@@ -1028,13 +1028,13 @@ func TestCreateServersConflicts(t *testing.T) {
10281028
{
10291029
Path: "/coffee/",
10301030
ProxyPass: "http://test_foo_80$request_uri",
1031-
ProxySetHeaders: baseHeaders,
1031+
ProxySetHeaders: httpBaseHeaders,
10321032
ResponseHeaders: http.ResponseHeaders{},
10331033
},
10341034
{
10351035
Path: "= /coffee",
10361036
ProxyPass: "http://test_bar_80$request_uri",
1037-
ProxySetHeaders: baseHeaders,
1037+
ProxySetHeaders: httpBaseHeaders,
10381038
ResponseHeaders: http.ResponseHeaders{},
10391039
},
10401040
createDefaultRootLocation(),
@@ -1068,13 +1068,13 @@ func TestCreateServersConflicts(t *testing.T) {
10681068
{
10691069
Path: "= /coffee",
10701070
ProxyPass: "http://test_foo_80$request_uri",
1071-
ProxySetHeaders: baseHeaders,
1071+
ProxySetHeaders: httpBaseHeaders,
10721072
ResponseHeaders: http.ResponseHeaders{},
10731073
},
10741074
{
10751075
Path: "/coffee/",
10761076
ProxyPass: "http://test_bar_80$request_uri",
1077-
ProxySetHeaders: baseHeaders,
1077+
ProxySetHeaders: httpBaseHeaders,
10781078
ResponseHeaders: http.ResponseHeaders{},
10791079
},
10801080
createDefaultRootLocation(),
@@ -1118,13 +1118,13 @@ func TestCreateServersConflicts(t *testing.T) {
11181118
{
11191119
Path: "/coffee/",
11201120
ProxyPass: "http://test_bar_80$request_uri",
1121-
ProxySetHeaders: baseHeaders,
1121+
ProxySetHeaders: httpBaseHeaders,
11221122
ResponseHeaders: http.ResponseHeaders{},
11231123
},
11241124
{
11251125
Path: "= /coffee",
11261126
ProxyPass: "http://test_baz_80$request_uri",
1127-
ProxySetHeaders: baseHeaders,
1127+
ProxySetHeaders: httpBaseHeaders,
11281128
ResponseHeaders: http.ResponseHeaders{},
11291129
},
11301130
createDefaultRootLocation(),
@@ -1243,13 +1243,13 @@ func TestCreateLocationsRootPath(t *testing.T) {
12431243
{
12441244
Path: "/path-1",
12451245
ProxyPass: "http://test_foo_80$request_uri",
1246-
ProxySetHeaders: baseHeaders,
1246+
ProxySetHeaders: httpBaseHeaders,
12471247
ResponseHeaders: http.ResponseHeaders{},
12481248
},
12491249
{
12501250
Path: "/path-2",
12511251
ProxyPass: "http://test_foo_80$request_uri",
1252-
ProxySetHeaders: baseHeaders,
1252+
ProxySetHeaders: httpBaseHeaders,
12531253
ResponseHeaders: http.ResponseHeaders{},
12541254
},
12551255
{
@@ -1268,17 +1268,18 @@ func TestCreateLocationsRootPath(t *testing.T) {
12681268
{
12691269
Path: "/path-1",
12701270
ProxyPass: "http://test_foo_80$request_uri",
1271-
ProxySetHeaders: baseHeaders,
1271+
ProxySetHeaders: httpBaseHeaders,
12721272
},
12731273
{
12741274
Path: "/path-2",
12751275
ProxyPass: "http://test_foo_80$request_uri",
1276-
ProxySetHeaders: baseHeaders,
1276+
ProxySetHeaders: httpBaseHeaders,
12771277
},
12781278
{
1279-
Path: "/grpc",
1280-
ProxyPass: "grpc://test_foo_80",
1281-
GRPC: true,
1279+
Path: "/grpc",
1280+
ProxyPass: "grpc://test_foo_80",
1281+
GRPC: true,
1282+
ProxySetHeaders: grpcBaseHeaders,
12821283
},
12831284
{
12841285
Path: "/",
@@ -1295,19 +1296,19 @@ func TestCreateLocationsRootPath(t *testing.T) {
12951296
{
12961297
Path: "/path-1",
12971298
ProxyPass: "http://test_foo_80$request_uri",
1298-
ProxySetHeaders: baseHeaders,
1299+
ProxySetHeaders: httpBaseHeaders,
12991300
ResponseHeaders: http.ResponseHeaders{},
13001301
},
13011302
{
13021303
Path: "/path-2",
13031304
ProxyPass: "http://test_foo_80$request_uri",
1304-
ProxySetHeaders: baseHeaders,
1305+
ProxySetHeaders: httpBaseHeaders,
13051306
ResponseHeaders: http.ResponseHeaders{},
13061307
},
13071308
{
13081309
Path: "/",
13091310
ProxyPass: "http://test_foo_80$request_uri",
1310-
ProxySetHeaders: baseHeaders,
1311+
ProxySetHeaders: httpBaseHeaders,
13111312
ResponseHeaders: http.ResponseHeaders{},
13121313
},
13131314
},
@@ -2027,9 +2028,51 @@ func TestGenerateProxySetHeaders(t *testing.T) {
20272028
},
20282029
},
20292030
{
2030-
msg: "grpc",
2031-
expectedHeaders: nil,
2032-
GRPC: true,
2031+
msg: "header filter with gRPC",
2032+
GRPC: true,
2033+
filters: &dataplane.HTTPFilters{
2034+
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
2035+
Add: []dataplane.HTTPHeader{
2036+
{
2037+
Name: "Authorization",
2038+
Value: "my-auth",
2039+
},
2040+
},
2041+
Set: []dataplane.HTTPHeader{
2042+
{
2043+
Name: "Accept-Encoding",
2044+
Value: "gzip",
2045+
},
2046+
},
2047+
Remove: []string{"my-header"},
2048+
},
2049+
},
2050+
expectedHeaders: []http.Header{
2051+
{
2052+
Name: "Authorization",
2053+
Value: "${authorization_header_var}my-auth",
2054+
},
2055+
{
2056+
Name: "Accept-Encoding",
2057+
Value: "gzip",
2058+
},
2059+
{
2060+
Name: "my-header",
2061+
Value: "",
2062+
},
2063+
{
2064+
Name: "Host",
2065+
Value: "$gw_api_compliant_host",
2066+
},
2067+
{
2068+
Name: "X-Forwarded-For",
2069+
Value: "$proxy_add_x_forwarded_for",
2070+
},
2071+
{
2072+
Name: "Authority",
2073+
Value: "$gw_api_compliant_host",
2074+
},
2075+
},
20332076
},
20342077
}
20352078

0 commit comments

Comments
 (0)