-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_roles.go
180 lines (153 loc) · 4.76 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
package artifactory
import (
"context"
"errors"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathListRoles(b *backend) *framework.Path {
return &framework.Path{
Pattern: "roles/?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathRoleList,
},
}
}
func pathRoles(b *backend) *framework.Path {
return &framework.Path{
Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the role",
},
"username": &framework.FieldSchema{
Type: framework.TypeString,
Description: "User name of the created access token",
},
"member_of_groups": &framework.FieldSchema{
Type: framework.TypeCommaStringSlice,
Description: "List of groups that the token is associated with.",
},
"ttl": &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "TTL for the access token created from the role.",
},
},
ExistenceCheck: b.operationRoleExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathRolesCreateUpdate,
logical.ReadOperation: b.pathRolesRead,
logical.UpdateOperation: b.pathRolesCreateUpdate,
logical.DeleteOperation: b.pathRolesDelete,
},
}
}
func (b *backend) pathRoleList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "role/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func readRole(ctx context.Context, s logical.Storage, name string) (*roleConfig, error) {
raw, err := s.Get(ctx, "role/"+name)
if err != nil {
return nil, err
}
if raw == nil {
return nil, nil
}
role := new(roleConfig)
if err := raw.DecodeJSON(role); err != nil {
return nil, err
}
return role, nil
}
func (b *backend) pathRolesRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if name == "" {
return logical.ErrorResponse("missing name"), nil
}
role, err := readRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
return nil, nil
}
// Generate the response
resp := &logical.Response{
Data: map[string]interface{}{
"username": role.Username,
"member_of_groups": role.MemberOfGroups,
"ttl": int64(role.TTL.Seconds()),
},
}
return resp, nil
}
func (b *backend) operationRoleExistenceCheck(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
entry, err := readRole(ctx, req.Storage, d.Get("name").(string))
if err != nil {
return false, err
}
return entry != nil, nil
}
func (b *backend) pathRolesCreateUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleName := d.Get("name").(string)
if roleName == "" {
return logical.ErrorResponse("missing role name"), nil
}
// Check if the role already exists
role, err := readRole(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
// Create a new entry object if this is a CreateOperation
if role == nil {
if req.Operation == logical.UpdateOperation {
return nil, errors.New("role entry not found during update operation")
}
role = new(roleConfig)
}
if username, ok := d.GetOk("username"); ok {
role.Username = username.(string)
}
if memberOfGroups, ok := d.GetOk("member_of_groups"); ok {
role.MemberOfGroups = memberOfGroups.([]string)
}
if len(role.MemberOfGroups) == 0 {
if role.Username == "" {
return logical.ErrorResponse("member_of_groups cannot be empty if no username supplied"), nil
}
// Assume user-scoped-token
// This will fail when creating tokens if the username is transient
role.MemberOfGroups = []string{"*"}
}
if tokenTTLRaw, ok := d.GetOk("ttl"); ok {
role.TTL = time.Duration(tokenTTLRaw.(int)) * time.Second
} else if req.Operation == logical.CreateOperation {
role.TTL = time.Duration(d.Get("ttl").(int)) * time.Second
}
entry, err := logical.StorageEntryJSON("role/"+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) pathRolesDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if err := req.Storage.Delete(ctx, "role/"+name); err != nil {
return nil, err
}
return nil, nil
}
type roleConfig struct {
Username string `json:"username"`
MemberOfGroups []string `json:"member_of_groups"`
TTL time.Duration `json:"lease"`
}