Skip to content

Commit

Permalink
Move escapeLocationPathVar to escapeLiteralDollar
Browse files Browse the repository at this point in the history
  • Loading branch information
bshelton229 committed Oct 9, 2018
1 parent 3dc131b commit 3686e4f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ var (
}
return true
},
"escapeLocationPathVar": escapeLocationPathVar,
"escapeLiteralDollar": escapeLiteralDollar,
"shouldConfigureLuaRestyWAF": shouldConfigureLuaRestyWAF,
"buildLuaSharedDictionaries": buildLuaSharedDictionaries,
"buildLocation": buildLocation,
Expand Down Expand Up @@ -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 ""
Expand Down
23 changes: 18 additions & 5 deletions internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3686e4f

Please sign in to comment.