-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathconfig_test.go
433 lines (415 loc) · 13.5 KB
/
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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package httpjson
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2/google"
"github.com/elastic/beats/v7/libbeat/common"
)
func TestConfigValidationCase1(t *testing.T) {
m := map[string]interface{}{
"http_method": "GET",
"http_request_body": map[string]interface{}{"test": "abc"},
"no_http_body": true,
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. no_http_body and http_request_body cannot coexist.")
}
}
func TestConfigValidationCase2(t *testing.T) {
m := map[string]interface{}{
"http_method": "GET",
"no_http_body": true,
"pagination": map[string]interface{}{"extra_body_content": map[string]interface{}{"test": "abc"}},
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. no_http_body and pagination.extra_body_content cannot coexist.")
}
}
func TestConfigValidationCase3(t *testing.T) {
m := map[string]interface{}{
"http_method": "GET",
"no_http_body": true,
"pagination": map[string]interface{}{"req_field": "abc"},
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. no_http_body and pagination.req_field cannot coexist.")
}
}
func TestConfigValidationCase4(t *testing.T) {
m := map[string]interface{}{
"http_method": "GET",
"pagination": map[string]interface{}{"header": map[string]interface{}{"field_name": "Link", "regex_pattern": "<([^>]+)>; *rel=\"next\"(?:,|$)"}, "req_field": "abc"},
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. pagination.header and pagination.req_field cannot coexist.")
}
}
func TestConfigValidationCase5(t *testing.T) {
m := map[string]interface{}{
"http_method": "GET",
"pagination": map[string]interface{}{"header": map[string]interface{}{"field_name": "Link", "regex_pattern": "<([^>]+)>; *rel=\"next\"(?:,|$)"}, "id_field": "abc"},
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. pagination.header and pagination.id_field cannot coexist.")
}
}
func TestConfigValidationCase6(t *testing.T) {
m := map[string]interface{}{
"http_method": "GET",
"pagination": map[string]interface{}{"header": map[string]interface{}{"field_name": "Link", "regex_pattern": "<([^>]+)>; *rel=\"next\"(?:,|$)"}, "extra_body_content": map[string]interface{}{"test": "abc"}},
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. pagination.header and extra_body_content cannot coexist.")
}
}
func TestConfigValidationCase7(t *testing.T) {
m := map[string]interface{}{
"http_method": "DELETE",
"no_http_body": true,
"url": "localhost",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
if err := cfg.Unpack(&conf); err == nil {
t.Fatal("Configuration validation failed. http_method DELETE is not allowed.")
}
}
func TestConfigMustFailWithInvalidURL(t *testing.T) {
m := map[string]interface{}{
"url": "::invalid::",
}
cfg := common.MustNewConfigFrom(m)
conf := newDefaultConfig()
err := cfg.Unpack(&conf)
assert.EqualError(t, err, `parse "::invalid::": missing protocol scheme accessing 'url'`)
}
func TestConfigOauth2Validation(t *testing.T) {
cases := []struct {
name string
expectedErr string
input map[string]interface{}
setup func()
teardown func()
}{
{
name: "can't set oauth2 and api_key together",
expectedErr: "invalid configuration: oauth2 and api_key or authentication_scheme cannot be set simultaneously accessing config",
input: map[string]interface{}{
"api_key": "an_api_key",
"oauth2": map[string]interface{}{
"token_url": "localhost",
"client": map[string]interface{}{
"id": "a_client_id",
"secret": "a_client_secret",
},
},
"url": "localhost",
},
},
{
name: "can set oauth2 and api_key together if oauth2 is disabled",
input: map[string]interface{}{
"api_key": "an_api_key",
"oauth2": map[string]interface{}{
"enabled": false,
"token_url": "localhost",
"client": map[string]interface{}{
"id": "a_client_id",
"secret": "a_client_secret",
},
},
"url": "localhost",
},
},
{
name: "can't set oauth2 and authentication_scheme",
expectedErr: "invalid configuration: oauth2 and api_key or authentication_scheme cannot be set simultaneously accessing config",
input: map[string]interface{}{
"authentication_scheme": "a_scheme",
"oauth2": map[string]interface{}{
"token_url": "localhost",
"client": map[string]interface{}{
"id": "a_client_id",
"secret": "a_client_secret",
},
},
"url": "localhost",
},
},
{
name: "token_url and client credentials must be set",
expectedErr: "invalid configuration: both token_url and client credentials must be provided accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{},
"url": "localhost",
},
},
{
name: "must fail with an unknown provider",
expectedErr: "invalid configuration: unknown provider \"unknown\" accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "unknown",
},
"url": "localhost",
},
},
{
name: "azure must have either tenant_id or token_url",
expectedErr: "invalid configuration: at least one of token_url or tenant_id must be provided accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "azure",
},
"url": "localhost",
},
},
{
name: "azure must have only one of token_url and tenant_id",
expectedErr: "invalid configuration: only one of token_url and tenant_id can be used accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "azure",
"azure.tenant_id": "a_tenant_id",
"token_url": "localhost",
},
"url": "localhost",
},
},
{
name: "azure must have client credentials set",
expectedErr: "invalid configuration: client credentials must be provided accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "azure",
"azure.tenant_id": "a_tenant_id",
},
"url": "localhost",
},
},
{
name: "azure config is valid",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "azure",
"azure": map[string]interface{}{
"tenant_id": "a_tenant_id",
},
"client.id": "a_client_id",
"client.secret": "a_client_secret",
},
"url": "localhost",
},
},
{
name: "google can't have token_url or client credentials set",
expectedErr: "invalid configuration: none of token_url and client credentials can be used, use google.credentials_file, google.jwt_file, google.credentials_json or ADC instead accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"azure": map[string]interface{}{
"tenant_id": "a_tenant_id",
},
"client.id": "a_client_id",
"client.secret": "a_client_secret",
"token_url": "localhost",
},
"url": "localhost",
},
},
{
name: "google must fail if no ADC available",
expectedErr: "invalid configuration: no authentication credentials were configured or detected (ADC) accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
},
"url": "localhost",
},
setup: func() {
// we change the default function to force a failure
findDefaultGoogleCredentials = func(context.Context, ...string) (*google.Credentials, error) {
return nil, errors.New("failed")
}
},
teardown: func() { findDefaultGoogleCredentials = google.FindDefaultCredentials },
},
{
name: "google must fail if credentials file not found",
expectedErr: "invalid configuration: the file \"./wrong\" cannot be found accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.credentials_file": "./wrong",
},
"url": "localhost",
},
},
{
name: "google must fail if ADC is wrongly set",
expectedErr: "invalid configuration: no authentication credentials were configured or detected (ADC) accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
},
"url": "localhost",
},
setup: func() { os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "./wrong") },
},
{
name: "google must work if ADC is set up",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
},
"url": "localhost",
},
setup: func() { os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "./testdata/credentials.json") },
},
{
name: "google must work if credentials_file is correct",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.credentials_file": "./testdata/credentials.json",
},
"url": "localhost",
},
},
{
name: "google must work if jwt_file is correct",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.jwt_file": "./testdata/credentials.json",
},
"url": "localhost",
},
},
{
name: "google must work if credentials_json is correct",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.credentials_json": []byte(`{
"type": "service_account",
"project_id": "foo",
"private_key_id": "x",
"client_email": "foo@bar.com",
"client_id": "0"
}`),
},
"url": "localhost",
},
},
{
name: "google must fail if credentials_json is not a valid JSON",
expectedErr: "invalid configuration: google.credentials_json must be valid JSON accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.credentials_json": []byte(`invalid`),
},
"url": "localhost",
},
},
{
name: "google must fail if the provided credentials file is not a valid JSON",
expectedErr: "invalid configuration: the file \"./testdata/invalid_credentials.json\" does not contain valid JSON accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.credentials_file": "./testdata/invalid_credentials.json",
},
"url": "localhost",
},
},
{
name: "date_cursor.date_format will fail if invalid",
expectedErr: "invalid configuration: date_format is not a valid date layout accessing 'date_cursor'",
input: map[string]interface{}{
"date_cursor": map[string]interface{}{"field": "foo", "url_field": "foo", "date_format": "1234"},
"url": "localhost",
},
},
{
name: "date_cursor must work with a valid date_format",
input: map[string]interface{}{
"date_cursor": map[string]interface{}{"field": "foo", "url_field": "foo", "date_format": time.RFC3339},
"url": "localhost",
},
},
{
name: "google must fail if the delegated_account is set without jwt_file",
expectedErr: "invalid configuration: google.delegated_account can only be provided with a jwt_file accessing 'oauth2'",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.credentials_file": "./testdata/credentials.json",
"google.delegated_account": "delegated@account.com",
},
"url": "localhost",
},
},
{
name: "google must work with delegated_account and a valid jwt_file",
input: map[string]interface{}{
"oauth2": map[string]interface{}{
"provider": "google",
"google.jwt_file": "./testdata/credentials.json",
"google.delegated_account": "delegated@account.com",
},
"url": "localhost",
},
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
if c.setup != nil {
c.setup()
}
if c.teardown != nil {
defer c.teardown()
}
cfg := common.MustNewConfigFrom(c.input)
conf := newDefaultConfig()
err := cfg.Unpack(&conf)
switch {
case c.expectedErr == "":
if err != nil {
t.Fatalf("Configuration validation failed. no error expected but got %q", err)
}
case c.expectedErr != "":
if err == nil || err.Error() != c.expectedErr {
t.Fatalf("Configuration validation failed. expecting %q error but got %q", c.expectedErr, err)
}
}
})
}
}