Skip to content

Commit

Permalink
Make literal $ character work in set $location_path
Browse files Browse the repository at this point in the history
  • Loading branch information
bshelton229 committed Oct 7, 2018
1 parent be5b86f commit 3dc131b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
15 changes: 15 additions & 0 deletions internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var (
}
return true
},
"escapeLocationPathVar": escapeLocationPathVar,
"shouldConfigureLuaRestyWAF": shouldConfigureLuaRestyWAF,
"buildLuaSharedDictionaries": buildLuaSharedDictionaries,
"buildLocation": buildLocation,
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 7 additions & 1 deletion rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3dc131b

Please sign in to comment.