Skip to content

Commit 727a89b

Browse files
committed
Add base gRPC headers
1 parent 5597b70 commit 727a89b

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
@@ -641,17 +641,17 @@ func TestCreateServers(t *testing.T) {
641641
{
642642
Path: "@rule0-route0",
643643
ProxyPass: "http://test_foo_80$request_uri",
644-
ProxySetHeaders: baseHeaders,
644+
ProxySetHeaders: httpBaseHeaders,
645645
},
646646
{
647647
Path: "@rule0-route1",
648648
ProxyPass: "http://test_foo_80$request_uri",
649-
ProxySetHeaders: baseHeaders,
649+
ProxySetHeaders: httpBaseHeaders,
650650
},
651651
{
652652
Path: "@rule0-route2",
653653
ProxyPass: "http://test_foo_80$request_uri",
654-
ProxySetHeaders: baseHeaders,
654+
ProxySetHeaders: httpBaseHeaders,
655655
},
656656
{
657657
Path: "/",
@@ -660,7 +660,7 @@ func TestCreateServers(t *testing.T) {
660660
{
661661
Path: "@rule1-route0",
662662
ProxyPass: "http://$test__route1_rule1$request_uri",
663-
ProxySetHeaders: baseHeaders,
663+
ProxySetHeaders: httpBaseHeaders,
664664
},
665665
{
666666
Path: "/test/",
@@ -669,17 +669,17 @@ func TestCreateServers(t *testing.T) {
669669
{
670670
Path: "/path-only/",
671671
ProxyPass: "http://invalid-backend-ref$request_uri",
672-
ProxySetHeaders: baseHeaders,
672+
ProxySetHeaders: httpBaseHeaders,
673673
},
674674
{
675675
Path: "= /path-only",
676676
ProxyPass: "http://invalid-backend-ref$request_uri",
677-
ProxySetHeaders: baseHeaders,
677+
ProxySetHeaders: httpBaseHeaders,
678678
},
679679
{
680680
Path: "/backend-tls-policy/",
681681
ProxyPass: "https://test_btp_80$request_uri",
682-
ProxySetHeaders: baseHeaders,
682+
ProxySetHeaders: httpBaseHeaders,
683683
ProxySSLVerify: &http.ProxySSLVerify{
684684
Name: "test-btp.example.com",
685685
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -688,7 +688,7 @@ func TestCreateServers(t *testing.T) {
688688
{
689689
Path: "= /backend-tls-policy",
690690
ProxyPass: "https://test_btp_80$request_uri",
691-
ProxySetHeaders: baseHeaders,
691+
ProxySetHeaders: httpBaseHeaders,
692692
ProxySSLVerify: &http.ProxySSLVerify{
693693
Name: "test-btp.example.com",
694694
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
@@ -792,12 +792,12 @@ func TestCreateServers(t *testing.T) {
792792
{
793793
Path: "= /exact",
794794
ProxyPass: "http://test_foo_80$request_uri",
795-
ProxySetHeaders: baseHeaders,
795+
ProxySetHeaders: httpBaseHeaders,
796796
},
797797
{
798798
Path: "@rule12-route0",
799799
ProxyPass: "http://test_foo_80$request_uri",
800-
ProxySetHeaders: baseHeaders,
800+
ProxySetHeaders: httpBaseHeaders,
801801
},
802802
{
803803
Path: "= /test",
@@ -859,7 +859,7 @@ func TestCreateServers(t *testing.T) {
859859
Path: "= /grpc/method",
860860
ProxyPass: "grpc://test_foo_80",
861861
GRPC: true,
862-
ProxySetHeaders: nil,
862+
ProxySetHeaders: grpcBaseHeaders,
863863
},
864864
{
865865
Path: "= /grpc-with-backend-tls-policy/method",
@@ -869,7 +869,7 @@ func TestCreateServers(t *testing.T) {
869869
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
870870
},
871871
GRPC: true,
872-
ProxySetHeaders: nil,
872+
ProxySetHeaders: grpcBaseHeaders,
873873
},
874874
}
875875
}
@@ -989,12 +989,12 @@ func TestCreateServersConflicts(t *testing.T) {
989989
{
990990
Path: "/coffee/",
991991
ProxyPass: "http://test_foo_80$request_uri",
992-
ProxySetHeaders: baseHeaders,
992+
ProxySetHeaders: httpBaseHeaders,
993993
},
994994
{
995995
Path: "= /coffee",
996996
ProxyPass: "http://test_bar_80$request_uri",
997-
ProxySetHeaders: baseHeaders,
997+
ProxySetHeaders: httpBaseHeaders,
998998
},
999999
createDefaultRootLocation(),
10001000
},
@@ -1027,12 +1027,12 @@ func TestCreateServersConflicts(t *testing.T) {
10271027
{
10281028
Path: "= /coffee",
10291029
ProxyPass: "http://test_foo_80$request_uri",
1030-
ProxySetHeaders: baseHeaders,
1030+
ProxySetHeaders: httpBaseHeaders,
10311031
},
10321032
{
10331033
Path: "/coffee/",
10341034
ProxyPass: "http://test_bar_80$request_uri",
1035-
ProxySetHeaders: baseHeaders,
1035+
ProxySetHeaders: httpBaseHeaders,
10361036
},
10371037
createDefaultRootLocation(),
10381038
},
@@ -1075,12 +1075,12 @@ func TestCreateServersConflicts(t *testing.T) {
10751075
{
10761076
Path: "/coffee/",
10771077
ProxyPass: "http://test_bar_80$request_uri",
1078-
ProxySetHeaders: baseHeaders,
1078+
ProxySetHeaders: httpBaseHeaders,
10791079
},
10801080
{
10811081
Path: "= /coffee",
10821082
ProxyPass: "http://test_baz_80$request_uri",
1083-
ProxySetHeaders: baseHeaders,
1083+
ProxySetHeaders: httpBaseHeaders,
10841084
},
10851085
createDefaultRootLocation(),
10861086
},
@@ -1198,12 +1198,12 @@ func TestCreateLocationsRootPath(t *testing.T) {
11981198
{
11991199
Path: "/path-1",
12001200
ProxyPass: "http://test_foo_80$request_uri",
1201-
ProxySetHeaders: baseHeaders,
1201+
ProxySetHeaders: httpBaseHeaders,
12021202
},
12031203
{
12041204
Path: "/path-2",
12051205
ProxyPass: "http://test_foo_80$request_uri",
1206-
ProxySetHeaders: baseHeaders,
1206+
ProxySetHeaders: httpBaseHeaders,
12071207
},
12081208
{
12091209
Path: "/",
@@ -1221,17 +1221,18 @@ func TestCreateLocationsRootPath(t *testing.T) {
12211221
{
12221222
Path: "/path-1",
12231223
ProxyPass: "http://test_foo_80$request_uri",
1224-
ProxySetHeaders: baseHeaders,
1224+
ProxySetHeaders: httpBaseHeaders,
12251225
},
12261226
{
12271227
Path: "/path-2",
12281228
ProxyPass: "http://test_foo_80$request_uri",
1229-
ProxySetHeaders: baseHeaders,
1229+
ProxySetHeaders: httpBaseHeaders,
12301230
},
12311231
{
1232-
Path: "/grpc",
1233-
ProxyPass: "grpc://test_foo_80",
1234-
GRPC: true,
1232+
Path: "/grpc",
1233+
ProxyPass: "grpc://test_foo_80",
1234+
GRPC: true,
1235+
ProxySetHeaders: grpcBaseHeaders,
12351236
},
12361237
{
12371238
Path: "/",
@@ -1248,17 +1249,17 @@ func TestCreateLocationsRootPath(t *testing.T) {
12481249
{
12491250
Path: "/path-1",
12501251
ProxyPass: "http://test_foo_80$request_uri",
1251-
ProxySetHeaders: baseHeaders,
1252+
ProxySetHeaders: httpBaseHeaders,
12521253
},
12531254
{
12541255
Path: "/path-2",
12551256
ProxyPass: "http://test_foo_80$request_uri",
1256-
ProxySetHeaders: baseHeaders,
1257+
ProxySetHeaders: httpBaseHeaders,
12571258
},
12581259
{
12591260
Path: "/",
12601261
ProxyPass: "http://test_foo_80$request_uri",
1261-
ProxySetHeaders: baseHeaders,
1262+
ProxySetHeaders: httpBaseHeaders,
12621263
},
12631264
},
12641265
},
@@ -1977,9 +1978,51 @@ func TestGenerateProxySetHeaders(t *testing.T) {
19771978
},
19781979
},
19791980
{
1980-
msg: "grpc",
1981-
expectedHeaders: nil,
1982-
GRPC: true,
1981+
msg: "header filter with gRPC",
1982+
GRPC: true,
1983+
filters: &dataplane.HTTPFilters{
1984+
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
1985+
Add: []dataplane.HTTPHeader{
1986+
{
1987+
Name: "Authorization",
1988+
Value: "my-auth",
1989+
},
1990+
},
1991+
Set: []dataplane.HTTPHeader{
1992+
{
1993+
Name: "Accept-Encoding",
1994+
Value: "gzip",
1995+
},
1996+
},
1997+
Remove: []string{"my-header"},
1998+
},
1999+
},
2000+
expectedHeaders: []http.Header{
2001+
{
2002+
Name: "Authorization",
2003+
Value: "${authorization_header_var}my-auth",
2004+
},
2005+
{
2006+
Name: "Accept-Encoding",
2007+
Value: "gzip",
2008+
},
2009+
{
2010+
Name: "my-header",
2011+
Value: "",
2012+
},
2013+
{
2014+
Name: "Host",
2015+
Value: "$gw_api_compliant_host",
2016+
},
2017+
{
2018+
Name: "X-Forwarded-For",
2019+
Value: "$proxy_add_x_forwarded_for",
2020+
},
2021+
{
2022+
Name: "Authority",
2023+
Value: "$gw_api_compliant_host",
2024+
},
2025+
},
19832026
},
19842027
}
19852028

0 commit comments

Comments
 (0)