-
Notifications
You must be signed in to change notification settings - Fork 19
/
path_config_test.go
298 lines (262 loc) · 8.93 KB
/
path_config_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azuresecrets
import (
"context"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
)
func TestConfig(t *testing.T) {
tests := []struct {
name string
config map[string]interface{}
expected map[string]interface{}
wantErr bool
}{
{
name: "root_password_ttl defaults to 6 months",
config: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"client_secret": "testClientSecret",
},
expected: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"environment": "",
"root_password_ttl": 15768000,
"identity_token_ttl": int64(0),
"identity_token_audience": "",
},
},
{
name: "root_password_ttl set if provided",
config: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"client_secret": "testClientSecret",
"root_password_ttl": "1m",
},
expected: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"environment": "",
"root_password_ttl": 60,
"identity_token_ttl": int64(0),
"identity_token_audience": "",
},
},
{
name: "environment set if provided",
config: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"client_secret": "testClientSecret",
"environment": "AZURECHINACLOUD",
},
expected: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"root_password_ttl": 15768000,
"environment": "AZURECHINACLOUD",
"identity_token_ttl": int64(0),
"identity_token_audience": "",
},
},
{
name: "client_secret and identity_token_audience are mutually exclusive",
config: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"client_secret": "testClientSecret",
"environment": "AZURECHINACLOUD",
"identity_token_audience": "vault-azure-secrets-d0f0d253",
},
expected: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"root_password_ttl": 15768000,
"environment": "AZURECHINACLOUD",
"identity_token_ttl": int64(0),
"identity_token_audience": "vault-azure-secrets-d0f0d253",
},
wantErr: true,
},
{
name: "wif happy path",
config: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"identity_token_ttl": int64(500),
"identity_token_audience": "vault-azure-secrets-d0f0d253",
},
expected: map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "",
"root_password_ttl": 15768000,
"environment": "",
"identity_token_ttl": int64(500),
"identity_token_audience": "vault-azure-secrets-d0f0d253",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
b, s := getTestBackendMocked(t, false)
testConfigCreate(t, b, s, tc.config, tc.wantErr)
if !tc.wantErr {
testConfigRead(t, b, s, tc.expected)
// Test that updating one element retains the others
tc.expected["tenant_id"] = "800e371d-ee51-4145-9ac8-5c43e4ceb79b"
configSubset := map[string]interface{}{
"tenant_id": "800e371d-ee51-4145-9ac8-5c43e4ceb79b",
}
testConfigUpdate(t, b, s, configSubset, tc.wantErr)
testConfigRead(t, b, s, tc.expected)
}
})
}
}
func TestConfigEnvironmentClouds(t *testing.T) {
b, s := getTestBackendMocked(t, false)
config := map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"client_secret": "testClientSecret",
"environment": "AZURECHINACLOUD",
"root_password_ttl": int((24 * time.Hour).Seconds()),
}
testConfigCreate(t, b, s, config, false)
tests := []struct {
env string
url string
expError bool
}{
{"AZURECHINACLOUD", "https://microsoftgraph.chinacloudapi.cn", false},
{"AZUREPUBLICCLOUD", "https://graph.microsoft.com", false},
{"AZUREUSGOVERNMENTCLOUD", "https://graph.microsoft.us", false},
{"invalidEnv", "", true},
}
for _, test := range tests {
expectedConfig := map[string]interface{}{
"environment": test.env,
}
// Error is in the response, not in the error variable.
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config",
Data: expectedConfig,
Storage: s,
})
if resp.Error() == nil && test.expError {
t.Fatal("expected error, got none")
} else if err != nil && !test.expError {
t.Fatalf("expected no errors: %s", err)
}
if !test.expError {
config, err := b.getConfig(context.Background(), s)
if err != nil {
t.Fatal(err)
}
clientSettings, err := b.getClientSettings(context.Background(), config)
if err != nil {
t.Fatal(err)
}
if clientSettings.GraphURI != test.url {
t.Fatalf("expected url %s, got %s", test.url, clientSettings.GraphURI)
}
}
}
}
func TestConfigDelete(t *testing.T) {
b, s := getTestBackendMocked(t, false)
// Test valid config
config := map[string]interface{}{
"subscription_id": "a228ceec-bf1a-4411-9f95-39678d8cdb34",
"tenant_id": "7ac36e27-80fc-4209-a453-e8ad83dc18c2",
"client_id": "testClientId",
"client_secret": "testClientSecret",
"environment": "AZURECHINACLOUD",
"root_password_ttl": int((24 * time.Hour).Seconds()),
"identity_token_audience": "",
"identity_token_ttl": int64(0),
}
testConfigCreate(t, b, s, config, false)
delete(config, "client_secret")
testConfigRead(t, b, s, config)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: "config",
Storage: s,
})
assertErrorIsNil(t, err)
if resp != nil && resp.IsError() {
t.Fatal(resp.Error())
}
config = map[string]interface{}{
"subscription_id": "",
"tenant_id": "",
"client_id": "",
"environment": "",
"root_password_ttl": 0,
"identity_token_audience": "",
"identity_token_ttl": int64(0),
}
testConfigRead(t, b, s, config)
}
func testConfigCreate(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}, wantErr bool) {
t.Helper()
testConfigCreateUpdate(t, b, logical.CreateOperation, s, d, wantErr)
}
func testConfigUpdate(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}, wantErr bool) {
t.Helper()
testConfigCreateUpdate(t, b, logical.UpdateOperation, s, d, wantErr)
}
func testConfigCreateUpdate(t *testing.T, b logical.Backend, op logical.Operation, s logical.Storage, d map[string]interface{}, wantErr bool) {
t.Helper()
// save and restore the client since the config change will clear it
settings := b.(*azureSecretBackend).settings
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: op,
Path: "config",
Data: d,
Storage: s,
})
b.(*azureSecretBackend).settings = settings
if !wantErr && err != nil {
t.Fatal(err)
}
if !wantErr && resp != nil && resp.IsError() {
t.Fatal(resp.Error())
}
if wantErr {
assert.True(t, resp.IsError() || err != nil, "expected error, got nil")
}
}
func testConfigRead(t *testing.T, b logical.Backend, s logical.Storage, expected map[string]interface{}) {
t.Helper()
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "config",
Storage: s,
})
if err != nil {
t.Fatal(err)
}
if resp != nil && resp.IsError() {
t.Fatal(resp.Error())
}
equal(t, expected, resp.Data)
}