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
/
path_creds_create.go
291 lines (261 loc) · 8.93 KB
/
path_creds_create.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package splunk
import (
"context"
"fmt"
"strings"
uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/strutil"
"github.com/hashicorp/vault/sdk/logical"
"github.com/splunk/vault-plugin-splunk/clients/splunk"
)
func (b *backend) pathCredsCreate() *framework.Path {
return &framework.Path{
Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the role",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.credsReadHandler,
},
HelpSynopsis: pathCredsCreateHelpSyn,
HelpDescription: pathCredsCreateHelpDesc,
}
}
func (b *backend) pathCredsCreateMulti() *framework.Path {
return &framework.Path{
Pattern: "creds/" + framework.GenericNameRegex("name") + "/" + framework.GenericNameRegex("node_fqdn"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the role",
},
"node_fqdn": {
Type: framework.TypeString,
Description: "FQDN for the Splunk Stack node",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.credsReadHandler,
},
HelpSynopsis: pathCredsCreateHelpSyn,
HelpDescription: pathCredsCreateHelpDesc,
}
}
func (b *backend) credsReadHandlerStandalone(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 logical.ErrorResponse(fmt.Sprintf("role not found: %q", name)), nil
}
config, err := connectionConfigLoad(ctx, req.Storage, role.Connection)
if err != nil {
return nil, err
}
// If role name isn't in allowed roles, send back a permission denied.
if !strutil.StrListContains(config.AllowedRoles, "*") && !strutil.StrListContainsGlob(config.AllowedRoles, name) {
return nil, fmt.Errorf("%q is not an allowed role for connection %q", name, role.Connection)
}
conn, err := b.ensureConnection(ctx, config)
if err != nil {
return nil, err
}
// Generate credentials
userUUID, err := generateUserID(role)
if err != nil {
return nil, err
}
userPrefix := role.UserPrefix
if role.UserPrefix == defaultUserPrefix {
userPrefix = fmt.Sprintf("%s_%s", role.UserPrefix, req.DisplayName)
}
username := fmt.Sprintf("%s_%s", userPrefix, userUUID)
passwd, err := generateUserPassword(role)
if err != nil {
return nil, fmt.Errorf("error generating new password %w", err)
}
opts := splunk.CreateUserOptions{
Name: username,
Password: passwd,
Roles: role.Roles,
DefaultApp: role.DefaultApp,
Email: role.Email,
TZ: role.TZ,
}
if _, _, err := conn.AccessControl.Authentication.Users.Create(&opts); err != nil {
return nil, err
}
resp := b.Secret(secretCredsType).Response(map[string]interface{}{
// return to user
"username": username,
"password": passwd,
"roles": role.Roles,
"connection": role.Connection,
"url": conn.Params().BaseURL,
}, map[string]interface{}{
// store (with lease)
"username": username,
"role": name,
"connection": role.Connection,
"url": conn.Params().BaseURL, // new in v0.7.0
})
resp.Secret.TTL = role.DefaultTTL
resp.Secret.MaxTTL = role.MaxTTL
return resp, nil
}
func findNode(nodeFQDN string, hosts []splunk.ServerInfoEntry, roleConfig *roleConfig) (*splunk.ServerInfoEntry, error) {
for _, host := range hosts {
// check if node_fqdn is in either of HostFQDN or Host. User might not always the FQDN on the cli input
if strings.EqualFold(host.Content.HostFQDN, nodeFQDN) || strings.EqualFold(host.Content.Host, nodeFQDN) {
// Return host if the requested node type is allowed
if strutil.StrListContains(roleConfig.AllowedServerRoles, "*") {
return &host, nil
}
for _, role := range host.Content.Roles {
if strutil.StrListContainsGlob(roleConfig.AllowedServerRoles, role) {
return &host, nil
}
}
return nil, fmt.Errorf("host %q does not have any of the allowed server roles: %q", nodeFQDN, roleConfig.AllowedServerRoles)
}
}
return nil, fmt.Errorf("host %q not found", nodeFQDN)
}
func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
node, _ := d.GetOk("node_fqdn")
nodeFQDN := node.(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
return logical.ErrorResponse(fmt.Sprintf("role not found: %q", name)), nil
}
config, err := connectionConfigLoad(ctx, req.Storage, role.Connection)
if err != nil {
return nil, err
}
// Check if isStandalone is set
if config.IsStandalone {
return logical.ErrorResponse("expected is_standalone to be unset for connection: %q", role.Connection), nil
}
// If role name isn't in allowed roles, send back a permission denied.
if !strutil.StrListContains(config.AllowedRoles, "*") && !strutil.StrListContainsGlob(config.AllowedRoles, name) {
return logical.ErrorResponse("%q is not an allowed role for connection %q", name, role.Connection), nil
}
conn, err := b.ensureConnection(ctx, config)
if err != nil {
return nil, err
}
nodes, _, err := conn.Deployment.SearchPeers(splunk.ServerInfoEntryFilterMinimal)
if err != nil {
b.Logger().Error("Error while reading SearchPeers from cluster master", "err", err)
return nil, fmt.Errorf("unable to read searchpeers from cluster master: %w", err)
}
foundNode, err := findNode(nodeFQDN, nodes, role)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
if foundNode.Content.Host == "" {
return nil, fmt.Errorf("host field unexpectedly empty for %q", nodeFQDN)
}
nodeFQDN = foundNode.Content.Host // the actual FQDN as returned by the cluster master, confusingly
// Re-create connection for node
conn, err = b.ensureNodeConnection(ctx, config, nodeFQDN)
if err != nil {
return nil, err
}
// Generate credentials
userUUID, err := generateUserID(role)
if err != nil {
return nil, err
}
userPrefix := role.UserPrefix
if role.UserPrefix == defaultUserPrefix {
userPrefix = fmt.Sprintf("%s_%s", role.UserPrefix, req.DisplayName)
}
username := fmt.Sprintf("%s_%s", userPrefix, userUUID)
passwd, err := generateUserPassword(role)
if err != nil {
return nil, fmt.Errorf("error generating new password: %w", err)
}
opts := splunk.CreateUserOptions{
Name: username,
Password: passwd,
Roles: role.Roles,
DefaultApp: role.DefaultApp,
Email: role.Email,
TZ: role.TZ,
}
if _, _, err := conn.AccessControl.Authentication.Users.Create(&opts); err != nil {
return nil, err
}
resp := b.Secret(secretCredsType).Response(map[string]interface{}{
// return to user
"username": username,
"password": passwd,
"roles": role.Roles,
"connection": role.Connection,
"url": conn.Params().BaseURL,
}, map[string]interface{}{
// store (with lease)
"username": username,
"role": name,
"connection": role.Connection,
"node_fqdn": nodeFQDN,
"url": conn.Params().BaseURL, // new in v0.7.0
})
resp.Secret.TTL = role.DefaultTTL
resp.Secret.MaxTTL = role.MaxTTL
return resp, nil
}
func (b *backend) credsReadHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
node_fqdn, present := d.GetOk("node_fqdn")
// if node_fqdn is specified then the treat the request for a multi-node deployment
if present {
b.Logger().Debug("node_fqdn specified for role. using clustered mode for getting temporary creds", "nodeFQDN", node_fqdn.(string), "role", name)
return b.credsReadHandlerMulti(ctx, req, d)
}
b.Logger().Debug("node_fqdn not specified for role. using standalone mode for getting temporary creds", "role", name)
return b.credsReadHandlerStandalone(ctx, req, d)
}
func generateUserID(roleConfig *roleConfig) (string, error) {
switch roleConfig.UserIDScheme {
case userIDSchemeUUID4_v0_5_0:
fallthrough
case userIDSchemeUUID4:
return uuid.GenerateUUID()
case userIDSchemeBase58_64:
return GenerateShortUUID(8)
case userIDSchemeBase58_128:
return GenerateShortUUID(16)
default:
return "", fmt.Errorf("invalid user_id_scheme: %q", roleConfig.UserIDScheme)
}
}
func generateUserPassword(roleConfig *roleConfig) (string, error) {
passwd, err := GeneratePassword(roleConfig.PasswordSpec)
if err == nil {
return passwd, nil
}
// fallback
return uuid.GenerateUUID()
}
// #nosec G101
const pathCredsCreateHelpSyn = `
Request Splunk credentials for a certain role.
`
const pathCredsCreateHelpDesc = `
This path reads Splunk credentials for a certain role. The credentials
will be generated on demand and will be automatically revoked when
their lease expires. Leases can be extended until a configured
maximum life-time.
`