4
4
Copyright (c) 2023 Aiven Ltd
5
5
See LICENSE for details
6
6
"""
7
- from karapace .client import Client
8
7
from karapace .kafka .admin import KafkaAdminClient
9
8
from karapace .schema_models import SchemaType , ValidatedTypedSchema
9
+ from tests .integration .utils .rest_client import RetryRestClient
10
10
from tests .utils import (
11
11
new_random_name ,
12
12
new_topic ,
29
29
reader = aiohttp .BasicAuth ("reader" , "secret" )
30
30
31
31
32
- async def test_sr_auth (registry_async_client_auth : Client ) -> None :
32
+ async def test_sr_auth (registry_async_retry_client_auth : RetryRestClient ) -> None :
33
33
subject = new_random_name ("cave-" )
34
34
35
- res = await registry_async_client_auth .post (f"subjects/{ quote (subject )} /versions" , json = {"schema" : schema_avro_json })
35
+ res = await registry_async_retry_client_auth .post (
36
+ f"subjects/{ quote (subject )} /versions" , json = {"schema" : schema_avro_json }, expected_response_code = 401
37
+ )
36
38
assert res .status_code == 401
37
39
38
- res = await registry_async_client_auth .post (
40
+ res = await registry_async_retry_client_auth .post (
39
41
f"subjects/{ quote (subject )} /versions" , json = {"schema" : schema_avro_json }, auth = aladdin
40
42
)
41
43
assert res .status_code == 200
42
44
sc_id = res .json ()["id" ]
43
45
assert sc_id >= 0
44
46
45
- res = await registry_async_client_auth .get (f"subjects/{ quote (subject )} /versions/latest" )
47
+ res = await registry_async_retry_client_auth .get (
48
+ f"subjects/{ quote (subject )} /versions/latest" , expected_response_code = 401
49
+ )
46
50
assert res .status_code == 401
47
- res = await registry_async_client_auth .get (f"subjects/{ quote (subject )} /versions/latest" , auth = aladdin )
51
+ res = await registry_async_retry_client_auth .get (f"subjects/{ quote (subject )} /versions/latest" , auth = aladdin )
48
52
assert res .status_code == 200
49
53
assert sc_id == res .json ()["id" ]
50
54
assert ValidatedTypedSchema .parse (SchemaType .AVRO , schema_avro_json ) == ValidatedTypedSchema .parse (
51
55
SchemaType .AVRO , res .json ()["schema" ]
52
56
)
53
57
54
58
55
- async def test_sr_auth_endpoints (registry_async_client_auth : Client ) -> None :
59
+ async def test_sr_auth_endpoints (registry_async_retry_client_auth : RetryRestClient ) -> None :
56
60
"""Test endpoints for authorization"""
57
61
58
62
subject = new_random_name ("any-" )
59
63
60
- res = await registry_async_client_auth .post (
61
- f"compatibility/subjects/{ quote (subject )} /versions/1" , json = {"schema" : schema_avro_json }
64
+ res = await registry_async_retry_client_auth .post (
65
+ f"compatibility/subjects/{ quote (subject )} /versions/1" ,
66
+ json = {"schema" : schema_avro_json },
67
+ expected_response_code = 401 ,
62
68
)
63
69
assert res .status_code == 401
64
70
65
- res = await registry_async_client_auth .get (f"config/{ quote (subject )} " )
71
+ res = await registry_async_retry_client_auth .get (f"config/{ quote (subject )} " , expected_response_code = 401 )
66
72
assert res .status_code == 401
67
73
68
- res = await registry_async_client_auth .put (f"config/{ quote (subject )} " , json = {"compatibility" : "NONE" })
74
+ res = await registry_async_retry_client_auth .put (
75
+ f"config/{ quote (subject )} " ,
76
+ json = {"compatibility" : "NONE" },
77
+ expected_response_code = 401 ,
78
+ )
69
79
assert res .status_code == 401
70
80
71
- res = await registry_async_client_auth .get ("config" )
81
+ res = await registry_async_retry_client_auth .get ("config" , expected_response_code = 401 )
72
82
assert res .status_code == 401
73
83
74
- res = await registry_async_client_auth .put ("config" , json = {"compatibility" : "NONE" })
84
+ res = await registry_async_retry_client_auth .put ("config" , json = {"compatibility" : "NONE" }, expected_response_code = 401 )
75
85
assert res .status_code == 401
76
86
77
- res = await registry_async_client_auth .get ("schemas/ids/1/versions" )
87
+ res = await registry_async_retry_client_auth .get ("schemas/ids/1/versions" , expected_response_code = 401 )
78
88
assert res .status_code == 401
79
89
80
90
# This is an exception that does not require authorization
81
- res = await registry_async_client_auth .get ("schemas/types" )
91
+ res = await registry_async_retry_client_auth .get ("schemas/types" )
82
92
assert res .status_code == 200
83
93
84
94
# but let's verify it answers normally if sending authorization header
85
- res = await registry_async_client_auth .get ("schemas/types" , auth = admin )
95
+ res = await registry_async_retry_client_auth .get ("schemas/types" , auth = admin )
86
96
assert res .status_code == 200
87
97
88
- res = await registry_async_client_auth .post (f"subjects/{ quote (subject )} /versions" , json = {"schema" : schema_avro_json })
98
+ res = await registry_async_retry_client_auth .post (
99
+ f"subjects/{ quote (subject )} /versions" , json = {"schema" : schema_avro_json }, expected_response_code = 401
100
+ )
89
101
assert res .status_code == 401
90
102
91
- res = await registry_async_client_auth .delete (f"subjects/{ quote (subject )} /versions/1" )
103
+ res = await registry_async_retry_client_auth .delete (f"subjects/{ quote (subject )} /versions/1" , expected_response_code = 401 )
92
104
assert res .status_code == 401
93
105
94
- res = await registry_async_client_auth .get (f"subjects/{ quote (subject )} /versions/1/schema" )
106
+ res = await registry_async_retry_client_auth .get (
107
+ f"subjects/{ quote (subject )} /versions/1/schema" , expected_response_code = 401
108
+ )
95
109
assert res .status_code == 401
96
110
97
- res = await registry_async_client_auth .get (f"subjects/{ quote (subject )} /versions/1/referencedby" )
111
+ res = await registry_async_retry_client_auth .get (
112
+ f"subjects/{ quote (subject )} /versions/1/referencedby" , expected_response_code = 401
113
+ )
98
114
assert res .status_code == 401
99
115
100
- res = await registry_async_client_auth .delete (f"subjects/{ quote (subject )} " )
116
+ res = await registry_async_retry_client_auth .delete (f"subjects/{ quote (subject )} " , expected_response_code = 401 )
101
117
assert res .status_code == 401
102
118
103
- res = await registry_async_client_auth .get ("mode" )
119
+ res = await registry_async_retry_client_auth .get ("mode" , expected_response_code = 401 )
104
120
assert res .status_code == 401
105
121
106
- res = await registry_async_client_auth .get (f"mode/{ quote (subject )} " )
122
+ res = await registry_async_retry_client_auth .get (f"mode/{ quote (subject )} " , expected_response_code = 401 )
107
123
assert res .status_code == 401
108
124
109
125
110
- async def test_sr_list_subjects (registry_async_client_auth : Client ) -> None :
126
+ async def test_sr_list_subjects (registry_async_retry_client_auth : RetryRestClient ) -> None :
111
127
cavesubject = new_random_name ("cave-" )
112
128
carpetsubject = new_random_name ("carpet-" )
113
129
114
- res = await registry_async_client_auth .post (
130
+ res = await registry_async_retry_client_auth .post (
115
131
f"subjects/{ quote (cavesubject )} /versions" , json = {"schema" : schema_avro_json }, auth = aladdin
116
132
)
117
133
assert res .status_code == 200
118
134
sc_id = res .json ()["id" ]
119
135
assert sc_id >= 0
120
136
121
- res = await registry_async_client_auth .post (
137
+ res = await registry_async_retry_client_auth .post (
122
138
f"subjects/{ quote (carpetsubject )} /versions" , json = {"schema" : schema_avro_json }, auth = admin
123
139
)
124
140
assert res .status_code == 200
125
141
126
- res = await registry_async_client_auth .get ("subjects" , auth = admin )
142
+ res = await registry_async_retry_client_auth .get ("subjects" , auth = admin )
127
143
assert res .status_code == 200
128
144
assert [cavesubject , carpetsubject ] == res .json ()
129
145
130
- res = await registry_async_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" )
146
+ res = await registry_async_retry_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" , expected_response_code = 401 )
131
147
assert res .status_code == 401
132
148
133
- res = await registry_async_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" , auth = admin )
149
+ res = await registry_async_retry_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" , auth = admin )
134
150
assert res .status_code == 200
135
151
assert [sc_id ] == res .json ()
136
152
137
- res = await registry_async_client_auth .get ("subjects" , auth = aladdin )
153
+ res = await registry_async_retry_client_auth .get ("subjects" , auth = aladdin )
138
154
assert res .status_code == 200
139
155
assert [cavesubject ] == res .json ()
140
156
141
- res = await registry_async_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" , auth = aladdin )
157
+ res = await registry_async_retry_client_auth .get (
158
+ f"subjects/{ quote (carpetsubject )} /versions" , auth = aladdin , expected_response_code = 403
159
+ )
142
160
assert res .status_code == 403
143
161
144
- res = await registry_async_client_auth .get ("subjects" , auth = reader )
162
+ res = await registry_async_retry_client_auth .get ("subjects" , auth = reader )
145
163
assert res .status_code == 200
146
164
assert [carpetsubject ] == res .json ()
147
165
148
- res = await registry_async_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" , auth = reader )
166
+ res = await registry_async_retry_client_auth .get (f"subjects/{ quote (carpetsubject )} /versions" , auth = reader )
149
167
assert res .status_code == 200
150
168
assert [1 ] == res .json ()
151
169
152
170
153
- async def test_sr_ids (registry_async_client_auth : Client ) -> None :
171
+ async def test_sr_ids (registry_async_retry_client_auth : RetryRestClient ) -> None :
154
172
cavesubject = new_random_name ("cave-" )
155
173
carpetsubject = new_random_name ("carpet-" )
156
174
157
- res = await registry_async_client_auth .post (
175
+ res = await registry_async_retry_client_auth .post (
158
176
f"subjects/{ quote (cavesubject )} /versions" , json = {"schema" : schema_avro_json }, auth = aladdin
159
177
)
160
178
assert res .status_code == 200
161
179
avro_sc_id = res .json ()["id" ]
162
180
assert avro_sc_id >= 0
163
181
164
- res = await registry_async_client_auth .post (
182
+ res = await registry_async_retry_client_auth .post (
165
183
f"subjects/{ quote (carpetsubject )} /versions" ,
166
184
json = {"schemaType" : "JSON" , "schema" : schema_jsonschema_json },
167
185
auth = admin ,
@@ -170,30 +188,34 @@ async def test_sr_ids(registry_async_client_auth: Client) -> None:
170
188
jsonschema_sc_id = res .json ()["id" ]
171
189
assert jsonschema_sc_id >= 0
172
190
173
- res = await registry_async_client_auth .get (f"schemas/ids/{ avro_sc_id } " , auth = aladdin )
191
+ res = await registry_async_retry_client_auth .get (f"schemas/ids/{ avro_sc_id } " , auth = aladdin )
174
192
assert res .status_code == 200
175
193
176
- res = await registry_async_client_auth .get (f"schemas/ids/{ jsonschema_sc_id } " , auth = aladdin )
194
+ res = await registry_async_retry_client_auth .get (
195
+ f"schemas/ids/{ jsonschema_sc_id } " , auth = aladdin , expected_response_code = 404
196
+ )
177
197
assert res .status_code == 404
178
198
assert {"error_code" : 40403 , "message" : "Schema not found" } == res .json ()
179
199
180
- res = await registry_async_client_auth .get (f"schemas/ids/{ avro_sc_id } " , auth = reader )
200
+ res = await registry_async_retry_client_auth .get (f"schemas/ids/{ avro_sc_id } " , auth = reader , expected_response_code = 404 )
181
201
assert res .status_code == 404
182
202
assert {"error_code" : 40403 , "message" : "Schema not found" } == res .json ()
183
203
184
- res = await registry_async_client_auth .get (f"schemas/ids/{ jsonschema_sc_id } " , auth = reader )
204
+ res = await registry_async_retry_client_auth .get (f"schemas/ids/{ jsonschema_sc_id } " , auth = reader )
185
205
assert res .status_code == 200
186
206
187
207
188
- async def test_sr_auth_forwarding (registry_async_auth_pair : List [str ]) -> None :
208
+ async def test_sr_auth_forwarding (
209
+ registry_async_auth_pair : List [str ], registry_async_retry_client_auth : RetryRestClient
210
+ ) -> None :
189
211
auth = requests .auth .HTTPBasicAuth ("admin" , "admin" )
190
212
191
213
# Test primary/replica forwarding with global config setting
192
214
primary_url , replica_url = registry_async_auth_pair
193
215
max_tries , counter = 5 , 0
194
216
wait_time = 0.5
195
217
for compat in ["FULL" , "BACKWARD" , "FORWARD" , "NONE" ]:
196
- resp = requests .put (f"{ replica_url } /config" , json = {"compatibility" : compat }, auth = auth )
218
+ resp = await registry_async_retry_client_auth .put (f"{ replica_url } /config" , json = {"compatibility" : compat }, auth = auth )
197
219
assert resp .ok
198
220
while True :
199
221
assert counter < max_tries , "Compat update not propagated"
@@ -213,7 +235,9 @@ async def test_sr_auth_forwarding(registry_async_auth_pair: List[str]) -> None:
213
235
214
236
215
237
# Test that Kafka REST API works when configured with Schema Registry requiring authorization
216
- async def test_rest_api_with_sr_auth (rest_async_client_registry_auth : Client , admin_client : KafkaAdminClient ) -> None :
238
+ async def test_rest_api_with_sr_auth (
239
+ rest_async_client_registry_auth : RetryRestClient , admin_client : KafkaAdminClient
240
+ ) -> None :
217
241
client = rest_async_client_registry_auth
218
242
219
243
topic = new_topic (admin_client , prefix = "cave-rest-" )
0 commit comments