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 pathbackend_test.go
349 lines (317 loc) · 9.04 KB
/
backend_test.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package splunk
import (
"context"
"fmt"
"testing"
"time"
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
"gotest.tools/v3/assert"
"github.com/splunk/vault-plugin-splunk/clients/splunk"
)
func TestBackend_basic(t *testing.T) {
b, err := testNewSplunkBackend(t)
if err != nil {
t.Fatal(err)
}
schemes := []string{
userIDSchemeUUID4_v0_5_0,
userIDSchemeUUID4,
userIDSchemeBase58_64,
userIDSchemeBase58_128,
}
for _, scheme := range schemes {
roleConfig := roleConfig{
Connection: "testconn",
Roles: []string{"admin"},
UserPrefix: defaultUserPrefix,
UserIDScheme: scheme,
}
logicaltest.Test(t, logicaltest.TestCase{
LogicalBackend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
testAccStepRole(t, "test", roleConfig),
testAccStepCredsRead(t, "test"),
testAccStepCredsReadMultiBadConfig(t, "test"),
},
})
}
}
func TestBackend_RotateRoot(t *testing.T) {
b, err := testNewSplunkBackend(t)
if err != nil {
t.Fatal(err)
}
logicaltest.Test(t, logicaltest.TestCase{
LogicalBackend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
testAccRotateRoot(t, "testconn"),
// and again, to check if we can still login
testAccRotateRoot(t, "testconn"),
},
})
}
func TestBackend_ConnectionCRUD(t *testing.T) {
b, err := testNewSplunkBackend(t)
if err != nil {
t.Fatal(err)
}
connConfig := splunkConfig{
Username: splunk.TestGlobalSplunkClient(t).Params().Config.ClientID,
URL: splunk.TestGlobalSplunkClient(t).Params().BaseURL,
AllowedRoles: []string{"*"},
Verify: true,
InsecureTLS: true,
CAChain: []string{},
RootCA: []string{},
ConnectTimeout: time.Duration(30) * time.Second,
}
logicaltest.Test(t, logicaltest.TestCase{
LogicalBackend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
testAccStepConnectionRead(t, "testconn", connConfig),
testAccStepConnectionDelete(t, "testconn"),
},
})
}
func TestBackend_RoleCRUD(t *testing.T) {
b, err := testNewSplunkBackend(t)
if err != nil {
t.Fatal(err)
}
testRoleConfig := roleConfig{
Connection: "testconn",
Roles: []string{"admin"},
AllowedServerRoles: []string{"*"},
PasswordSpec: DefaultPasswordSpec(),
UserPrefix: "my-custom-prefix",
UserIDScheme: userIDSchemeUUID4,
}
logicaltest.Test(t, logicaltest.TestCase{
LogicalBackend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
testAccStepRole(t, "test", testRoleConfig),
testAccStepRoleMissingRoleName(t),
testAccStepRoleMissingRoles(t, "MISSING"),
testAccStepRoleRead(t, "test", testRoleConfig),
testAccStepRoleDelete(t, "test"),
},
})
emptyUserPrefixConfig := testRoleConfig
emptyUserPrefixConfig.UserPrefix = ""
logicaltest.Test(t, logicaltest.TestCase{
LogicalBackend: b,
Steps: []logicaltest.TestStep{
testEmptyUserPrefix(t, "test", emptyUserPrefixConfig),
},
})
userIDSchemeConfig := testRoleConfig
userIDSchemeConfig.UserIDScheme = "-invalid-"
logicaltest.Test(t, logicaltest.TestCase{
LogicalBackend: b,
Steps: []logicaltest.TestStep{
testUserIDScheme(t, "test", "-invalid-", userIDSchemeConfig),
},
})
}
// Test steps
// Connection
func testAccStepConfig(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.UpdateOperation,
Path: "config/testconn",
Data: map[string]interface{}{
"url": splunk.TestGlobalSplunkClient(t).Params().BaseURL,
"username": splunk.TestGlobalSplunkClient(t).Params().Config.ClientID,
"password": splunk.TestGlobalSplunkClient(t).Params().Config.ClientSecret,
"allowed_roles": "*",
"insecure_tls": true,
},
}
}
func testAccStepConnectionRead(t *testing.T, conn string, config splunkConfig) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "config/" + conn,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
expected := config.toResponseData()
expected["id"] = resp.Data["id"].(string)
assert.DeepEqual(t, expected, resp.Data)
return nil
},
}
}
func testAccStepConnectionDelete(t *testing.T, conn string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.DeleteOperation,
Path: "config/" + conn,
}
}
// Role
func testAccStepRole(t *testing.T, role string, config roleConfig) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.UpdateOperation,
Path: rolesPrefix + role,
Data: config.toResponseData(),
}
}
func testAccStepRoleMissingRoles(t *testing.T, role string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.UpdateOperation,
Path: rolesPrefix + role,
Data: map[string]interface{}{
"connection": "testconn",
},
ErrorOk: true,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
assert.Error(t, resp.Error(), "roles cannot be empty")
return nil
},
}
}
func testAccStepRoleMissingRoleName(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.CreateOperation,
Path: rolesPrefix,
Data: map[string]interface{}{
"connection": "testconn",
},
ErrorOk: true,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
assert.Error(t, resp.Error(), "cannot write to a path ending in '/'")
return nil
},
}
}
func testEmptyUserPrefix(t *testing.T, role string, config roleConfig) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.CreateOperation,
Path: rolesPrefix + role,
Data: config.toResponseData(),
ErrorOk: true,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
assert.Error(t, resp.Error(), "user_prefix can't be set to empty string")
return nil
},
}
}
func testUserIDScheme(t *testing.T, role, idScheme string, config roleConfig) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.CreateOperation,
Path: rolesPrefix + role,
Data: config.toResponseData(),
ErrorOk: true,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
assert.Error(t, resp.Error(), fmt.Sprintf("invalid user_id_scheme: %q", idScheme))
return nil
},
}
}
func testAccStepCredsRead(t *testing.T, role string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "creds/" + role,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
var d struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
URL string `mapstructure:"url"`
}
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
// check that generated user can login
conn := splunk.NewTestSplunkClient(d.URL, d.Username, d.Password)
_, _, err := conn.Introspection.ServerInfo()
assert.NilError(t, err)
// XXXX check that generated user is deleted if lease expires
return nil
},
}
}
func testAccStepCredsReadMultiBadConfig(t *testing.T, role string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "creds/" + role + "/someNonExistentNodeID",
ErrorOk: true,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
assert.Error(t, resp.Error(), `host "someNonExistentNodeID" not found`)
return nil
},
}
}
func testAccRotateRoot(t *testing.T, conn string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.UpdateOperation,
Path: "rotate-root/" + conn,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
var d struct {
Username string `mapstructure:"username"`
}
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
assert.Assert(t, d.Username != "")
return nil
},
}
}
func testAccStepRoleRead(t *testing.T, role string, config roleConfig) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: rolesPrefix + role,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
expected := config.toResponseData()
assert.DeepEqual(t, expected, resp.Data)
return nil
},
}
}
func testAccStepRoleDelete(t *testing.T, role string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.DeleteOperation,
Path: rolesPrefix + role,
}
}
// Helpers
func testNewSplunkBackend(t *testing.T) (logical.Backend, error) {
t.Helper()
if splunk.TestGlobalSplunkClient(t) == nil {
t.SkipNow()
}
config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
return Factory(context.Background(), config)
}