-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
caddyfile_authz_test.go
864 lines (858 loc) · 26.4 KB
/
caddyfile_authz_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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright 2022 Paul Greenberg [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package security
import (
"fmt"
"testing"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/google/go-cmp/cmp"
"github.com/greenpau/go-authcrunch/pkg/errors"
)
func TestParseCaddyfileAuthorization(t *testing.T) {
testcases := []struct {
name string
d *caddyfile.Dispenser
want string
shouldErr bool
err error
}{
{
name: "test valid authorization policy config",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
crypto key verify 0e2fdcf8-6868-41a7-884b-7308795fc286
set auth url /auth
allow roles authp/admin authp/user
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"auth_url_path": "/auth",
"auth_redirect_query_param": "redirect_url",
"auth_redirect_status_code": 302,
"name": "mypolicy",
"auth_url_path": "/auth",
"access_list_rules": [
{
"conditions": [
"match roles authp/admin authp/user"
],
"action": "allow log debug"
}
],
"crypto_key_configs": [
{
"id": "0",
"usage": "verify",
"token_name": "access_token",
"source": "config",
"algorithm": "hmac",
"token_lifetime": 900,
"token_secret": "0e2fdcf8-6868-41a7-884b-7308795fc286"
}
]
}
]
}
}`,
},
{
name: "test valid authorization policy config with misc settings",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
crypto key verify 0e2fdcf8-6868-41a7-884b-7308795fc286
set auth url /auth
set token sources query
set forbidden url /forbidden
set redirect status 302
set redirect query parameter return_path_url
disable auth redirect query
disable auth redirect
validate path acl
validate source address
validate bearer header
with basic auth portal default realm local
with api key auth portal default realm local
allow roles authp/admin authp/user
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"name": "mypolicy",
"auth_url_path": "/auth",
"disable_auth_redirect": true,
"disable_auth_redirect_query": true,
"auth_redirect_query_param": "return_path_url",
"auth_redirect_status_code": 302,
"access_list_rules": [
{
"conditions": [
"match roles authp/admin authp/user"
],
"action": "allow log debug"
}
],
"crypto_key_configs": [
{
"id": "0",
"usage": "verify",
"token_name": "access_token",
"source": "config",
"algorithm": "hmac",
"token_lifetime": 900,
"token_secret": "0e2fdcf8-6868-41a7-884b-7308795fc286"
}
],
"auth_proxy_config": {
"portal_name": "default",
"basic_auth": {
"enabled": true,
"realms": {
"local": true
}
},
"api_key_auth": {
"enabled": true,
"realms": {
"local": true
}
}
},
"allowed_token_sources": [
"query"
],
"forbidden_url": "/forbidden",
"validate_bearer_header": true,
"validate_method_path": true,
"validate_access_list_path_claim": true,
"validate_source_address": true
}
]
}
}`,
},
{
name: "test valid authorization policy config with custom acl",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy2 {
crypto key verify 0e2fdcf8-6868-41a7-884b-7308795fc286
bypass uri exact /foo
set user identity id
inject headers with claims
inject header "X-Picture" from picture
enable js redirect
set auth url /auth
enable strip token
enable additional scopes
acl rule {
comment allow users
match role authp/user
allow stop log info
}
acl rule {
comment default deny
match any
deny log warn
}
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"name": "mypolicy2",
"auth_url_path": "/auth",
"auth_redirect_query_param": "redirect_url",
"auth_redirect_status_code": 302,
"redirect_with_javascript": true,
"access_list_rules": [
{
"comment": "comment allow users",
"conditions": [
"match role authp/user"
],
"action": "allow stop log info"
},
{
"comment": "comment default deny",
"conditions": [
"match any"
],
"action": "deny log warn"
}
],
"crypto_key_configs": [
{
"id": "0",
"usage": "verify",
"token_name": "access_token",
"source": "config",
"algorithm": "hmac",
"token_lifetime": 900,
"token_secret": "0e2fdcf8-6868-41a7-884b-7308795fc286"
}
],
"strip_token_enabled": true,
"additional_scopes": true,
"user_identity_field": "id",
"pass_claims_with_headers": true,
"redirect_with_javascript": true,
"header_injection_configs": [
{
"header": "X-Picture",
"field": "picture"
}
],
"bypass_configs": [
{
"match_type": "exact",
"uri": "/foo"
}
]
}
]
}
}`,
},
{
name: "test valid authorization policy with custom acl shortcuts",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
allow roles authp/admin authp/user
allow roles authp/guest with get to /foo
allow origin any
deny iss foo
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"name": "mypolicy",
"auth_url_path": "/auth",
"auth_redirect_query_param": "redirect_url",
"auth_redirect_status_code": 302,
"access_list_rules": [
{
"conditions": ["match roles authp/admin authp/user"],
"action": "allow log debug"
},
{
"conditions": [
"match roles authp/guest",
"match method GET",
"partial match path /foo"
],
"action": "allow log debug"
},
{
"conditions": ["field origin exists"],
"action": "allow log debug"
},
{
"conditions": ["match iss foo"],
"action": "deny stop log warn"
}
],
"validate_method_path": true
}
]
}
}`,
},
{
name: "test valid authorization policy with custom acl",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl rule {
match roles authp/admin authp/user
allow stop log info
}
acl default deny
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"name": "mypolicy",
"auth_url_path": "/auth",
"auth_redirect_query_param": "redirect_url",
"auth_redirect_status_code": 302,
"access_list_rules": [
{
"conditions": ["match roles authp/admin authp/user"],
"action": "allow stop log info"
},
{
"conditions": ["match any"],
"action": "deny"
}
]
}
]
}
}`,
},
{
name: "test valid authorization policy with enabled login hint",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
enable login hint
allow roles authp/admin authp/user
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"name": "mypolicy",
"auth_url_path": "/auth",
"auth_redirect_query_param": "redirect_url",
"auth_redirect_status_code": 302,
"login_hint_validators": ["email", "phone", "alphanumeric"],
"access_list_rules": [
{
"conditions": [
"match roles authp/admin authp/user"
],
"action": "allow log debug"
}
]
}
]
}
}`,
},
{
name: "test valid authorization policy with enabled login hint with validators",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
enable login hint with email phone
allow roles authp/admin authp/user
}
}`),
want: `{
"config": {
"authorization_policies": [
{
"name": "mypolicy",
"auth_url_path": "/auth",
"auth_redirect_query_param": "redirect_url",
"auth_redirect_status_code": 302,
"login_hint_validators": ["email", "phone"],
"access_list_rules": [
{
"conditions": [
"match roles authp/admin authp/user"
],
"action": "allow log debug"
}
]
}
]
}
}`,
},
{
name: "test malformed authorization policy definition",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy foo {
bypass uri /foo/bar
}
}`),
shouldErr: true,
err: fmt.Errorf("wrong argument count or unexpected line ending after '%s', at %s:%d", "foo", tf, 3),
},
{
name: "test unsupported authorization policy keyword",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
foo bar
}
}`),
shouldErr: true,
err: errors.ErrMalformedDirective.WithArgs(
mkcp(authzPrefix, "policy", "foo"),
[]string{"bar"},
),
},
// Authorization header injection.
{
name: "test authorization policy injection with unsupported directive",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
inject foo
}
}`),
shouldErr: true,
err: fmt.Errorf("unsupported directive for security.authorization.policy.inject: %v, at %s:%d", "foo", tf, 4),
},
{
name: "test authorization policy header injection with too many args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
inject header bar baz foo bar
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.inject directive %q is invalid, at %s:%d", "header bar baz foo bar", tf, 4),
},
{
name: "test authorization policy header injection with bad syntax",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
inject header "X-Picture" foo picture
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.inject directive %q has invalid syntax, at %s:%d", "header X-Picture foo picture", tf, 4),
},
{
name: "test authorization policy injection without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
inject
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.inject directive has no value, at %s:%d", tf, 4),
},
{
name: "test authorization policy injection without empty args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
inject header "X-Picture" from " "
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.inject %s erred: undefined field name, at %s:%d", "header X-Picture from \" \"", tf, 4),
},
// Enable features.
{
name: "test authorization policy enable without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
enable
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.enable directive has no value, at %s:%d", tf, 4),
},
{
name: "test authorization policy injection with unsupported directive",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
enable foo
}
}`),
shouldErr: true,
err: fmt.Errorf("unsupported directive for security.authorization.policy.enable: %v, at %s:%d", "foo", tf, 4),
},
// Validate features.
{
name: "test authorization policy validate without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
validate
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.validate directive has no value, at %s:%d", tf, 4),
},
{
name: "test authorization policy validate with unsupported directive",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
validate foo
}
}`),
shouldErr: true,
err: fmt.Errorf("unsupported directive for security.authorization.policy.validate: %v, at %s:%d", "foo", tf, 4),
},
// Disabled features.
{
name: "test authorization policy disable without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
disable
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.disable directive has no value, at %s:%d", tf, 4),
},
{
name: "test authorization policy disable with unsupported directive",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
disable foo
}
}`),
shouldErr: true,
err: fmt.Errorf("unsupported directive for security.authorization.policy.disable: %v, at %s:%d", "foo", tf, 4),
},
// Configure features.
{
name: "test authorization policy set without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
set
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.set directive has no value, at %s:%d", tf, 4),
},
{
name: "test authorization policy set with unsupported directive",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
set foo
}
}`),
shouldErr: true,
err: fmt.Errorf("unsupported directive for security.authorization.policy.set: %v, at %s:%d", "foo", tf, 4),
},
{
name: "test authorization policy set redirect status success",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
set redirect status 200
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.set %v directive contains invalid value, at %s:%d", "redirect status 200", tf, 4),
},
{
name: "test authorization policy set redirect status alphanumeric",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
set redirect status foo
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.set %v directive failed: %v, at %s:%d",
"redirect status foo", "strconv.Atoi: parsing \"foo\": invalid syntax", tf, 4),
},
// With features.
{
name: "test authorization policy with without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
with
}
}`),
shouldErr: true,
err: fmt.Errorf("security.authorization.policy.with directive has no value, at %s:%d", tf, 4),
},
{
name: "test authorization policy with with unsupported directive",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
with foo
}
}`),
shouldErr: true,
err: fmt.Errorf("unsupported directive for security.authorization.policy.with: %v, at %s:%d", "foo", tf, 4),
},
// Crypto errors.
{
name: "test authorization policy crypto with too little args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
crypto foo bar
}
}`),
shouldErr: true,
err: fmt.Errorf(
"%v, at %s:%d",
errors.ErrConfigDirectiveShort.WithArgs(
"security.authorization.policy.crypto",
[]string{"foo", "bar"},
), tf, 4,
),
},
{
name: "test authorization policy crypto with unsupported args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
crypto foo bar baz
}
}`),
shouldErr: true,
err: fmt.Errorf(
"%v, at %s:%d",
errors.ErrConfigDirectiveValueUnsupported.WithArgs(
"security.authorization.policy.crypto",
[]string{"foo", "bar", "baz"},
), tf, 4,
),
},
// Bypass errors.
{
name: "test authorization policy bypass without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
bypass
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.bypass directive has no value, at %s:%d",
tf, 4,
),
},
{
name: "test authorization policy bypass with wrong args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
bypass foo
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.bypass %s is invalid, at %s:%d",
"foo", tf, 4,
),
},
{
name: "test authorization policy bypass with invalid keyword",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
bypass foo bar baz
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.bypass %s is invalid, at %s:%d",
"foo bar baz", tf, 4,
),
},
{
name: "test authorization policy bypass with invalid syntax",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
bypass uri bar baz
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.bypass %s erred: %v, at %s:%d",
"uri bar baz", "invalid \"bar\" bypass match type", tf, 4,
),
},
// ACL errors.
{
name: "test authorization policy acl without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.acl directive has no value, at %s:%d",
tf, 4,
),
},
{
name: "test authorization policy acl rule with args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl rule foo
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.acl directive %q is too long, at %s:%d",
"rule foo", tf, 4,
),
},
{
name: "test authorization policy acl default with args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl default allow bar
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.acl directive %q is too long, at %s:%d",
"default allow bar", tf, 4,
),
},
{
name: "test authorization policy acl default with args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl default foo
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.acl directive %q must have either allow or deny, at %s:%d",
"default foo", tf, 4,
),
},
{
name: "test authorization policy acl invalid",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl foo
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.acl directive value of %q is unsupported, at %s:%d",
"foo", tf, 4,
),
},
{
name: "test authorization policy acl rule without comment value",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
acl rule {
comment
}
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.acl rule directive %v has no values, at %s:%d",
"comment", tf, 5,
),
},
// ACL shortcuts errors.
{
name: "test authorization policy acl shortcut without args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
allow
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.allow directive has no value, at %s:%d",
tf, 4,
),
},
{
name: "test authorization policy acl shortcut without too few args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
allow foo
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.allow directive %q is too short, at %s:%d",
"foo", tf, 4,
),
},
{
name: "test authorization policy acl shortcut with unsupported args",
d: caddyfile.NewTestDispenser(`
security {
authorization policy mypolicy {
allow roles foo method get to /foo bar
}
}`),
shouldErr: true,
err: fmt.Errorf(
"security.authorization.policy.allow directive value of %q is unsupported, at %s:%d",
"roles foo method get to /foo bar", tf, 4,
),
},
// Post config processing errors.
{
name: "test authorization invalid keyword",
d: caddyfile.NewTestDispenser(`
security {
authorization foo bar {
baz zag
}
}`),
shouldErr: true,
err: errors.ErrMalformedDirective.WithArgs(authzPrefix, []string{"foo", "bar"}),
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
app, err := parseCaddyfile(tc.d, nil)
if err != nil {
if !tc.shouldErr {
t.Fatalf("expected success, got: %v", err)
}
if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" {
t.Fatalf("unexpected error: %v, want: %v", err, tc.err)
}
return
}
if tc.shouldErr {
t.Fatalf("unexpected success, want: %v", tc.err)
}
got := unpack(t, string(app.(httpcaddyfile.App).Value))
want := unpack(t, tc.want)
if diff := cmp.Diff(want, got); diff != "" {
t.Logf("JSON: %v", string(app.(httpcaddyfile.App).Value))
t.Errorf("parseCaddyfileAuthorization() mismatch (-want +got):\n%s", diff)
}
})
}
}