config: urlencode interpolation function
This escapes all characters that might have a special interpretation when embedded into a portion of a URL, including slashes, equals signs and ampersands.
This commit is contained in:
parent
5e6b518744
commit
06fd5d7a3e
|
@ -14,6 +14,7 @@ import (
|
|||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
|
@ -111,6 +112,7 @@ func Funcs() map[string]ast.Function {
|
|||
"title": interpolationFuncTitle(),
|
||||
"trimspace": interpolationFuncTrimSpace(),
|
||||
"upper": interpolationFuncUpper(),
|
||||
"urlencode": interpolationFuncURLEncode(),
|
||||
"zipmap": interpolationFuncZipMap(),
|
||||
}
|
||||
}
|
||||
|
@ -1517,3 +1519,14 @@ func interpolationFuncFlatten() ast.Function {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
func interpolationFuncURLEncode() ast.Function {
|
||||
return ast.Function{
|
||||
ArgTypes: []ast.Type{ast.TypeString},
|
||||
ReturnType: ast.TypeString,
|
||||
Callback: func(args []interface{}) (interface{}, error) {
|
||||
s := args[0].(string)
|
||||
return url.QueryEscape(s), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2557,3 +2557,30 @@ func TestInterpolateFuncFlatten(t *testing.T) {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncURLEncode(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
{
|
||||
`${urlencode("abc123-_")}`,
|
||||
"abc123-_",
|
||||
false,
|
||||
},
|
||||
{
|
||||
`${urlencode("foo:bar@localhost?foo=bar&bar=baz")}`,
|
||||
"foo%3Abar%40localhost%3Ffoo%3Dbar%26bar%3Dbaz",
|
||||
false,
|
||||
},
|
||||
{
|
||||
`${urlencode("mailto:email?subject=this+is+my+subject")}`,
|
||||
"mailto%3Aemail%3Fsubject%3Dthis%2Bis%2Bmy%2Bsubject",
|
||||
false,
|
||||
},
|
||||
{
|
||||
`${urlencode("foo/bar")}`,
|
||||
"foo%2Fbar",
|
||||
false,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -386,6 +386,8 @@ The supported built-in functions are:
|
|||
|
||||
* `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case.
|
||||
|
||||
* `urlencode(string)` - Returns an URL-safe copy of the string.
|
||||
|
||||
* `uuid()` - Returns a UUID string in RFC 4122 v4 format. This string will change with every invocation of the function, so in order to prevent diffs on every plan & apply, it must be used with the [`ignore_changes`](/docs/configuration/resources.html#ignore-changes) lifecycle attribute.
|
||||
|
||||
* `values(map)` - Returns a list of the map values, in the order of the keys
|
||||
|
|
Loading…
Reference in New Issue