forked from datastax/vault-plugin-secrets-datastax-astra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_config.go
287 lines (273 loc) · 8.29 KB
/
path_config.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
package datastax_astra
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
configStoragePath = "config"
forwardSlash = "/"
configsListPath = "configs/?"
)
// astraConfig includes the minimum configuration
// required to instantiate a new astra client.
type astraConfig struct {
AstraToken string `json:"astra_token"`
URL string `json:"url"`
OrgId string `json:"org_id"`
LogicalName string `json:"logical_name"`
DefaultLeaseRenewTime string `json:"renewal_time"`
}
func pathConfig(b *datastaxAstraBackend) *framework.Path {
return &framework.Path{
Pattern: "config",
Fields: map[string]*framework.FieldSchema{
"url": {
Type: framework.TypeString,
Description: "The url to access astra",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "url",
Sensitive: false,
},
},
"astra_token": {
Type: framework.TypeString,
Description: "token",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "AstraToken",
Sensitive: true,
},
},
"org_id": {
Type: framework.TypeString,
Description: "UUID of organization",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "org_id",
Sensitive: false,
},
},
"logical_name": {
Type: framework.TypeString,
Description: "logical name of config",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "logical_name",
Sensitive: false,
},
},
"renewal_time": {
Type: framework.TypeString,
Description: "Default lease time in seconds, minutes or hours for renew operation. Use the duration intials after the number. for e.g. 5s, 5m, 5h",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Name: "renewal_time",
Sensitive: false,
},
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigDelete,
},
},
HelpSynopsis: pathConfigHelpSynopsis,
HelpDescription: pathConfigHelpDescription,
}
}
func pathConfigList(b *datastaxAstraBackend) *framework.Path {
return &framework.Path{
Pattern: configsListPath,
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathConfigList,
Summary: "List all configs.",
},
},
}
}
func (b *datastaxAstraBackend) pathConfigList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
matches := map[string]interface{}{}
objList, err := req.Storage.List(ctx, "config/")
if err != nil {
return nil, errors.New("failed to load config list")
}
if len(objList) == 0 {
return nil, errors.New("no objects found")
}
for _, key := range objList {
fmt.Println(key)
cid := "config/" + key
m, err := getConfig(ctx, req.Storage, key)
if err != nil {
return nil, errors.New("failed to get config for org in list")
}
matches[cid] = m.AstraToken
}
var keys []string
for k := range matches {
keys = append(keys, k)
}
return logical.ListResponseWithInfo(keys, matches), nil
}
// pathConfigRead reads the configuration and outputs information.
func (b *datastaxAstraBackend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
orgId := data.Get("org_id").(string)
logicalName := data.Get("logical_name").(string)
if logicalName != "" && orgId == "" {
configs, err := listConfig(ctx, req.Storage)
if err != nil {
return nil, errors.New("error getting configs")
}
if len(configs) == 0 {
return nil, errors.New("no configs found")
}
for _, orgId := range configs {
m, err := getConfig(ctx, req.Storage, orgId)
if err != nil {
return nil, errors.New("failed to get config for org in list")
}
if m.LogicalName == logicalName {
return &logical.Response{
Data: map[string]interface{}{
"astra_token": m.AstraToken,
"url": m.URL,
"org_id": m.OrgId,
"logical_name": m.LogicalName,
"renewal_time": m.DefaultLeaseRenewTime,
},
}, nil
}
}
return nil, errors.New("no config found for logical_name = " + logicalName)
}
if orgId != "" {
config, err := getConfig(ctx, req.Storage, orgId)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("config does not exist for org")
}
return &logical.Response{
Data: map[string]interface{}{
"astra_token": config.AstraToken,
"url": config.URL,
"org_id": config.OrgId,
"logical_name": config.LogicalName,
"renewal_time": config.DefaultLeaseRenewTime,
},
}, nil
}
return nil, errors.New("please provide org_id or logical_name")
}
// pathConfigWrite updates the configuration for the backend
func (b *datastaxAstraBackend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
token, ok := data.GetOk("astra_token")
if !ok {
return nil, errors.New("astra_token not present")
}
url, ok := data.GetOk("url")
if !ok {
return nil, errors.New("url not present")
}
orgId, ok := data.GetOk("org_id")
if !ok {
return nil, errors.New("org_id not present")
}
logicalName, ok := data.GetOk("logical_name")
if !ok {
return nil, errors.New("logical_name not present")
}
c, err := getConfig(ctx, req.Storage, orgId.(string))
if err != nil {
return nil, err
}
if c != nil {
return nil, errors.New("config already exists")
}
renewalTime := data.Get("renewal_time")
config := astraConfig{
AstraToken: token.(string),
URL: url.(string),
OrgId: orgId.(string),
LogicalName: logicalName.(string),
DefaultLeaseRenewTime: renewalTime.(string),
}
err = saveConfig(ctx, &config, req.Storage)
if err != nil {
return nil, err
}
// reset the client so the next invocation will pick up the new configuration
b.reset()
return nil, nil
}
func saveConfig(ctx context.Context, config *astraConfig, s logical.Storage) error {
entry, err := logical.StorageEntryJSON(configStoragePath+forwardSlash+config.OrgId, config)
if err != nil {
return err
}
if err = s.Put(ctx, entry); err != nil {
return err
}
return nil
}
// pathConfigDelete removes the configuration for the backend
func (b *datastaxAstraBackend) pathConfigDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
orgId := data.Get("org_id").(string)
if orgId == "" {
return nil, errors.New("invalid org_id")
}
err := req.Storage.Delete(ctx, configStoragePath+forwardSlash+orgId)
if err != nil {
return nil, err
}
b.reset()
return nil, nil
}
func getConfig(ctx context.Context, s logical.Storage, orgId string) (*astraConfig, error) {
entry, err := s.Get(ctx, configStoragePath+forwardSlash+orgId)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
config := astraConfig{}
if err := entry.DecodeJSON(&config); err != nil {
return nil, fmt.Errorf("error reading root configuration: %w", err)
}
// return the config, we are done
return &config, nil
}
func listConfig(ctx context.Context, s logical.Storage) ([]string, error) {
objList, err := s.List(ctx, configStoragePath+forwardSlash)
if err != nil {
return nil, errors.New("failed to load config list")
}
if len(objList) == 0 {
return nil, errors.New("no configs found")
}
return objList, nil
}
// pathConfigHelpSynopsis summarizes the help text for the configuration
const pathConfigHelpSynopsis = `Configure the Datastax Astra backend.`
// pathConfigHelpDescription describes the help text for the configuration
const pathConfigHelpDescription = `
The Datastax Astra secret backend requires credentials for managing
tokens issued to users working with an astra organization.
You must create a default token and specify the
Astra endpoint before using this secrets backend.`