This repository has been archived by the owner on Jan 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpaths_creds.go
83 lines (66 loc) · 1.87 KB
/
paths_creds.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
package natsbackend
import (
"context"
"github.com/edgefarm/vault-plugin-secrets-nats/pkg/stm"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// JWTStorage represents a Creds stored in the backend
type CredsStorage struct {
Creds string `json:"creds"`
}
// CredsParameters represents the parameters for a Creds operation
type CredsParameters struct {
Operator string `json:"operator,omitempty"`
Account string `json:"account,omitempty"`
User string `json:"user,omitempty"`
CredsStorage
}
// CredsData represents the the data returned by a Creds operation
type CredsData struct {
CredsStorage
}
func pathCreds(b *NatsBackend) []*framework.Path {
paths := []*framework.Path{}
paths = append(paths, pathUserCreds(b)...)
return paths
}
func readCreds(ctx context.Context, storage logical.Storage, path string) (*CredsStorage, error) {
return getFromStorage[CredsStorage](ctx, storage, path)
}
func deleteCreds(ctx context.Context, storage logical.Storage, path string) error {
return deleteFromStorage(ctx, storage, path)
}
func createResponseCredsData(creds *CredsStorage) (*logical.Response, error) {
d := &CredsData{
CredsStorage: *creds,
}
rval := map[string]interface{}{}
err := stm.StructToMap(d, &rval)
if err != nil {
return nil, err
}
resp := &logical.Response{
Data: rval,
}
return resp, nil
}
func addCreds(ctx context.Context, storage logical.Storage, path string, params CredsParameters) error {
creds, err := getFromStorage[CredsStorage](ctx, storage, path)
if err != nil {
return err
}
if creds == nil {
creds = &CredsStorage{}
}
creds.Creds = params.Creds
// store the nkey
err = storeInStorage(ctx, storage, path, creds)
if err != nil {
return err
}
return nil
}
func listCreds(ctx context.Context, storage logical.Storage, path string) ([]string, error) {
return storage.List(ctx, path)
}