-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvarstorer.go
294 lines (240 loc) · 6.49 KB
/
varstorer.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"reflect"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
cfgtypes "github.com/cloudfoundry/config-server/types"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
)
type VarsFSStore struct {
FS boshsys.FileSystem
ValueGeneratorFactory cfgtypes.ValueGeneratorFactory
path string
statics boshtpl.StaticVariables
}
var _ boshtpl.Variables = VarsFSStore{}
func NewVarsFSStore(path string) *VarsFSStore {
return &VarsFSStore{
FS: boshsys.NewOsFileSystemWithStrictTempRoot(boshlog.NewLogger(boshlog.LevelNone)),
ValueGeneratorFactory: cfgtypes.NewValueGeneratorConcrete(nil),
path: path,
statics: map[string]interface{}{},
}
}
func (s VarsFSStore) LoadAndStore(varsDefinitions []boshtpl.VariableDefinition) error {
for _, def := range varsDefinitions {
_, _, err := s.Get(def)
if err != nil {
return err
}
}
vars, err := s.load()
if err != nil {
return err
}
return s.save(vars)
}
func (s VarsFSStore) IsSet() bool { return len(s.path) > 0 }
func (s VarsFSStore) Get(varDef boshtpl.VariableDefinition) (interface{}, bool, error) {
vars, err := s.load()
if err != nil {
return nil, false, err
}
val, found := vars[varDef.Name]
if found {
return val, true, nil
}
if len(varDef.Type) == 0 {
return nil, false, nil
}
val, err = s.generateAndSet(varDef)
if err != nil {
return nil, false, bosherr.WrapErrorf(err, "Generating variable '%s'", varDef.Name)
}
return val, true, nil
}
func (s VarsFSStore) List() ([]boshtpl.VariableDefinition, error) {
vars, err := s.load()
if err != nil {
return nil, err
}
return vars.List()
}
func (s VarsFSStore) generateAndSet(varDef boshtpl.VariableDefinition) (interface{}, error) {
optionBase64 := struct {
Base64 bool `mapstructure:"base64"`
}{}
if opts, ok := varDef.Options.(map[interface{}]interface{}); ok {
err := mapstructure.Decode(varDef.Options, &optionBase64)
if err != nil {
return nil, err
}
delete(opts, "base64")
varDef.Options = opts
}
generator, err := s.ValueGeneratorFactory.GetGenerator(varDef.Type)
if err != nil {
return nil, err
}
val, err := generator.Generate(varDef.Options)
if err != nil {
return nil, err
}
if optionBase64.Base64 {
val, err = s.b64Value(val)
if err != nil {
return nil, err
}
}
err = s.set(varDef.Name, val)
if err != nil {
return nil, err
}
return val, nil
}
func (s VarsFSStore) b64Value(val interface{}) (interface{}, error) {
if reflect.TypeOf(val).Kind() == reflect.Struct {
result, err := yaml.Marshal(val)
if err != nil {
return nil, err
}
newVal := map[interface{}]interface{}{}
err = yaml.Unmarshal(result, &newVal)
if err != nil {
return nil, err
}
val = newVal
}
switch v := val.(type) {
case []interface{}:
for i, newV := range v {
v[i], _ = s.b64Value(newV)
}
return v, nil
case map[interface{}]interface{}:
for newK, newV := range v {
v[newK], _ = s.b64Value(newV)
}
return v, nil
case string:
return base64.StdEncoding.EncodeToString([]byte(v)), nil
}
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v", val))), nil
}
func (s VarsFSStore) set(key string, val interface{}) error {
vars, err := s.load()
if err != nil {
return err
}
vars[key] = val
return s.save(vars)
}
func (s VarsFSStore) load() (boshtpl.StaticVariables, error) {
if s.FS == nil {
s.FS = boshsys.NewOsFileSystemWithStrictTempRoot(boshlog.NewLogger(boshlog.LevelNone))
}
vars := s.statics
if s.FS.FileExists(s.path) {
bytes, err := s.FS.ReadFile(s.path)
if err != nil {
return vars, err
}
err = yaml.Unmarshal(bytes, &vars)
if err != nil {
return vars, bosherr.WrapErrorf(err, "Deserializing variables file store '%s'", s.path)
}
}
if vars == nil {
return boshtpl.StaticVariables{}, nil
}
return vars, nil
}
func (s VarsFSStore) save(vars boshtpl.StaticVariables) error {
if s.FS == nil {
s.FS = boshsys.NewOsFileSystemWithStrictTempRoot(boshlog.NewLogger(boshlog.LevelNone))
}
bytes, err := yaml.Marshal(vars)
if err != nil {
return bosherr.WrapErrorf(err, "Serializing variables")
}
err = s.FS.WriteFile(s.path, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing variables to file store '%s'", s.path)
}
return nil
}
type VarsCertLoader struct {
vars boshtpl.Variables
}
func NewVarsCertLoader(vars boshtpl.Variables) VarsCertLoader {
return VarsCertLoader{vars}
}
func (l VarsCertLoader) LoadCerts(name string) (*x509.Certificate, *rsa.PrivateKey, error) {
val, found, err := l.vars.Get(boshtpl.VariableDefinition{Name: name})
if err != nil {
return nil, nil, err
} else if !found {
return nil, nil, fmt.Errorf("Expected to find variable '%s' with a certificate", name)
}
// Convert to YAML for easier struct parsing
valBytes, err := yaml.Marshal(val)
if err != nil {
return nil, nil, bosherr.WrapErrorf(err, "Expected variable '%s' to be serializable", name)
}
type CertVal struct {
Certificate string
PrivateKey string `yaml:"private_key"`
}
var certVal CertVal
err = yaml.Unmarshal(valBytes, &certVal)
if err != nil {
return nil, nil, bosherr.WrapErrorf(err, "Expected variable '%s' to be deserializable", name)
}
crt, err := l.parseCertificate(certVal.Certificate)
if err != nil {
return nil, nil, err
}
key, err := l.parsePrivateKey(certVal.PrivateKey)
if err != nil {
return nil, nil, err
}
return crt, key, nil
}
func (VarsCertLoader) parseCertificate(data string) (*x509.Certificate, error) {
fromB64, err := base64.StdEncoding.DecodeString(data)
if err == nil {
data = string(fromB64)
}
cpb, _ := pem.Decode([]byte(data))
if cpb == nil {
return nil, bosherr.Error("Certificate did not contain PEM formatted block")
}
crt, err := x509.ParseCertificate(cpb.Bytes)
if err != nil {
return nil, bosherr.WrapError(err, "Parsing certificate")
}
return crt, nil
}
func (VarsCertLoader) parsePrivateKey(data string) (*rsa.PrivateKey, error) {
fromB64, err := base64.StdEncoding.DecodeString(data)
if err == nil {
data = string(fromB64)
}
kpb, _ := pem.Decode([]byte(data))
if kpb == nil {
return nil, bosherr.Error("Private key did not contain PEM formatted block")
}
key, err := x509.ParsePKCS1PrivateKey(kpb.Bytes)
if err != nil {
return nil, bosherr.WrapError(err, "Parsing private key")
}
return key, nil
}