-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
296 lines (237 loc) · 6.77 KB
/
storage.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
package storagenomad
import (
"context"
"encoding/base64"
"fmt"
"io/fs"
"os"
"path"
"strings"
"time"
"github.com/caddyserver/certmagic"
nomad "github.com/hashicorp/nomad/api"
"go.uber.org/zap"
)
// NomadStorage allows to store certificates and other TLS resources
// in a shared cluster environment using Nomad Variables.
type NomadStorage struct {
certmagic.Storage
NomadClient *nomad.Client
logger *zap.SugaredLogger
Address string `json:"address"`
Token string `json:"token"`
Timeout int `json:"timeout"`
Prefix string `json:"prefix"`
ValuePrefix string `json:"value_prefix"`
TlsEnabled bool `json:"tls_enabled"`
TlsInsecure bool `json:"tls_insecure"`
}
// New connects to Nomad and returns a NomadStorage
func New() *NomadStorage {
// create NomadStorage and pre-set values
s := NomadStorage{
ValuePrefix: DefaultValuePrefix,
Prefix: DefaultPrefix,
Timeout: DefaultTimeout,
}
return &s
}
func (ns *NomadStorage) readyKey(key string) string {
return ns.escapeKey(ns.prefixKey(key))
}
func (ns *NomadStorage) prefixKey(key string) string {
return path.Join(ns.Prefix, key)
}
func (ns *NomadStorage) escapeKey(key string) string {
return strings.ReplaceAll(key, ".", "_")
}
// Store saves data value as a variable in Nomad
func (ns NomadStorage) Store(ctx context.Context, key string, value []byte) error {
escapedAndPrefixedKey := ns.readyKey(key)
loggy("VALUE TO STORE: %s", string(value))
items := &nomad.VariableItems{
"Value": base64.StdEncoding.EncodeToString(value),
"Modified": time.Now().Format(time.RFC3339),
}
sv := &nomad.Variable{
Path: escapedAndPrefixedKey,
Items: *items,
}
opts := NomaWriteDefaults(ctx)
if _, _, err := ns.NomadClient.Variables().Create(sv, opts); err != nil {
msg := fmt.Sprintf("unable to store data for %s", escapedAndPrefixedKey)
return wrapError(err, msg)
}
return nil
}
// Load retrieves the value for a key from Nomad KV
func (ns NomadStorage) Load(ctx context.Context, key string) ([]byte, error) {
path := ns.readyKey(key)
opts := NomadQueryDefaults(ctx)
loggy("loading key: %s", path)
v, _, err := ns.NomadClient.Variables().Peek(path, opts)
loggy("peeked key: %s", path)
if err != nil {
loggy("error not nil")
msg := fmt.Sprintf("unable to read data for %s", ns.readyKey(key))
return nil, wrapError(err, msg)
}
loggy("checking v nil")
if v == nil {
loggy("v is nil")
return nil, fs.ErrNotExist
}
loggy("v is not nil")
items := v.Items
loggy("got items")
if val, ok := items["Value"]; ok {
return base64.StdEncoding.DecodeString(val)
}
loggy("wat")
return nil, fs.ErrNotExist
}
// Delete a key from Nomad KV
func (ns NomadStorage) Delete(ctx context.Context, key string) error {
path := ns.readyKey(key)
loggy("deleting key: %s", path)
opts := NomaWriteDefaults(ctx)
if _, err := ns.NomadClient.Variables().Delete(path, opts); err != nil {
msg := fmt.Sprintf("unable to delete data for %s", ns.readyKey(key))
return wrapError(err, msg)
}
return nil
}
// Exists checks if a key exists
func (ns NomadStorage) Exists(ctx context.Context, key string) bool {
path := ns.readyKey(key)
loggy("checking existence: %s", path)
opts := NomadQueryDefaults(ctx)
v, _, err := ns.NomadClient.Variables().Peek(path, opts)
if err != nil {
return false
}
if v == nil {
return false
}
i := v.Items
if _, ok := i["Value"]; ok {
return true
}
return false
}
// List returns a list with all keys under a given prefix
func (ns NomadStorage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var keysFound []string
path := ns.prefixKey(prefix)
loggy("listing: %s", path)
loggy("1")
opts := NomadQueryDefaults(ctx)
loggy("2")
keys, _, err := ns.NomadClient.Variables().PrefixList(path, opts)
loggy("3")
if err != nil {
loggy("oh no 1")
msg := fmt.Sprintf("unable to list data for %s", path)
return nil, wrapError(err, msg)
}
loggy("4")
for _, k := range keys {
key := k.Path
if strings.HasPrefix(key, path) {
pf := path + "/"
trimmedKey := strings.TrimPrefix(key, pf)
isNested := strings.Contains(trimmedKey, "/")
if recursive || !isNested {
matchingPath := strings.TrimPrefix(key, ns.Prefix+"/")
keysFound = append(keysFound, matchingPath)
}
}
}
loggy("5")
if len(keys) == 0 {
loggy("6")
return keysFound, fs.ErrNotExist
}
return keysFound, nil
}
// Stat returns statistic data of a key
func (ns NomadStorage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
path := ns.readyKey(key)
loggy("stat: %s", path)
opts := NomadQueryDefaults(ctx)
v, _, err := ns.NomadClient.Variables().Peek(path, opts)
if err != nil {
msg := fmt.Sprintf("unable to read stats for %s", path)
return certmagic.KeyInfo{}, wrapError(err, msg)
}
if v == nil {
return certmagic.KeyInfo{}, fs.ErrNotExist
}
items := v.Items
modified, mok := items["Modified"]
val, vok := items["Value"]
t, err := time.Parse(time.RFC3339, modified)
if err != nil {
msg := fmt.Sprintf("error parsing time when getting stats on %s", path)
return certmagic.KeyInfo{}, wrapError(err, msg)
}
if mok && vok {
return certmagic.KeyInfo{
Key: key,
Modified: t,
Size: int64(len(val)),
IsTerminal: false,
}, nil
}
msg := fmt.Sprintf("error reading value for stats %s", path)
return certmagic.KeyInfo{}, fmt.Errorf(msg)
}
func (ns NomadStorage) Lock(ctx context.Context, key string) error {
loggy("Locking")
return nil
}
func (ns NomadStorage) Unlock(ctx context.Context, key string) error {
loggy("Unlocking")
return nil
}
func (ns *NomadStorage) createNomadClient() error {
// get the default config
nomadCfg := nomad.DefaultConfig()
if ns.Address != "" {
nomadCfg.Address = ns.Address
}
if ns.Token != "" {
nomadCfg.SecretID = ns.Token
}
// if ns.TlsEnabled {
// nomadCfg.Scheme = "https"
// }
nomadCfg.TLSConfig.Insecure = ns.TlsInsecure
// set a dial context to prevent default keepalive
// nomadCfg.Transport.DialContext = (&net.Dialer{
// Timeout: time.Duration(ns.Timeout) * time.Second,
// KeepAlive: time.Duration(ns.Timeout) * time.Second,
// }).DialContext
// create the Nomad API client
nomadClient, err := nomad.NewClient(nomadCfg)
if err != nil {
return wrapError(err, "unable to create Nomad client")
}
if _, err := nomadClient.Agent().NodeName(); err != nil {
return wrapError(err, "unable to ping Nomad")
}
ns.NomadClient = nomadClient
return nil
}
func NomadQueryDefaults(ctx context.Context) *nomad.QueryOptions {
opts := &nomad.QueryOptions{}
return opts.WithContext(ctx)
}
func NomaWriteDefaults(ctx context.Context) *nomad.WriteOptions {
opts := &nomad.WriteOptions{}
return opts.WithContext(ctx)
}
func loggy(format string, a ...any) (int, error) {
msg := fmt.Sprintf(format, a...)
return fmt.Fprintln(os.Stderr, msg)
}