forked from rucio/rucio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_oidc.py
1982 lines (1767 loc) · 106 KB
/
test_oidc.py
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
996
997
998
999
1000
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# 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.
import time
import traceback
import uuid
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch
from urllib.parse import parse_qs, urlparse
import pytest
from jwkest.jwt import JWT
from oic import rndstr
from rucio.common.config import config_get_bool
from rucio.common.exception import CannotAuthenticate, DatabaseException, Duplicate
from rucio.common.types import InternalAccount
from rucio.core.account import add_account
from rucio.core.authentication import redirect_auth_oidc, validate_auth_token
from rucio.core.identity import add_account_identity
from rucio.core.oidc import EXPECTED_OIDC_AUDIENCE, EXPECTED_OIDC_SCOPE, _token_cache_get, _token_cache_set, get_auth_oidc, get_token_for_account_operation, get_token_oidc, oidc_identity_string
from rucio.db.sqla import models
from rucio.db.sqla.constants import AccountType, IdentityType
from rucio.db.sqla.session import get_session
from rucio.tests.common_server import get_vo
NEW_TOKEN_DICT = {'access_token': 'eyJ3bG...',
'expires_in': 3599,
'id_token': {'sub': 'abcdefg23', 'iss': 'https://test_auth_url_string/', 'nonce': 'mynonce'},
'scope': 'openid profile',
'token_type': 'Bearer',
'audience': 'rucio'}
EXCHANGED_TOKEN_DICT = {'access_token': 'eyJ3bG...',
'expires_in': 3599,
'id_token': {'sub': 'abcdefg23', 'iss': 'https://test_auth_url_string/', 'nonce': 'mynonce'},
'scope': 'openid profile',
'token_type': 'Bearer',
'audience': 'rucio'}
def encode_access_token(tokenlist):
# tokenlist = [tokenstr, scope, audience, sub, iss]
identity = oidc_identity_string(tokenlist[3], tokenlist[4])
tokenout = tokenlist[0] + '_||_' + tokenlist[1] + '_||_' + tokenlist[2] + '_||_' + identity
return tokenout
def dencode_access_token(tokenstr):
# tokenlist = [tokenstr, scope, audience, sub, iss]
tokenout = tokenstr.split("_||_")
dictout = {'token': tokenout[0],
'scope': tokenout[1],
'audience': tokenout[2],
'identity': tokenout[3]}
return dictout
def save_validated_token(token, valid_dict, extra_dict=None, session=None):
"""
Save JWT token to the Rucio DB.
:param token: Authentication token as a variable-length string.
:param valid_dict: Validation Rucio dictionary as the output
of the __get_rucio_jwt_dict function
:returns: DB token object if successful, raises an exception otherwise.
"""
try:
if not extra_dict:
extra_dict = {}
new_token = models.Token(account=valid_dict.get('account', None),
token=token,
oidc_scope=valid_dict.get('authz_scope', None),
expired_at=valid_dict.get('lifetime', None),
audience=valid_dict.get('audience', None),
identity=valid_dict.get('identity', None),
refresh=extra_dict.get('refresh', False),
refresh_token=extra_dict.get('refresh_token', None),
refresh_expired_at=extra_dict.get('refresh_expired_at', None),
refresh_lifetime=extra_dict.get('refresh_lifetime', None),
refresh_start=extra_dict.get('refresh_start', None),
ip=extra_dict.get('ip', None))
new_token.save(session=session)
return new_token
except Exception as error:
raise Exception(error.args)
def validate_jwt(token, **kwargs):
account = kwargs.get('account', None)
oidc_token_dict = dencode_access_token(token)
expirydate = datetime.utcnow() + timedelta(seconds=3600)
validate_dict = {'account': account,
'identity': oidc_token_dict['identity'],
'lifetime': expirydate,
'audience': oidc_token_dict['audience'],
'authz_scope': oidc_token_dict['scope']}
return validate_dict
def create_preexisting_exchange_token(request_args, session=None):
oidc_tokens = EXCHANGED_TOKEN_DICT.copy()
oidc_tokens['scope'] = request_args['scope']
oidc_tokens['audience'] = request_args['audience']
oidc_tokens['expires_in'] = 3600
oidc_tokens['id_token'] = {'sub': request_args['client_id'], 'iss': request_args['issuer']}
access_token = encode_access_token([request_args['token'],
request_args['scope'],
request_args['audience'],
request_args['client_id'],
request_args['issuer']])
validate_dict = validate_jwt(access_token, account=request_args['account'])
pre_existing_token = save_validated_token(access_token, validate_dict, session=session)
return pre_existing_token
def get_mock_oidc_client(**kwargs):
# issuer_id = kwargs.get('issuer_id', None)
# redirect_to = kwargs.get('redirect_to', None)
state = str(kwargs.get('state', None))
nonce = str(kwargs.get('nonce', None))
# scope = kwargs.get('scope', None)
# audience = kwargs.get('audience', None)
# first_init = kwargs.get('first_init', None)
return {'client': MockClientOIDC(),
'state': state,
'nonce': nonce,
'auth_url': 'https://test_auth_url_string?state=' + state + '&nonce=' + nonce,
'redirect': 'https://test_redirect_string'}
def get_oauth_session_row(account, state=None, session=None):
if state:
result = session.query(models.OAuthRequest).filter_by(account=account, state=state).all() # pylint: disable=no-member
else:
result = session.query(models.OAuthRequest).filter_by(account=account).all() # pylint: disable=no-member
return result
def get_token_row(access_token, account=None, session=None) -> models.Token:
if account:
token = session.get(models.Token, access_token)
if token:
assert token.account == account
return token
else:
return session.get(models.Token, access_token)
class MockADMINClientISSOIDC(MagicMock):
# pylint: disable=unused-argument
client_secret = 'topsecret_nr1'
@classmethod
def do_any(cls, request=None, request_args=None, response=None):
oidc_tokens = EXCHANGED_TOKEN_DICT.copy()
oidc_tokens['scope'] = request_args['scope']
oidc_tokens['audience'] = request_args['audience']
oidc_tokens['id_token'] = {'sub': request_args['client_id'], 'iss': 'https://test_issuer/'}
# we need to passs the full dict in the access_token key again in order to have a chance to bypas the token validation method
access_token = encode_access_token([oidc_tokens['access_token'], oidc_tokens['scope'],
oidc_tokens['audience'], request_args['client_id'], 'https://test_issuer/'])
oidc_tokens['access_token'] = access_token
return oidc_tokens
@classmethod
def construct_AuthorizationRequest(cls, request_args=None):
return None
@classmethod
def parse_response(cls, AuthorizationResponse, info=None, sformat="urlencoded"):
return None
class MockResponse:
def __init__(self, json_data):
self.json_data = json_data
def json(self):
return self.json_data
class MockClientOIDC(MagicMock):
# pylint: disable=unused-argument
@classmethod
def do_access_token_request(cls, state=None, request_args={},
authn_method="client_secret_basic", skew=120):
if request_args['code'] == 'wrongcode':
return {'error': 'Unknown AuthZ code provided'}
else:
return NEW_TOKEN_DICT
@classmethod
def construct_AuthorizationRequest(cls, request_args=None):
return None
@classmethod
def parse_response(cls, AuthorizationResponse, info=None, sformat="urlencoded"):
return None
client_secret = 'topsecret_nr1'
@classmethod
def do_any(cls, Message, endpoint=None, state=None, request_args=None, authn_method=None):
oidc_tokens = EXCHANGED_TOKEN_DICT.copy()
oidc_token_dict = dencode_access_token(request_args['subject_token'])
user_sub = oidc_token_dict['identity'].split(',')[0].split('=')[1]
user_issuer = oidc_token_dict['identity'].split(',')[1].split('=')[1]
oidc_tokens['scope'] = request_args['scope']
oidc_tokens['audience'] = request_args['audience']
oidc_tokens['id_token'] = {'sub': user_sub, 'iss': user_issuer}
# we need to passs the full dict in the access_token key again in order to have a chance to bypas the token validation method
access_token = encode_access_token([oidc_tokens['access_token'], oidc_tokens['scope'],
oidc_tokens['audience'], user_sub, user_issuer])
oidc_tokens['access_token'] = access_token
return MockResponse(oidc_tokens)
class MockADMINClientOtherISSOIDC(MagicMock):
# pylint: disable=unused-argument
client_secret = 'topsecret_nr2'
@classmethod
def do_any(cls, request=None, request_args=None, response=None):
oidc_tokens = EXCHANGED_TOKEN_DICT.copy()
oidc_tokens['scope'] = request_args['scope']
oidc_tokens['audience'] = request_args['audience']
oidc_tokens['id_token'] = {'sub': request_args['client_id'], 'iss': 'https://test_other_issuer/'}
# we need to passs the full dict in the access_token key again in order to have a chance to bypas the token validation method
access_token = encode_access_token([oidc_tokens['access_token'], oidc_tokens['scope'],
oidc_tokens['audience'], request_args['client_id'], 'https://test_other_issuer/'])
oidc_tokens['access_token'] = access_token
return oidc_tokens
@classmethod
def construct_AuthorizationRequest(cls, request_args=None):
return None
@classmethod
def parse_response(cls, AuthorizationResponse, info=None, sformat="urlencoded"):
return None
@pytest.mark.noparallel(reason='fails when run in parallel')
class TestAuthCoreAPIoidc:
""" OIDC Core API Testing: Testing creation of authorization URL for Rucio Client,
token request, token exchange, admin token request, finding token for an account.
TO-DO tests for: exchange_token_oidc, get_token_for_account_operation, get_admin_token_oidc
setUp function (below) runs first (nose does this automatically)
"""
# pylint: disable=unused-argument
def setup_method(self):
if config_get_bool('common', 'multi_vo', raise_exception=False, default=False):
self.vo = {'vo': get_vo()}
else:
self.vo = {}
self.db_session = get_session()
self.accountstring = 'test_' + rndstr()
self.accountstring = self.accountstring.lower()
self.account = InternalAccount(self.accountstring, **self.vo)
self.adminaccountstring = 'admin_' + rndstr()[:-1] # Too long to use full string
print("ADMIN ACOUNT STRING: ", self.adminaccountstring)
self.adminaccountstring = self.adminaccountstring.lower()
self.adminaccount = InternalAccount(self.adminaccountstring, **self.vo)
self.adminaccSUB = str('adminSUB' + rndstr()).lower()
self.adminaccSUB_otherISS = str('adminSUB_otherISS' + rndstr()).lower()
self.adminClientSUB = str('adminclientSUB' + rndstr()).lower()
self.adminClientSUB_otherISS = str('adminclientSUB_otherISS' + rndstr()).lower()
try:
add_account(self.account, AccountType.USER, 'rucio@email.com', session=self.db_session)
except Duplicate:
pass
try:
add_account(self.adminaccount, AccountType.SERVICE, 'rucio@email.com', session=self.db_session)
except Duplicate:
pass
try:
add_account_identity('SUB=knownsub, ISS=https://test_issuer/', IdentityType.OIDC, self.account, 'rucio_test@test.com', session=self.db_session)
add_account_identity('SUB=%s, ISS=https://test_issuer/' % self.adminaccSUB, IdentityType.OIDC, self.adminaccount, 'rucio_test@test.com', session=self.db_session)
add_account_identity('SUB=%s, ISS=https://test_other_issuer/' % self.adminaccSUB_otherISS, IdentityType.OIDC, self.adminaccount, 'rucio_test@test.com', session=self.db_session)
add_account_identity('SUB=%s, ISS=https://test_issuer/' % self.adminClientSUB, IdentityType.OIDC, self.adminaccount, 'rucio_test@test.com', session=self.db_session)
add_account_identity('SUB=%s, ISS=https://test_other_issuer/' % self.adminClientSUB_otherISS, IdentityType.OIDC, self.adminaccount, 'rucio_test@test.com', session=self.db_session)
except DatabaseException:
pass
def teardown_method(self):
self.db_session.remove()
def get_auth_init_and_mock_response(self, code_response, account=None, polling=False, auto=True, session=None):
"""
OIDC creates entry in oauth_requests table
returns: auth_query_string (state=xxx&code=yyy
as would be returned from the IdP
after a successful authentication)
"""
if not account:
account = self.account
kwargs = {
'auth_scope': 'openid profile',
'audience': 'rucio',
'issuer': 'dummy_admin_iss_nickname',
'auto': auto,
'polling': polling,
'refresh_lifetime': 96,
'ip': None,
'webhome': 'https://rucio-test.cern.ch/ui',
}
auth_url = get_auth_oidc(account, session=session, **kwargs)
print("[get_auth_init_and_mock_response] got auth_url:", auth_url)
# get the state from the auth_url and add an arbitrary code value to the query string
# to mimick a return of IdP with authz_code
urlparsed = urlparse(auth_url)
if ('_polling' in auth_url) or (not polling and not auto):
auth_url = redirect_auth_oidc(urlparsed.query, session=session)
print("[get_auth_init_and_mock_response] got redirect auth_url:", auth_url)
urlparsed = urlparse(auth_url)
urlparams = parse_qs(urlparsed.query)
assert 'state' in urlparams
state = urlparams["state"][0]
assert 'nonce' in urlparams
nonce = urlparams["nonce"][0]
auth_query_string = "state=" + state + "&code=" + code_response
return {'state': state, 'nonce': nonce, 'auth_url': auth_url, 'auth_query_string': auth_query_string}
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_auth_oidc_url(self, mock_clients, mock_oidc_client):
""" OIDC Auth URL generation
Runs the Test:
- calling the respective function
End:
- checking the URL to be as expected
"""
mock_oidc_client.side_effect = get_mock_oidc_client
try:
kwargs = {'auth_scope': 'openid profile',
'audience': 'rucio',
'issuer': 'dummy_admin_iss_nickname',
'auto': False,
'polling': False,
'refresh_lifetime': 96,
'ip': None,
'webhome': None}
# testing classical CLI login init, expecting user to be
# redirected via Rucio Auth server to the IdP issuer for login
auth_url = get_auth_oidc(self.account, session=self.db_session, **kwargs)
assert 'https://test_redirect_string/auth/oidc_redirect?' in auth_url and '_polling' not in auth_url
# testing classical CLI login init, expecting user to be redirected
# via Rucio Auth server to the IdP issuer for login and Rucio Client
# to be polling the Rucio Auth server for token until done so
kwargs['polling'] = True
auth_url = get_auth_oidc(self.account, session=self.db_session, **kwargs)
assert 'https://test_redirect_string/auth/oidc_redirect?' in auth_url and '_polling' in auth_url
# testing classical CLI login init, with the Rucio Client being
# trusted with IdP user credentials (auto = True). Rucio Client
# gets directly the auth_url pointing it to the IdP
kwargs['polling'] = False
kwargs['auto'] = True
auth_url = get_auth_oidc(self.account, session=self.db_session, **kwargs)
assert 'https://test_auth_url_string' in auth_url
# testing webui login URL (auto = True, polling = False)
kwargs['webhome'] = 'https://back_to_rucio_ui_page'
auth_url = get_auth_oidc(InternalAccount('webui', **self.vo), session=self.db_session, **kwargs)
assert 'https://test_auth_url_string' in auth_url
except:
print(traceback.format_exc())
def test_get_token_oidc_unknown_state(self):
""" OIDC Token request with unknown state from IdP
Runs the Test:
- requesting token with parameters without coresponding
DB entry (in oauth_Requests table)
End:
- checking the relevant exception to be thrown
"""
try:
auth_query_string = "state=" + rndstr() + "&code=" + rndstr()
get_token_oidc(auth_query_string, session=self.db_session)
except CannotAuthenticate:
assert "could not keep track of responses from outstanding requests" in traceback.format_exc()
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_token_oidc_unknown_code(self, mock_clients, mock_oidc_client):
""" OIDC Token request with unknown code from IdP
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- calling the get_token_oidc core function
End:
- checking the relevant exception to be thrown
"""
mock_oidc_client.side_effect = get_mock_oidc_client
try:
auth_init_response = self.get_auth_init_and_mock_response(code_response='wrongcode', session=self.db_session)
# check if DB entry exists
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
except CannotAuthenticate:
assert "Unknown AuthZ code provided" in traceback.format_exc()
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_token_oidc_unknown_nonce(self, mock_clients, mock_oidc_client):
""" OIDC Token request with unknown nonce from IdP
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- calling the get_token_oidc core function
End:
- checking the relevant exception to be thrown
"""
mock_oidc_client.side_effect = get_mock_oidc_client
try:
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), session=self.db_session)
# check if DB entry exists
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
NEW_TOKEN_DICT['id_token']['nonce'] = 'wrongnonce'
get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
except CannotAuthenticate:
assert "This points to possible replay attack !" in traceback.format_exc()
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_token_oidc_unknown_account_identity(self, mock_clients, mock_oidc_client):
""" OIDC Token request with unknown account identity in the token from IdP
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- calling the get_token_oidc core function
End:
- checking the relevant exception to be thrown
"""
mock_oidc_client.side_effect = get_mock_oidc_client
try:
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), session=self.db_session)
# check if DB entry exists
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
NEW_TOKEN_DICT['id_token'] = {'sub': 'unknownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
except CannotAuthenticate:
assert "OIDC identity 'SUB=unknownsub, ISS=https://test_issuer/' of the '" + self.accountstring + "' account is unknown to Rucio." in traceback.format_exc()
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_token_oidc_unknown_webui_account_identity(self, mock_clients, mock_oidc_client):
""" OIDC Token request with unknown webui identity in the token from IdP
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- calling the get_token_oidc core function
End:
- checking the relevant exception to be thrown
"""
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), account=InternalAccount('webui', **self.vo), session=self.db_session)
# check if DB entry exists
oauth_session_row = get_oauth_session_row(InternalAccount('webui', **self.vo), state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
NEW_TOKEN_DICT['id_token'] = {'sub': 'unknownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict['webhome'] is None
assert token_dict['token'] is None
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_access_token_oidc_success(self, mock_clients, mock_oidc_client):
""" OIDC Request for access token - success
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- filling the right identity into the token (mocking the IdP response)
- calling the get_token_oidc core function
End:
- checking the relevant exception to be thrown
"""
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), session=self.db_session)
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# mocking the token response
access_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
db_token = get_token_row(access_token, account=self.account, session=self.db_session)
assert db_token
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_access_token_oidc_webui_success(self, mock_clients, mock_oidc_client):
""" OIDC Request for access token via webui 'account' - success
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- filling the right identity into the token (mocking the IdP response)
- calling the get_token_oidc core function
End:
- checking if the right token is saved in the DB and if it is present
in the return dict of the get_token_oidc fucntion
"""
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), account=InternalAccount('webui', **self.vo), session=self.db_session)
oauth_session_row = get_oauth_session_row(InternalAccount('webui', **self.vo), state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# mocking the token response
access_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
assert token_dict['webhome'] is not None
assert token_dict['token']['token'] == access_token
# not checking the account specifically as it may be that the
# identity was registered for other accounts in previous tests
db_token = get_token_row(access_token, session=self.db_session)
assert db_token
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_access_token_oidc_cli_polling_success(self, mock_clients, mock_oidc_client):
""" OIDC Request for access token while client is polling - success
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- filling the right identity into the token (mocking the IdP response)
- calling the get_token_oidc core function
End:
- checking if the token is in the DB and no token is being returned from the core function
"""
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), polling=True, auto=False, session=self.db_session)
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# mocking the token response
access_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
assert token_dict['polling'] is True
assert 'token' not in token_dict
# not checking the account specifically as it may be that the
# identity was registered for other accounts in previous tests
db_token = get_token_row(access_token, session=self.db_session)
assert db_token
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_access_token_oidc_cli_fetchcode_success(self, mock_clients, mock_oidc_client):
""" OIDC Request for access token, client receives fetchcode - success
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- filling the right identity into the token (mocking the IdP response)
- calling the get_token_oidc core function
End:
- checking if the token is in the DB and a fetchcode is being returned from the core function
- fetching the token
"""
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), polling=False, auto=False, session=self.db_session)
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# mocking the token response
access_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
assert 'fetchcode' in token_dict
assert 'token' not in token_dict
# not checking the account specifically as it may be that the
# identity was registered for other accounts in previous tests
db_token = get_token_row(access_token, session=self.db_session)
assert db_token
token = redirect_auth_oidc(token_dict['fetchcode'], fetchtoken=True, session=self.db_session)
assert token == access_token
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_access_and_refresh_tokens_oidc_success(self, mock_clients, mock_oidc_client):
""" OIDC Request for access and refresh tokens - success
Runs the Test:
- getting the auth_query_string (mocking the IdP response)
and with it the corresponding entry in the oauth_requests table
- filling the right identity into the token (mocking the IdP response)
- calling the get_token_oidc core function
End:
- checking the relevant exception to be thrown
"""
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), session=self.db_session)
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# mocking the token response
access_token = rndstr()
refresh_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['refresh_token'] = refresh_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
db_token = get_token_row(access_token, account=self.account, session=self.db_session)
assert db_token
assert db_token.token == access_token
assert db_token.refresh_token == refresh_token
@patch('rucio.core.oidc.JWS')
@patch('rucio.core.oidc.__get_rucio_jwt_dict')
@patch('rucio.core.oidc.OIDC_CLIENTS')
def test_validate_and_save_external_token_success(self, mock_oidc_clients, mock_jwt_dict, mock_jws):
""" OIDC validate externally provided token with correct audience, scope and issuer - success
Runs the Test:
- mocking the OIDC client, and token validation dictionary pretending
the externally passed token is valid (time, issuer, audience, scope all as expected)
- calling the validate_auth_token core function (which is being called
e.g. when trying to validate tokens passed to rucio in the header of a request
End:
- checking if the external token has been saved in the DB
"""
mock_oidc_clients.return_value = {'https://test_issuer/': MockClientOIDC()}
token_validate_dict = {'account': self.account,
'identity': 'SUB=knownsub, ISS=https://test_issuer/',
'lifetime': datetime.utcfromtimestamp(time.time() + 60),
'audience': 'rucio',
'authz_scope': 'openid profile'}
mock_jwt_dict.return_value = token_validate_dict
# mocking the token response
access_token = rndstr() + '.' + rndstr() + '.' + rndstr()
# trying to validate a token that does not exist in the Rucio DB
value = validate_auth_token(access_token, session=self.db_session)
# checking if validation went OK (we bypassed it with the dictionary above)
assert value == token_validate_dict
# most importantly, check that the token was saved in Rucio DB
db_token = get_token_row(access_token, account=self.account, session=self.db_session)
assert db_token
assert db_token.token == access_token
@patch('rucio.core.oidc.JWS')
@patch('rucio.core.oidc.__get_rucio_jwt_dict')
@patch('rucio.core.oidc.OIDC_CLIENTS')
def test_validate_and_save_external_token_fail(self, mock_oidc_clients, mock_jwt_dict, mock_jws):
""" OIDC validate externally provided token with correct audience, scope and issuer - failure
Runs the Test:
- mocking the OIDC client, and token validation dictionary pretending
the externally passed token has invalid audience
- calling the validate_auth_token core function (which is being called
e.g. when trying to validate tokens passed to rucio in the header of a request
End:
- checking if the external token was not saved in the DB
"""
mock_oidc_clients.return_value = {'https://test_issuer/': MockClientOIDC()}
token_validate_dict = {'account': self.account,
'identity': 'SUB=knownsub, ISS=https://test_issuer/',
'lifetime': datetime.utcfromtimestamp(time.time() + 60),
'audience': 'unknown_audience',
'authz_scope': 'openid profile'}
mock_jwt_dict.return_value = token_validate_dict
# mocking the token response
access_token = rndstr() + '.' + rndstr() + '.' + rndstr()
# trying to validate a token that does not exist in the Rucio DB
with pytest.raises(CannotAuthenticate):
validate_auth_token(access_token, session=self.db_session)
# most importantly, check that the token was saved in Rucio DB
db_token = get_token_row(access_token, account=self.account, session=self.db_session)
assert not db_token
@patch('rucio.core.oidc.__get_rucio_jwt_dict')
@patch('rucio.core.oidc.OIDC_ADMIN_CLIENTS')
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_AT_for_account_operation_1(self, mock_clients, mock_oidc_client, admin_clients, validate_jwt_dict):
""" Request for OIDC token for FTS transfer (adminON_useraccREQ_hasSUBtoken_NoFinalPreexistingToken)
Setting initial conditions:
- requesting Rucio Admin token
- for a user account name
- user HAS valid subject token
- final token for FTS transfer with required scope and audience does NOT exist already
Runs the Test:
- see actions below
End:
- checking that the final result has issuer same as user OIDC identity issuer of the subject token
- checking that the final token belongs to the admin account
- final token has the requested scope and audience claims in
"""
# ---------------------------
# setting conditions of the test
req_scope = 'transfer_scope_' + rndstr() + ' some_other_scope' + rndstr()
req_audience = 'transfer_audience_' + rndstr() + ' some_other_audience' + rndstr()
req_account = self.account
final_token_account = self.adminaccount
req_admin = True
# ---------------------------
# giving a USER a subject token
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), session=self.db_session)
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# mocking the token response
access_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
db_token = get_token_row(access_token, account=self.account, session=self.db_session)
assert db_token
# ---------------------------
# preparing the expected resulting token
expected_access_token_strpart = rndstr()
EXCHANGED_TOKEN_DICT['access_token'] = expected_access_token_strpart
expected_access_token = encode_access_token([expected_access_token_strpart, req_scope, req_audience, self.adminClientSUB, 'https://test_issuer/'])
# for OIDC_ADMIN_CLIENTS in __get_admin_token_oidc we need to mock result of __get_rucio_oidc_clients
MockAdminOIDCClients = {'https://test_other_issuer/': MockADMINClientOtherISSOIDC(client_id=self.adminClientSUB_otherISS),
'https://test_issuer/': MockADMINClientISSOIDC(client_id=self.adminClientSUB)}
admin_clients.__getitem__.side_effect = MockAdminOIDCClients.__getitem__
admin_clients.__iter__.side_effect = MockAdminOIDCClients.__iter__
admin_clients.__contains__.side_effect = MockAdminOIDCClients.__contains__
admin_clients.keys.side_effect = MockAdminOIDCClients.keys
validate_jwt_dict.side_effect = validate_jwt
mock_oidc_client.side_effect = get_mock_oidc_client
# ---------------------------
# ASKING FOR THE TOKEN
new_token_dict = get_token_for_account_operation(req_account, req_audience=req_audience, req_scope=req_scope, admin=req_admin, session=self.db_session)
# ---------------------------
# Check if token has been received
assert new_token_dict
# ---------------------------
# Check of token being in DB under the expected account
db_token = get_token_row(new_token_dict['token'], account=final_token_account, session=self.db_session)
assert db_token
# ---------------------------
# Check hat the final result has issuer same as user OIDC identity issuer of the subject token
assert 'https://test_issuer/' in db_token.identity
assert self.adminClientSUB in db_token.identity
# ---------------------------
# Check hat the final result has issuer same as user OIDC identity issuer of the subject token
assert req_scope == db_token.oidc_scope
# ---------------------------
# Check hat the final result has issuer same as user OIDC identity issuer of the subject token
assert req_audience == db_token.audience
# ---------------------------
# Check that the resulting token is NOT same as original
assert token_dict['token'] != new_token_dict['token']
# -----
# check that result is as expected
assert expected_access_token == new_token_dict['token']
@patch('rucio.core.oidc.__get_rucio_jwt_dict')
@patch('rucio.core.oidc.OIDC_ADMIN_CLIENTS')
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_AT_for_account_operation_2(self, mock_clients, mock_oidc_client, admin_clients, validate_jwt_dict):
""" Request for OIDC token for FTS transfer (adminON_useraccREQ_hasSUBtoken_HasPreexistingFinalToken)
Setting initial conditions:
- requesting Rucio Admin token
- for a user account name
- user HAS valid subject token
- final token for FTS transfer with required scope and audience does exist already
Runs the Test:
- see actions below
End:
- checking that the final result has issuer same as user OIDC identity issuer of the subject token
- checking that the final token belongs to the admin account
- final token has the requested scope and audience claims in and is the same as the preexisting one
"""
# ---------------------------
# setting conditions of the test
req_scope = 'transfer_scope_' + rndstr() + ' some_other_scope' + rndstr()
req_audience = 'transfer_audience_' + rndstr() + ' some_other_audience' + rndstr()
req_account = self.account
final_token_account = self.adminaccount
final_token_issuer = 'https://test_issuer/'
req_admin = True
# ---------------------------
# giving a USER a subject token
mock_oidc_client.side_effect = get_mock_oidc_client
auth_init_response = self.get_auth_init_and_mock_response(code_response=rndstr(), session=self.db_session)
oauth_session_row = get_oauth_session_row(self.account, state=auth_init_response['state'], session=self.db_session)
assert oauth_session_row
# ---------------------------
# mocking the token response
access_token = rndstr()
NEW_TOKEN_DICT['access_token'] = access_token
NEW_TOKEN_DICT['id_token'] = {'sub': 'knownsub', 'iss': 'https://test_issuer/', 'nonce': auth_init_response['nonce']}
token_dict = get_token_oidc(auth_init_response['auth_query_string'], session=self.db_session)
assert token_dict
db_token = get_token_row(access_token, account=self.account, session=self.db_session)
assert db_token
# ---------------------------
# giving the final token account PRE EXISTING FINAL token
preexisting_access_token_strpart = rndstr()
request_args = {'scope': req_scope,
'audience': req_audience,
'client_id': self.adminClientSUB,
'issuer': final_token_issuer,
'account': final_token_account,
'token': preexisting_access_token_strpart}
expected_preexisting_access_token_object = create_preexisting_exchange_token(request_args, session=self.db_session)
expected_preexisting_access_token = expected_preexisting_access_token_object.token
db_token = get_token_row(expected_preexisting_access_token, account=final_token_account, session=self.db_session)
assert db_token
# ---------------------------
# preparing the expected resulting token
not_expected_access_token_strpart = rndstr()
EXCHANGED_TOKEN_DICT['access_token'] = not_expected_access_token_strpart
not_expected_access_token = encode_access_token([not_expected_access_token_strpart, req_scope, req_audience, self.adminClientSUB, 'https://test_issuer/'])
# mocking additional objects
MockAdminOIDCClients = {'https://test_other_issuer/': MockADMINClientOtherISSOIDC(client_id=self.adminClientSUB_otherISS),
'https://test_issuer/': MockADMINClientISSOIDC(client_id=self.adminClientSUB)}
admin_clients.__getitem__.side_effect = MockAdminOIDCClients.__getitem__
admin_clients.__iter__.side_effect = MockAdminOIDCClients.__iter__
admin_clients.__contains__.side_effect = MockAdminOIDCClients.__contains__
admin_clients.keys.side_effect = MockAdminOIDCClients.keys
validate_jwt_dict.side_effect = validate_jwt
mock_oidc_client.side_effect = get_mock_oidc_client
# ---------------------------
# ASKING FOR THE TOKEN
new_token_dict = get_token_for_account_operation(req_account, req_audience=req_audience, req_scope=req_scope, admin=req_admin, session=self.db_session)
# ---------------------------
# Check if token has been received
assert new_token_dict
# ---------------------------
# Check of token being in DB under the expected account
db_token = get_token_row(new_token_dict['token'], account=final_token_account, session=self.db_session)
assert db_token
# ---------------------------
# Check hat the final result has issuer same as user OIDC identity issuer of the subject token
assert 'https://test_issuer/' in db_token.identity
# and that the SUB claim is as expected the admin client_id
assert self.adminClientSUB in db_token.identity
# ---------------------------
# Check hat the final result has issuer same as user OIDC identity issuer of the subject token
assert req_scope == db_token.oidc_scope
# ---------------------------
# Check hat the final result has issuer same as user OIDC identity issuer of the subject token
assert req_audience == db_token.audience
# ---------------------------
# check that the not expected token is not in the DB
db_token = get_token_row(not_expected_access_token, session=self.db_session)
assert not db_token
# ---------------------------
# Check that it has the expected token string
assert expected_preexisting_access_token == new_token_dict['token']
@patch('rucio.core.oidc.__get_rucio_jwt_dict')
@patch('rucio.core.oidc.OIDC_ADMIN_CLIENTS')
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_AT_for_account_operation_3(self, mock_clients, mock_oidc_client, admin_clients, validate_jwt_dict):
""" Request for OIDC token for FTS transfer (adminON_useraccREQ_NOSUBtoken_NoFinalPreexistingToken)
Setting initial conditions:
- requesting Rucio Admin token
- for a user account name
- user DOES NOT HAVE valid subject token
- final token for FTS transfer with required scope and audience does NOT exist apriori
Runs the Test:
- see actions below
End:
- checking that the final result is NOne as the user has no valid OIDC token in the DB to start with !
"""
# ---------------------------
# mocking additional objects
MockAdminOIDCClients = {'https://test_other_issuer/': MockADMINClientOtherISSOIDC(client_id=self.adminClientSUB_otherISS),
'https://test_issuer/': MockADMINClientISSOIDC(client_id=self.adminClientSUB)}
admin_clients.__getitem__.side_effect = MockAdminOIDCClients.__getitem__
admin_clients.__iter__.side_effect = MockAdminOIDCClients.__iter__
admin_clients.__contains__.side_effect = MockAdminOIDCClients.__contains__
admin_clients.keys.side_effect = MockAdminOIDCClients.keys
validate_jwt_dict.side_effect = validate_jwt
mock_oidc_client.side_effect = get_mock_oidc_client
# setting conditions of the test
req_scope = 'transfer_scope_' + rndstr() + ' some_other_scope' + rndstr()
req_audience = 'transfer_audience_' + rndstr() + ' some_other_audience' + rndstr()
req_account = self.account
req_admin = True
# ---------------------------
# ASKING FOR THE TOKEN
new_token_dict = get_token_for_account_operation(req_account, req_audience=req_audience, req_scope=req_scope, admin=req_admin, session=self.db_session)
# ---------------------------
# Check if NO token has been received
assert not new_token_dict
@patch('rucio.core.oidc.__get_rucio_jwt_dict')
@patch('rucio.core.oidc.OIDC_ADMIN_CLIENTS')
@patch('rucio.core.oidc.__get_init_oidc_client')
@patch('rucio.core.oidc.__get_rucio_oidc_clients')
def test_get_AT_for_account_operation_4(self, mock_clients, mock_oidc_client, admin_clients, validate_jwt_dict):
""" Request for OIDC token for FTS transfer (adminON_useraccREQ_NOSUBtoken_HasPreexistingFinalToken)
Setting initial conditions:
- requesting Rucio Admin token
- for a user account name
- user HAS NO valid subject token