-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
353 lines (294 loc) · 8.65 KB
/
field.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package secretly
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
yaml "gopkg.in/yaml.v3"
)
// Default values for optional field tags.
const (
// Defaults.
DefaultType = "text"
DefaultVersion = "0"
// Tags.
tagIgnored = "ignored"
tagKey = "key"
tagName = "name"
tagSplitWords = "split_words"
tagType = "type"
tagVersion = "version"
)
var (
regexGatherWords = regexp.MustCompile("([^A-Z]+|[A-Z]+[^A-Z]+|[A-Z]+)")
regexAcronym = regexp.MustCompile("([A-Z]+)([A-Z][^A-Z]+)")
ErrInvalidJSONSecret = errors.New("secret is not valid json")
ErrInvalidYAMLSecret = errors.New("secret is not valid yaml")
ErrSecretMissingKey = errors.New("secret is missing provided key")
)
// Field represents a field in a struct,
// exposing its secretly tag values
// and reference to the underlying value.
type Field struct {
SecretType string
SecretName string
SecretVersion string
MapKeyName string // NOTE: Only used when secretType is "json" or "yaml"
SplitWords bool
Value reflect.Value
}
// NewField constructs a field referencing the provided reflect.Value with the tags from
// the reflect.StructField applied
func NewField(fValue reflect.Value, fStructField reflect.StructField) (Field, error) {
var (
newField Field
ok bool
err error
)
// Set the reference to the field's reflection
newField.Value = fValue
// Get the split_words value, setting it to false if not explicitly set
newField.SplitWords, ok, err = parseOptionalStructTagKey[bool](fStructField, tagSplitWords)
if err != nil {
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagSplitWords,
Err: err,
}
}
if !ok {
newField.SplitWords = false
}
// Get the type value, setting it to the default, "text", if not explicitly set.
// Also perform validation to ensure only valid types are provided
newField.SecretType, ok, err = parseOptionalStructTagKey[string](fStructField, tagType)
if err != nil {
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagType,
Err: err,
}
}
if !ok {
newField.SecretType = DefaultType
}
switch newField.SecretType {
case "text", "json", "yaml":
default:
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagType,
Err: ErrInvalidSecretType,
}
}
// Get the name value, setting it to the field's name if not explicitly set.
// Split the words if the default value was used and split_words was set to true
newField.SecretName, ok, err = parseOptionalStructTagKey[string](fStructField, tagName)
if err != nil {
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagName,
Err: err,
}
}
if !ok {
newField.SecretName = fStructField.Name
if newField.SplitWords {
newField.SecretName = splitWords(newField.SecretName)
}
}
// Get the key value, if the type is "json" or "yaml", and setting it to the field's name
// if not explicitly set. Split the words if the default value was used and
// split_words was set to true
switch newField.SecretType {
case "json", "yaml":
newField.MapKeyName, ok, err = parseOptionalStructTagKey[string](fStructField, tagKey)
if err != nil {
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagKey,
Err: err,
}
}
if !ok {
newField.MapKeyName = fStructField.Name
if newField.SplitWords {
newField.MapKeyName = splitWords(newField.MapKeyName)
}
}
default:
if _, ok = fStructField.Tag.Lookup(tagKey); ok {
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagKey,
Err: ErrSecretTypeDoesNotSupportKey,
}
}
}
// Get the version value, setting it to the default, "default", if not explicitly
// set. Split the words if the default value was used and split_words was set to true
newField.SecretVersion, ok, err = parseOptionalStructTagKey[string](fStructField, tagVersion)
if err != nil {
return Field{}, StructTagError{
Name: fStructField.Name,
Key: tagVersion,
Err: err,
}
}
if !ok {
newField.SecretVersion = DefaultVersion
}
return newField, nil
}
// Name returns the resolved name of the field. If the secret type is "json" or "yaml",
// the secret name and key name are combined. If "split_words" is true, the combination
// of secret name and key name are transformed into uppercase, snake case.
func (f *Field) Name() string {
name := f.SecretName
switch f.SecretType {
case "json", "yaml":
var delimiter string
if f.SplitWords {
delimiter = "_"
}
name += delimiter + f.MapKeyName
}
return name
}
// Set sets the field's reflect.Value with b.
func (f *Field) Set(b []byte) error {
switch f.SecretType {
case "text":
return f.setText(b)
case "json":
return f.setJSON(b)
case "yaml":
return f.setYAML(b)
default:
return ErrInvalidSecretType
}
}
// setText sets the field's underlying value,
// handling the input as a "text" secret.
func (f *Field) setText(b []byte) error {
const ErrFailedConvertFormat = "failed to convert secret \"%s's\" key, \"%s\" to %s: %w"
byteString := string(b)
valueType := f.Value.Type()
switch f.Value.Kind() {
case reflect.String:
f.Value.SetString(byteString)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var (
value int64
err error
)
if f.Value.Kind() == reflect.Int64 && valueType.PkgPath() == "time" && valueType.Name() == "Duration" {
var d time.Duration
d, err = time.ParseDuration(byteString)
value = int64(d)
} else {
value, err = strconv.ParseInt(byteString, 0, valueType.Bits())
}
if err != nil {
t := fmt.Sprintf("int%d", valueType.Bits())
return fmt.Errorf(ErrFailedConvertFormat, f.SecretName, f.MapKeyName, t, err)
}
f.Value.SetInt(value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value, err := strconv.ParseUint(byteString, 0, valueType.Bits())
if err != nil {
t := fmt.Sprintf("uint%d", valueType.Bits())
return fmt.Errorf(ErrFailedConvertFormat, f.SecretName, f.MapKeyName, t, err)
}
f.Value.SetUint(value)
case reflect.Bool:
value, err := strconv.ParseBool(byteString)
if err != nil {
return fmt.Errorf(ErrFailedConvertFormat, f.SecretName, f.MapKeyName, "bool", err)
}
f.Value.SetBool(value)
case reflect.Float32, reflect.Float64:
value, err := strconv.ParseFloat(byteString, valueType.Bits())
if err != nil {
t := fmt.Sprintf("float%d", valueType.Bits())
return fmt.Errorf(ErrFailedConvertFormat, f.SecretName, f.MapKeyName, t, err)
}
f.Value.SetFloat(value)
}
return nil
}
// setJSON sets the field's underlying value,
// handling the input as a "json" secret.
func (f *Field) setJSON(b []byte) error {
var secretMap map[string]string
err := json.Unmarshal(b, &secretMap)
if err != nil {
return ErrInvalidJSONSecret
}
if value, ok := secretMap[f.MapKeyName]; ok {
return f.setText([]byte(value))
}
return fmt.Errorf("%w: secret \"%s\" missing \"%s\"", ErrSecretMissingKey, f.SecretName, f.MapKeyName)
}
// setYAML sets the field's underlying value,
// handling the input as a "yaml" secret
func (f *Field) setYAML(b []byte) error {
var secretMap map[string]string
err := yaml.Unmarshal(b, &secretMap)
if err != nil {
return ErrInvalidYAMLSecret
}
if value, ok := secretMap[f.MapKeyName]; ok {
return f.setText([]byte(value))
}
return fmt.Errorf("%w: secret \"%s\" missing \"%s\"", ErrSecretMissingKey, f.SecretName, f.MapKeyName)
}
// parseOptionalStructTagKey parses the provided key's value from the struct field,
// returning the value as the type T, a bool indicating if the key was present, and an
// error if the key's value was not a valid T
func parseOptionalStructTagKey[T any](structField reflect.StructField, key string) (value T, ok bool, err error) {
var raw string
if raw, ok = structField.Tag.Lookup(key); ok { // If key present
switch any(value).(type) {
case string:
value = any(raw).(T)
case int:
i, err := strconv.Atoi(raw)
if err != nil {
break
}
value = any(i).(T)
case bool:
b, err := strconv.ParseBool(raw)
if err != nil {
break
}
value = any(b).(T)
}
if err != nil {
return value, false, fmt.Errorf("invalid struct tag key value: %w", err)
}
}
return value, ok, nil
}
// splitWords converts the camelCase/PascalCase string, s, to snake_case
func splitWords(s string) string {
const minAcronymLength = 3
words := regexGatherWords.FindAllStringSubmatch(s, -1)
if len(words) == 0 {
return s
}
var name []string
for _, words := range words {
if m := regexAcronym.FindStringSubmatch(words[0]); len(m) == minAcronymLength {
name = append(name, m[1], m[2])
} else {
name = append(name, words[0])
}
}
return strings.Join(name, "_")
}