forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_plugins_test.go
322 lines (298 loc) · 7.81 KB
/
sys_plugins_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
package api
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/strutil"
)
func TestRegisterPlugin(t *testing.T) {
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandlerRegister))
defer mockVaultServer.Close()
cfg := DefaultConfig()
cfg.Address = mockVaultServer.URL
client, err := NewClient(cfg)
if err != nil {
t.Fatal(err)
}
err = client.Sys().RegisterPluginWithContext(context.Background(), &RegisterPluginInput{
Version: "v1.0.0",
})
if err != nil {
t.Fatal(err)
}
}
func TestListPlugins(t *testing.T) {
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandlerList))
defer mockVaultServer.Close()
cfg := DefaultConfig()
cfg.Address = mockVaultServer.URL
client, err := NewClient(cfg)
if err != nil {
t.Fatal(err)
}
for name, tc := range map[string]struct {
input ListPluginsInput
expectedPlugins map[consts.PluginType][]string
}{
"no type specified": {
input: ListPluginsInput{},
expectedPlugins: map[consts.PluginType][]string{
consts.PluginTypeCredential: {"alicloud"},
consts.PluginTypeDatabase: {"cassandra-database-plugin"},
consts.PluginTypeSecrets: {"ad", "alicloud"},
},
},
"only auth plugins": {
input: ListPluginsInput{Type: consts.PluginTypeCredential},
expectedPlugins: map[consts.PluginType][]string{
consts.PluginTypeCredential: {"alicloud"},
},
},
"only database plugins": {
input: ListPluginsInput{Type: consts.PluginTypeDatabase},
expectedPlugins: map[consts.PluginType][]string{
consts.PluginTypeDatabase: {"cassandra-database-plugin"},
},
},
"only secret plugins": {
input: ListPluginsInput{Type: consts.PluginTypeSecrets},
expectedPlugins: map[consts.PluginType][]string{
consts.PluginTypeSecrets: {"ad", "alicloud"},
},
},
} {
t.Run(name, func(t *testing.T) {
resp, err := client.Sys().ListPluginsWithContext(context.Background(), &tc.input)
if err != nil {
t.Fatal(err)
}
for pluginType, expected := range tc.expectedPlugins {
actualPlugins := resp.PluginsByType[pluginType]
if len(expected) != len(actualPlugins) {
t.Fatal("Wrong number of plugins", expected, actualPlugins)
}
for i := range actualPlugins {
if expected[i] != actualPlugins[i] {
t.Fatalf("Expected %q but got %q", expected[i], actualPlugins[i])
}
}
for _, expectedPlugin := range expected {
found := false
for _, plugin := range resp.Details {
if plugin.Type == pluginType.String() && plugin.Name == expectedPlugin {
found = true
break
}
}
if !found {
t.Errorf("Expected to find %s plugin %s but not found in details: %#v", pluginType.String(), expectedPlugin, resp.Details)
}
}
}
for _, actual := range resp.Details {
pluginType, err := consts.ParsePluginType(actual.Type)
if err != nil {
t.Fatal(err)
}
if !strutil.StrListContains(tc.expectedPlugins[pluginType], actual.Name) {
t.Errorf("Did not expect to find %s in details", actual.Name)
}
}
})
}
}
func TestGetPlugin(t *testing.T) {
for name, tc := range map[string]struct {
version string
body string
expected GetPluginResponse
}{
"builtin": {
body: getResponse,
expected: GetPluginResponse{
Args: nil,
Builtin: true,
Command: "",
Name: "azure",
SHA256: "",
DeprecationStatus: "supported",
Version: "v0.14.0+builtin",
},
},
"external": {
version: "v1.0.0",
body: getResponseExternal,
expected: GetPluginResponse{
Args: []string{},
Builtin: false,
Command: "azure-plugin",
Name: "azure",
SHA256: "8ba442dba253803685b05e35ad29dcdebc48dec16774614aa7a4ebe53c1e90e1",
DeprecationStatus: "",
Version: "v1.0.0",
},
},
"old server": {
body: getResponseOldServerVersion,
expected: GetPluginResponse{
Args: nil,
Builtin: true,
Command: "",
Name: "azure",
SHA256: "",
DeprecationStatus: "",
Version: "",
},
},
} {
t.Run(name, func(t *testing.T) {
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultHandlerInfo(tc.body)))
defer mockVaultServer.Close()
cfg := DefaultConfig()
cfg.Address = mockVaultServer.URL
client, err := NewClient(cfg)
if err != nil {
t.Fatal(err)
}
input := GetPluginInput{
Name: "azure",
Type: consts.PluginTypeSecrets,
}
if tc.version != "" {
input.Version = tc.version
}
info, err := client.Sys().GetPluginWithContext(context.Background(), &input)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tc.expected, *info) {
t.Errorf("expected: %#v\ngot: %#v", tc.expected, info)
}
})
}
}
func mockVaultHandlerInfo(body string) func(w http.ResponseWriter, _ *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(body))
}
}
const getResponse = `{
"request_id": "e93d3f93-8e4f-8443-a803-f1c97c495241",
"lease_id": "",
"renewable": false,
"lease_duration": 0,
"data": {
"args": null,
"builtin": true,
"command": "",
"deprecation_status": "supported",
"name": "azure",
"sha256": "",
"version": "v0.14.0+builtin"
},
"wrap_info": null,
"warnings": null,
"auth": null
}`
const getResponseExternal = `{
"request_id": "e93d3f93-8e4f-8443-a803-f1c97c495241",
"lease_id": "",
"renewable": false,
"lease_duration": 0,
"data": {
"args": [],
"builtin": false,
"command": "azure-plugin",
"name": "azure",
"sha256": "8ba442dba253803685b05e35ad29dcdebc48dec16774614aa7a4ebe53c1e90e1",
"version": "v1.0.0"
},
"wrap_info": null,
"warnings": null,
"auth": null
}`
const getResponseOldServerVersion = `{
"request_id": "e93d3f93-8e4f-8443-a803-f1c97c495241",
"lease_id": "",
"renewable": false,
"lease_duration": 0,
"data": {
"args": null,
"builtin": true,
"command": "",
"name": "azure",
"sha256": ""
},
"wrap_info": null,
"warnings": null,
"auth": null
}`
func mockVaultHandlerList(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(listUntypedResponse))
}
const listUntypedResponse = `{
"request_id": "82601a91-cd7a-718f-feca-f573449cc1bb",
"lease_id": "",
"renewable": false,
"lease_duration": 0,
"data": {
"auth": [
"alicloud"
],
"database": [
"cassandra-database-plugin"
],
"secret": [
"ad",
"alicloud"
],
"some_other_unexpected_key": [
{
"objectKey": "objectValue"
},
{
"arbitraryData": 7
}
],
"detailed": [
{
"type": "auth",
"name": "alicloud",
"version": "v0.13.0+builtin",
"builtin": true,
"deprecation_status": "supported"
},
{
"type": "database",
"name": "cassandra-database-plugin",
"version": "v1.13.0+builtin.vault",
"builtin": true,
"deprecation_status": "supported"
},
{
"type": "secret",
"name": "ad",
"version": "v0.14.0+builtin",
"builtin": true,
"deprecation_status": "supported"
},
{
"type": "secret",
"name": "alicloud",
"version": "v0.13.0+builtin",
"builtin": true,
"deprecation_status": "supported"
}
]
},
"wrap_info": null,
"warnings": null,
"auth": null
}`
func mockVaultHandlerRegister(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(registerResponse))
}
const registerResponse = `{}`