Skip to content

Commit

Permalink
Merge pull request #3194 from bshelton229/literal-dollar-character
Browse files Browse the repository at this point in the history
Make literal $ character work in set $location_path
  • Loading branch information
k8s-ci-robot authored Oct 9, 2018
2 parents 808c2be + 3686e4f commit f56ab42
Show file tree
Hide file tree
Showing 3 changed files with 48 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
},
"escapeLiteralDollar": escapeLiteralDollar,
"shouldConfigureLuaRestyWAF": shouldConfigureLuaRestyWAF,
"buildLuaSharedDictionaries": buildLuaSharedDictionaries,
"buildLocation": buildLocation,
Expand Down Expand Up @@ -161,6 +162,20 @@ var (
}
)

// 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 escapeLiteralDollar(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
26 changes: 26 additions & 0 deletions internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,3 +858,29 @@ func TestBuildUpstreamName(t *testing.T) {
}
}
}

func TestEscapeLiteralDollar(t *testing.T) {
escapedPath := escapeLiteralDollar("/$")
expected := "/${literal_dollar}"
if escapedPath != expected {
t.Errorf("Expected %v but returned %v", expected, escapedPath)
}

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 %v but returned %v", 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 @@ -913,7 +919,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 | escapeLiteralDollar }}";

{{ if $all.Cfg.EnableOpentracing }}
opentracing_propagate_context;
Expand Down

0 comments on commit f56ab42

Please sign in to comment.