Skip to content

Commit

Permalink
feat(template) add json escaping functions. (#70)
Browse files Browse the repository at this point in the history
* feat(template) add json escaping functions.

* doc(template): add json escape functions.
  • Loading branch information
Zenithar committed Oct 21, 2021
1 parent 5b784e8 commit 0d668fb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ FEATURES:
* to/vault: Support Vault >1.9 custom metadata for bundle metadata publication. [#68](https://github.com/elastic/harp/pull/68)
* from/vault: Support Vault >1.9 custom metadata for bundle metadata retrieval. [#68](https://github.com/elastic/harp/pull/68)
* from/vault: Support legacy bundle metadata format. [#69](https://github.com/elastic/harp/pull/69)
* template/engine: `jsonEscape` / `jsonUnescape` is added to handle string escaping using JSON character escaping strategy [#70](https://github.com/elastic/harp/pull/70)
* template/engine: `unquote` is added to unquote a `quote` escaped string [#70](https://github.com/elastic/harp/pull/70)

## 0.1.24

Expand Down
29 changes: 22 additions & 7 deletions pkg/template/engine/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package engine

import (
"encoding/base64"
"encoding/json"
"net/url"
"strconv"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
Expand Down Expand Up @@ -74,13 +77,6 @@ func FuncMap(secretReaders []SecretReaderFunc) template.FuncMap {
// Bech32
"bech32enc": bech32.Encode,
"bech32dec": crypto.Bech32Decode,
// URL
"urlPathEscape": url.PathEscape,
"urlPathUnescape": url.PathUnescape,
"urlQueryEscape": url.QueryEscape,
"urlQueryUnescape": url.QueryUnescape,
// Escape
"shellEscape": shellescape.Quote,
// Base64
"b64urlenc": func(in string) string {
return base64.URLEncoding.EncodeToString([]byte(in))
Expand All @@ -89,6 +85,25 @@ func FuncMap(secretReaders []SecretReaderFunc) template.FuncMap {
out, err := base64.URLEncoding.DecodeString(in)
return string(out), err
},
// Escaping
"urlPathEscape": url.PathEscape,
"urlPathUnescape": url.PathUnescape,
"urlQueryEscape": url.QueryEscape,
"urlQueryUnescape": url.QueryUnescape,
"shellEscape": shellescape.Quote,
"jsonEscape": func(in string) (string, error) {
b, err := json.Marshal(in)
// Trim the beginning and trailing " character
return strings.Trim(string(b), `"`), err
},
"jsonUnescape": func(in string) (string, error) {
var out string
if err := json.Unmarshal([]byte(in), &out); err != nil {
return "", err
}
return out, nil
},
"unquote": strconv.Unquote,
}

for k, v := range extra {
Expand Down
12 changes: 12 additions & 0 deletions pkg/template/engine/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ func TestFuncs(t *testing.T) {
tpl: `{{ fromYamlArray . }}`,
expect: `[error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go value of type []interface {}]`,
vars: `hello: world`,
}, {
tpl: `{{ jsonEscape . }}`,
expect: `backslash: \\, A: \u0026 \u003c`,
vars: `backslash: \, A: & <`,
}, {
tpl: `{{ jsonUnescape . }}`,
expect: `backslash: \, A: & <`,
vars: `"backslash: \\, A: \u0026 \u003c"`,
}, {
tpl: `{{ unquote . }}`,
expect: `{"channel":"buu","name":"john", "msg":"doe"}`,
vars: `"{\"channel\":\"buu\",\"name\":\"john\", \"msg\":\"doe\"}"`,
}}

for _, tt := range tests {
Expand Down
9 changes: 9 additions & 0 deletions samples/onboarding/1-template-engine/2-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ https://logstash:{{ paranoidPassword | urlQueryEscape }}@ingester.es.cloud:1234
https://logstash:K3iDayow9%5Cav67HawD6%210k~8lhcm8oLVUBt2wE%3E%5DLBJQJVj%3AfIx%2Fuo%40%7B%3D6kvgXHK@ingester.es.cloud:1234%
```
#### jsonEscape / jsonUnescape
Apply JSON ecaping strategy to a string
```ruby
{{ "backslash: \, A: & <" | jsonEscape }}
backslash: \\, A: \u0026 \u003c
```
### Secret loader
#### secret
Expand Down

0 comments on commit 0d668fb

Please sign in to comment.