-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhydrate.go
More file actions
55 lines (45 loc) · 1.17 KB
/
Copy pathhydrate.go
File metadata and controls
55 lines (45 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cloudsecrets
import (
"context"
"fmt"
"reflect"
"golang.org/x/sync/errgroup"
)
// Hydrate recursively walks a given config (struct pointer) and hydrates all
// string values matching "$SECRET:" prefix using the given secrets provider.
//
// The secret values to be replaced must have a format of "$SECRET:{name|path}".
func Hydrate(ctx context.Context, provider SecretsProvider, config any) error {
v := reflect.ValueOf(config)
if v.Kind() != reflect.Pointer {
return fmt.Errorf("passed config must be a pointer")
}
if v.IsNil() {
return fmt.Errorf("passed config is nil")
}
for v.Kind() == reflect.Pointer {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return fmt.Errorf("passed config must be pointer to a struct, got pointer to %s", v.Kind())
}
keys := collectSecretKeys(v)
secrets := make([]secret, len(keys))
g := &errgroup.Group{}
for i, key := range keys {
i, key := i, key
g.Go(func() error {
value, err := provider.FetchSecret(ctx, key)
secrets[i] = secret{
key: key,
value: value,
fetchErr: err,
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
return replaceSecrets(v, secrets)
}