Skip to content

Commit 60bead2

Browse files
committed
Add base gRPC headers
1 parent 8a9a1ad commit 60bead2

File tree

2 files changed

+93
-37
lines changed

2 files changed

+93
-37
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,25 @@ var baseHeaders = []http.Header{
3030
Name: "X-Forwarded-For",
3131
Value: "$proxy_add_x_forwarded_for",
3232
},
33-
{
33+
}
34+
35+
// httpBaseHeaders contains the constant headers set in each HTTP server block
36+
var httpBaseHeaders = append(baseHeaders,
37+
http.Header{
3438
Name: "Upgrade",
3539
Value: "$http_upgrade",
3640
},
37-
{
41+
http.Header{
3842
Name: "Connection",
3943
Value: "$connection_upgrade",
40-
},
41-
}
44+
})
45+
46+
// grpcBaseHeaders contains the constant headers set in each gRPC server block
47+
var grpcBaseHeaders = append(baseHeaders,
48+
http.Header{
49+
Name: "Authority",
50+
Value: "$gw_api_compliant_host",
51+
})
4252

4353
func executeServers(conf dataplane.Configuration) []executeResult {
4454
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers)
@@ -556,8 +566,11 @@ func createMatchLocation(path string) http.Location {
556566
func generateProxySetHeaders(filters *dataplane.HTTPFilters, grpc bool) []http.Header {
557567
var headers []http.Header
558568
if !grpc {
559-
headers = make([]http.Header, len(baseHeaders))
560-
copy(headers, baseHeaders)
569+
headers = make([]http.Header, len(httpBaseHeaders))
570+
copy(headers, httpBaseHeaders)
571+
} else {
572+
headers = make([]http.Header, len(grpcBaseHeaders))
573+
copy(headers, grpcBaseHeaders)
561574
}
562575

563576
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
@@ -614,17 +614,17 @@ func TestCreateServers(t *testing.T) {
614614
{
615615
Path: "@rule0-route0",
616616
ProxyPass: "http://test_foo_80$request_uri",
617-
ProxySetHeaders: baseHeaders,
617+
ProxySetHeaders: httpBaseHeaders,
618618
},
619619
{
620620
Path: "@rule0-route1",
621621
ProxyPass: "http://test_foo_80$request_uri",
622-
ProxySetHeaders: baseHeaders,
622+
ProxySetHeaders: httpBaseHeaders,
623623
},
624624
{
625625
Path: "@rule0-route2",
626626
ProxyPass: "http://test_foo_80$request_uri",
627-
ProxySetHeaders: baseHeaders,
627+
ProxySetHeaders: httpBaseHeaders,
628628
},
629629
{
630630
Path: "/",
@@ -633,7 +633,7 @@ func TestCreateServers(t *testing.T) {
633633
{
634634
Path: "@rule1-route0",
635635
ProxyPass: "http://$test__route1_rule1$request_uri",
636-
ProxySetHeaders: baseHeaders,
636+
ProxySetHeaders: httpBaseHeaders,
637637
},
638638
{
639639
Path: "/test/",
@@ -642,17 +642,17 @@ func TestCreateServers(t *testing.T) {
642642
{
643643
Path: "/path-only/",
644644
ProxyPass: "http://invalid-backend-ref$request_uri",
645-
ProxySetHeaders: baseHeaders,
645+
ProxySetHeaders: httpBaseHeaders,
646646
},
647647
{
648648
Path: "= /path-only",
649649
ProxyPass: "http://invalid-backend-ref$request_uri",
650-
ProxySetHeaders: baseHeaders,
650+
ProxySetHeaders: httpBaseHeaders,
651651
},
652652
{
653653
Path: "/backend-tls-policy/",
654654
ProxyPass: "https://test_btp_80$request_uri",
655-
ProxySetHeaders: baseHeaders,
655+
ProxySetHeaders: httpBaseHeaders,
656656
ProxySSLVerify: &http.ProxySSLVerify{
657657
Name: "test-btp.example.com",
658658
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -661,7 +661,7 @@ func TestCreateServers(t *testing.T) {
661661
{
662662
Path: "= /backend-tls-policy",
663663
ProxyPass: "https://test_btp_80$request_uri",
664-
ProxySetHeaders: baseHeaders,
664+
ProxySetHeaders: httpBaseHeaders,
665665
ProxySSLVerify: &http.ProxySSLVerify{
666666
Name: "test-btp.example.com",
667667
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -765,12 +765,12 @@ func TestCreateServers(t *testing.T) {
765765
{
766766
Path: "= /exact",
767767
ProxyPass: "http://test_foo_80$request_uri",
768-
ProxySetHeaders: baseHeaders,
768+
ProxySetHeaders: httpBaseHeaders,
769769
},
770770
{
771771
Path: "@rule12-route0",
772772
ProxyPass: "http://test_foo_80$request_uri",
773-
ProxySetHeaders: baseHeaders,
773+
ProxySetHeaders: httpBaseHeaders,
774774
},
775775
{
776776
Path: "= /test",
@@ -832,7 +832,7 @@ func TestCreateServers(t *testing.T) {
832832
Path: "= /grpc/method",
833833
ProxyPass: "grpc://test_foo_80",
834834
GRPC: true,
835-
ProxySetHeaders: nil,
835+
ProxySetHeaders: grpcBaseHeaders,
836836
},
837837
{
838838
Path: "= /grpc-with-backend-tls-policy/method",
@@ -842,7 +842,7 @@ func TestCreateServers(t *testing.T) {
842842
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
843843
},
844844
GRPC: true,
845-
ProxySetHeaders: nil,
845+
ProxySetHeaders: grpcBaseHeaders,
846846
},
847847
}
848848
}
@@ -962,12 +962,12 @@ func TestCreateServersConflicts(t *testing.T) {
962962
{
963963
Path: "/coffee/",
964964
ProxyPass: "http://test_foo_80$request_uri",
965-
ProxySetHeaders: baseHeaders,
965+
ProxySetHeaders: httpBaseHeaders,
966966
},
967967
{
968968
Path: "= /coffee",
969969
ProxyPass: "http://test_bar_80$request_uri",
970-
ProxySetHeaders: baseHeaders,
970+
ProxySetHeaders: httpBaseHeaders,
971971
},
972972
createDefaultRootLocation(),
973973
},
@@ -1000,12 +1000,12 @@ func TestCreateServersConflicts(t *testing.T) {
10001000
{
10011001
Path: "= /coffee",
10021002
ProxyPass: "http://test_foo_80$request_uri",
1003-
ProxySetHeaders: baseHeaders,
1003+
ProxySetHeaders: httpBaseHeaders,
10041004
},
10051005
{
10061006
Path: "/coffee/",
10071007
ProxyPass: "http://test_bar_80$request_uri",
1008-
ProxySetHeaders: baseHeaders,
1008+
ProxySetHeaders: httpBaseHeaders,
10091009
},
10101010
createDefaultRootLocation(),
10111011
},
@@ -1048,12 +1048,12 @@ func TestCreateServersConflicts(t *testing.T) {
10481048
{
10491049
Path: "/coffee/",
10501050
ProxyPass: "http://test_bar_80$request_uri",
1051-
ProxySetHeaders: baseHeaders,
1051+
ProxySetHeaders: httpBaseHeaders,
10521052
},
10531053
{
10541054
Path: "= /coffee",
10551055
ProxyPass: "http://test_baz_80$request_uri",
1056-
ProxySetHeaders: baseHeaders,
1056+
ProxySetHeaders: httpBaseHeaders,
10571057
},
10581058
createDefaultRootLocation(),
10591059
},
@@ -1171,12 +1171,12 @@ func TestCreateLocationsRootPath(t *testing.T) {
11711171
{
11721172
Path: "/path-1",
11731173
ProxyPass: "http://test_foo_80$request_uri",
1174-
ProxySetHeaders: baseHeaders,
1174+
ProxySetHeaders: httpBaseHeaders,
11751175
},
11761176
{
11771177
Path: "/path-2",
11781178
ProxyPass: "http://test_foo_80$request_uri",
1179-
ProxySetHeaders: baseHeaders,
1179+
ProxySetHeaders: httpBaseHeaders,
11801180
},
11811181
{
11821182
Path: "/",
@@ -1194,17 +1194,18 @@ func TestCreateLocationsRootPath(t *testing.T) {
11941194
{
11951195
Path: "/path-1",
11961196
ProxyPass: "http://test_foo_80$request_uri",
1197-
ProxySetHeaders: baseHeaders,
1197+
ProxySetHeaders: httpBaseHeaders,
11981198
},
11991199
{
12001200
Path: "/path-2",
12011201
ProxyPass: "http://test_foo_80$request_uri",
1202-
ProxySetHeaders: baseHeaders,
1202+
ProxySetHeaders: httpBaseHeaders,
12031203
},
12041204
{
1205-
Path: "/grpc",
1206-
ProxyPass: "grpc://test_foo_80",
1207-
GRPC: true,
1205+
Path: "/grpc",
1206+
ProxyPass: "grpc://test_foo_80",
1207+
GRPC: true,
1208+
ProxySetHeaders: grpcBaseHeaders,
12081209
},
12091210
{
12101211
Path: "/",
@@ -1221,17 +1222,17 @@ func TestCreateLocationsRootPath(t *testing.T) {
12211222
{
12221223
Path: "/path-1",
12231224
ProxyPass: "http://test_foo_80$request_uri",
1224-
ProxySetHeaders: baseHeaders,
1225+
ProxySetHeaders: httpBaseHeaders,
12251226
},
12261227
{
12271228
Path: "/path-2",
12281229
ProxyPass: "http://test_foo_80$request_uri",
1229-
ProxySetHeaders: baseHeaders,
1230+
ProxySetHeaders: httpBaseHeaders,
12301231
},
12311232
{
12321233
Path: "/",
12331234
ProxyPass: "http://test_foo_80$request_uri",
1234-
ProxySetHeaders: baseHeaders,
1235+
ProxySetHeaders: httpBaseHeaders,
12351236
},
12361237
},
12371238
},
@@ -1950,9 +1951,51 @@ func TestGenerateProxySetHeaders(t *testing.T) {
19501951
},
19511952
},
19521953
{
1953-
msg: "grpc",
1954-
expectedHeaders: nil,
1955-
GRPC: true,
1954+
msg: "header filter with gRPC",
1955+
GRPC: true,
1956+
filters: &dataplane.HTTPFilters{
1957+
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
1958+
Add: []dataplane.HTTPHeader{
1959+
{
1960+
Name: "Authorization",
1961+
Value: "my-auth",
1962+
},
1963+
},
1964+
Set: []dataplane.HTTPHeader{
1965+
{
1966+
Name: "Accept-Encoding",
1967+
Value: "gzip",
1968+
},
1969+
},
1970+
Remove: []string{"my-header"},
1971+
},
1972+
},
1973+
expectedHeaders: []http.Header{
1974+
{
1975+
Name: "Authorization",
1976+
Value: "${authorization_header_var}my-auth",
1977+
},
1978+
{
1979+
Name: "Accept-Encoding",
1980+
Value: "gzip",
1981+
},
1982+
{
1983+
Name: "my-header",
1984+
Value: "",
1985+
},
1986+
{
1987+
Name: "Host",
1988+
Value: "$gw_api_compliant_host",
1989+
},
1990+
{
1991+
Name: "X-Forwarded-For",
1992+
Value: "$proxy_add_x_forwarded_for",
1993+
},
1994+
{
1995+
Name: "Authority",
1996+
Value: "$gw_api_compliant_host",
1997+
},
1998+
},
19561999
},
19572000
}
19582001

0 commit comments

Comments
 (0)