10
10
from requests .structures import CaseInsensitiveDict
11
11
import responses
12
12
13
- from pbench .server import JSON
13
+ from pbench .server import JSON , PbenchServerConfig
14
14
import pbench .server .auth
15
15
from pbench .server .auth import Connection , OpenIDClient , OpenIDClientError
16
16
import pbench .server .auth .auth as Auth
@@ -529,7 +529,9 @@ def test_get_auth_token_succ(self, monkeypatch, make_logger):
529
529
"headers" ,
530
530
[{"Authorization" : "not-bearer my-token" }, {"Authorization" : "no-space" }, {}],
531
531
)
532
- def test_get_auth_token_fail (self , monkeypatch , make_logger , headers ):
532
+ def test_get_auth_token_fail (
533
+ self , monkeypatch , server_config , make_logger , headers
534
+ ):
533
535
"""Verify error handling fetching the authorization token from HTTP
534
536
headers
535
537
"""
@@ -548,6 +550,7 @@ def record_abort(code: int, message: str = ""):
548
550
549
551
app = Flask ("test-get-auth-token-fail" )
550
552
app .logger = make_logger
553
+ app .server_config = server_config
551
554
with app .app_context ():
552
555
monkeypatch .setattr (Auth , "request" , MockRequest (headers = headers ))
553
556
try :
@@ -563,36 +566,41 @@ def record_abort(code: int, message: str = ""):
563
566
)
564
567
assert record ["code" ] == expected_code
565
568
566
- def test_verify_auth (self , make_logger , pbench_drb_token ):
569
+ def test_verify_auth (self , server_config , make_logger , pbench_drb_token ):
567
570
"""Verify success path of verify_auth"""
568
571
app = Flask ("test-verify-auth" )
569
572
app .logger = make_logger
573
+ app .server_config = server_config
570
574
with app .app_context ():
571
575
current_app .secret_key = jwt_secret
572
576
user = Auth .verify_auth (pbench_drb_token )
573
577
assert user .id == DRB_USER_ID
574
578
575
- def test_verify_auth_invalid (self , make_logger , pbench_drb_token_invalid ):
579
+ def test_verify_auth_invalid (
580
+ self , server_config , make_logger , pbench_drb_token_invalid
581
+ ):
576
582
"""Verify handling of an invalid (expired) token in verify_auth"""
577
583
app = Flask ("test-verify-auth-invalid" )
578
584
app .logger = make_logger
585
+ app .server_config = server_config
579
586
with app .app_context ():
580
587
current_app .secret_key = jwt_secret
581
588
user = Auth .verify_auth (pbench_drb_token_invalid )
582
589
assert user is None
583
590
584
- def test_verify_auth_invsig (self , make_logger , pbench_drb_token ):
591
+ def test_verify_auth_invsig (self , server_config , make_logger , pbench_drb_token ):
585
592
"""Verify handling of a token with an invalid signature"""
586
593
app = Flask ("test-verify-auth-invsig" )
587
594
app .logger = make_logger
595
+ app .server_config = server_config
588
596
with app .app_context ():
589
597
current_app .secret_key = jwt_secret
590
598
user = Auth .verify_auth (pbench_drb_token + "1" )
591
599
assert user is None
592
600
593
601
@pytest .mark .parametrize ("roles" , [["ROLE" ], ["ROLE1" , "ROLE2" ], [], None ])
594
602
def test_verify_auth_oidc (
595
- self , monkeypatch , db_session , rsa_keys , make_logger , roles
603
+ self , monkeypatch , server_config , db_session , rsa_keys , make_logger , roles
596
604
):
597
605
"""Verify OIDC token offline verification success path"""
598
606
client_id = "us"
@@ -613,14 +621,15 @@ def test_verify_auth_oidc(
613
621
614
622
app = Flask ("test-verify-auth-oidc" )
615
623
app .logger = make_logger
624
+ app .server_config = server_config
616
625
with app .app_context ():
617
626
user = Auth .verify_auth (token )
618
627
619
628
assert user .id == "12345"
620
629
assert user .roles == (roles if roles else [])
621
630
622
631
def test_verify_auth_oidc_user_update (
623
- self , monkeypatch , db_session , rsa_keys , make_logger
632
+ self , monkeypatch , server_config , db_session , rsa_keys , make_logger
624
633
):
625
634
"""Verify we update our internal user database when we get updated user
626
635
payload from the OIDC token for an existing user."""
@@ -637,14 +646,15 @@ def test_verify_auth_oidc_user_update(
637
646
638
647
app = Flask ("test-verify-auth-oidc-user-update" )
639
648
app .logger = make_logger
649
+ app .server_config = server_config
640
650
with app .app_context ():
641
651
user = Auth .verify_auth (token )
642
652
643
653
assert user .id == "12345"
644
654
assert user .roles == []
645
655
assert user .username == "dummy"
646
656
647
- # Generate a new token with a role for the same user
657
+ # Generate a token with a new username for the same UUID
648
658
token , expected_payload = gen_rsa_token (
649
659
client_id ,
650
660
rsa_keys ["private_key" ],
@@ -657,7 +667,54 @@ def test_verify_auth_oidc_user_update(
657
667
assert user .roles == ["ROLE" ]
658
668
assert user .username == "new_dummy"
659
669
660
- def test_verify_auth_oidc_invalid (self , monkeypatch , rsa_keys , make_logger ):
670
+ def test_verify_auth_oidc_user_admin (
671
+ self ,
672
+ monkeypatch ,
673
+ server_config : PbenchServerConfig ,
674
+ db_session ,
675
+ rsa_keys ,
676
+ make_logger ,
677
+ ):
678
+ """Verify we update our internal user database when we get updated user
679
+ payload from the OIDC token for an existing user."""
680
+ client_id = "us"
681
+ token , expected_payload = gen_rsa_token (client_id , rsa_keys ["private_key" ])
682
+
683
+ # Mock the Connection object and generate an OpenIDClient object,
684
+ # installing it as Auth module's OIDC client.
685
+ config = mock_connection (
686
+ monkeypatch , client_id , public_key = rsa_keys ["public_key" ]
687
+ )
688
+ oidc_client = OpenIDClient .construct_oidc_client (config )
689
+ monkeypatch .setattr (Auth , "oidc_client" , oidc_client )
690
+ server_config ._conf .set ("pbench-server" , "admin-role" , "friend,dummy,admin" )
691
+
692
+ app = Flask ("test-verify-auth-oidc-user-admin" )
693
+ app .logger = make_logger
694
+ app .server_config = server_config
695
+ with app .app_context ():
696
+ user = Auth .verify_auth (token )
697
+
698
+ assert user .id == "12345"
699
+ assert user .roles == ["ADMIN" ]
700
+ assert user .username == "dummy"
701
+
702
+ # Generate a token with a role and new username for the same UUID
703
+ token , expected_payload = gen_rsa_token (
704
+ client_id ,
705
+ rsa_keys ["private_key" ],
706
+ username = "friend" ,
707
+ oidc_client_roles = ["ROLE" ],
708
+ )
709
+ with app .app_context ():
710
+ user = Auth .verify_auth (token )
711
+ assert user .id == "12345"
712
+ assert user .roles == ["ROLE" , "ADMIN" ]
713
+ assert user .username == "friend"
714
+
715
+ def test_verify_auth_oidc_invalid (
716
+ self , monkeypatch , server_config , rsa_keys , make_logger
717
+ ):
661
718
"""Verify OIDC token offline verification via Auth.verify_auth() fails
662
719
gracefully with an invalid token
663
720
"""
@@ -677,14 +734,15 @@ def tio_exc(token: str) -> JSON:
677
734
678
735
app = Flask ("test-verify-auth-oidc-invalid" )
679
736
app .logger = make_logger
737
+ app .server_config = server_config
680
738
with app .app_context ():
681
739
monkeypatch .setattr (oidc_client , "token_introspect" , tio_exc )
682
740
user = Auth .verify_auth (token )
683
741
684
742
assert user is None
685
743
686
744
def test_verify_auth_api_key (
687
- self , monkeypatch , rsa_keys , make_logger , pbench_drb_api_key
745
+ self , monkeypatch , server_config , rsa_keys , make_logger , pbench_drb_api_key
688
746
):
689
747
"""Verify api_key verification via Auth.verify_auth()"""
690
748
@@ -699,14 +757,15 @@ def tio_exc(token: str) -> JSON:
699
757
700
758
app = Flask ("test_verify_auth_api_key" )
701
759
app .logger = make_logger
760
+ app .server_config = server_config
702
761
with app .app_context ():
703
762
monkeypatch .setattr (oidc_client , "token_introspect" , tio_exc )
704
763
current_app .secret_key = jwt_secret
705
764
user = Auth .verify_auth (pbench_drb_api_key .key )
706
765
assert user .id == DRB_USER_ID
707
766
708
767
def test_verify_auth_api_key_invalid (
709
- self , monkeypatch , rsa_keys , make_logger , pbench_invalid_api_key
768
+ self , monkeypatch , server_config , rsa_keys , make_logger , pbench_invalid_api_key
710
769
):
711
770
"""Verify api_key verification via Auth.verify_auth() fails
712
771
gracefully with an invalid token
@@ -722,6 +781,7 @@ def tio_exc(token: str) -> JSON:
722
781
723
782
app = Flask ("test_verify_auth_api_key_invalid" )
724
783
app .logger = make_logger
784
+ app .server_config = server_config
725
785
with app .app_context ():
726
786
monkeypatch .setattr (oidc_client , "token_introspect" , tio_exc )
727
787
current_app .secret_key = jwt_secret
0 commit comments