@@ -61,21 +61,39 @@ def __exit__(self, exc_type, exc_value, traceback):
6161
6262class TestClientCertAuth :
6363
64- # Exact match tests for "test.client.restapi.sonic"
64+ # Exact matching tests for "test.client.restapi.sonic"
6565
6666 def test_exact_match_success (self , setup_restapi_client ):
6767 _ , _ , _ , restapi_client = setup_restapi_client
6868 with ClientCert ("test.client.restapi.sonic" ) as client_cert :
6969 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
7070 assert r .status_code == 200
7171
72- def test_exact_match_failure (self , setup_restapi_client ):
72+ def test_exact_match_failure_1 (self , setup_restapi_client ):
7373 _ , _ , _ , restapi_client = setup_restapi_client
7474 with ClientCert ("client.restapi.sonic" ) as client_cert :
7575 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
7676 assert r .status_code == 401
7777
78- # Wildcard match tests for "*.example.sonic"
78+ def test_exact_match_failure_2 (self , setup_restapi_client ):
79+ _ , _ , _ , restapi_client = setup_restapi_client
80+ with ClientCert ("test.client.restapi.com" ) as client_cert :
81+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
82+ assert r .status_code == 401
83+
84+ def test_exact_match_failure_3 (self , setup_restapi_client ):
85+ _ , _ , _ , restapi_client = setup_restapi_client
86+ with ClientCert ("sub.test.client.restapi.sonic" ) as client_cert :
87+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
88+ assert r .status_code == 401
89+
90+ def test_exact_match_failure_4 (self , setup_restapi_client ):
91+ _ , _ , _ , restapi_client = setup_restapi_client
92+ with ClientCert ("TEST.CLIENT.RESTAPI.SONIC" ) as client_cert :
93+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
94+ assert r .status_code == 401
95+
96+ # Wildcard matching tests for "*.example.sonic"
7997
8098 def test_wildcard_match_success_1 (self , setup_restapi_client ):
8199 _ , _ , _ , restapi_client = setup_restapi_client
@@ -85,7 +103,19 @@ def test_wildcard_match_success_1(self, setup_restapi_client):
85103
86104 def test_wildcard_match_success_2 (self , setup_restapi_client ):
87105 _ , _ , _ , restapi_client = setup_restapi_client
88- with ClientCert ("another.test.example.sonic" ) as client_cert :
106+ with ClientCert ("a.example.sonic" ) as client_cert :
107+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
108+ assert r .status_code == 200
109+
110+ def test_wildcard_match_success_3 (self , setup_restapi_client ):
111+ _ , _ , _ , restapi_client = setup_restapi_client
112+ with ClientCert ("sub.test.example.sonic" ) as client_cert :
113+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
114+ assert r .status_code == 200
115+
116+ def test_wildcard_match_success_4 (self , setup_restapi_client ):
117+ _ , _ , _ , restapi_client = setup_restapi_client
118+ with ClientCert ("TEST.example.sonic" ) as client_cert :
89119 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
90120 assert r .status_code == 200
91121
@@ -119,38 +149,76 @@ def test_wildcard_match_failure_5(self, setup_restapi_client):
119149 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
120150 assert r .status_code == 401
121151
122- # Wildcard match tests for "*test.sonic"
152+ def test_wildcard_match_failure_6 (self , setup_restapi_client ):
153+ _ , _ , _ , restapi_client = setup_restapi_client
154+ with ClientCert (".example.sonic" ) as client_cert :
155+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
156+ assert r .status_code == 401
123157
124- def test_wildcard_match_success_a (self , setup_restapi_client ):
158+ def test_wildcard_match_failure_7 (self , setup_restapi_client ):
159+ _ , _ , _ , restapi_client = setup_restapi_client
160+ with ClientCert ("TEST.EXAMPLE.SONIC" ) as client_cert :
161+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
162+ assert r .status_code == 401
163+
164+ # Matching tests for "*test.sonic" (invalid CN)
165+
166+ def test_invalid_match_failure_1 (self , setup_restapi_client ):
125167 _ , _ , _ , restapi_client = setup_restapi_client
126168 with ClientCert ("mytest.sonic" ) as client_cert :
127169 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
128- assert r .status_code == 200
170+ assert r .status_code == 401
129171
130- def test_wildcard_match_success_b (self , setup_restapi_client ):
172+ def test_invalid_match_failure_2 (self , setup_restapi_client ):
131173 _ , _ , _ , restapi_client = setup_restapi_client
132174 with ClientCert ("test.sonic" ) as client_cert :
133175 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
134- assert r .status_code == 200
176+ assert r .status_code == 401
135177
136- def test_wildcard_match_success_c (self , setup_restapi_client ):
178+ def test_invalid_match_failure_3 (self , setup_restapi_client ):
137179 _ , _ , _ , restapi_client = setup_restapi_client
138- with ClientCert ("example .test.sonic" ) as client_cert :
180+ with ClientCert ("sub .test.sonic" ) as client_cert :
139181 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
140- assert r .status_code == 200
182+ assert r .status_code == 401
141183
142- def test_wildcard_match_failure_a (self , setup_restapi_client ):
184+ def test_invalid_match_failure_4 (self , setup_restapi_client ):
143185 _ , _ , _ , restapi_client = setup_restapi_client
144186 with ClientCert ("est.sonic" ) as client_cert :
145187 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
146188 assert r .status_code == 401
147189
148- def test_wildcard_match_failure_b (self , setup_restapi_client ):
190+ def test_invalid_match_failure_5 (self , setup_restapi_client ):
149191 _ , _ , _ , restapi_client = setup_restapi_client
150192 with ClientCert ("test.sonico" ) as client_cert :
151193 r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
152194 assert r .status_code == 401
153195
196+ # Corner cases
197+
198+ def test_empty_cn (self , setup_restapi_client ):
199+ _ , _ , _ , restapi_client = setup_restapi_client
200+ with ClientCert ("" ) as client_cert :
201+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
202+ assert r .status_code == 401
203+
204+ def test_missing_tld (self , setup_restapi_client ):
205+ _ , _ , _ , restapi_client = setup_restapi_client
206+ with ClientCert ("test.example." ) as client_cert :
207+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
208+ assert r .status_code == 401
209+
210+ def test_match_all (self , setup_restapi_client ):
211+ _ , _ , _ , restapi_client = setup_restapi_client
212+ with ClientCert ("*" ) as client_cert :
213+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
214+ assert r .status_code == 401
215+
216+ def test_ends_with_dot (self , setup_restapi_client ):
217+ _ , _ , _ , restapi_client = setup_restapi_client
218+ with ClientCert ("*." ) as client_cert :
219+ r = restapi_client .get_heartbeat (client_cert = (client_cert .cert , client_cert .key ))
220+ assert r .status_code == 401
221+
154222
155223class TestRestApiPositive :
156224 """Normal behaviour tests"""
0 commit comments