fix golangci-lint findings

This commit is contained in:
Christian Groschupp 2023-09-18 13:29:06 +02:00
parent 42dd6b643b
commit 47cb871d10
4 changed files with 21 additions and 22 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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) {

View file

@ -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")