-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpath_roles.go
248 lines (219 loc) · 7.54 KB
/
path_roles.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
package datastax_astra
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"time"
)
const (
roleStoragePath = "role/"
roleStorageKeyDelimiter = ":"
defaultTtl = time.Duration(24*3600) * time.Second
defaultMaxTtl = time.Duration(24*3600) * time.Second
)
func pathRole(b *datastaxAstraBackend) *framework.Path {
return &framework.Path{
Pattern: "role",
Fields: map[string]*framework.FieldSchema{
"role_name": {
Type: framework.TypeLowerCaseString,
Description: "The name of the role as it should appear in Vault.",
},
"role_id": {
Type: framework.TypeString,
Description: "UUID of the role as seen in Astra.",
},
"org_id": {
Type: framework.TypeString,
Description: "UUID of the organization in Astra.",
},
"ttl": {
Type: framework.TypeDurationSecond,
Description: "Default ttl in seconds, minutes or hours for the token. If unset or set to 0, it will default to 24 hours. If this value is bigger than max_ttl, it will be clamped to the max_ttl value. Use the duration initials after the number. for e.g. 5s, 5m, 5h",
Required: false,
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: "Maximum ttl in seconds, minutes or hours for the token. If unset or set to 0, it will default to 24 hours. Use the duration initials after the number. for e.g. 5s, 5m, 5h",
Required: false,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathRoleWrite,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRoleWrite,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathRoleRead,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathRoleDelete,
},
},
ExistenceCheck: b.pathRoleExistenceCheck,
HelpSynopsis: pathRoleHelpSynopsis,
HelpDescription: pathRoleHelpDescription,
}
}
func readRoleUsingKey(ctx context.Context, s logical.Storage, roleKey string) (*astraRoleEntry, error) {
if roleKey == "" {
return nil, errors.New("role key is an empty string")
}
entry, err := s.Get(ctx, roleStoragePath+roleKey)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
role := &astraRoleEntry{}
err = entry.DecodeJSON(role)
if err != nil {
return nil, errors.New("error retrieving role " + roleKey + ": " + err.Error())
}
return role, nil
}
func readRole(ctx context.Context, s logical.Storage, roleName, orgId string) (*astraRoleEntry, error) {
if roleName == "" {
return nil, errors.New("role name is an empty string")
}
if orgId == "" {
return nil, errors.New("org ID is an empty string")
}
return readRoleUsingKey(ctx, s, orgId+roleStorageKeyDelimiter+roleName)
}
func saveRole(ctx context.Context, s logical.Storage, role *astraRoleEntry) error {
entry, err := logical.StorageEntryJSON(roleStoragePath+role.OrgId+roleStorageKeyDelimiter+role.RoleName, role)
if err != nil {
return err
}
if entry == nil {
return errors.New("error creating role entry " + role.RoleName + ": " + err.Error())
}
err = s.Put(ctx, entry)
if err != nil {
return err
}
return nil
}
// pathRolesRead makes a request to Vault storage to read a role and return response data
func (b *datastaxAstraBackend) pathRoleRead(ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
role, err := readRole(ctx, req.Storage, d.Get("role_name").(string), d.Get("org_id").(string))
if err != nil {
return nil, err
}
if role == nil {
return nil, nil
}
return &logical.Response{
Data: role.ToResponseData(),
}, nil
}
// pathRolesWrite makes a request to Vault storage to update a role based on the attributes passed to the role configuration
func (b *datastaxAstraBackend) pathRoleWrite(ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleNameRaw, ok := d.GetOk("role_name")
if !ok {
return logical.ErrorResponse("please provide a role_name argument"), nil
}
roleName := roleNameRaw.(string)
orgIdRaw, ok := d.GetOk("org_id")
if !ok {
return logical.ErrorResponse("please provide an org_id argument"), nil
}
orgId := orgIdRaw.(string)
role, err := readRole(ctx, req.Storage, roleName, orgId)
if err != nil {
return nil, err
}
createOperation := req.Operation == logical.CreateOperation
if role == nil {
role = &astraRoleEntry{}
role.RoleName = roleName
role.OrgId = orgId
}
roleId, ok := d.GetOk("role_id")
if ok {
role.RoleId = roleId.(string)
} else if !ok && createOperation {
return logical.ErrorResponse("please provide a role_id argument"), nil
}
ttl, ok := d.GetOk("ttl")
if ok {
if ttl.(int) > 0 {
role.TTL = time.Duration(ttl.(int)) * time.Second
} else {
role.TTL = defaultTtl
b.logger.Warn(fmt.Sprintf("A ttl value of 0 was provided; defaulting to %s", role.TTL))
}
} else if !ok && createOperation {
role.TTL = defaultTtl
b.logger.Warn(fmt.Sprintf("No ttl value provided; defaulting to %s", role.TTL))
}
maxTtl, ok := d.GetOk("max_ttl")
if ok {
if maxTtl.(int) > 0 {
role.MaxTTL = time.Duration(maxTtl.(int)) * time.Second
} else {
role.MaxTTL = defaultMaxTtl
b.logger.Warn(fmt.Sprintf("A max_ttl value of 0 was provided; defaulting to %s", role.MaxTTL))
}
} else if !ok && createOperation {
role.MaxTTL = defaultMaxTtl
b.logger.Warn(fmt.Sprintf("No max_ttl value provided; defaulting to %s", role.MaxTTL))
}
if role.TTL > role.MaxTTL {
role.TTL = role.MaxTTL
b.logger.Warn(fmt.Sprintf("The ttl value provided is greater than max_ttl (%s); setting ttl to %s", role.MaxTTL, role.TTL))
}
err = saveRole(ctx, req.Storage, role)
if err != nil {
return nil, err
}
b.logger.Info(fmt.Sprintf(
"%s role %s with token settings TTL: %s and MaxTTL: %s",
operationToStringVerb(req.Operation),
roleName,
role.TTL,
role.MaxTTL))
return nil, nil
}
// pathRolesDelete makes a request to Vault storage to delete a role
func (b *datastaxAstraBackend) pathRoleDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleName, ok := d.GetOk("role_name")
if !ok {
return logical.ErrorResponse("please provide a role_name argument"), nil
}
orgId, ok := d.GetOk("org_id")
if !ok {
return logical.ErrorResponse("please provide an org_id argument"), nil
}
err := req.Storage.Delete(ctx, roleStoragePath+orgId.(string)+roleStorageKeyDelimiter+roleName.(string))
if err != nil {
return nil, err
}
b.logger.Info("Deleted role " + roleName.(string))
return nil, nil
}
func (b *datastaxAstraBackend) pathRoleExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
// The existence check determines whether the logical.Request.Operation value is Create or Update. In this case
// we will skip the argument validation as it will be performed in the write path implementation.
obj, err := req.Storage.Get(ctx, roleStoragePath + data.Get("org_id").(string) + roleStorageKeyDelimiter + data.Get("role_name").(string))
if err != nil {
return false, errors.New("error retrieving role from storage for existence check: " + err.Error())
}
return obj != nil, nil
}
const (
pathRoleHelpSynopsis = `Manages the Vault role for generating Astra tokens.`
pathRoleHelpDescription = `
This path allows you to read and write roles used to generate Astra tokens.
You can configure a role to manage a token by setting the 'name' (role name)
and 'org_id' (Astra organisation id) fields.
`
)