-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.go
404 lines (344 loc) · 10.2 KB
/
backend.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package streamnative
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect"
"strconv"
"strings"
"time"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/logical"
)
// backend wraps the backend framework and adds a map for storing key value pairs
type backend struct {
*framework.Backend
}
var _ logical.Factory = Factory
func GetSnctl() string {
snctl, snctlSet := os.LookupEnv("SNCTL_PATH")
if snctlSet {
return snctl
}
return "snctl"
}
// Factory configures and returns Mock backends
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b, err := newBackend()
if err != nil {
return nil, err
}
if conf == nil {
return nil, fmt.Errorf("configuration passed into backend is nil")
}
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func newBackend() (*backend, error) {
b := &backend{}
b.Backend = &framework.Backend{
Help: strings.TrimSpace(helpText),
BackendType: logical.TypeLogical,
Paths: framework.PathAppend(
b.paths(),
),
}
return b, nil
}
func (b *backend) paths() []*framework.Path {
return []*framework.Path{
{
Pattern: framework.MatchAllRegex("path"),
Fields: map[string]*framework.FieldSchema{
"path": {
Type: framework.TypeString,
Description: "Specifies the path of the secret.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handleRead,
Summary: "Retrieve the secret from the map.",
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleWrite,
Summary: "Store a secret at the specified location.",
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.handleWrite,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.handleDelete,
Summary: "Deletes the secret at the specified location.",
},
},
ExistenceCheck: b.handleExistenceCheck,
},
}
}
func (b *backend) handleExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
out, err := req.Storage.Get(ctx, req.Path)
if err != nil {
return false, errwrap.Wrapf("existence check failed: {{err}}", err)
}
return out != nil, nil
}
func (b *backend) readCachedToken(data map[string]interface{}, path string) *string {
// TTL in whole seconds
ttl, hasTtl := data["ttl"]
if hasTtl {
cachedAt, hasCachedAt := data["cachedAt"]
if hasCachedAt {
token, hasToken := data["cachedToken"]
if hasToken {
ttl64, err := ttl.(json.Number).Int64()
if err != nil {
panic("ttl is not integer")
}
cachedAt64, err := cachedAt.(json.Number).Int64()
if err != nil {
panic("cachedAt is not integer")
}
now := time.Now().UnixMilli()
expiresAt := cachedAt64 + (ttl64 * 1000)
if now < expiresAt {
tokenStr := token.(string)
return &tokenStr
}
}
}
}
return nil
}
func (b *backend) saveCachedToken(ctx context.Context, req *logical.Request, path string,
data map[string]interface{}, token string) error {
_, hasTtl := data["ttl"]
// If no ttl, do not cache tokens.
if !hasTtl {
return nil
}
data["cachedAt"] = time.Now().UnixMilli()
data["cachedToken"] = token
buf, err := json.Marshal(data)
if err == nil {
// Store kv pairs in map at specified path
ent := &logical.StorageEntry{
Key: path,
Value: buf,
}
err = req.Storage.Put(ctx, ent)
if err == nil {
b.Logger().Debug("Token cache saved", "path", path)
} else {
b.Logger().Error("Saving to storage failed", "error", err)
}
} else {
b.Logger().Error("JSON encoding failed", "error", err)
}
return err
}
func validateKeyData(data map[string]interface{}) *logical.Response {
keyFileBytes := data["key-file"]
org := data["organization"]
cluster := data["cluster"]
if keyFileBytes == nil {
resp := logical.ErrorResponse("No 'key-file' set")
return resp
}
if org == nil {
resp := logical.ErrorResponse("No 'organization' set")
return resp
}
if cluster == nil {
resp := logical.ErrorResponse("No 'cluster' set")
return resp
}
return nil
}
func (b *backend) readNewToken(ctx context.Context, req *logical.Request, path string, data map[string]interface{}) (*string, error) {
b.Logger().Debug("Reading new token")
if err := b.requireSnctlConfig(); err != nil {
b.Logger().Error("Initializing snctl config failed", "error", err)
return nil, err
}
keyFileBytes := data["key-file"]
org := data["organization"]
cluster := data["cluster"]
// TempFile is always created with 0600 permissions
tmpKeyFile, err := ioutil.TempFile(os.TempDir(), "snio-key-*.json")
if err != nil {
b.Logger().Error("Failed to open temp file", "error", err)
return nil, err
}
defer os.Remove(tmpKeyFile.Name())
ioutil.WriteFile(tmpKeyFile.Name(), []byte(keyFileBytes.(string)), 0600)
if err := b.activateServiceAccount(tmpKeyFile.Name()); err != nil {
b.Logger().Error("Activating service account failed", "error", err)
return nil, err
}
cmd := exec.Command(GetSnctl(), "-n", org.(string), "auth", "get-token", cluster.(string), "-f", tmpKeyFile.Name())
out, err := cmd.CombinedOutput()
if err != nil {
b.Logger().Error("Failed to run `snctl auth get-token`", "error", err, "out", out)
return nil, err
}
token := string(out)
err = b.saveCachedToken(ctx, req, path, data, token)
if err != nil {
return nil, err
}
return &token, nil
}
func (b *backend) handleRead(ctx context.Context, req *logical.Request, fieldData *framework.FieldData) (*logical.Response, error) {
path := fieldData.Get("path").(string)
// Decode the data
var data map[string]interface{}
ent, err := req.Storage.Get(ctx, path)
if err != nil {
b.Logger().Error("Reading from storage failed", "error", err)
return nil, errwrap.Wrapf("Reading from storage failed: {{err}}", err)
}
secretBytes := ent.Value
if secretBytes == nil {
resp := logical.ErrorResponse("No value at %v%v", req.MountPoint, path)
return resp, nil
}
if err := jsonutil.DecodeJSON(secretBytes, &data); err != nil {
b.Logger().Error("JSON decoding failed", "error", err)
return nil, errwrap.Wrapf("json decoding failed: {{err}}", err)
}
if invalidResponse := validateKeyData(data); invalidResponse != nil {
return invalidResponse, nil
}
token := b.readCachedToken(data, path)
if token == nil {
token, err = b.readNewToken(ctx, req, path, data)
if err != nil {
return nil, err
}
}
outData := map[string]interface{}{
"token": *token,
}
// Generate the response
resp := &logical.Response{
Data: outData,
}
return resp, nil
}
func (b *backend) initializeSnctlConfig() error {
b.Logger().Info("Initializing snctl config")
cmd := exec.Command(GetSnctl(), "config", "init")
out, err := cmd.CombinedOutput()
if err != nil {
b.Logger().Error("Failed to run `snctl config init`", "error", err, "out", out)
}
return err
}
// Initialize once if config dir does not exist.
// snctl config init
func (b *backend) requireSnctlConfig() error {
home, err := os.UserHomeDir()
if err != nil {
return errwrap.Wrapf("No user HOME directory: {{err}}", err)
}
path := home + "/.snctl"
// TODO: Enter Mutex here
_, err = os.ReadDir(path)
if err != nil {
// Clear error and attempt to initialize
err = b.initializeSnctlConfig()
}
// Return remaining error, if any.
return err
}
func (b *backend) activateServiceAccount(secretKey string) error {
// Set a dummy oauth key. The dummy key is overwritten with per-request data.
// snctl auth activate-service-account --key-file ~/service-account-key.json
cmd := exec.Command(GetSnctl(), "auth", "activate-service-account", "--key-file", secretKey)
out, err := cmd.CombinedOutput()
if err != nil {
b.Logger().Error("Failed to run `snctl auth activate-service-account`", "error", err, "out", out)
}
return err
}
func (b *backend) handleWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
path := data.Get("path").(string)
if len(req.Data) == 0 {
b.Logger().Info("Clearing service account", "path", path)
// clear the key file
err := req.Storage.Delete(ctx, path)
if err != nil {
b.Logger().Error("Deleting from storage failed", "error", err)
return nil, errwrap.Wrapf("Deleting from storage failed: {{err}}", err)
}
return nil, nil
}
stringTtl, hasTtl := req.Data["ttl"]
if hasTtl {
var ttl64 int64 = 0
var err error = nil
switch stringTtl.(type) {
case int:
ttl64 = int64(stringTtl.(int))
case int64:
ttl64 = stringTtl.(int64)
case json.Number:
ttl64, err = stringTtl.(json.Number).Int64()
case float64:
ttl64 = int64(stringTtl.(float64))
case string:
ttl32, err2 := strconv.Atoi(stringTtl.(string))
if err2 == nil {
ttl64 = int64(ttl32)
} else {
err = err2
}
default:
return nil, fmt.Errorf("ttl is not a scalar: {{type}}", reflect.TypeOf(stringTtl).Name())
}
if err != nil {
return nil, errwrap.Wrapf("ttl is not an integer: {{err}}", err)
}
req.Data["ttl"] = ttl64
}
// Example key file
// {"type":"sn_service_account","client_id":"...","client_secret":"...","client_email":"...","issuer_url":"https://auth.streamnative.cloud"}
// JSON encode the data
buf, err := json.Marshal(req.Data)
if err != nil {
return nil, errwrap.Wrapf("json encoding failed: {{err}}", err)
}
b.Logger().Info("Saving service account")
// Store kv pairs in map at specified path
ent := &logical.StorageEntry{
Key: path,
Value: buf,
}
err = req.Storage.Put(ctx, ent)
if err != nil {
b.Logger().Error("Putting to storage failed", "error", err)
return nil, errwrap.Wrapf("Putting to storage failed: {{err}}", err)
}
return nil, nil
}
func (b *backend) handleDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
path := data.Get("path").(string)
// Remove entry for specified path
err := req.Storage.Delete(ctx, path)
if err != nil {
b.Logger().Error("Deleting from storage failed", "error", err)
return nil, errwrap.Wrapf("Deleting from storage failed: {{err}}", err)
}
return nil, nil
}
const helpText = `
The StreamNative backend generates Pulsar JWTs on-demand using the StreamNative API.
`