This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath_roles.go
221 lines (201 loc) · 6.57 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
package splunk
import (
"context"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
rolesPrefix = "roles/"
defaultUserPrefix = "vault"
userIDSchemeUUID4_v0_5_0 = ""
userIDSchemeUUID4 = "uuid4"
userIDSchemeBase58_64 = "base58-64"
userIDSchemeBase58_128 = "base58-128"
)
func (b *backend) pathRoles() *framework.Path {
return &framework.Path{
Pattern: rolesPrefix + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the role",
},
"connection": {
Type: framework.TypeString,
Description: "Name of the Splunk connection this role acts on",
},
"default_ttl": {
Type: framework.TypeDurationSecond,
Description: "Default TTL for role",
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: "Maximum time a credential is valid for",
},
"roles": {
Type: framework.TypeCommaStringSlice,
Description: "Comma-separated string or list of Splunk roles.",
},
"allowed_server_roles": {
Type: framework.TypeCommaStringSlice,
Description: trimIndent(`
Comma-separated string or array of node type (glob) patterns that are allowed
to fetch credentials for. If empty, no nodes are allowed. If "*", all
node types are allowed.`),
Default: []string{"*"},
},
"default_app": {
Type: framework.TypeString,
Description: trimIndent(`
User default app. Overrides the default app inherited from the user roles.`),
},
"email": {
Type: framework.TypeString,
Description: "User email address.",
},
"tz": {
Type: framework.TypeString,
Description: "User time zone.",
},
"user_prefix": {
Type: framework.TypeString,
Description: "Prefix for creating new users.",
Default: defaultUserPrefix,
},
"user_id_scheme": {
Type: framework.TypeLowerCaseString,
Description: fmt.Sprintf("ID generation scheme (%s, %s, %s). Default: %s",
userIDSchemeUUID4, userIDSchemeBase58_64, userIDSchemeBase58_128, userIDSchemeBase58_64),
Default: userIDSchemeBase58_64,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.rolesReadHandler,
logical.CreateOperation: b.rolesWriteHandler,
logical.UpdateOperation: b.rolesWriteHandler,
logical.DeleteOperation: b.rolesDeleteHandler,
},
ExistenceCheck: b.rolesExistenceCheckHandler,
HelpSynopsis: pathRoleHelpSyn,
HelpDescription: pathRoleHelpDesc,
}
}
// Returning 'true' forces an UpdateOperation, CreateOperation otherwise.
func (b *backend) rolesExistenceCheckHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
name := d.Get("name").(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return false, err
}
return role != nil, nil
}
func (b *backend) rolesReadHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
return nil, nil
}
resp := &logical.Response{
Data: role.toResponseData(),
}
return resp, nil
}
func (b *backend) rolesWriteHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
role = &roleConfig{}
}
if connRaw, ok := getValue(data, req.Operation, "connection"); ok {
role.Connection = connRaw.(string)
}
if role.Connection == "" {
return logical.ErrorResponse("empty Splunk connection name"), nil
}
if defaultTTLRaw, ok := getValue(data, req.Operation, "default_ttl"); ok {
role.DefaultTTL = time.Duration(defaultTTLRaw.(int)) * time.Second
}
if maxTTLRaw, ok := getValue(data, req.Operation, "max_ttl"); ok {
role.MaxTTL = time.Duration(maxTTLRaw.(int)) * time.Second
}
if allowedServerRoles, ok := getValue(data, req.Operation, "allowed_server_roles"); ok {
role.AllowedServerRoles = allowedServerRoles.([]string)
}
role.PasswordSpec = DefaultPasswordSpec() // XXX make configurable
if roles, ok := getValue(data, req.Operation, "roles"); ok {
role.Roles = roles.([]string)
}
if len(role.Roles) == 0 {
return logical.ErrorResponse("roles cannot be empty"), nil
}
if defaultAppRaw, ok := getValue(data, req.Operation, "default_app"); ok {
role.DefaultApp = defaultAppRaw.(string)
}
if emailRaw, ok := getValue(data, req.Operation, "email"); ok {
role.Email = emailRaw.(string)
}
if tzRaw, ok := getValue(data, req.Operation, "tz"); ok {
role.TZ = tzRaw.(string)
}
if userPrefixRaw, ok := getValue(data, req.Operation, "user_prefix"); ok {
role.UserPrefix = userPrefixRaw.(string)
}
if role.UserPrefix == "" {
return logical.ErrorResponse("user_prefix can't be set to empty string"), nil
}
if userIDSchemeRaw, ok := getValue(data, req.Operation, "user_id_scheme"); ok {
role.UserIDScheme = userIDSchemeRaw.(string)
}
switch role.UserIDScheme {
case userIDSchemeUUID4_v0_5_0:
case userIDSchemeUUID4:
case userIDSchemeBase58_64:
case userIDSchemeBase58_128:
default:
return logical.ErrorResponse("invalid user_id_scheme: %q", role.UserIDScheme), nil
}
if err := role.store(ctx, req.Storage, name); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) rolesDeleteHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if err := req.Storage.Delete(ctx, rolesPrefix+name); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathRolesList() *framework.Path {
return &framework.Path{
Pattern: rolesPrefix + "?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.rolesListHandler,
},
HelpSynopsis: pathRoleHelpSyn,
HelpDescription: pathRoleHelpDesc,
}
}
func (b *backend) rolesListHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, rolesPrefix)
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
const pathRoleHelpSyn = `
Manage the roles that can be created with this backend.
`
const pathRoleHelpDesc = `
This path lets you manage the roles that can be created with this backend.
See the documentation for roles/name for a full list of accepted
connection details.
`