Skip to content

Commit

Permalink
Merge pull request #11 from 0xsequence/setMapValue
Browse files Browse the repository at this point in the history
Use hook to set map key after setter
  • Loading branch information
VojtechVitek authored Oct 11, 2024
2 parents ebefdc2 + 223d048 commit 4379e2b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type secretField struct {

type collector struct {
fields []*secretField
hooks []func()
err error
}

Expand Down Expand Up @@ -50,10 +51,14 @@ func (g *collector) collectSecretFields(v reflect.Value, path string) {
ptr := reflect.New(item.Type())
ptr.Elem().Set(item)

g.hooks = append(g.hooks, func() {
v.SetMapIndex(key, ptr.Elem())
})

g.collectSecretFields(ptr, fmt.Sprintf("%v[%v]", path, key))

// Set the modified struct back into the map
v.SetMapIndex(key, ptr.Elem())

} else {
g.collectSecretFields(item, fmt.Sprintf("%v[%v]", path, key))
}
Expand Down
10 changes: 9 additions & 1 deletion hydrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,13 @@ func hydrateConfig(ctx context.Context, provider secretsProvider, v reflect.Valu
})
}

return g.Wait()
if err := g.Wait(); err != nil {
return fmt.Errorf("failed to hydrate config: %w", err)
}

for _, hook := range c.hooks {
hook()
}

return nil
}
22 changes: 22 additions & 0 deletions hydrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type config struct {
Analytics analytics
Pass string
JWTSecrets []string
Services map[string]service
}

type service struct {
URL string
Auth string
Pass string
}

type db struct {
Expand Down Expand Up @@ -52,6 +59,7 @@ func TestReplacePlaceholdersWithSecrets(t *testing.T) {
"pass": "secret",
"jwtSecretV1": "some-old-secret",
"jwtSecretV2": "changeme-now",
"auth": "auth-secret",
},
conf: &config{
Pass: "$SECRET:pass",
Expand All @@ -66,6 +74,13 @@ func TestReplacePlaceholdersWithSecrets(t *testing.T) {
AuthToken: "$SECRET:analyticsPassword",
},
JWTSecrets: []string{"$SECRET:jwtSecretV2", "$SECRET:jwtSecretV1"},
Services: map[string]service{
"a": {
URL: "http://localhost:8000",
Auth: "$SECRET:auth",
Pass: "$SECRET:jwtSecretV2",
},
},
},
wantErr: false,
wantConf: &config{
Expand All @@ -84,6 +99,13 @@ func TestReplacePlaceholdersWithSecrets(t *testing.T) {
"changeme-now",
"some-old-secret",
},
Services: map[string]service{
"a": {
URL: "http://localhost:8000",
Auth: "auth-secret",
Pass: "changeme-now",
},
},
},
},
{
Expand Down

0 comments on commit 4379e2b

Please sign in to comment.