Skip to content

Commit

Permalink
fix(ibmsm): Copy keys from KV types (#535)
Browse files Browse the repository at this point in the history
* fix(ibmsm): Copy keys from KV types

* chore(ibmsm): fmt
  • Loading branch information
jkayani authored Aug 17, 2023
1 parent 4b074c7 commit f261e31
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
8 changes: 6 additions & 2 deletions pkg/backends/ibmsecretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ func (d IBMSecretData) GetSecret() (map[string]interface{}, error) {
}
case *ibmsm.KVSecret:
{
result["data"] = v.Data
for k, v := range v.Data {
result[k] = v
}
}
default:
{
Expand Down Expand Up @@ -254,7 +256,9 @@ func (d IBMVersionedSecretData) GetSecret() (map[string]interface{}, error) {
case *ibmsm.KVSecretVersion:
{
if *v.PayloadAvailable {
result["data"] = v.Data
for k, v := range v.Data {
result[k] = v
}
}
}
default:
Expand Down
70 changes: 65 additions & 5 deletions pkg/backends/ibmsecretsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"reflect"
"strings"
"testing"
"sync"
"testing"

"github.com/IBM/go-sdk-core/v5/core"
ibmsm "github.com/IBM/secrets-manager-go-sdk/secretsmanagerv2"
Expand All @@ -20,10 +20,10 @@ type MockIBMSMClient struct {
// It is shared b/w both GetSecret and GetSecretVersion for simplicity, even though each writes to a different field
GetSecretLock sync.RWMutex

GetSecretCalledWith *ibmsm.GetSecretOptions
GetSecretCallCount int
GetSecretVersionCalledWith *ibmsm.GetSecretVersionOptions
GetSecretVersionCallCount int
GetSecretCalledWith *ibmsm.GetSecretOptions
GetSecretCallCount int
GetSecretVersionCalledWith *ibmsm.GetSecretVersionOptions
GetSecretVersionCallCount int
}

var BIG_GROUP_LEN int = types.IBMMaxPerPage + 1
Expand Down Expand Up @@ -61,6 +61,7 @@ func (m *MockIBMSMClient) ListSecrets(listAllSecretsOptions *ibmsm.ListSecretsOp
otype := "username_password"
ctype := "public_cert"
itype := "iam_credentials"
ktype := "kv"
smallGroupSecrets := []ibmsm.SecretMetadataIntf{
&ibmsm.ArbitrarySecretMetadata{
Name: &name,
Expand All @@ -86,6 +87,12 @@ func (m *MockIBMSMClient) ListSecrets(listAllSecretsOptions *ibmsm.ListSecretsOp
SecretGroupID: &smallGroup,
ID: &itype,
},
&ibmsm.KVSecretMetadata{
Name: &name,
SecretType: &ktype,
SecretGroupID: &smallGroup,
ID: &ktype,
},
}

// Empty secret group
Expand Down Expand Up @@ -141,6 +148,17 @@ func (m *MockIBMSMClient) GetSecret(getSecretOptions *ibmsm.GetSecretOptions) (r
ID: &id,
ApiKey: &payload,
}, nil, nil
} else if *getSecretOptions.ID == "kv" {
name := "my-secret"
id := "kv"
payload := map[string]interface{}{
"hello": "there",
}
return &ibmsm.KVSecret{
Name: &name,
ID: &id,
Data: payload,
}, nil, nil
} else {
name := "my-secret"
id := "username_password"
Expand Down Expand Up @@ -366,6 +384,48 @@ func TestIBMSecretsManagerGetSecrets(t *testing.T) {
}
})

t.Run("Retrieves payload of KV secrets", func(t *testing.T) {
mock := MockIBMSMClient{}
sm := backends.NewIBMSecretsManagerBackend(&mock)

res, err := sm.GetSecrets("ibmcloud/kv/secrets/groups/small-group", "", nil)
if err != nil {
t.Fatalf("%s", err)
}

// Properly calls ListSecrets
var offset int64 = 0
expectedListArgs := &ibmsm.ListSecretsOptions{
Groups: []string{"small-group"},
Offset: &offset,
}
if !reflect.DeepEqual(mock.ListSecretsOptionCalledWith[0], expectedListArgs) {
t.Errorf("expectedListArgs: %s, got: %s.", expectedListArgs.Groups, mock.ListSecretsOptionCalledWith[0].Groups)
}
if len(mock.ListSecretsOptionCalledWith) > 1 {
t.Errorf("ListSecrets should be called %d times got %d", 1, len(mock.ListSecretsOptionCalledWith))
}

// Properly calls GetSecret
id := "kv"
expectedGetArgs := &ibmsm.GetSecretOptions{
ID: &id,
}
if !reflect.DeepEqual(mock.GetSecretCalledWith, expectedGetArgs) {
t.Errorf("Retrieved ID and SecretType do not match expected")
}

// Correct data
expected := map[string]interface{}{
"my-secret": map[string]interface{}{
"hello": "there",
},
}
if !reflect.DeepEqual(res, expected) {
t.Errorf("expected: %s, got: %s.", expected, res)
}
})

t.Run("Properly retrieves versioned secrets", func(t *testing.T) {
mock := MockIBMSMClient{}
sm := backends.NewIBMSecretsManagerBackend(&mock)
Expand Down

0 comments on commit f261e31

Please sign in to comment.