From 3dc131bd57bd5e19b559b36f0063b4245d556ea1 Mon Sep 17 00:00:00 2001 From: Bryan Shelton Date: Wed, 3 Oct 2018 15:05:12 -0700 Subject: [PATCH 1/2] Make literal $ character work in set $location_path --- internal/ingress/controller/template/template.go | 15 +++++++++++++++ .../ingress/controller/template/template_test.go | 13 +++++++++++++ rootfs/etc/nginx/template/nginx.tmpl | 8 +++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 2d5e5522c..132051cb3 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -120,6 +120,7 @@ var ( } return true }, + "escapeLocationPathVar": escapeLocationPathVar, "shouldConfigureLuaRestyWAF": shouldConfigureLuaRestyWAF, "buildLuaSharedDictionaries": buildLuaSharedDictionaries, "buildLocation": buildLocation, @@ -161,6 +162,20 @@ var ( } ) +// escapeLocationPathVar will replace the $ character with ${literal_dollar} +// which is made to work via the following configuration in the http section of +// the template: +// geo $literal_dollar { +// default "$"; +// } +func escapeLocationPathVar(input interface{}) string { + inputStr, ok := input.(string) + if !ok { + return "" + } + return strings.Replace(inputStr, `$`, `${literal_dollar}`, -1) +} + // formatIP will wrap IPv6 addresses in [] and return IPv4 addresses // without modification. If the input cannot be parsed as an IP address // it is returned without modification. diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index d8622e2f6..fd3c5d063 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -834,3 +834,16 @@ func TestBuildUpstreamName(t *testing.T) { } } } + +func TestEscapeLocationPathVar(t *testing.T) { + escapedPath := escapeLocationPathVar("/$") + expected := "/${literal_dollar}" + if escapedPath != expected { + t.Errorf("Expected %s but got %s", expected, escapedPath) + } + escapedPath = escapeLocationPathVar(false) + expected = "" + if escapedPath != expected { + t.Errorf("Expected %s but got %s", expected, escapedPath) + } +} diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 39b8b130b..23d6dfa1b 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -347,6 +347,12 @@ http { } {{ end }} + # Create a variable that contains the literal $ character. + # This works because the geo module will not resolve variables. + geo $literal_dollar { + default "$"; + } + server_name_in_redirect off; port_in_redirect off; @@ -970,7 +976,7 @@ stream { set $ingress_name "{{ $ing.Rule }}"; set $service_name "{{ $ing.Service }}"; set $service_port "{{ $location.Port }}"; - set $location_path "{{ $location.Path }}"; + set $location_path "{{ $location.Path | escapeLocationPathVar }}"; {{ if $all.Cfg.EnableOpentracing }} opentracing_propagate_context; From 3686e4f3669b1b8a8742d87971c543f6fb7e16bf Mon Sep 17 00:00:00 2001 From: Bryan Shelton Date: Tue, 9 Oct 2018 12:58:50 -0700 Subject: [PATCH 2/2] Move escapeLocationPathVar to escapeLiteralDollar --- .../ingress/controller/template/template.go | 6 ++--- .../controller/template/template_test.go | 23 +++++++++++++++---- rootfs/etc/nginx/template/nginx.tmpl | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 132051cb3..6643027e7 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -120,7 +120,7 @@ var ( } return true }, - "escapeLocationPathVar": escapeLocationPathVar, + "escapeLiteralDollar": escapeLiteralDollar, "shouldConfigureLuaRestyWAF": shouldConfigureLuaRestyWAF, "buildLuaSharedDictionaries": buildLuaSharedDictionaries, "buildLocation": buildLocation, @@ -162,13 +162,13 @@ var ( } ) -// escapeLocationPathVar will replace the $ character with ${literal_dollar} +// escapeLiteralDollar will replace the $ character with ${literal_dollar} // which is made to work via the following configuration in the http section of // the template: // geo $literal_dollar { // default "$"; // } -func escapeLocationPathVar(input interface{}) string { +func escapeLiteralDollar(input interface{}) string { inputStr, ok := input.(string) if !ok { return "" diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index fd3c5d063..a5b11f75f 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -835,15 +835,28 @@ func TestBuildUpstreamName(t *testing.T) { } } -func TestEscapeLocationPathVar(t *testing.T) { - escapedPath := escapeLocationPathVar("/$") +func TestEscapeLiteralDollar(t *testing.T) { + escapedPath := escapeLiteralDollar("/$") expected := "/${literal_dollar}" if escapedPath != expected { - t.Errorf("Expected %s but got %s", expected, escapedPath) + t.Errorf("Expected %v but returned %v", expected, escapedPath) } - escapedPath = escapeLocationPathVar(false) + + escapedPath = escapeLiteralDollar("/hello-$/world-$/") + expected = "/hello-${literal_dollar}/world-${literal_dollar}/" + if escapedPath != expected { + t.Errorf("Expected %v but returned %v", expected, escapedPath) + } + + leaveUnchagned := "/leave-me/unchagned" + escapedPath = escapeLiteralDollar(leaveUnchagned) + if escapedPath != leaveUnchagned { + t.Errorf("Expected %v but returned %v", leaveUnchagned, escapedPath) + } + + escapedPath = escapeLiteralDollar(false) expected = "" if escapedPath != expected { - t.Errorf("Expected %s but got %s", expected, escapedPath) + t.Errorf("Expected %v but returned %v", expected, escapedPath) } } diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 23d6dfa1b..714d46dd7 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -976,7 +976,7 @@ stream { set $ingress_name "{{ $ing.Rule }}"; set $service_name "{{ $ing.Service }}"; set $service_port "{{ $location.Port }}"; - set $location_path "{{ $location.Path | escapeLocationPathVar }}"; + set $location_path "{{ $location.Path | escapeLiteralDollar }}"; {{ if $all.Cfg.EnableOpentracing }} opentracing_propagate_context;