Skip to content

Commit

Permalink
Merge pull request #9 from 0xsequence/Add-test-for-maps-and-double-po…
Browse files Browse the repository at this point in the history
…inters

Add test for maps and double pointers, support hydrating maps
  • Loading branch information
LukasJenicek authored Oct 10, 2024
2 parents ade4468 + 9830764 commit a2c8fab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
6 changes: 6 additions & 0 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func (g *collector) collectSecretFields(v reflect.Value, path string) {
g.collectSecretFields(item, fmt.Sprintf("%v[%v]", path, i))
}

case reflect.Map:
for _, key := range v.MapKeys() {
item := v.MapIndex(key)
g.collectSecretFields(item, fmt.Sprintf("%v[%v]", path, key))
}

case reflect.String:
secretName, found := strings.CutPrefix(v.String(), "$SECRET:")
if !found {
Expand Down
55 changes: 48 additions & 7 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@ import (
"testing"
)

type config1 struct {
DB dbConfig
JWTSecrets []jwtSecret
Providers map[string]*providerConfig
DoublePtr **providerConfig
unexported dbConfig
}

type dbConfig struct {
User string
Password string
}

type jwtSecret string

type config1 struct {
DB dbConfig
JWTSecrets []jwtSecret
unexported dbConfig
type providerConfig struct {
Name string
Secret string
}

type jwtSecret string

func TestCollectFields(t *testing.T) {
tt := []struct {
Name string
Input any
Out []string // field paths
Error bool
}{
{
Name: "Basic DB config with no creds",
Input: &config1{
DB: dbConfig{
User: "db-user",
Expand All @@ -35,6 +44,17 @@ func TestCollectFields(t *testing.T) {
Out: []string{},
},
{
Name: "Basic DB config with creds",
Input: &config1{
DB: dbConfig{
User: "db-user",
Password: "$SECRET:db-password",
},
},
Out: []string{"db-password"},
},
{
Name: "Slice of secrets",
Input: &config1{
DB: dbConfig{
User: "db-user",
Expand All @@ -45,6 +65,25 @@ func TestCollectFields(t *testing.T) {
Out: []string{"secretName", "jwtSecret1", "jwtSecret2"},
},
{
Name: "Map with secrets",
Input: &config1{
Providers: map[string]*providerConfig{
"provider1": {Name: "provider1", Secret: "$SECRET:secretProvider1"},
"provider2": {Name: "provider2", Secret: "$SECRET:secretProvider2"},
"provider3": {Name: "provider3", Secret: "$SECRET:secretProvider3"},
},
},
Out: []string{"secretProvider1", "secretProvider2", "secretProvider3"},
},
{
Name: "Double pointer",
Input: &config1{
DoublePtr: ptr(&providerConfig{Name: "double-pointer", Secret: "$SECRET:double-pointer-secret"}),
},
Out: []string{"double-pointer-secret"},
},
{
Name: "Unexported field should fail to hydrate",
Input: &config1{
unexported: dbConfig{ // unexported fields can't be updated via reflect pkg
User: "db-user",
Expand All @@ -58,7 +97,7 @@ func TestCollectFields(t *testing.T) {

for i, tc := range tt {
i, tc := i, tc
t.Run(fmt.Sprintf("tt[%v]", i), func(t *testing.T) {
t.Run(fmt.Sprintf("tt[%v]: %v", i, tc.Name), func(t *testing.T) {
v := reflect.ValueOf(tc.Input)

c := &collector{}
Expand All @@ -85,3 +124,5 @@ func TestCollectFields(t *testing.T) {
})
}
}

func ptr[T any](v T) *T { return &v }

0 comments on commit a2c8fab

Please sign in to comment.