@@ -3,19 +3,15 @@ package splunk
3
3
import (
4
4
"context"
5
5
"fmt"
6
+
6
7
"github.com/hashicorp/errwrap"
7
- "github.com/hashicorp/go-uuid"
8
+ uuid "github.com/hashicorp/go-uuid"
8
9
"github.com/hashicorp/vault/helper/strutil"
9
10
"github.com/hashicorp/vault/logical"
10
11
"github.com/hashicorp/vault/logical/framework"
11
12
"github.com/splunk/vault-plugin-splunk/clients/splunk"
12
13
)
13
14
14
- const (
15
- SEARCHHEAD = "search_head"
16
- INDEXER = "indexer"
17
- )
18
-
19
15
func (b * backend ) pathCredsCreate () * framework.Path {
20
16
return & framework.Path {
21
17
Pattern : "creds/" + framework .GenericNameRegex ("name" ),
@@ -84,7 +80,7 @@ func (b *backend) credsReadHandlerStandalone(ctx context.Context, req *logical.R
84
80
}
85
81
86
82
// Generate credentials
87
- userUUID , err := uuid . GenerateUUID ( )
83
+ userUUID , err := generateUserID ( role )
88
84
if err != nil {
89
85
return nil , err
90
86
}
@@ -93,7 +89,7 @@ func (b *backend) credsReadHandlerStandalone(ctx context.Context, req *logical.R
93
89
userPrefix = fmt .Sprintf ("%s_%s" , role .UserPrefix , req .DisplayName )
94
90
}
95
91
username := fmt .Sprintf ("%s_%s" , userPrefix , userUUID )
96
- passwd , err := uuid . GenerateUUID ( )
92
+ passwd , err := generateUserPassword ( role )
97
93
if err != nil {
98
94
return nil , errwrap .Wrapf ("error generating new password {{err}}" , err )
99
95
}
@@ -128,20 +124,23 @@ func (b *backend) credsReadHandlerStandalone(ctx context.Context, req *logical.R
128
124
return resp , nil
129
125
}
130
126
131
- func findNode (nodeFQDN string , hosts []splunk.ServerInfoEntry ) (bool , error ) {
127
+ func findNode (nodeFQDN string , hosts []splunk.ServerInfoEntry , roleConfig * roleConfig ) (bool , error ) {
132
128
for _ , host := range hosts {
133
129
// check if node_fqdn is in either of HostFQDN or Host. User might not always the FQDN on the cli input
134
130
if host .Content .HostFQDN == nodeFQDN || host .Content .Host == nodeFQDN {
135
- // Return true if the requested node is a search head
131
+ // Return true if the requested node type is allowed
132
+ if strutil .StrListContains (roleConfig .AllowedNodeTypes , "*" ) {
133
+ return true , nil
134
+ }
136
135
for _ , role := range host .Content .Roles {
137
- if role == SEARCHHEAD {
136
+ if strutil . StrListContainsGlob ( roleConfig . AllowedNodeTypes , role ) {
138
137
return true , nil
139
138
}
140
139
}
141
- return false , fmt .Errorf ("host: %s isn't search head; creating ephemeral creds is only supported for search heads " , nodeFQDN )
140
+ return false , fmt .Errorf ("host %q does not have an allowed node type " , nodeFQDN )
142
141
}
143
142
}
144
- return false , fmt .Errorf ("host: %s not found" , nodeFQDN )
143
+ return false , fmt .Errorf ("host %q not found" , nodeFQDN )
145
144
}
146
145
147
146
func (b * backend ) credsReadHandlerMulti (ctx context.Context , req * logical.Request , d * framework.FieldData ) (* logical.Response , error ) {
@@ -180,7 +179,7 @@ func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Reques
180
179
b .Logger ().Error ("Error while reading SearchPeers from cluster master" , err )
181
180
return nil , errwrap .Wrapf ("unable to read searchpeers from cluster master: {{err}}" , err )
182
181
}
183
- _ , err = findNode (nodeFQDN , nodes )
182
+ _ , err = findNode (nodeFQDN , nodes , role )
184
183
if err != nil {
185
184
return nil , err
186
185
}
@@ -193,7 +192,7 @@ func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Reques
193
192
return nil , err
194
193
}
195
194
// Generate credentials
196
- userUUID , err := uuid . GenerateUUID ( )
195
+ userUUID , err := generateUserID ( role )
197
196
if err != nil {
198
197
return nil , err
199
198
}
@@ -202,11 +201,10 @@ func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Reques
202
201
userPrefix = fmt .Sprintf ("%s_%s" , role .UserPrefix , req .DisplayName )
203
202
}
204
203
username := fmt .Sprintf ("%s_%s" , userPrefix , userUUID )
205
- passwd , err := uuid . GenerateUUID ( )
204
+ passwd , err := generateUserPassword ( role )
206
205
if err != nil {
207
206
return nil , errwrap .Wrapf ("error generating new password: {{err}}" , err )
208
207
}
209
- conn .Params ().BaseURL = nodeFQDN
210
208
opts := splunk.CreateUserOptions {
211
209
Name : username ,
212
210
Password : passwd ,
@@ -251,6 +249,19 @@ func (b *backend) credsReadHandler(ctx context.Context, req *logical.Request, d
251
249
return b .credsReadHandlerStandalone (ctx , req , d )
252
250
}
253
251
252
+ func generateUserID (roleConfig * roleConfig ) (string , error ) {
253
+ return uuid .GenerateUUID ()
254
+ }
255
+
256
+ func generateUserPassword (roleConfig * roleConfig ) (string , error ) {
257
+ passwd , err := GeneratePassword (roleConfig .PasswordSpec )
258
+ if err == nil {
259
+ return passwd , nil
260
+ }
261
+ // fallback
262
+ return uuid .GenerateUUID ()
263
+ }
264
+
254
265
const pathCredsCreateHelpSyn = `
255
266
Request Splunk credentials for a certain role.
256
267
`
0 commit comments