Remove variables with $ before feeding into url.Parse

Signed-off-by: Gerald Pape <gerald@giantswarm.io>
This commit is contained in:
Gerald Pape 2023-04-24 15:27:40 +02:00
parent 057c1b26fb
commit 41f6f8c9c1
No known key found for this signature in database
GPG key ID: 27830AA75B7320B4
3 changed files with 25 additions and 3 deletions

View file

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

View file

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

View file

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