From 41f6f8c9c12e9a877bee05cc1e0b3701761d14eb Mon Sep 17 00:00:00 2001 From: Gerald Pape Date: Mon, 24 Apr 2023 15:27:40 +0200 Subject: [PATCH] Remove variables with $ before feeding into url.Parse Signed-off-by: Gerald Pape --- .../nginx-configuration/annotations.md | 3 +++ internal/ingress/annotations/mirror/main.go | 7 ++++--- .../ingress/annotations/mirror/main_test.go | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/user-guide/nginx-configuration/annotations.md b/docs/user-guide/nginx-configuration/annotations.md index b515a9f3b..ab4a8e389 100755 --- a/docs/user-guide/nginx-configuration/annotations.md +++ b/docs/user-guide/nginx-configuration/annotations.md @@ -945,6 +945,9 @@ The mirror backend can be set by applying: nginx.ingress.kubernetes.io/mirror-target: https://test.env.com/$request_uri ``` +!!! attention + When `nginx.ingress.kubernetes.io/mirror-host` is not defined, the `Host` header value is extracted from `nginx.ingress.kubernetes.io/mirror-target`, which is processed using [`url.Parse`](https://pkg.go.dev/net/url#Parse). However, if there is no `/` separator, `url.Parse` is unable to distinguish between additional path variables and the port number in the `mirror-target` value. + By default the request-body is sent to the mirror backend, but can be turned off by applying: ```yaml diff --git a/internal/ingress/annotations/mirror/main.go b/internal/ingress/annotations/mirror/main.go index cd54a9826..9cb1b0ede 100644 --- a/internal/ingress/annotations/mirror/main.go +++ b/internal/ingress/annotations/mirror/main.go @@ -93,12 +93,13 @@ func (a mirror) Parse(ing *networking.Ingress) (interface{}, error) { config.Host, err = parser.GetStringAnnotation("mirror-host", ing) if err != nil { if config.Target != "" { - url, err := parser.StringToURL(config.Target) + target := strings.Split(config.Target, "$") + + url, err := parser.StringToURL(target[0]) if err != nil { config.Host = "" } else { - hostname := strings.Split(url.Hostname(), "$") - config.Host = hostname[0] + config.Host = url.Hostname() } } } diff --git a/internal/ingress/annotations/mirror/main_test.go b/internal/ingress/annotations/mirror/main_test.go index f744ab552..add90d768 100644 --- a/internal/ingress/annotations/mirror/main_test.go +++ b/internal/ingress/annotations/mirror/main_test.go @@ -48,6 +48,24 @@ func TestParse(t *testing.T) { Target: "https://test.env.com/$request_uri", Host: "test.env.com", }}, + {map[string]string{backendURL: "https://test.env.com$request_uri"}, &Config{ + Source: ngxURI, + RequestBody: "on", + Target: "https://test.env.com$request_uri", + Host: "test.env.com", + }}, + {map[string]string{backendURL: "https://test.env.com:8080$request_uri"}, &Config{ + Source: ngxURI, + RequestBody: "on", + Target: "https://test.env.com:8080$request_uri", + Host: "test.env.com", + }}, + {map[string]string{backendURL: "https://test.env.com:8080/$request_uri"}, &Config{ + Source: ngxURI, + RequestBody: "on", + Target: "https://test.env.com:8080/$request_uri", + Host: "test.env.com", + }}, {map[string]string{requestBody: "off"}, &Config{ Source: "", RequestBody: "off",