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

feat: add support for AWS SM plaintext secrets #262

Merged
merged 1 commit into from
May 9, 2024
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ Your standard `AWS_DEFAULT_REGION`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`
- Key format
- `env_sync` - path based
- `env` - path based
- Handles plain text secrets (rather than key/value pairs) via the `plaintext: true` property

### Example Config

Expand All @@ -648,6 +649,19 @@ aws_secretsmanager:
path: /prod/billing-svc/vars/mg
```

Plain text secrets are useful for files; instead of using the usual JSON encoded key/value pairs. This example shows how to get a plaintext secret:

```yaml
aws_secretsmanager:
env_sync:
path: /prod/billing-svc
env:
MY_FILE:
path: /prod/billing-svc/some-file
plaintext: true
```


## AWS Parameter store

### Authentication
Expand Down
37 changes: 23 additions & 14 deletions pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const (
Medium Severity = "medium"
Low Severity = "low"
None Severity = "none"

// PlainTextKey the key used for plaintext secrets
PlainTextKey = "plaintext"
)

type RemapKeyPath struct {
Expand All @@ -29,6 +32,7 @@ type KeyPath struct {
RemapWith *map[string]RemapKeyPath `yaml:"remap_with,omitempty"`
Decrypt bool `yaml:"decrypt,omitempty"`
Optional bool `yaml:"optional,omitempty"`
Plaintext bool `yaml:"plaintext,omitempty"`
Severity Severity `yaml:"severity,omitempty" default:"high"`
RedactWith string `yaml:"redact_with,omitempty" default:"**REDACTED**"`
Source string `yaml:"source,omitempty"`
Expand All @@ -43,6 +47,9 @@ type WizardAnswers struct {
}

func (k *KeyPath) EffectiveKey() string {
if k.Plaintext {
return PlainTextKey
}
key := k.Env
if k.Field != "" {
key = k.Field
Expand Down Expand Up @@ -94,24 +101,26 @@ func (k *KeyPath) FoundWithKey(key, v string) EnvEntry {

func (k *KeyPath) WithEnv(env string) KeyPath {
return KeyPath{
Env: env,
Path: k.Path,
Field: k.Field,
Decrypt: k.Decrypt,
Optional: k.Optional,
Source: k.Source,
Sink: k.Sink,
Env: env,
Path: k.Path,
Field: k.Field,
Decrypt: k.Decrypt,
Optional: k.Optional,
Source: k.Source,
Sink: k.Sink,
Plaintext: k.Plaintext,
}
}
func (k *KeyPath) SwitchPath(path string) KeyPath {
return KeyPath{
Path: path,
Field: k.Field,
Env: k.Env,
Decrypt: k.Decrypt,
Optional: k.Optional,
Source: k.Source,
Sink: k.Sink,
Path: path,
Field: k.Field,
Env: k.Env,
Decrypt: k.Decrypt,
Optional: k.Optional,
Source: k.Source,
Sink: k.Sink,
Plaintext: k.Plaintext,
}
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/providers/aws_secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var defaultDeletionRecoveryWindowInDays int64 = 7

const versionSplit = ","

//nolint
// nolint
func init() {
metaInfo := core.MetaInfo{
Name: "aws_secretsmanager",
Expand Down Expand Up @@ -235,6 +235,11 @@ func (a *AWSSecretsManager) getSecret(kp core.KeyPath) (map[string]string, error
if res == nil || res.SecretString == nil {
return nil, fmt.Errorf("data not found at %q", kp.Path)
}
if kp.Plaintext {
return map[string]string{
core.PlainTextKey: *res.SecretString,
}, nil
}

var secret map[string]interface{}
err = json.Unmarshal([]byte(*res.SecretString), &secret)
Expand Down
19 changes: 19 additions & 0 deletions pkg/providers/aws_secretsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ func TestAWSSecretsManager(t *testing.T) {
AssertProvider(t, &s, true)
}

func TestAWSSecretsManagerPlainText(t *testing.T) {
ctrl := gomock.NewController(t)
// Assert that Bar() is invoked.
defer ctrl.Finish()
client := mock_providers.NewMockAWSSecretsManagerClient(ctrl)
path := "settings/prod/billing-svc"
in := secretsmanager.GetSecretValueInput{SecretId: &path}
data := `hello-world`
out := secretsmanager.GetSecretValueOutput{
SecretString: &data,
}
client.EXPECT().GetSecretValue(gomock.Any(), gomock.Eq(&in)).Return(&out, nil).AnyTimes()
s := AWSSecretsManager{
client: client,
logger: GetTestLogger(),
}
AssertProviderPlainText(t, &s, data)
}

func TestAWSSecretsManagerFailures(t *testing.T) {
ctrl := gomock.NewController(t)
// Assert that Bar() is invoked.
Expand Down
15 changes: 15 additions & 0 deletions pkg/providers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ func AssertProvider(t *testing.T, s core.Provider, sync bool) {
}
}

func AssertProviderPlainText(t *testing.T, s core.Provider, expected string) {
p := core.NewPopulate(map[string]string{"stage": "prod"})

kp := p.KeyPath(core.KeyPath{Field: "MG_KEY", Path: "settings/{{stage}}/billing-svc", Decrypt: true, Plaintext: true})
kpenv := p.KeyPath(core.KeyPath{Env: "MG_KEY", Path: "settings/{{stage}}/billing-svc", Decrypt: true, Plaintext: true})

ent, err := s.Get(kp)
assert.Nil(t, err)
assert.Equal(t, ent.Value, expected)

ent, err = s.Get(kpenv)
assert.Nil(t, err)
assert.Equal(t, ent.Value, expected)
}

func ConfigurableAssertProvider(t *testing.T, s core.Provider, sync bool, setField bool) {
p := core.NewPopulate(map[string]string{"stage": "prod"})

Expand Down
Loading