-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathregistry_test.go
349 lines (320 loc) · 8.96 KB
/
registry_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package builtinplugins
import (
"bufio"
"fmt"
"os"
"reflect"
"regexp"
"testing"
credUserpass "github.com/hashicorp/vault/builtin/credential/userpass"
"github.com/hashicorp/vault/helper/constants"
dbMysql "github.com/hashicorp/vault/plugins/database/mysql"
"github.com/hashicorp/vault/sdk/helper/consts"
"golang.org/x/exp/slices"
)
// Test_RegistryGet exercises the (registry).Get functionality by comparing
// factory types and ok response.
func Test_RegistryGet(t *testing.T) {
tests := []struct {
name string
builtin string
pluginType consts.PluginType
want BuiltinFactory
wantOk bool
}{
{
name: "non-existent builtin",
builtin: "foo",
pluginType: consts.PluginTypeCredential,
want: nil,
wantOk: false,
},
{
name: "bad plugin type",
builtin: "app-id",
pluginType: 9000,
want: nil,
wantOk: false,
},
{
name: "known builtin lookup",
builtin: "userpass",
pluginType: consts.PluginTypeCredential,
want: toFunc(credUserpass.Factory),
wantOk: true,
},
{
name: "removed builtin lookup",
builtin: "app-id",
pluginType: consts.PluginTypeCredential,
want: nil,
wantOk: true,
},
{
name: "known builtin lookup",
builtin: "mysql-database-plugin",
pluginType: consts.PluginTypeDatabase,
want: dbMysql.New(dbMysql.DefaultUserNameTemplate),
wantOk: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got BuiltinFactory
got, ok := Registry.Get(tt.builtin, tt.pluginType)
if ok {
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
t.Fatalf("got type: %T, want type: %T", got, tt.want)
}
}
if tt.wantOk != ok {
t.Fatalf("error: got %v, want %v", ok, tt.wantOk)
}
})
}
}
// Test_RegistryKeyCounts is a light unit test used to check the builtin
// registry lists for each plugin type and make sure they match in length.
func Test_RegistryKeyCounts(t *testing.T) {
tests := []struct {
name string
pluginType consts.PluginType
want int // use slice length as test condition
entWant int
wantOk bool
}{
{
name: "bad plugin type",
pluginType: 9001,
want: 0,
},
{
name: "number of auth plugins",
pluginType: consts.PluginTypeCredential,
want: 18,
entWant: 1,
},
{
name: "number of database plugins",
pluginType: consts.PluginTypeDatabase,
want: 17,
},
{
name: "number of secrets plugins",
pluginType: consts.PluginTypeSecrets,
want: 19,
entWant: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
keys := Registry.Keys(tt.pluginType)
want := tt.want
if constants.IsEnterprise {
want += tt.entWant
}
if len(keys) != want {
t.Fatalf("got size: %d, want size: %d", len(keys), want)
}
})
}
}
// Test_RegistryContains exercises the (registry).Contains functionality.
func Test_RegistryContains(t *testing.T) {
tests := []struct {
name string
builtin string
pluginType consts.PluginType
want bool
}{
{
name: "non-existent builtin",
builtin: "foo",
pluginType: consts.PluginTypeCredential,
want: false,
},
{
name: "bad plugin type",
builtin: "app-id",
pluginType: 9001,
want: false,
},
{
name: "known builtin lookup",
builtin: "approle",
pluginType: consts.PluginTypeCredential,
want: true,
},
{
name: "removed builtin lookup",
builtin: "app-id",
pluginType: consts.PluginTypeCredential,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Registry.Contains(tt.builtin, tt.pluginType)
if got != tt.want {
t.Fatalf("error: got %v, wanted %v", got, tt.want)
}
})
}
}
// Test_RegistryStatus exercises the (registry).Status functionality.
func Test_RegistryStatus(t *testing.T) {
tests := []struct {
name string
builtin string
pluginType consts.PluginType
want consts.DeprecationStatus
wantOk bool
}{
{
name: "non-existent builtin and valid type",
builtin: "foo",
pluginType: consts.PluginTypeCredential,
want: consts.Unknown,
wantOk: false,
},
{
name: "mismatch builtin and plugin type",
builtin: "app-id",
pluginType: consts.PluginTypeSecrets,
want: consts.Unknown,
wantOk: false,
},
{
name: "existing builtin and invalid plugin type",
builtin: "app-id",
pluginType: 9000,
want: consts.Unknown,
wantOk: false,
},
{
name: "supported builtin lookup",
builtin: "approle",
pluginType: consts.PluginTypeCredential,
want: consts.Supported,
wantOk: true,
},
{
name: "deprecated builtin lookup",
builtin: "pcf",
pluginType: consts.PluginTypeCredential,
want: consts.Deprecated,
wantOk: true,
},
{
name: "removed builtin lookup",
builtin: "app-id",
pluginType: consts.PluginTypeCredential,
want: consts.Removed,
wantOk: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := Registry.DeprecationStatus(tt.builtin, tt.pluginType)
if got != tt.want {
t.Fatalf("got %+v, wanted %+v", got, tt.want)
}
if ok != tt.wantOk {
t.Fatalf("got ok: %t, want ok: %t", ok, tt.wantOk)
}
})
}
}
// Test_RegistryMatchesGenOpenapi ensures that the plugins mounted in gen_openapi.sh match registry.go
func Test_RegistryMatchesGenOpenapi(t *testing.T) {
const scriptPath = "../../scripts/gen_openapi.sh"
// parseScript fetches the contents of gen_openapi.sh script & extract the relevant lines
parseScript := func(path string) ([]string, []string, error) {
f, err := os.Open(scriptPath)
if err != nil {
return nil, nil, fmt.Errorf("could not open gen_openapi.sh script: %w", err)
}
defer f.Close()
// This is a hack: the gen_openapi script contains a conditional block to
// enable the enterprise plugins, whose lines are indented. Tweak the
// regexp to only include the indented lines on enterprise.
leading := "^"
if constants.IsEnterprise {
leading = "^ *"
}
var (
credentialBackends []string
credentialBackendsRe = regexp.MustCompile(leading + `vault auth enable (?:-.+ )*(?:"([a-zA-Z]+)"|([a-zA-Z]+))$`)
secretsBackends []string
secretsBackendsRe = regexp.MustCompile(leading + `vault secrets enable (?:-.+ )*(?:"([a-zA-Z]+)"|([a-zA-Z]+))$`)
)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if m := credentialBackendsRe.FindStringSubmatch(line); m != nil {
credentialBackends = append(credentialBackends, m[1])
}
if m := secretsBackendsRe.FindStringSubmatch(line); m != nil {
secretsBackends = append(secretsBackends, m[1])
}
}
if err := scanner.Err(); err != nil {
return nil, nil, fmt.Errorf("error scanning gen_openapi.sh: %v", err)
}
return credentialBackends, secretsBackends, nil
}
// ensureInRegistry ensures that the given plugin is in registry and marked as "supported"
ensureInRegistry := func(t *testing.T, name string, pluginType consts.PluginType) {
t.Helper()
// "database" will not be present in registry, it is represented as
// a list of database plugins instead
if name == "database" && pluginType == consts.PluginTypeSecrets {
return
}
deprecationStatus, ok := Registry.DeprecationStatus(name, pluginType)
if !ok {
t.Errorf("%q %s backend is missing from registry.go; please remove it from gen_openapi.sh", name, pluginType)
}
if deprecationStatus == consts.Removed {
t.Errorf("%q %s backend is marked 'removed' in registry.go; please remove it from gen_openapi.sh", name, pluginType)
}
}
// ensureInScript ensures that the given plugin name is in gen_openapi.sh script
ensureInScript := func(t *testing.T, scriptBackends []string, name string) {
t.Helper()
for _, excluded := range []string{
"oidc", // alias for "jwt"
"openldap", // alias for "ldap"
} {
if name == excluded {
return
}
}
if !slices.Contains(scriptBackends, name) {
t.Errorf("%q backend could not be found in gen_openapi.sh, please add it there", name)
}
}
// test starts here
scriptCredentialBackends, scriptSecretsBackends, err := parseScript(scriptPath)
if err != nil {
t.Fatal(err)
}
for _, name := range scriptCredentialBackends {
ensureInRegistry(t, name, consts.PluginTypeCredential)
}
for _, name := range scriptSecretsBackends {
ensureInRegistry(t, name, consts.PluginTypeSecrets)
}
for name, backend := range Registry.credentialBackends {
if backend.DeprecationStatus == consts.Supported {
ensureInScript(t, scriptCredentialBackends, name)
}
}
for name, backend := range Registry.logicalBackends {
if backend.DeprecationStatus == consts.Supported {
ensureInScript(t, scriptSecretsBackends, name)
}
}
}