11"""Test Financial-Grade API policy with oauth2 certificate bound access token (advanced profile)"""
22
3- import os
43import pytest
54import requests
65
76from testsuite import rawobj
8- from testsuite .utils import blame
7+ from testsuite .utils import blame , warn_and_skip
98from testsuite .rhsso import OIDCClientAuth , OIDCClientAuthHook
10- # pylint: disable=reimported,unused-import
11- from testsuite .tests .apicast .policy .tls .conftest import (manager , superdomain , server_authority , staging_gateway ,
12- gateway_options , gateway_environment )
13- # pylint: disable=reimported,unused-import
14- from testsuite .tests .apicast .policy .tls .conftest import (certificate , superdomain , valid_authority , create_cert ,
15- superdomain , manager )
9+ from testsuite .certificates import Certificate
10+
11+ # pylint: disable=reimported, unused-import
12+ # flake8: noqa
13+ from testsuite .tests .apicast .policy .tls .conftest import (
14+ certificate ,
15+ manager ,
16+ superdomain ,
17+ server_authority ,
18+ staging_gateway ,
19+ gateway_options ,
20+ gateway_environment ,
21+ valid_authority ,
22+ create_cert ,
23+ )
1624
1725
18- @pytest .fixture ()
19- def mtls_client_cert ():
26+ @pytest .fixture (scope = "module" )
27+ def mtls_client_cert (request , testconfig ):
2028 """Clients certificate. CA of this cert needs to be trusted by the sso"""
21- # todo create certs dynamically
22- script_dir = os .path .dirname (os .path .abspath (__file__ ))
23- crt_path = os .path .join (script_dir , "certificates" , "client.crt" )
24- key_path = os .path .join (script_dir , "certificates" , "client.key" )
25- return crt_path , key_path
29+ try :
30+ crt = testconfig ["shared_certs" ]["client_certs" ]["valid" ][0 ]
31+ except IndexError :
32+ warn_and_skip ("Valid tools cert is not available, skipping fapi advanced tests" )
33+ cert = Certificate (crt ["key" ], crt ["crt" ])
34+ if not testconfig ["skip_cleanup" ]:
35+ request .addfinalizer (cert .delete_files )
36+ return cert .files ["certificate" ], cert .files ["key" ]
2637
2738
39+ # flake8: noqa
2840@pytest .fixture ()
2941def unknown_cert (certificate ):
3042 """Cert which won't be used for obtaining the token"""
@@ -40,10 +52,13 @@ def service(service):
4052 3scale APIcast
4153 Fapi
4254 """
43- fapi_policy = rawobj .PolicyConfig ("fapi" , configuration = {
44- "validate_x_fapi_customer_ip_address" : True ,
45- "validate_oauth2_certificate_bound_access_token" : True
46- })
55+ fapi_policy = rawobj .PolicyConfig (
56+ "fapi" ,
57+ configuration = {
58+ "validate_x_fapi_customer_ip_address" : True ,
59+ "validate_oauth2_certificate_bound_access_token" : True ,
60+ },
61+ )
4762 service .proxy .list ().policies .insert (1 , fapi_policy )
4863 return service
4964
@@ -56,16 +71,15 @@ def fapi_sso_client_id():
5671
5772# pylint: disable=too-many-arguments
5873@pytest .fixture (scope = "module" )
59- def application (service , custom_application , custom_app_plan , lifecycle_hooks , request , fapi_sso_client_id ,
60- rhsso_service_info ):
74+ def application (
75+ service , custom_application , custom_app_plan , lifecycle_hooks , request , fapi_sso_client_id , rhsso_service_info
76+ ):
6177 """application bound to the account and service existing over whole testing session"""
6278 plan = custom_app_plan (rawobj .ApplicationPlan (blame (request , blame (request , "fapi" ))), service )
6379 app = custom_application (
64- rawobj .Application (
65- name = fapi_sso_client_id ,
66- app_id = fapi_sso_client_id ,
67- application_plan = plan ),
68- hooks = lifecycle_hooks )
80+ rawobj .Application (name = fapi_sso_client_id , app_id = fapi_sso_client_id , application_plan = plan ),
81+ hooks = lifecycle_hooks ,
82+ )
6983 service .proxy .deploy ()
7084 app .register_auth ("oidc" , OIDCClientAuth .partial (rhsso_service_info ))
7185 return app
@@ -88,14 +102,15 @@ def fapi_sso_client(rhsso_service_info, fapi_sso_client_id):
88102 "tls.client.certificate.bound.access.tokens" : "true" ,
89103 "client.authentication.type" : "tls" ,
90104 "x509.allow.regex.pattern.comparison" : "true" ,
91- }
105+ },
92106 }
93107 return rhsso_service_info .realm .create_client (fapi_sso_client_id , ** client_config )
94108
95109
96110# pylint: disable=too-few-public-methods
97111class FapiClient :
98112 """Temporary class for sending requests to apicast. This should be replaced by using fixture api_client"""
113+
99114 def __init__ (self , base_url , verify ):
100115 self .base_url = base_url
101116 self .http_session = requests .Session ()
0 commit comments