diff --git a/internal/ingress/annotations/customheaders/main.go b/internal/ingress/annotations/customheaders/main.go index cb2938131..774e9c3d3 100644 --- a/internal/ingress/annotations/customheaders/main.go +++ b/internal/ingress/annotations/customheaders/main.go @@ -58,7 +58,7 @@ var customHeadersAnnotation = parser.Annotation{ Group: "backend", Annotations: parser.AnnotationFields{ customHeadersConfigMapAnnotation: { - Validator: parser.ValidateRegex(*parser.BasicCharsRegex, true), + Validator: parser.ValidateRegex(parser.BasicCharsRegex, true), Scope: parser.AnnotationScopeLocation, Risk: parser.AnnotationRiskMedium, Documentation: `This annotation sets the name of a ConfigMap that specifies headers to pass to the client. diff --git a/internal/ingress/annotations/customheaders/main_test.go b/internal/ingress/annotations/customheaders/main_test.go index 150fd0720..81c0b795a 100644 --- a/internal/ingress/annotations/customheaders/main_test.go +++ b/internal/ingress/annotations/customheaders/main_test.go @@ -101,11 +101,11 @@ func TestCustomHeadersParseAnnotations(t *testing.T) { t.Errorf("expected a *Config type") } - expected_response_headers := map[string]string{} - expected_response_headers["Content-Type"] = "application/json" - expected_response_headers["Access-Control-Max-Age"] = "600" + expectedResponseHeaders := map[string]string{} + expectedResponseHeaders["Content-Type"] = "application/json" + expectedResponseHeaders["Access-Control-Max-Age"] = "600" - c := &Config{expected_response_headers} + c := &Config{expectedResponseHeaders} if !reflect.DeepEqual(c, val) { t.Errorf("expected %v but got %v", c, val) diff --git a/internal/ingress/controller/template/configmap.go b/internal/ingress/controller/template/configmap.go index 25827a08e..f78dbad03 100644 --- a/internal/ingress/controller/template/configmap.go +++ b/internal/ingress/controller/template/configmap.go @@ -255,7 +255,7 @@ func ReadConfig(src map[string]string) config.Configuration { if val, ok := conf[globalAllowedResponseHeaders]; ok { delete(conf, globalAllowedResponseHeaders) - if len(val) != 0 { + if val != "" { harr := splitAndTrimSpace(val, ",") for _, header := range harr { if !customheaders.ValidHeader(header) { diff --git a/test/e2e/annotations/customheaders.go b/test/e2e/annotations/customheaders.go index 529d6b31b..274ce8278 100644 --- a/test/e2e/annotations/customheaders.go +++ b/test/e2e/annotations/customheaders.go @@ -26,6 +26,10 @@ import ( "k8s.io/ingress-nginx/test/e2e/framework" ) +const ( + customHeaderHost = "custom-headers" +) + var _ = framework.DescribeAnnotation("custom-headers-*", func() { f := framework.NewDefaultFramework("custom-headers") @@ -34,49 +38,44 @@ var _ = framework.DescribeAnnotation("custom-headers-*", func() { }) ginkgo.It("should return status code 200 when no custom-headers is configured", func() { - host := "custom-headers" - - ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil) + ing := framework.NewSingleIngress(customHeaderHost, "/", customHeaderHost, f.Namespace, framework.EchoService, 80, nil) f.EnsureIngress(ing) - f.WaitForNginxServer(host, + f.WaitForNginxServer(customHeaderHost, func(server string) bool { return strings.Contains(server, "server_name custom-headers") }) f.HTTPTestClient(). GET("/"). - WithHeader("Host", host). + WithHeader("Host", customHeaderHost). Expect(). Status(http.StatusOK). - Body().Contains(fmt.Sprintf("host=%v", host)) + Body().Contains(fmt.Sprintf("host=%v", customHeaderHost)) }) ginkgo.It("should return status code 503 when custom-headers is configured with an invalid secret", func() { - host := "custom-headers" annotations := map[string]string{ "nginx.ingress.kubernetes.io/custom-headers": f.Namespace + "/custom-headers", } - ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations) + ing := framework.NewSingleIngress(customHeaderHost, "/", customHeaderHost, f.Namespace, framework.EchoService, 80, annotations) f.EnsureIngress(ing) - f.WaitForNginxServer(host, + f.WaitForNginxServer(customHeaderHost, func(server string) bool { return strings.Contains(server, "server_name custom-headers") }) f.HTTPTestClient(). GET("/"). - WithHeader("Host", host). + WithHeader("Host", customHeaderHost). Expect(). Status(http.StatusServiceUnavailable). Body().Contains("503 Service Temporarily Unavailable") }) ginkgo.It(`should set "more_set_headers 'My-Custom-Header' '42';" when custom-headers are set`, func() { - host := "custom-headers" - annotations := map[string]string{ "nginx.ingress.kubernetes.io/custom-headers": f.Namespace + "/custom-headers", } @@ -87,23 +86,23 @@ var _ = framework.DescribeAnnotation("custom-headers-*", func() { }) f.UpdateNginxConfigMapData("global-allowed-response-headers", "My-Custom-Header,My-Custom-Header-Dollar") - ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations) + ing := framework.NewSingleIngress(customHeaderHost, "/", customHeaderHost, f.Namespace, framework.EchoService, 80, annotations) f.EnsureIngress(ing) - f.WaitForNginxServer(host, + f.WaitForNginxServer(customHeaderHost, func(server string) bool { return strings.Contains(server, `more_set_headers "My-Custom-Header: 42";`) }) f.HTTPTestClient(). GET("/"). - WithHeader("Host", host). + WithHeader("Host", customHeaderHost). Expect(). Status(http.StatusOK). Header("My-Custom-Header").Contains("42") f.HTTPTestClient(). GET("/"). - WithHeader("Host", host). + WithHeader("Host", customHeaderHost). Expect(). Status(http.StatusOK). Header("My-Custom-Header-Dollar").Contains("$remote_addr")