-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpath_roles.go
207 lines (176 loc) · 5.53 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
package u2fauth
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/tokenutil"
"github.com/hashicorp/vault/sdk/logical"
)
type RoleEntry struct {
//Name string `json:"name" mapstructure:"name"`
tokenutil.TokenParams `mapstructure:",squash"`
// Policies []string
// // Duration after which the user will be revoked unless renewed
// TTL time.Duration
// // Maximum duration for which user can be valid
// MaxTTL time.Duration
// BoundCIDRs []*sockaddr.SockAddrMarshaler
}
func pathRolesList(b *backend) *framework.Path {
return &framework.Path{
Pattern: "roles/?",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathRoleList,
},
HelpSynopsis: pathRoleHelpSyn,
HelpDescription: pathRoleHelpDesc,
}
}
func pathRoles(b *backend) *framework.Path {
p := &framework.Path{
Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the role.",
},
// "token_policies": &framework.FieldSchema{
// Type: framework.TypeCommaStringSlice,
// Description: "Comma-separated list of policies",
// },
// "ttl": &framework.FieldSchema{
// Type: framework.TypeString,
// Default: "",
// Description: "The lease duration which decides login expiration",
// },
// "max_ttl": &framework.FieldSchema{
// Type: framework.TypeString,
// Default: "",
// Description: "Maximum duration after which login should expire",
// },
// "token_bound_cidrs": &framework.FieldSchema{
// Type: framework.TypeCommaStringSlice,
// Description: "",
// Deprecated: true,
// },
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.DeleteOperation: b.pathRoleDelete,
logical.ReadOperation: b.pathRoleRead,
logical.UpdateOperation: b.pathRoleWrite,
logical.CreateOperation: b.pathRoleWrite,
},
ExistenceCheck: b.RoleExistenceCheck,
HelpSynopsis: pathRoleHelpSyn,
HelpDescription: pathRoleHelpDesc,
}
tokenutil.AddTokenFields(p.Fields)
return p
}
func (b *backend) role(ctx context.Context, s logical.Storage, name string) (*RoleEntry, error) {
if name == "" {
return nil, fmt.Errorf("missing name")
}
entry, err := s.Get(ctx, "roles/"+strings.ToLower(name))
//b.Logger().Debug("device", "entry", entry)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result RoleEntry
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
//b.Logger().Debug("device", "result", result)
return &result, nil
}
func (b *backend) setRole(ctx context.Context, s logical.Storage, name string, dEntry *RoleEntry) error {
entry, err := logical.StorageEntryJSON("roles/"+name, dEntry)
if err != nil {
return err
}
return s.Put(ctx, entry)
}
func (b *backend) RoleExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
entry, err := b.role(ctx, req.Storage, data.Get("name").(string))
if err != nil {
return false, err
}
return entry != nil, nil
}
func (b *backend) pathRoleList(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
devices, err := req.Storage.List(ctx, "roles/")
if err != nil {
return nil, err
}
return logical.ListResponse(devices), nil
}
func (b *backend) pathRoleDelete(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
err := req.Storage.Delete(ctx, "roles/"+strings.ToLower(d.Get("name").(string)))
if err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathRoleRead(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := strings.ToLower(d.Get("name").(string))
device, err := b.role(ctx, req.Storage, name)
b.Logger().Debug("pathRoleRead", "name", name)
if err != nil {
return nil, err
}
if device == nil {
return nil, nil
}
respData := map[string]interface{}{}
device.PopulateTokenData(respData)
return &logical.Response{
Data: respData,
}, nil
}
func (b *backend) roleCreateUpdate(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := strings.ToLower(d.Get("name").(string))
b.Logger().Debug("roleCreateUpdate", "name", name)
dEntry, err := b.role(ctx, req.Storage, name)
if err != nil {
return nil, err
}
// Due to existence check, user will only be nil if it's a create operation
if dEntry == nil {
dEntry = &RoleEntry{}
}
if err := dEntry.ParseTokenFields(req, d); err != nil {
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
}
//b.Logger().Debug("deviceCreateUpdate", "dentry", dEntry)
return nil, b.setRole(ctx, req.Storage, name, dEntry)
}
func (b *backend) pathRoleWrite(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
tokenPolicies := d.Get("token_policies").([]string)
if req.Operation == logical.CreateOperation && len(tokenPolicies) == 0 {
return logical.ErrorResponse("missing token_policies"), logical.ErrInvalidRequest
}
return b.roleCreateUpdate(ctx, req, d)
}
const pathRoleHelpSyn = `
Manage u2f devices roles
`
const pathRoleHelpDesc = `
This endpoint allows you to create, read, update, and delete roles for u2f devices
that are allowed to authenticate.
Deleting a role will not revoke auth for prior authenticated devices.
To do this, do a revoke on their tokens.
`