From 4cbaa414688cbc9e67abe43a835b4eabeb33be02 Mon Sep 17 00:00:00 2001 From: jgoelen Date: Wed, 31 Jul 2024 08:50:41 +0200 Subject: [PATCH 1/3] Add Equal func for customheaders.Config --- internal/ingress/annotations/customheaders/main.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/ingress/annotations/customheaders/main.go b/internal/ingress/annotations/customheaders/main.go index 774e9c3d3a..bc0ef2eb52 100644 --- a/internal/ingress/annotations/customheaders/main.go +++ b/internal/ingress/annotations/customheaders/main.go @@ -18,6 +18,7 @@ package customheaders import ( "fmt" + "reflect" "regexp" "k8s.io/klog/v2" @@ -35,6 +36,18 @@ type Config struct { Headers map[string]string `json:"headers,omitempty"` } +// Equal tests for equality between two Config types +func (c1 *Config) Equal(c2 *Config) bool { + if c1 == c2 { + return true + } + if c1 == nil || c2 == nil { + return false + } + + return reflect.DeepEqual(c1.Headers, c2.Headers) +} + var ( headerRegexp = regexp.MustCompile(`^[a-zA-Z\d\-_]+$`) valueRegexp = regexp.MustCompile(`^[a-zA-Z\d_ :;.,\\/"'?!(){}\[\]@<>=\-+*#$&\x60|~^%]+$`) From 1280f560eed960edc1c1f5d501e58f79151d15f6 Mon Sep 17 00:00:00 2001 From: jgoelen Date: Wed, 31 Jul 2024 11:07:28 +0200 Subject: [PATCH 2/3] Add equality check of CustomHeaders to a Location --- pkg/apis/ingress/types_equals.go | 4 ++++ test/manifests/configuration-a.json | 7 ++++++- test/manifests/configuration-b.json | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/apis/ingress/types_equals.go b/pkg/apis/ingress/types_equals.go index eeed9a06e4..e8ef2af4dc 100644 --- a/pkg/apis/ingress/types_equals.go +++ b/pkg/apis/ingress/types_equals.go @@ -470,6 +470,10 @@ func (l1 *Location) Equal(l2 *Location) bool { return false } + if !l1.CustomHeaders.Equal(&l2.CustomHeaders) { + return false + } + return true } diff --git a/test/manifests/configuration-a.json b/test/manifests/configuration-a.json index ba513c616a..f9599d77cd 100644 --- a/test/manifests/configuration-a.json +++ b/test/manifests/configuration-a.json @@ -302,7 +302,12 @@ "validationDepth": 0 }, "use-port-in-redirects": false, - "configuration-snippet": "" + "configuration-snippet": "", + "customHeaders": { + "headers": { + "Server": "HAL9000" + } + } }] }, { "hostname": "dev.mycompany.com", diff --git a/test/manifests/configuration-b.json b/test/manifests/configuration-b.json index 9e40785b4d..d2e71bb29c 100644 --- a/test/manifests/configuration-b.json +++ b/test/manifests/configuration-b.json @@ -302,7 +302,12 @@ "validationDepth": 0 }, "use-port-in-redirects": false, - "configuration-snippet": "" + "configuration-snippet": "", + "customHeaders": { + "headers": { + "Server": "HAL9000" + } + } }] }, { "hostname": "dev.mycompany.com", From 1d3b302c8b1fc2b38add4668b3b20a1882027c89 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Thu, 12 Dec 2024 13:13:19 +0100 Subject: [PATCH 3/3] test(customheaders): add test for annotation added reproduces https://github.com/kubernetes/ingress-nginx/issues/11680 --- test/e2e/annotations/customheaders.go | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/e2e/annotations/customheaders.go b/test/e2e/annotations/customheaders.go index 274ce8278c..d859c51d69 100644 --- a/test/e2e/annotations/customheaders.go +++ b/test/e2e/annotations/customheaders.go @@ -107,4 +107,53 @@ var _ = framework.DescribeAnnotation("custom-headers-*", func() { Status(http.StatusOK). Header("My-Custom-Header-Dollar").Contains("$remote_addr") }) + + ginkgo.It(`should set "more_set_headers 'My-Custom-Header' '42';" when custom-headers annotation is added`, func() { + f.CreateConfigMap("custom-headers", map[string]string{ + "My-Custom-Header": "42", + "My-Custom-Header-Dollar": "$remote_addr", + }) + f.UpdateNginxConfigMapData("global-allowed-response-headers", "My-Custom-Header,My-Custom-Header-Dollar") + + annotations := map[string]string{} + ing := framework.NewSingleIngress(customHeaderHost, "/", customHeaderHost, f.Namespace, framework.EchoService, 80, annotations) + f.EnsureIngress(ing) + + f.WaitForNginxServer(customHeaderHost, + func(server string) bool { + return strings.Contains(server, "server_name custom-headers") + }) + + f.HTTPTestClient(). + GET("/"). + WithHeader("Host", customHeaderHost). + Expect(). + Status(http.StatusOK). + Header("My-Custom-Header").Equal("") + + annotations = map[string]string{ + "nginx.ingress.kubernetes.io/custom-headers": f.Namespace + "/custom-headers", + } + + ing.Annotations = annotations + + f.UpdateIngress(ing) + f.WaitForNginxServer(customHeaderHost, + func(server string) bool { + return strings.Contains(server, `more_set_headers "My-Custom-Header: 42";`) + }) + + f.HTTPTestClient(). + GET("/"). + WithHeader("Host", customHeaderHost). + Expect(). + Status(http.StatusOK). + Header("My-Custom-Header").Contains("42") + f.HTTPTestClient(). + GET("/"). + WithHeader("Host", customHeaderHost). + Expect(). + Status(http.StatusOK). + Header("My-Custom-Header-Dollar").Contains("$remote_addr") + }) })