-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.go
162 lines (124 loc) · 3.52 KB
/
fetch.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Package gcpsecretfetch is a utility library for getting secrets from GCP Secret Manager.
package gcpsecretfetch
import (
"fmt"
"github.com/panjf2000/ants/v2"
"github.com/pkg/errors"
"github.com/spf13/viper"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
"strings"
"reflect"
"sync"
)
// InitializeConfig initializes a config struct by getting the secrets from GCP Secret Manager
//
// This function works by reflecting on the fields in the passed pointer struct, and
// then dispatching calls to get the secret payloads from GCP for the corresponding secrets.
// The function must be passed a pointer to an arbitrary config struct, and
// the config struct must only have string fields.
func InitializeConfig(cfg interface{}, project string, opts ...ConfigOption) error {
s, err := validateStruct(cfg)
if err != nil {
return err
}
client, err := newClient(project, opts)
if err != nil {
return err
}
defer client.gcpClient.Close()
return client.fetch(s)
}
func (svc *secretClient) accessSecretVersion(name string) ([]byte, error) {
req := &secretmanagerpb.AccessSecretVersionRequest{
Name: fmt.Sprintf("projects/%s/secrets/%s/versions/latest", svc.project, name),
}
secret, err := svc.gcpClient.AccessSecretVersion(svc.ctx, req)
if err != nil {
return nil, err
}
return secret.Payload.Data, nil
}
type params struct {
v *reflect.Value
name string
viper *viper.Viper
}
func validateStruct(cfg interface{}) (reflect.Value, error) {
t := reflect.TypeOf(cfg)
var s reflect.Value
if t.Kind() != reflect.Ptr {
return s, errors.New("cfg argument must be a pointer to a struct, got:" + t.Kind().String())
}
s = reflect.ValueOf(cfg).Elem()
if s.Kind() != reflect.Struct {
return s, errors.New("cfg argument must be a pointer to a struct, got: " + s.Kind().String())
}
for i := 0; i < s.NumField(); i++ {
if err := validateField(s, i); err != nil {
return s, err
}
}
return s, nil
}
func validateField(s reflect.Value, i int) error {
f := s.Field(i)
name := s.Type().Field(i).Name
if !f.IsValid() || !f.CanSet() {
return errors.New(fmt.Sprintf("field %s is not valid - check if field is value and that it is exported from struct", name))
}
if f.Kind() != reflect.String {
return errors.New(fmt.Sprintf("pointer struct can only contain string fields - field '%s' is of type '%s'", name, f.Type().Name()))
}
return nil
}
func (svc *secretClient) fetch(s reflect.Value) error {
var wg sync.WaitGroup
type errorStruct struct {
err error
name string
}
c := make(chan errorStruct, s.NumField())
p, _ := ants.NewPoolWithFunc(svc.concurrency, func(i interface{}) {
defer wg.Done()
p := i.(params)
err := svc.setValueFromGcp(p)
if err == nil {
return
}
c <- errorStruct{err, p.name}
})
defer p.Release()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
name := s.Type().Field(i).Name
wg.Add(1)
err := p.Invoke(params{v: &f, name: name, viper: svc.v})
if err != nil {
return err
}
}
wg.Wait()
close(c)
var errs []string
for e := range c {
errs = append(errs, e.name+": "+e.err.Error())
}
if len(errs) != 0 {
return errors.New("Error when fetching secrets: " + strings.Join(errs, ", "))
}
return nil
}
var mux sync.Mutex
func (svc *secretClient) setValueFromGcp(p params) error {
secretString, err := svc.accessSecretVersion(strings.ToUpper(p.name))
if err != nil {
return err
}
if p.viper != nil {
mux.Lock()
p.viper.Set(p.name, secretString)
mux.Unlock()
}
p.v.SetString(string(secretString))
return nil
}