This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath_roles.go
252 lines (208 loc) · 6.9 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
249
250
251
252
package artifactory
import (
"context"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *backend) pathListRoles() *framework.Path {
return &framework.Path{
Pattern: "roles/?$",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathRoleList,
},
},
HelpSynopsis: `List configured roles with this backend.`,
}
}
func (b *backend) pathRoles() *framework.Path {
return &framework.Path{
Pattern: "roles/" + framework.GenericNameWithAtRegex("role"),
Fields: map[string]*framework.FieldSchema{
"role": {
Type: framework.TypeString,
Required: true,
Description: `The name of the role, must be conform to alphanumeric plus at, dash, and period.`,
},
"grant_type": {
Type: framework.TypeString,
Description: `Optional. Defaults to 'client_credentials' when creating the access token. You likely don't need to change this.'`,
},
"username": {
Type: framework.TypeString,
Required: true,
Description: `Required. The username for which the access token is created. If the user does not exist, Artifactory will create a transient user. Note that non-admininstrative access tokens can only create tokens for themselves.`,
},
"scope": {
Type: framework.TypeString,
Required: true,
Description: `Required. See the JFrog Artifactory REST documentation on "Create Token" for a full and up to date description.`,
},
"audience": {
Type: framework.TypeString,
Description: `Optional. See the JFrog Artifactory REST documentation on "Create Token" for a full and up to date description.`,
},
"default_ttl": {
Type: framework.TypeDurationSecond,
Description: `Default TTL for issued access tokens. If unset, uses the backend's default_ttl. Cannot exceed max_ttl.`,
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: `Maximum TTL that an access token can be renewed for. If unset, uses the backend's max_ttl. Cannot exceed backend's max_ttl.`,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathRoleRead,
Summary: `Read information about the specified role.`,
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathRoleWrite,
Summary: `Write information about the specified role.`,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRoleWrite,
Summary: `Overwrite information about the specified role.`,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathRoleDelete,
Summary: `Delete the specified role.`,
},
},
HelpSynopsis: `Manage data related to roles used to issue Artifactory access tokens.`,
}
}
type artifactoryRole struct {
GrantType string `json:"grant_type"`
Username string `json:"username,omitempty"`
Scope string `json:"scope"`
Audience string `json:"audience,omitempty"`
DefaultTTL time.Duration `json:"default_ttl,omitempty"`
MaxTTL time.Duration `json:"max_ttl,omitempty"`
}
func (b *backend) pathRoleList(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
b.rolesMutex.RLock()
defer b.rolesMutex.RUnlock()
entries, err := req.Storage.List(ctx, "roles/")
if err != nil {
return nil, err
}
if entries == nil {
return logical.ErrorResponse("no roles found"), nil
}
return logical.ListResponse(entries), nil
}
func (b *backend) pathRoleWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.rolesMutex.Lock()
b.configMutex.RLock()
defer b.configMutex.RUnlock()
defer b.rolesMutex.Unlock()
config, err := b.fetchAdminConfiguration(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return logical.ErrorResponse("backend not configured"), nil
}
roleName := data.Get("role").(string)
if roleName == "" {
return logical.ErrorResponse("missing role"), nil
}
createOperation := (req.Operation == logical.CreateOperation)
role := &artifactoryRole{}
if !createOperation {
existingRole, err := b.Role(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if existingRole != nil {
role = existingRole
}
}
if value, ok := data.GetOk("grant_type"); ok {
role.GrantType = value.(string)
}
if value, ok := data.GetOk("username"); ok {
role.Username = value.(string)
}
if value, ok := data.GetOk("scope"); ok {
role.Scope = value.(string)
}
if value, ok := data.GetOk("audience"); ok {
role.Audience = value.(string)
}
// Looking at database/path_roles.go, it doesn't do any validation on these values during role creation.
if value, ok := data.GetOk("default_ttl"); ok {
role.DefaultTTL = time.Duration(value.(int)) * time.Second
}
if value, ok := data.GetOk("max_ttl"); ok {
role.MaxTTL = time.Duration(value.(int)) * time.Second
}
if role.Scope == "" {
return logical.ErrorResponse("missing scope"), nil
}
if role.Username == "" {
return logical.ErrorResponse("missing username"), nil
}
entry, err := logical.StorageEntryJSON("roles/"+roleName, role)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathRoleRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.rolesMutex.RLock()
defer b.rolesMutex.RUnlock()
roleName := data.Get("role").(string)
if roleName == "" {
return logical.ErrorResponse("missing role"), nil
}
role, err := b.Role(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if role == nil {
return logical.ErrorResponse("no such role"), nil
}
return &logical.Response{
Data: b.roleToMap(roleName, *role),
}, nil
}
func (b *backend) Role(ctx context.Context, storage logical.Storage, roleName string) (*artifactoryRole, error) {
entry, err := storage.Get(ctx, "roles/"+roleName)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var role artifactoryRole
if err := entry.DecodeJSON(&role); err != nil {
return nil, err
}
return &role, nil
}
func (b *backend) roleToMap(roleName string, role artifactoryRole) map[string]interface{} {
return map[string]interface{}{
"role": roleName,
"grant_type": role.GrantType,
"username": role.Username,
"scope": role.Scope,
"audience": role.Audience,
"default_ttl": role.DefaultTTL.Seconds(),
"max_ttl": role.MaxTTL.Seconds(),
}
}
func (b *backend) pathRoleDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.rolesMutex.Lock()
defer b.rolesMutex.Unlock()
err := req.Storage.Delete(ctx, "roles/"+data.Get("role").(string))
if err != nil {
return nil, err
}
return nil, nil
}