-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpath_credentials.go
352 lines (315 loc) · 10.5 KB
/
path_credentials.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
package datastax_astra
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// pathCredentials extends the Vault API with a `/token` endpoint for a role.
func pathCredentials(b *datastaxAstraBackend) *framework.Path {
return &framework.Path{
Pattern: "org/token",
Fields: map[string]*framework.FieldSchema{
"org_id": {
Type: framework.TypeString,
Description: "name of the org for which token is being requested",
Required: true,
DisplayAttrs: &framework.DisplayAttributes{
Sensitive: false,
},
},
"role_name": {
Type: framework.TypeLowerCaseString,
Description: "name of the role for which token is being requested",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Sensitive: false,
},
},
"logical_name": {
Type: framework.TypeLowerCaseString,
Description: "Logical name to reference this token by. Ignored if running in sidecar mode",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Sensitive: false,
},
},
"metadata": {
Type: framework.TypeKVPairs,
Description: "Arbitrary key=value",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{Sensitive: false},
},
"client_id": {
Type: framework.TypeString,
Description: "ClientId for the token. Ignored if running in sidecar mode",
Required: false,
DisplayAttrs: &framework.DisplayAttributes{
Sensitive: false,
},
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathCredentialsRead,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathCredentialsUpdate,
},
},
HelpSynopsis: pathCredentialsHelpSyn,
HelpDescription: pathCredentialsHelpDesc,
}
}
func readTokenUsingClientId(ctx context.Context, s logical.Storage, clientId string) (*astraToken, error) {
credsList, err := s.List(ctx, "token/")
if err != nil {
return nil, errors.New("failed to get token list: " + err.Error())
}
if len(credsList) == 0 {
return nil, errors.New("no tokens found")
}
for i := 0; i < len(credsList); i++ {
token, err := readToken(ctx, s, credsList[i])
if err != nil {
return nil, errors.New("unable to retrieve token information: " + err.Error())
}
if token.ClientID == clientId {
return token, nil
}
}
return nil, nil
}
func calculateTokenId(roleEntry *astraRoleEntry, logicalName string) string {
tokenRef := fmt.Sprintf("%s:%s:%s", roleEntry.OrgId, roleEntry.RoleName, logicalName)
return fmt.Sprintf("%x", sha256.Sum256([]byte(tokenRef)))
}
func readToken(ctx context.Context, s logical.Storage, tokenId string) (*astraToken, error) {
token, err := s.Get(ctx, "token/"+tokenId)
if err != nil {
return nil, err
}
if token == nil {
return nil, nil
}
result := &astraToken{}
err = token.DecodeJSON(result)
if err != nil {
return nil, err
}
return result, nil
}
func saveToken(ctx context.Context, s logical.Storage, token *astraToken, tokenId string) error {
entry, err := logical.StorageEntryJSON("token/"+tokenId, token)
if err != nil {
return err
}
err = s.Put(ctx, entry)
if err != nil {
return err
}
return nil
}
func (b *datastaxAstraBackend) createToken(ctx context.Context, s logical.Storage, roleEntry *astraRoleEntry, logicalName string, metadata map[string]string) (*astraToken, error) {
client, err := b.getClient(ctx, s, roleEntry.OrgId)
if err != nil {
return nil, err
}
var token *astraToken
token, err = createTokenInAstra(client, roleEntry, logicalName, metadata)
if err != nil {
errMsg := "error creating Astra token: " + err.Error()
b.logger.Error(errMsg)
return nil, errors.New(errMsg)
}
if token == nil {
errMsg := "failed to create Astra token"
b.logger.Error(errMsg)
return nil, errors.New(errMsg)
}
// If logicalName is a non-empty string we will use that along with the org ID and role name to store the token,
// otherwise use the token clientID. In standard mode the logicalName will be set a non-empty string, but in
// sidecar mode it is set to an empty string.
var tokenId string
if logicalName != "" {
tokenId = calculateTokenId(roleEntry, logicalName)
} else {
tokenId = token.ClientID
}
err = saveToken(ctx, s, token, tokenId)
if err != nil {
return nil, err
}
return token, nil
}
func (b *datastaxAstraBackend) generateTokenResponse(token *astraToken, tokenId string, roleEntry *astraRoleEntry) (*logical.Response, error) {
resp := b.Secret(astraTokenType).Response(
token.ToResponseData(),
map[string]interface{}{
"orgId": roleEntry.OrgId,
"clientId": token.ClientID,
"roleName": roleEntry.RoleName,
"tokenId": tokenId,
})
if roleEntry.TTL > 0 {
resp.Secret.TTL = roleEntry.TTL
}
if roleEntry.MaxTTL > 0 {
resp.Secret.MaxTTL = roleEntry.MaxTTL
}
resp.Secret.Renewable = true
b.logger.Info(fmt.Sprintf("Created token '%s' with TTL: %s and MaxTTL: %s", token.ClientID, roleEntry.TTL, roleEntry.MaxTTL))
return resp, nil
}
func (b *datastaxAstraBackend) pathCredentialsStandardMode(ctx context.Context, req *logical.Request, d *framework.FieldData, orgId string, readOnly bool) (*logical.Response, error) {
clientIdRaw, ok := d.GetOk("client_id")
if ok && readOnly {
clientId := clientIdRaw.(string)
token, err := readTokenUsingClientId(ctx, req.Storage, clientId)
if token == nil {
if err == nil {
// No error means no token existed with that clientId
return nil, errors.New("no token found with clientId " + clientId)
} else {
return nil, err
}
}
return &logical.Response{Data: token.ToResponseData()}, nil
}
roleNameRaw, ok := d.GetOk("role_name")
if !ok {
return logical.ErrorResponse("please provide a role_name argument"), nil
}
roleName := roleNameRaw.(string)
logicalNameRaw, ok := d.GetOk("logical_name")
if !ok {
return logical.ErrorResponse("please provide a logical_name argument"), nil
}
logicalName := logicalNameRaw.(string)
// In standard mode logical_name must be set to a non-empty string value, this is
// so we can calculate a tokenId when storing it
if logicalName == "" {
return nil, errors.New("logical_name is set to an empty string; a non-empty value must be provided")
}
roleEntry, err := readRole(ctx, req.Storage, roleName, orgId)
if err != nil {
return nil, errors.New("error retrieving role " + roleName + ": " + err.Error())
}
if roleEntry == nil {
return nil, errors.New("unable to find role " + roleName)
}
if roleEntry.RoleId == "" {
return nil, nil
}
tokenId := calculateTokenId(roleEntry, logicalName)
token, err := readToken(ctx, req.Storage, tokenId)
if err != nil {
return nil, errors.New("error attempting to retrieve token for org ID" + orgId + ", role " + roleName + ", with logical name " + logicalName + ": " + err.Error())
}
// Return behaviour depends on whether we are reading or writing
if readOnly {
if token == nil {
return nil, errors.New("unable to find token for org ID" + orgId + ", role " + roleName + ", with logical name " + logicalName)
}
return &logical.Response{Data: token.ToResponseData()}, nil
}
// If we are writing (not readOnly), create the token and generate a secret response
if token != nil {
return nil, errors.New("token already exists for org ID " + orgId + ", role " + roleName + ", with logical name " + logicalName)
}
metadata := make(map[string]string)
metadataRaw, ok, err := d.GetOkErr("metadata")
if err != nil {
return nil, errors.New("error parsing metadata: " + err.Error())
}
if ok {
metadata = metadataRaw.(map[string]string)
}
token, err = b.createToken(ctx, req.Storage, roleEntry, logicalName, metadata)
if err != nil {
return nil, err
}
return b.generateTokenResponse(token, tokenId, roleEntry)
}
func (b *datastaxAstraBackend) pathCredentialsSidecarMode(ctx context.Context, req *logical.Request, d *framework.FieldData, orgId string) (*logical.Response, error) {
roleNameRaw, ok := d.GetOk("role_name")
if !ok {
return logical.ErrorResponse("please provide a role_name argument"), nil
}
roleName := roleNameRaw.(string)
roleEntry, err := readRole(ctx, req.Storage, roleName, orgId)
if err != nil {
return nil, errors.New("error retrieving role " + roleName + ": " + err.Error())
}
if roleEntry == nil {
return nil, errors.New("unable to find role " + roleName)
}
if roleEntry.RoleId == "" {
return nil, nil
}
metadata := make(map[string]string)
metadataRaw, ok, err := d.GetOkErr("metadata")
if err != nil {
return nil, errors.New("error parsing metadata: " + err.Error())
}
if ok {
metadata = metadataRaw.(map[string]string)
}
// In sidecar mode logical_name is ignored, so make it an empty string
// to force storing the token using its ClientId
token, err := b.createToken(ctx, req.Storage, roleEntry, "", metadata)
if err != nil {
return nil, err
}
return b.generateTokenResponse(token, token.ClientID, roleEntry)
}
func (b *datastaxAstraBackend) pathCredentialsRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
orgIdRaw, ok := d.GetOk("org_id")
if !ok {
return logical.ErrorResponse("please provide an org_id argument"), nil
}
orgId := orgIdRaw.(string)
config, err := readConfig(ctx, req.Storage, orgId)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("unable to find config for org ID" + orgId)
}
switch config.CallerMode {
case StandardCallerMode:
return b.pathCredentialsStandardMode(ctx, req, d, orgId, true)
case SidecarCallerMode:
return b.pathCredentialsSidecarMode(ctx, req, d, orgId)
}
return nil, nil
}
func (b *datastaxAstraBackend) pathCredentialsUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
orgIdRaw, ok := d.GetOk("org_id")
if !ok {
return logical.ErrorResponse("please provide an org_id argument"), nil
}
orgId := orgIdRaw.(string)
config, err := readConfig(ctx, req.Storage, orgId)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("unable to find config for org ID" + orgId)
}
switch config.CallerMode {
case StandardCallerMode:
return b.pathCredentialsStandardMode(ctx, req, d, orgId, false)
case SidecarCallerMode:
return b.pathCredentialsSidecarMode(ctx, req, d, orgId)
}
return nil, nil
}
const pathCredentialsHelpSyn = `
Generate a AstraCS token from a specific Vault role.
`
const pathCredentialsHelpDesc = `
This path generates a Astra CS token based on a particular role.
`