Skip to content

Commit

Permalink
🐛 Support protobuf string values for VaultType. (#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
preslavgerchev authored Oct 23, 2023
1 parent 184d064 commit 1702932
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
40 changes: 23 additions & 17 deletions providers-sdk/v1/vault/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,32 @@ import (
"strings"
)

// supports both the protobuf string and the custom defined string
// representations of a vault type
func NewVaultType(name string) (VaultType, error) {
entry := strings.TrimSpace(strings.ToLower(name))
var code VaultType
ok := false
marshalMapOk := false
for k := range vaultMarshalNameMap {
if vaultMarshalNameMap[k] == entry {
ok = true
marshalMapOk = true
code = k
break
}
}
if !ok {
return VaultType_None, errors.New("unknown type value: " + string(name))
if !marshalMapOk {
// also support the auto-generated protobuf string values for all the enum values
protoMapOk := false
for k, v := range VaultType_value {
if k == name {
protoMapOk = true
code = VaultType(v)
break
}
}
if !protoMapOk {
return VaultType_None, errors.New("unknown type value: " + string(name))
}
}

return code, nil
Expand Down Expand Up @@ -67,21 +80,14 @@ func (t *VaultType) UnmarshalJSON(data []byte) error {
if err != nil {
var name string
err = json.Unmarshal(data, &name)
entry := strings.TrimSpace(strings.ToLower(name))

var code VaultType
ok := false
for k := range vaultMarshalNameMap {
if vaultMarshalNameMap[k] == entry {
ok = true
code = k
break
}
if err != nil {
return err
}
if !ok {
return errors.New("unknown type value: " + string(data))
c, err := NewVaultType(name)
if err != nil {
return err
}
*t = code
*t = c
}
return nil
}
Expand Down
8 changes: 7 additions & 1 deletion providers-sdk/v1/vault/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ func TestVaultTypeParser(t *testing.T) {
- gcp-secret-manager
- aws-secrets-manager
- aws-parameter-store
- GCPBerglas
- AWSParameterStore
- Memory
`

v := []VaultType{}
yaml.Unmarshal([]byte(content), &v)

assert.Equal(t, 8, len(v))
assert.Equal(t, 11, len(v))
assert.Equal(t, VaultType_None, v[0])
assert.Equal(t, VaultType_KeyRing, v[1])
assert.Equal(t, VaultType_LinuxKernelKeyring, v[2])
Expand All @@ -36,6 +39,9 @@ func TestVaultTypeParser(t *testing.T) {
assert.Equal(t, VaultType_GCPSecretsManager, v[5])
assert.Equal(t, VaultType_AWSSecretsManager, v[6])
assert.Equal(t, VaultType_AWSParameterStore, v[7])
assert.Equal(t, VaultType_GCPBerglas, v[8])
assert.Equal(t, VaultType_AWSParameterStore, v[9])
assert.Equal(t, VaultType_Memory, v[10])
}

func TestVaultTypeMarshal(t *testing.T) {
Expand Down

0 comments on commit 1702932

Please sign in to comment.