/
api_test.go
995 lines (938 loc) · 35.7 KB
/
api_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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/******************************************************************************
*
* Copyright 2019-2020 SAP SE
*
* 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 authapi_test
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/sapcc/go-bits/assert"
"github.com/sapcc/keppel/internal/keppel"
"github.com/sapcc/keppel/internal/test"
)
////////////////////////////////////////////////////////////////////////////////
// The testcases in this file encode a lot of knowledge that I gained by
// torturing the auth API of Docker Hub. DO NOT CHANGE stuff unless you have
// verified how the Docker Hub auth endpoint works.
// For the record, the auth endpoint of Docker Hub can be found by
//
// curl -si https://index.docker.io/v2/ | grep Authenticate
type TestCase struct {
// request
Scope string
AnonymousLogin bool
// situation
CannotPush bool
CannotPull bool
CannotDelete bool
RBACPolicy *keppel.RBACPolicy
// result
GrantedActions string
AdditionalScopes []string
}
var (
policyAnonPull = keppel.RBACPolicy{
RepositoryPattern: "fo+",
Permissions: []keppel.RBACPermission{keppel.GrantsAnonymousPull},
}
policyAnonFirstPull = keppel.RBACPolicy{
RepositoryPattern: "fo+",
Permissions: []keppel.RBACPermission{keppel.GrantsAnonymousPull, keppel.GrantsAnonymousFirstPull},
}
policyPullMatches = keppel.RBACPolicy{
RepositoryPattern: "fo+",
UserNamePattern: "correct.*",
Permissions: []keppel.RBACPermission{keppel.GrantsPull},
}
policyPushMatches = keppel.RBACPolicy{
RepositoryPattern: "fo+",
UserNamePattern: "correct.*",
Permissions: []keppel.RBACPermission{keppel.GrantsPull, keppel.GrantsPush},
}
policyDeleteMatches = keppel.RBACPolicy{
RepositoryPattern: "fo+",
UserNamePattern: "correct.*",
Permissions: []keppel.RBACPermission{keppel.GrantsPull, keppel.GrantsDelete},
}
policyPullDoesNotMatch = keppel.RBACPolicy{
RepositoryPattern: "fo+",
UserNamePattern: "doesnotmatch",
Permissions: []keppel.RBACPermission{keppel.GrantsPull},
}
policyPushDoesNotMatch = keppel.RBACPolicy{
RepositoryPattern: "doesnotmatch",
UserNamePattern: "correct.*",
Permissions: []keppel.RBACPermission{keppel.GrantsPull, keppel.GrantsPush},
}
policyDeleteDoesNotMatch = keppel.RBACPolicy{
RepositoryPattern: "fo+",
UserNamePattern: "doesnotmatch",
Permissions: []keppel.RBACPermission{keppel.GrantsPull, keppel.GrantsDelete},
}
)
var testCases = []TestCase{
// basic success case
{Scope: "repository:test1/foo:pull",
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
GrantedActions: "delete"},
// not allowed to pull
{Scope: "repository:test1/foo:pull",
CannotPull: true, GrantedActions: ""},
{Scope: "repository:test1/foo:push",
CannotPull: true, GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, GrantedActions: "push"},
{Scope: "repository:test1/foo:delete",
CannotPull: true, GrantedActions: "delete"},
// not allowed to push
{Scope: "repository:test1/foo:pull",
CannotPush: true, GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
CannotPush: true, GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPush: true, GrantedActions: "pull"},
{Scope: "repository:test1/foo:delete",
CannotPush: true, GrantedActions: "delete"},
// not allowed to pull nor push
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, GrantedActions: ""},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, GrantedActions: ""},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, GrantedActions: "delete"},
// not allowed to delete
{Scope: "repository:test1/foo:pull",
CannotDelete: true, GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
CannotDelete: true, GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
CannotDelete: true, GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
CannotDelete: true, GrantedActions: ""},
// catalog access always allowed if username/password are ok (access to
// specific accounts is filtered later)
{Scope: "registry:catalog:*",
GrantedActions: "*",
AdditionalScopes: []string{"keppel_account:test1:view"}},
{Scope: "registry:catalog:*",
CannotPull: true, GrantedActions: "*"},
{Scope: "registry:catalog:*",
CannotPush: true, GrantedActions: "*",
AdditionalScopes: []string{"keppel_account:test1:view"}},
{Scope: "registry:catalog:*",
CannotPull: true, CannotPush: true, GrantedActions: "*"},
{Scope: "registry:catalog:*",
CannotDelete: true, GrantedActions: "*",
AdditionalScopes: []string{"keppel_account:test1:view"}},
// unknown resources/actions for resource type "registry"
{Scope: "registry:test1/foo:pull",
GrantedActions: ""},
{Scope: "registry:catalog:pull",
GrantedActions: ""},
// incomplete scope syntax
{Scope: "",
GrantedActions: ""},
{Scope: "repository",
GrantedActions: ""},
{Scope: "repository:",
GrantedActions: ""},
{Scope: "repository:test1",
GrantedActions: ""},
{Scope: "repository:test1/",
GrantedActions: ""},
{Scope: "repository:test1/foo",
GrantedActions: ""},
{Scope: "repository:test1/foo:",
GrantedActions: ""},
{Scope: "repository:test1:pull",
GrantedActions: ""},
{Scope: "repository:test1/:pull",
GrantedActions: ""},
// invalid scope syntax (overlong repository name)
{Scope: fmt.Sprintf("repository:test1/%s:pull", strings.Repeat("a", 300)),
GrantedActions: ""},
// invalid scope syntax (malformed repository name)
{Scope: "repository:test1/???:pull",
GrantedActions: ""},
// anonymous login when RBAC policies do not allow access
{Scope: "repository:test1/foo:pull", AnonymousLogin: true,
GrantedActions: ""},
{Scope: "repository:test1/foo:push", AnonymousLogin: true,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push", AnonymousLogin: true,
GrantedActions: ""},
{Scope: "repository:test1/foo:delete", AnonymousLogin: true,
GrantedActions: ""},
// anonymous pull (but not push) is allowed by a matching RBAC policy
{Scope: "repository:test1/foo:pull", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:delete", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
// RBAC policy with RepositoryPattern only works when repository name matches
{Scope: "repository:test1/foobar:pull", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
{Scope: "repository:test1/foobar:push", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
{Scope: "repository:test1/foobar:pull,push", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
{Scope: "repository:test1/foobar:delete", AnonymousLogin: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
// RBAC policy for anonymous pull also enables pull access for all authenticated users
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyAnonPull,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyAnonPull,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyAnonPull,
GrantedActions: ""},
// RBAC policy for anonymous pull does not change anything if the user already has pull access
{Scope: "repository:test1/foo:pull",
RBACPolicy: &policyAnonPull,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
RBACPolicy: &policyAnonPull,
GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
RBACPolicy: &policyAnonPull,
GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
RBACPolicy: &policyAnonPull,
GrantedActions: "delete"},
// anonymous first pull is allowed by a matching RBAC policy
{Scope: "repository:test1/foo:pull", AnonymousLogin: true,
RBACPolicy: &policyAnonFirstPull,
GrantedActions: "pull,anonymous_first_pull"},
{Scope: "repository:test1/foo:push", AnonymousLogin: true,
RBACPolicy: &policyAnonFirstPull,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push", AnonymousLogin: true,
RBACPolicy: &policyAnonFirstPull,
GrantedActions: "pull,anonymous_first_pull"},
{Scope: "repository:test1/foo:delete", AnonymousLogin: true,
RBACPolicy: &policyAnonFirstPull,
GrantedActions: ""},
// RBAC policy with CanPull grants pull permissions to matching users
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullMatches,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullMatches,
GrantedActions: ""},
// RBAC policy with CanPull does not grant permissions if it does not match
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPullDoesNotMatch,
GrantedActions: ""},
// RBAC policy with CanPull does not change anything if the user already has pull access
{Scope: "repository:test1/foo:pull",
RBACPolicy: &policyPullMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
RBACPolicy: &policyPullMatches,
GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
RBACPolicy: &policyPullMatches,
GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
RBACPolicy: &policyPullMatches,
GrantedActions: "delete"},
// RBAC policy with CanPull/CanPush grants pull/push permissions to matching users
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushMatches,
GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushMatches,
GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushMatches,
GrantedActions: ""},
// RBAC policy with CanPull/CanPush does not grant permissions if it does not match
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyPushDoesNotMatch,
GrantedActions: ""},
// RBAC policy with CanPull/CanPush does not change anything if the user already has pull/push access
{Scope: "repository:test1/foo:pull",
RBACPolicy: &policyPushMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
RBACPolicy: &policyPushMatches,
GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
RBACPolicy: &policyPushMatches,
GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
RBACPolicy: &policyPushMatches,
GrantedActions: "delete"},
// RBAC policy with CanPull/CanDelete grants pull/delete permissions to matching users
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteMatches,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteMatches,
GrantedActions: "delete"},
// RBAC policy with CanPull/CanDelete does not grant permissions if it does not match
{Scope: "repository:test1/foo:pull",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:pull,push",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteDoesNotMatch,
GrantedActions: ""},
{Scope: "repository:test1/foo:delete",
CannotPull: true, CannotPush: true, CannotDelete: true,
RBACPolicy: &policyDeleteDoesNotMatch,
GrantedActions: ""},
// RBAC policy with CanPull/CanDelete does not change anything if the user already has pull/push access
{Scope: "repository:test1/foo:pull",
RBACPolicy: &policyDeleteMatches,
GrantedActions: "pull"},
{Scope: "repository:test1/foo:push",
RBACPolicy: &policyDeleteMatches,
GrantedActions: "push"},
{Scope: "repository:test1/foo:pull,push",
RBACPolicy: &policyDeleteMatches,
GrantedActions: "pull,push"},
{Scope: "repository:test1/foo:delete",
RBACPolicy: &policyDeleteMatches,
GrantedActions: "delete"},
}
// TODO expect refresh_token when offline_token=true is given
func setupPrimary(t *testing.T, extraOptions ...test.SetupOption) test.Setup {
s := test.NewSetup(t,
append(extraOptions,
test.WithAnycast(true),
test.WithAccount(keppel.Account{Name: "test1", AuthTenantID: "test1authtenant"}),
)...,
)
s.AD.ExpectedUserName = "correctusername"
s.AD.ExpectedPassword = "correctpassword"
return s
}
func setupSecondary(t *testing.T) test.Setup {
s := test.NewSetup(t,
test.IsSecondaryTo(nil),
test.WithAnycast(true),
test.WithAccount(keppel.Account{Name: "test2", AuthTenantID: "test1authtenant"}),
)
s.AD.ExpectedUserName = "correctusername"
s.AD.ExpectedPassword = "correctpassword"
return s
}
// jwtAccess appears in type jwtToken.
type jwtAccess struct {
Type string `json:"type"`
Name string `json:"name"`
Actions []string `json:"actions"`
}
// jwtToken contains the parsed contents of the payload section of a JWT token.
type jwtToken struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
Audience string `json:"aud"`
ExpiresAt int64 `json:"exp"`
NotBefore int64 `json:"nbf"`
IssuedAt int64 `json:"iat"`
TokenID string `json:"jti"`
Access []jwtAccess `json:"access"`
// The EmbeddedAuthorization is ignored by this test. It will be exercised
// indirectly in the registry API tests since the registry API uses attributes
// from the EmbeddedAuthorization.
Ignored map[string]any `json:"kea"`
}
// jwtContents contains what we expect in a JWT token payload section. This type
// implements assert.HTTPResponseBody and can therefore be used with
// assert.HTTPRequest.
type jwtContents struct {
Issuer string
Subject string
Audience string
Access []jwtAccess
}
// AssertResponseBody implements the assert.HTTPResponseBody interface.
func (c jwtContents) AssertResponseBody(t *testing.T, requestInfo string, responseBodyBytes []byte) (ok bool) {
t.Helper()
var responseBody struct {
Token string `json:"token"`
// optional fields (all listed so that we can use DisallowUnknownFields())
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn uint64 `json:"expires_in"`
IssuedAt string `json:"issued_at"`
}
dec := json.NewDecoder(bytes.NewReader(responseBodyBytes))
dec.DisallowUnknownFields()
err := dec.Decode(&responseBody)
if err != nil {
t.Logf("token was: %s", string(responseBodyBytes))
t.Errorf("%s: cannot decode response body: %s", requestInfo, err.Error())
return false
}
// extract payload from token
tokenFields := strings.Split(responseBody.Token, ".")
if len(tokenFields) != 3 {
t.Logf("JWT is %s", responseBody.Token)
t.Errorf("%s: expected token with 3 parts, got %d parts", requestInfo, len(tokenFields))
return false
}
tokenBytes, err := base64.RawURLEncoding.DecodeString(tokenFields[1])
if err != nil {
t.Logf("JWT is %s", responseBody.Token)
t.Errorf("%s: cannot decode JWT payload section: %s", requestInfo, err.Error())
return false
}
// decode token
var token jwtToken
dec = json.NewDecoder(bytes.NewReader(tokenBytes))
dec.DisallowUnknownFields()
err = dec.Decode(&token)
if err != nil {
t.Logf("token JSON is %s", string(tokenBytes))
t.Errorf("%s: cannot deserialize JWT payload section: %s", requestInfo, err.Error())
return false
}
// check token attributes for correctness
ok = true
ok = ok && assert.DeepEqual(t, "token.Access for "+requestInfo, token.Access, c.Access)
ok = ok && assert.DeepEqual(t, "token.Audience for "+requestInfo, token.Audience, c.Audience)
ok = ok && assert.DeepEqual(t, "token.Issuer for "+requestInfo, token.Issuer, c.Issuer)
ok = ok && assert.DeepEqual(t, "token.Subject for "+requestInfo, token.Subject, c.Subject)
// check remaining token attributes for plausibility
nowUnix := time.Now().Unix()
if nowUnix >= token.ExpiresAt {
t.Errorf("%s: ExpiresAt should be in the future, but is %d seconds in the past", requestInfo, nowUnix-token.ExpiresAt)
ok = false
}
if nowUnix < token.NotBefore {
t.Errorf("%s: NotBefore should be now or in the past, but is %d seconds in the future", requestInfo, token.NotBefore-nowUnix)
ok = false
}
if nowUnix < token.IssuedAt {
t.Errorf("%s: IssuedAt should be now or in the past, but is %d seconds in the future", requestInfo, token.IssuedAt-nowUnix)
ok = false
}
return ok
}
func TestIssueToken(t *testing.T) {
s := setupPrimary(t)
service := s.Config.APIPublicHostname
for idx, c := range testCases {
t.Logf("----- testcase %d/%d -----\n", idx+1, len(testCases))
// setup RBAC policies for test
rbacPoliciesJSONStr := ""
if c.RBACPolicy != nil {
buf, err := json.Marshal([]keppel.RBACPolicy{*c.RBACPolicy})
if err != nil {
t.Fatal(err.Error())
}
rbacPoliciesJSONStr = string(buf)
}
_, err := s.DB.Exec(`UPDATE accounts SET rbac_policies_json = $1 WHERE name = $2`, rbacPoliciesJSONStr, "test1")
if err != nil {
t.Fatal(err.Error())
}
// setup permissions for test
var perms []string
if c.CannotDelete {
perms = append(perms, string(keppel.CanDeleteFromAccount)+":othertenant")
} else {
perms = append(perms, string(keppel.CanDeleteFromAccount)+":test1authtenant")
}
if c.CannotPush {
perms = append(perms, string(keppel.CanPushToAccount)+":othertenant")
} else {
perms = append(perms, string(keppel.CanPushToAccount)+":test1authtenant")
}
if c.CannotPull {
perms = append(perms, string(keppel.CanPullFromAccount)+":othertenant")
perms = append(perms, string(keppel.CanViewAccount)+":othertenant")
} else {
perms = append(perms, string(keppel.CanPullFromAccount)+":test1authtenant")
perms = append(perms, string(keppel.CanViewAccount)+":test1authtenant")
}
s.AD.GrantedPermissions = strings.Join(perms, ",")
// setup Authorization header for test
req := assert.HTTPRequest{
Method: "GET",
ExpectStatus: http.StatusOK,
}
if !c.AnonymousLogin {
req.Header = map[string]string{
"Authorization": keppel.BuildBasicAuthHeader("correctusername", "correctpassword"),
}
}
// build URL query string for test
query := url.Values{}
if service != "" {
query.Set("service", service)
}
if c.Scope != "" {
query.Set("scope", c.Scope)
}
req.Path = "/keppel/v1/auth?" + query.Encode()
// build expected tokenContents to match against
expectedContents := jwtContents{
Audience: service,
Issuer: "keppel-api@registry.example.org",
Subject: "correctusername",
}
if c.AnonymousLogin {
expectedContents.Subject = ""
}
if c.GrantedActions != "" {
fields := strings.SplitN(c.Scope, ":", 3)
expectedContents.Access = []jwtAccess{{
Type: fields[0],
Name: fields[1],
Actions: strings.Split(c.GrantedActions, ","),
}}
}
if len(c.AdditionalScopes) > 0 {
for _, scope := range c.AdditionalScopes {
fields := strings.SplitN(scope, ":", 3)
expectedContents.Access = append(expectedContents.Access, jwtAccess{
Type: fields[0],
Name: fields[1],
Actions: strings.Split(fields[2], ","),
})
}
}
req.ExpectBody = expectedContents
// execute request
req.Check(t, s.Handler)
}
}
func TestInvalidCredentials(t *testing.T) {
s := setupPrimary(t)
h := s.Handler
service := s.Config.APIPublicHostname
// execute normal GET requests that would result in a token with granted
// actions, if we didn't give the wrong username (in the first call) or
// password (in the second call)
urlPath := url.URL{
Path: "/keppel/v1/auth",
RawQuery: url.Values{
"service": {service},
"scope": {"repository:test1/foo:pull"},
}.Encode(),
}
req := assert.HTTPRequest{
Method: "GET",
Path: urlPath.String(),
Header: map[string]string{},
ExpectStatus: http.StatusUnauthorized,
ExpectBody: assert.JSONObject{"details": "incorrect username or password"},
}
t.Logf("----- test malformed credentials with service %q -----\n", service)
req.Header["Authorization"] = "Bogus 65082567y295847y62"
req.ExpectBody = assert.JSONObject{"details": "malformed Authorization header"}
req.Check(t, h)
req.Header["Authorization"] = "Basic 65082567y2958)*&@@"
req.Check(t, h)
req.Header["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte("onlyusername"))
req.Check(t, h)
t.Logf("----- test wrong username with service %q -----\n", service)
req.Header["Authorization"] = keppel.BuildBasicAuthHeader("wrongusername", "correctpassword")
req.ExpectBody = assert.JSONObject{"details": "wrong credentials"}
req.Check(t, h)
t.Logf("----- test wrong password with service %q -----\n", service)
req.Header["Authorization"] = keppel.BuildBasicAuthHeader("correctusername", "wrongpassword")
req.Check(t, h)
}
type anycastTestCase struct {
// request
AccountName string
Service string
Handler http.Handler
// result
ErrorMessage string
HasAccess bool
Issuer string
}
func TestAnycastAndDomainRemappedTokens(t *testing.T) {
test.WithRoundTripper(func(tt *test.RoundTripper) {
s1 := setupPrimary(t)
s2 := setupSecondary(t)
h1 := s1.Handler
h2 := s2.Handler
// setup permissions for test
perms := fmt.Sprintf("%s:test1authtenant,%s:test1authtenant", keppel.CanPullFromAccount, keppel.CanViewAccount)
s1.AD.GrantedPermissions = perms
s2.AD.GrantedPermissions = perms
localService1 := s1.Config.APIPublicHostname
localService2 := s2.Config.APIPublicHostname
anycastService := s1.Config.AnycastAPIPublicHostname
anycastTestCases := []anycastTestCase{
// when asking for a local token (i.e. not giving the anycast hostname as
// service), no reverse-proxying is done and we only see the local accounts
{AccountName: "test1", Service: localService1, Handler: h1,
HasAccess: true, Issuer: localService1},
{AccountName: "test2", Service: localService1, Handler: h1,
HasAccess: false, Issuer: localService1},
{AccountName: "test1", Service: localService2, Handler: h2,
HasAccess: false, Issuer: localService2},
{AccountName: "test2", Service: localService2, Handler: h2,
HasAccess: true, Issuer: localService2},
// asking for a token for someone else's local service will never work
{AccountName: "test1", Service: localService2, Handler: h1,
ErrorMessage: `cannot issue tokens for service: "%SERVICE%"`},
{AccountName: "test2", Service: localService2, Handler: h1,
ErrorMessage: `cannot issue tokens for service: "%SERVICE%"`},
{AccountName: "test1", Service: localService1, Handler: h2,
ErrorMessage: `cannot issue tokens for service: "%SERVICE%"`},
{AccountName: "test2", Service: localService1, Handler: h2,
ErrorMessage: `cannot issue tokens for service: "%SERVICE%"`},
// when asking for an anycast token, the request if reverse-proxied if
// necessary and we will see the Keppel hosting the primary account as
// issuer
{AccountName: "test1", Service: anycastService, Handler: h1,
HasAccess: true, Issuer: localService1},
{AccountName: "test2", Service: anycastService, Handler: h1,
HasAccess: true, Issuer: localService2},
{AccountName: "test1", Service: anycastService, Handler: h2,
HasAccess: true, Issuer: localService1},
{AccountName: "test2", Service: anycastService, Handler: h2,
HasAccess: true, Issuer: localService2},
// asking for a token for an account that doesn't exist will never work
{AccountName: "test3", Service: localService1, Handler: h1,
HasAccess: false, Issuer: localService1},
{AccountName: "test3", Service: localService2, Handler: h2,
HasAccess: false, Issuer: localService2},
{AccountName: "test3", Service: anycastService, Handler: h1,
HasAccess: false, Issuer: localService1},
{AccountName: "test3", Service: anycastService, Handler: h2,
HasAccess: false, Issuer: localService2},
}
correctAuthHeader := map[string]string{
"Authorization": keppel.BuildBasicAuthHeader("correctusername", "correctpassword"),
}
for idx, c := range anycastTestCases {
for _, withDomainRemapping := range []bool{false, true} {
t.Logf("----- testcase %d/%d with domain remapping: %t -----\n", idx+1, len(anycastTestCases), withDomainRemapping)
var (
domainPrefix string
scopeRepoName string
)
if withDomainRemapping {
domainPrefix = c.AccountName + "."
scopeRepoName = "foo"
} else {
domainPrefix = ""
scopeRepoName = c.AccountName + "/foo"
}
req := assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?scope=repository:%s:pull&service=%s%s", scopeRepoName, domainPrefix, c.Service),
Header: correctAuthHeader,
}
if c.ErrorMessage == "" {
req.ExpectStatus = http.StatusOK
// build jwtContents struct to contain issued token against
expectedContents := jwtContents{
Audience: domainPrefix + c.Service,
Issuer: "keppel-api@" + domainPrefix + c.Issuer,
Subject: "correctusername",
}
if c.HasAccess {
expectedContents.Access = []jwtAccess{{
Type: "repository",
Name: scopeRepoName,
Actions: []string{"pull"},
}}
}
req.ExpectBody = expectedContents
} else {
msg := strings.ReplaceAll(c.ErrorMessage, "%SERVICE%", domainPrefix+c.Service)
req.ExpectStatus = http.StatusBadRequest
req.ExpectBody = assert.JSONObject{"details": msg}
}
req.Check(t, c.Handler)
}
}
// test that catalog access is not allowed on anycast (since we don't know
// which peer to ask for authentication)
assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?service=%s&scope=registry:catalog:*", anycastService),
Header: correctAuthHeader,
ExpectStatus: http.StatusOK,
ExpectBody: jwtContents{
Audience: anycastService,
Issuer: "keppel-api@" + localService1,
Subject: "correctusername",
Access: nil,
},
}.Check(t, h1)
// test that catalog access is allowed for domain-remapped APIs, but only
// for the account name specified in the domain
assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?service=test1.%s&scope=registry:catalog:*", localService1),
Header: correctAuthHeader,
ExpectStatus: http.StatusOK,
ExpectBody: jwtContents{
Audience: "test1." + localService1,
Issuer: "keppel-api@test1." + localService1,
Subject: "correctusername",
Access: []jwtAccess{
{Type: "registry", Name: "catalog", Actions: []string{"*"}},
{Type: "keppel_account", Name: "test1", Actions: []string{"view"}},
},
},
}.Check(t, h1)
assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?service=something-else.%s&scope=registry:catalog:*", localService1),
Header: correctAuthHeader,
ExpectStatus: http.StatusOK,
ExpectBody: jwtContents{
Audience: "something-else." + localService1,
Issuer: "keppel-api@something-else." + localService1,
Subject: "correctusername",
Access: []jwtAccess{
{Type: "registry", Name: "catalog", Actions: []string{"*"}},
// no keppel_account:test1:view since the API is restricted to the non-existent account "something-else"
},
},
}.Check(t, h1)
})
}
func TestMultiScope(t *testing.T) {
// It turns out that it's allowed to send multiple scopes in a single auth
// request, which produces a token with a union of all granted scopes. This
// test covers some basic cases of multi-scopes.
s := setupPrimary(t)
h := s.Handler
service := s.Config.APIPublicHostname
// various shorthands for the testcases below
correctAuthHeader := map[string]string{
"Authorization": keppel.BuildBasicAuthHeader("correctusername", "correctpassword"),
}
makeJWTContents := func(access []jwtAccess) jwtContents {
return jwtContents{
Audience: service,
Issuer: "keppel-api@" + service,
Subject: "correctusername",
Access: access,
}
}
makePerms := func(perms ...keppel.Permission) string {
var fields []string
for _, perm := range perms {
fields = append(fields, string(perm)+":test1authtenant")
}
return strings.Join(fields, ",")
}
// case 1: multiple actions on the same resource and we get everything we ask for
s.AD.GrantedPermissions = makePerms(keppel.CanViewAccount, keppel.CanPullFromAccount, keppel.CanPushToAccount, keppel.CanDeleteFromAccount)
assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?service=%s&scope=repository:test1/foo:pull&scope=repository:test1/foo:push&scope=repository:test1/foo:delete", service),
Header: correctAuthHeader,
ExpectStatus: http.StatusOK,
ExpectBody: makeJWTContents([]jwtAccess{{
Type: "repository",
Name: "test1/foo",
Actions: []string{"pull", "push", "delete"},
}}),
}.Check(t, h)
// case 2: overlapping actions on the same resource and we get everything except "delete"
s.AD.GrantedPermissions = makePerms(keppel.CanViewAccount, keppel.CanPullFromAccount, keppel.CanPushToAccount)
assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?service=%s&scope=repository:test1/foo:pull,delete&scope=repository:test1/foo:pull,push&scope=repository:test1/foo:delete", service),
Header: correctAuthHeader,
ExpectStatus: http.StatusOK,
ExpectBody: makeJWTContents([]jwtAccess{{
Type: "repository",
Name: "test1/foo",
Actions: []string{"pull", "push"}, // "pull" was mentioned twice in the scopes - this verifies that it was deduplicated
}}),
}.Check(t, h)
// case 3: actions on multiple resources and we reject access to one of the resources entirely
s.AD.GrantedPermissions = makePerms(keppel.CanViewAccount, keppel.CanPullFromAccount, keppel.CanPushToAccount)
assert.HTTPRequest{
Method: "GET",
Path: fmt.Sprintf("/keppel/v1/auth?service=%s&scope=repository:test1/foo:pull,push&scope=repository:test2/foo:pull,push&scope=registry:catalog:*", service),
Header: correctAuthHeader,
ExpectStatus: http.StatusOK,
ExpectBody: makeJWTContents([]jwtAccess{
{
Type: "repository",
Name: "test1/foo",
Actions: []string{"pull", "push"},
},
{
Type: "registry",
Name: "catalog",
Actions: []string{"*"},
},
{
Type: "keppel_account",
Name: "test1",
Actions: []string{"view"},
},
}),
}.Check(t, h)
}
func TestIssuerKeyRotation(t *testing.T) {
// phase 1: issue a token with the previous issuer key
s := setupPrimary(t, test.WithPreviousIssuerKey, test.WithoutCurrentIssuerKey)
_, respBodyBytes := assert.HTTPRequest{
Method: "GET",
Path: "/keppel/v1/auth?service=registry.example.org&scope=keppel_api:info:access",
Header: map[string]string{"Authorization": keppel.BuildBasicAuthHeader("correctusername", "correctpassword")},
ExpectStatus: http.StatusOK,
ExpectBody: jwtContents{
Audience: "registry.example.org",
Issuer: "keppel-api@registry.example.org",
Subject: "correctusername",
Access: []jwtAccess{{
Type: "keppel_api",
Name: "info",
Actions: []string{"access"},
}},
},
}.Check(t, s.Handler)
var respBody struct {
Token string `json:"token"`
}
err := json.Unmarshal(respBodyBytes, &respBody)
if err != nil {
t.Fatal(err.Error())
}
// test that it (obviously) gets accepted by the same API that issued it
assert.HTTPRequest{
Method: "GET",
Path: "/v2/",
Header: map[string]string{"Authorization": "Bearer " + respBody.Token},
ExpectStatus: http.StatusOK,
}.Check(t, s.Handler)
// phase 2: check that the token still gets accepted when a new key gets rotated in
s = setupPrimary(t, test.WithPreviousIssuerKey)
assert.HTTPRequest{
Method: "GET",
Path: "/v2/",
Header: map[string]string{"Authorization": "Bearer " + respBody.Token},
ExpectStatus: http.StatusOK,
}.Check(t, s.Handler)
// phase 3: check that the token does NOT get accepted anymore when the old key has rotated out
s = setupPrimary(t)
assert.HTTPRequest{
Method: "GET",
Path: "/v2/",
Header: map[string]string{"Authorization": "Bearer " + respBody.Token},
ExpectStatus: http.StatusUnauthorized,
ExpectBody: assert.JSONObject{
"errors": []assert.JSONObject{{
"code": string(keppel.ErrUnauthorized),
"message": "token is unverifiable: error while executing keyfunc: token signed by unknown key",
"detail": nil,
}},
},
}.Check(t, s.Handler)
}