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 support for Go templating #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,18 @@ The objects field of the SecretProviderClass can contain the following sub-field
```
If you use the jmesPath field, you must provide the following two sub-fields:
* path: This required field is the [JMES path](https://jmespath.org/specification.html) to use for retrieval
* objectAlias: This required field specifies the file name under which the key-value pair secret will be mounted.
* objectAlias: This required field specifies the file name under which the key-value pair secret will be mounted.

* objectTemplate: Tgus optional field specifies a Go template for transforming the JSON object containing all key-value secrets to a desired format. For example, to transform secrets into a format that can be sourced in bash:
```
objects: |
- objectName: "MySecret"
objectType: "secretsmanager"
objectTemplate: |
{{ range $k, $v := . }}export {{ $k }}={{ $v }}
{{ end }}

```

## Additional Considerations

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/aws/aws-sdk-go v1.37.0
github.com/jmespath/go-jmespath v0.4.0
google.golang.org/grpc v1.35.0
gopkg.in/yaml.v2 v2.3.0
k8s.io/api v0.20.2
k8s.io/apimachinery v0.20.2
k8s.io/client-go v0.20.2
Expand Down
878 changes: 878 additions & 0 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions provider/secret_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type SecretDescriptor struct {
//Optional array to specify what json key value pairs to extract from a secret and mount as individual secrets
JMESPath []JMESPathEntry `json:"jmesPath"`

// Optional Go template to use for transforming the secret value
ObjectTemplate string `json:objectTemplate`

// Path translation character (not part of YAML spec).
translate string `json:"-"`

Expand Down
29 changes: 29 additions & 0 deletions provider/secret_value.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package provider

import (
"bytes"
"encoding/json"
"fmt"
"text/template"

"github.com/jmespath/go-jmespath"
)

Expand Down Expand Up @@ -60,3 +63,29 @@ func (p *SecretValue) getJsonSecrets() (s []*SecretValue, e error) {
}
return jsonValues, nil
}

func (p *SecretValue) getTemplatedSecrets() (*SecretValue, error) {

var data interface{}
err := json.Unmarshal(p.Value, &data)
if err != nil {
return nil, fmt.Errorf("Invalid JSON used with template in secret: %s", p.Descriptor.ObjectName)
}

objectTemplate := p.Descriptor.ObjectTemplate
t, err := template.New("secretTemplate").Parse(objectTemplate)
if err != nil {
return nil, fmt.Errorf("Invalid template %s", objectTemplate)
}

var result bytes.Buffer
err = t.Execute(&result, data)
if err != nil {
return nil, err
}
secretValue := SecretValue{
Value: result.Bytes(),
Descriptor: p.Descriptor,
}
return &secretValue, nil
}
65 changes: 65 additions & 0 deletions provider/secret_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,68 @@ func TestInvalidJMESResultType(t *testing.T) {

RunGetJsonSecretTest(t, jsonContent, path, objectAlias, expectedErrorMessage)
}

func RunGetTemplatedSecrets(t *testing.T, jsonContent string, objectTemplate string) (*SecretValue, error) {
descriptor := SecretDescriptor{
ObjectName: TEST_OBJECT_NAME,
ObjectTemplate: objectTemplate,
}

secretValue := SecretValue{
Value: []byte(jsonContent),
Descriptor: descriptor,
}

secrets, err := secretValue.getTemplatedSecrets()

if err != nil {
return nil, err
}

return secrets, nil
}
func TestSecretsTemplateValidJSON(t *testing.T) {
jsonContent := `{"username": 3, "password": "abc"}`
template := `
{{ range $k, $v := . }}export {{ $k }}={{ $v }}
{{ end }}`
expectedResult := `
export password=abc
export username=3
`
secrets, err := RunGetTemplatedSecrets(t, jsonContent, template)
result := string(secrets.Value)
if err != nil {
t.Fatalf("Failed during template execution %v", err)
}

if result != expectedResult {
t.Fatalf("Templated result is not correct\n got: %s\n want: %s", result, expectedResult )
}
}

func TestSecretsTemplateInvalidJSON(t *testing.T) {
jsonContent := `{"username": 3`
template := `
{{ range $k, $v := . }}export {{ $k }}={{ $v }}
{{ end }}`
expectedErrorMessage := fmt.Sprintf("Invalid JSON used with template in secret: %s", TEST_OBJECT_NAME)
_, err := RunGetTemplatedSecrets(t, jsonContent, template)

if err == nil || err.Error() != expectedErrorMessage {
t.Fatalf("Expected error: %s, got error: %v", expectedErrorMessage, err)
}
}

func TestSecretsTemplateInvalidTemplate(t *testing.T) {
jsonContent := `{"username": 3}`
template := `
{ range $k, $v := . }}export {{ $k }}={{ $v }}
{{ end }}`
expectedErrorMessage := fmt.Sprintf("Invalid template %s", template)
_, err := RunGetTemplatedSecrets(t, jsonContent, template)

if err == nil || err.Error() != expectedErrorMessage {
t.Fatalf("Expected error: %s, got error: %v", expectedErrorMessage, err)
}
}
9 changes: 9 additions & 0 deletions provider/secrets_manager_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ func (p *SecretsManagerProvider) GetSecretValues(

values = append(values, jsonSecrets...)

//Transform secrets using provided template
if secret.Descriptor.ObjectTemplate != "" {
templatedSecrets, err := secret.getTemplatedSecrets()
if err != nil {
return nil, err
}
values = append(values, templatedSecrets)
}

// Update the version in the current version map.
for _, jsonSecret := range jsonSecrets {
jsonDescriptor := jsonSecret.Descriptor
Expand Down