Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base64 encode / decode custom template functions #409

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Adds support for tracing with OpenTelemetry. [cyberark/secrets-provider-for-k8s#398](https://github.com/cyberark/secrets-provider-for-k8s/pull/398)
- Adds support for Base64 encode/decode functions in custom templates. [cyberark/secrets-provider-for-k8s#409](https://github.com/cyberark/secrets-provider-for-k8s/pull/409)

### Fixed
- If the Secrets Provider is run in Push-to-File mode, it no longer errors out
Expand Down
18 changes: 18 additions & 0 deletions PUSH_TO_FILE.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,24 @@ respectively:
For a full list of global Go text template functions, reference the official
[`text/template` documentation](https://pkg.go.dev/text/template#hdr-Functions).

### Additional Template Functions

Custom templates also support a limited number of additional functions. Currently
supported functions are:

- `b64enc`: Base64 encode a value.
- `b64dec`: Base64 decode a value.

These can be used as follows:

```
{{ secret "alias" | b64enc }}
{{ secret "alias" | b64dec }}
```

When using the `b64dec` function, an error will occur if the value retrieved from
Conjur is not a valid Base64 encoded string.

### Execution "Double-Pass"

To avoid leaking sensitive secret data to logs, and to ensure that a
Expand Down
2 changes: 2 additions & 0 deletions pkg/secrets/pushtofile/push_to_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func pushToWriter(
// when the template is executed.
panic(fmt.Sprintf("secret alias %q not present in specified secrets for group", alias))
},
"b64enc": b64encTemplateFunc,
"b64dec": b64decTemplateFunc,
}).Parse(groupTemplate)
if err != nil {
return err
Expand Down
23 changes: 23 additions & 0 deletions pkg/secrets/pushtofile/push_to_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ var writeToFileTestCases = []pushToWriterTestCase{
secrets: []*Secret{{Alias: "alias", Value: "\" ' & < > \000"}},
assert: assertGoodOutput("&#34; &#39; &amp; &lt; &gt; \uFFFD"),
},
{
description: "base64 encoding",
template: `{{secret "alias" | b64enc}}`,
secrets: []*Secret{{Alias: "alias", Value: "secret value"}},
assert: assertGoodOutput("c2VjcmV0IHZhbHVl"),
},
{
description: "base64 decoding",
template: `{{secret "alias" | b64dec}}`,
secrets: []*Secret{{Alias: "alias", Value: "c2VjcmV0IHZhbHVl"}},
assert: assertGoodOutput("secret value"),
},
{
description: "base64 decoding invalid input",
template: `{{secret "alias" | b64dec}}`,
secrets: []*Secret{{Alias: "alias", Value: "c2VjcmV0IHZhbHVl_invalid"}},
assert: func(t *testing.T, s string, err error) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "value could not be base64 decoded")
// Ensure the error doesn't contain the actual secret
assert.NotContains(t, err.Error(), "c2VjcmV0IHZhbHVl_invalid")
},
},
{
description: "iterate over secret key-value pairs",
template: `{{- range $index, $secret := .SecretsArray -}}
Expand Down
12 changes: 12 additions & 0 deletions pkg/secrets/pushtofile/secret_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,18 @@ func TestNewSecretGroups(t *testing.T) {
assert.Contains(t, errs[0].Error(), `secret alias "x" not present in specified secrets`)
})

t.Run("pass custom format first-pass at execution with base64 decoding", func(t *testing.T) {
// The string "REDACTED" is valid Base64 so no error is produced in the first-pass.
_, errs := NewSecretGroups("/basepath", "", map[string]string{
"conjur.org/conjur-secrets.first": "- path/to/secret/first1\n",
"conjur.org/secret-file-path.first": "firstfilepath",
"conjur.org/secret-file-format.first": "template",
"conjur.org/secret-file-template.first": `{{ secret "first1" | b64dec }}`,
})

assert.Len(t, errs, 0)
})

t.Run("custom template - happy case from template file", func(t *testing.T) {
// Setup mocks
closableBuf := new(ClosableBuffer)
Expand Down
26 changes: 26 additions & 0 deletions pkg/secrets/pushtofile/template_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pushtofile

import "encoding/base64"

// Define template functions that don't need access to secrets in this file
// to keep the push_to_writer.go file cleaner with only the functions that
// require access to secrets.

// b64enc is a custom template function for performing a base64 encode
// on a secret value.
func b64encTemplateFunc(value string) string {
return base64.StdEncoding.EncodeToString([]byte(value))
}

// b64dec is a custom template function for performing a base64 decode
// on a secret value.
func b64decTemplateFunc(encValue string) string {
decValue, err := base64.StdEncoding.DecodeString(encValue)
if err == nil {
return string(decValue)
}

// Panic in a template function is captured as an error
// when the template is executed.
panic("value could not be base64 decoded")
}