-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
164 lines (141 loc) · 4.08 KB
/
options.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
package secretly
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v3"
)
type (
// ProcessOptions are optional modifiers for secret processing.
ProcessOption func(fields) error
unmarshalFunc func([]byte, any) error
secretConfig struct {
Type secretType `json:"type" yaml:"type"`
Name string `json:"name" yaml:"name"`
Key string `json:"key" yaml:"key"`
Version string `json:"version" yaml:"version"`
SplitWords bool `json:"split_words" yaml:"split_words"`
}
)
var ErrInvalidFileType = errors.New("invalid file type")
// WithDefaultVersion overwrites the default version, [secretly.DefaultVersion],
// with the provided version.
// Use this to set the default version to aliases like "latest" or "AWSCURRENT".
func WithDefaultVersion(version string) ProcessOption {
return func(fields fields) error {
for i, f := range fields {
if f.secretVersion == DefaultVersion {
fields[i].secretVersion = version
}
}
return nil
}
}
// WithCache caches secrets in memory
// to avoid unnecessary calls to the secret manager.
// Do not use this option if you want your application
// to handle secrets changes without restarting.
func WithCache() ProcessOption {
return func(fields fields) error {
cache := newCache()
for i := range fields {
fields[i].cache = cache
}
return nil
}
}
// WithPatch returns an ProcessOption which overwrites
// the specified/default field values with the provided patch.
// Can be used to overwrite any of the configurable field values.
//
// Must be written in either YAML or YAML compatible JSON
func WithPatch(patch []byte) ProcessOption {
return func(fields fields) error {
return setFieldsWithPatch(yaml.Unmarshal, patch, fields)
}
}
// WithPatchFile returns an ProcessOption which overwrites
// the specified/default field values with the provided patch.
// Can be used to overwrite any of the configurable field values.
//
// Types of patch files are determined by their extensions.
// Accepted patch file types are:
// 1. JSON (.json)
// 2. YAML (.yaml,.yml)
func WithPatchFile(filePath string) ProcessOption {
return func(fields fields) error {
b, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("reading patch file: %w", err)
}
switch ext := filepath.Ext(filePath); ext {
case ".json":
err = setFieldsWithPatch(json.Unmarshal, b, fields)
case ".yaml", ".yml":
err = setFieldsWithPatch(yaml.Unmarshal, b, fields)
default:
err = fmt.Errorf("%w: %s", ErrInvalidFileType, ext)
}
if err != nil {
return fmt.Errorf("applying patch: %w", err)
}
return nil
}
}
// setFieldsWithPatch overwrites fields applying unmarshal to the bytes, b.
func setFieldsWithPatch(unmarshal unmarshalFunc, b []byte, fields fields) error {
secretConfigMap := make(map[string]secretConfig, len(fields))
err := unmarshal(b, &secretConfigMap)
if err != nil {
return err
}
for i, f := range fields {
fmt.Println(f.Name())
sc, ok := secretConfigMap[f.Name()]
if !ok {
continue
}
if sc.Type != "" {
fields[i].secretType = sc.Type
}
if sc.Name != "" {
fields[i].secretName = sc.Name
}
if sc.Key != "" {
fields[i].mapKeyName = sc.Key
}
if sc.Version != "" {
fields[i].secretVersion = sc.Version
}
if sc.SplitWords {
fields[i].splitWords = sc.SplitWords
}
}
return nil
}
// WithVersionsFromEnv returns an ProcessOption which overwrites
// the specified/default secret versions with versions from the environment.
// Environment variables are to be named with the following logic:
//
// if prefix
// uppercase( prefix + "_" + field.FullName() ) + "_VERSION"
// else
// uppercase( field.FullName() ) + "_VERSION"
func WithVersionsFromEnv(prefix string) ProcessOption {
return func(fields fields) error {
if prefix != "" {
prefix += "_"
}
for i, field := range fields {
name := strings.ReplaceAll(field.Name(), "-", "_")
key := strings.ToUpper(prefix + name + "_VERSION")
if v, ok := os.LookupEnv(key); ok {
fields[i].secretVersion = v // TODO: Support types other than string
}
}
return nil
}
}