| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import pytest |
| 19 |
from httpx import BasicAuth |
| 20 |
|
| 21 |
from confluent_kafka.schema_registry import AsyncSchemaRegistryClient |
| 22 |
from confluent_kafka.schema_registry.rules.encryption.encrypt_executor import \ |
| 23 |
FieldEncryptionExecutor |
| 24 |
from confluent_kafka.schema_registry.serde import RuleError |
| 25 |
|
| 26 |
TEST_URL = 'http://SchemaRegistry:65534' |
| 27 |
TEST_USERNAME = 'sr_user' |
| 28 |
TEST_USER_PASSWORD = 'sr_user_secret' |
| 29 |
TEST_POOL = 'sr_pool' |
| 30 |
TEST_CLUSTER = 'lsrc-1234' |
| 31 |
TEST_SCOPE = 'sr_scope' |
| 32 |
TEST_ENDPOINT = 'http://oauth_endpoint' |
| 33 |
|
| 34 |
""" |
| 35 |
Tests to ensure all configurations are handled correctly. |
| 36 |
|
| 37 |
""" |
| 38 |
|
| 39 |
|
| 40 |
def test_config_url_invalid(): |
| 41 |
conf = {'url': 'htt://SchemaRegistry:65534'} |
| 42 |
with pytest.raises(ValueError) as e: |
| 43 |
AsyncSchemaRegistryClient(conf) |
| 44 |
assert e.match('Invalid url htt://SchemaRegistry:65534') |
| 45 |
|
| 46 |
|
| 47 |
def test_config_url_invalid_type(): |
| 48 |
conf = {'url': dict()} |
| 49 |
with pytest.raises(TypeError, match="url must be a str," |
| 50 |
" not <(.*)>$"): |
| 51 |
AsyncSchemaRegistryClient(conf) |
| 52 |
|
| 53 |
|
| 54 |
def test_config_url_none(): |
| 55 |
conf = {} |
| 56 |
with pytest.raises(ValueError, match="Missing required configuration" |
| 57 |
" property url"): |
| 58 |
AsyncSchemaRegistryClient(conf) |
| 59 |
|
| 60 |
|
| 61 |
def test_config_url_trailing_slash(): |
| 62 |
conf = {'url': 'http://SchemaRegistry:65534/'} |
| 63 |
test_client = AsyncSchemaRegistryClient(conf) |
| 64 |
assert test_client._rest_client.base_urls == [TEST_URL] |
| 65 |
|
| 66 |
|
| 67 |
def test_config_ssl_key_no_certificate(): |
| 68 |
conf = {'url': TEST_URL, |
| 69 |
'ssl.key.location': '/ssl/keys/client'} |
| 70 |
with pytest.raises(ValueError, match="ssl.certificate.location required" |
| 71 |
" when configuring ssl.key.location" |
| 72 |
" or ssl.key.password"): |
| 73 |
AsyncSchemaRegistryClient(conf) |
| 74 |
|
| 75 |
|
| 76 |
def test_config_ssl_password_no_certificate(): |
| 77 |
conf = {'url': TEST_URL, |
| 78 |
'ssl.key.password': 'sesame'} |
| 79 |
with pytest.raises(ValueError, match="ssl.certificate.location required" |
| 80 |
" when configuring ssl.key.location" |
| 81 |
" or ssl.key.password"): |
| 82 |
AsyncSchemaRegistryClient(conf) |
| 83 |
|
| 84 |
|
| 85 |
def test_config_auth_url(): |
| 86 |
conf = { |
| 87 |
'url': 'http://' |
| 88 |
+ TEST_USERNAME + ":" |
| 89 |
+ TEST_USER_PASSWORD + '@SchemaRegistry:65534'} |
| 90 |
test_client = AsyncSchemaRegistryClient(conf) |
| 91 |
assert (test_client._rest_client.session.auth._auth_header == |
| 92 |
BasicAuth(TEST_USERNAME, TEST_USER_PASSWORD)._auth_header) |
| 93 |
|
| 94 |
|
| 95 |
def test_config_auth_url_and_userinfo(): |
| 96 |
conf = { |
| 97 |
'url': 'http://' |
| 98 |
+ TEST_USERNAME + ":" |
| 99 |
+ TEST_USER_PASSWORD + '@SchemaRegistry:65534', |
| 100 |
'basic.auth.credentials.source': 'user_info', |
| 101 |
'basic.auth.user.info': TEST_USERNAME + ":" + TEST_USER_PASSWORD} |
| 102 |
|
| 103 |
with pytest.raises(ValueError, match="basic.auth.user.info configured with" |
| 104 |
" userinfo credentials in the URL." |
| 105 |
" Remove userinfo credentials from the" |
| 106 |
" url or remove basic.auth.user.info" |
| 107 |
" from the configuration"): |
| 108 |
AsyncSchemaRegistryClient(conf) |
| 109 |
|
| 110 |
|
| 111 |
def test_config_auth_userinfo(): |
| 112 |
conf = {'url': TEST_URL, |
| 113 |
'basic.auth.user.info': TEST_USERNAME + ':' + TEST_USER_PASSWORD} |
| 114 |
|
| 115 |
test_client = AsyncSchemaRegistryClient(conf) |
| 116 |
assert (test_client._rest_client.session.auth._auth_header == |
| 117 |
BasicAuth(TEST_USERNAME, TEST_USER_PASSWORD)._auth_header) |
| 118 |
|
| 119 |
|
| 120 |
def test_config_auth_userinfo_invalid(): |
| 121 |
conf = {'url': TEST_URL, |
| 122 |
'basic.auth.user.info': 'lookmanocolon'} |
| 123 |
|
| 124 |
with pytest.raises(ValueError, match="basic.auth.user.info must be in the" |
| 125 |
" form of {username}:{password}$"): |
| 126 |
AsyncSchemaRegistryClient(conf) |
| 127 |
|
| 128 |
|
| 129 |
def test_bearer_config(): |
| 130 |
conf = {'url': TEST_URL, |
| 131 |
'bearer.auth.credentials.source': "OAUTHBEARER"} |
| 132 |
|
| 133 |
with pytest.raises(ValueError, match=r"Missing required bearer configuration properties: (.*)"): |
| 134 |
AsyncSchemaRegistryClient(conf) |
| 135 |
|
| 136 |
|
| 137 |
def test_oauth_bearer_config_missing(): |
| 138 |
conf = {'url': TEST_URL, |
| 139 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 140 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 141 |
'bearer.auth.identity.pool.id': TEST_POOL} |
| 142 |
|
| 143 |
with pytest.raises(ValueError, match=r"Missing required OAuth configuration properties: (.*)"): |
| 144 |
AsyncSchemaRegistryClient(conf) |
| 145 |
|
| 146 |
|
| 147 |
def test_oauth_bearer_config_invalid(): |
| 148 |
conf = {'url': TEST_URL, |
| 149 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 150 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 151 |
'bearer.auth.identity.pool.id': 1} |
| 152 |
|
| 153 |
with pytest.raises(TypeError, match=r"identity pool id must be a str, not (.*)"): |
| 154 |
AsyncSchemaRegistryClient(conf) |
| 155 |
|
| 156 |
conf = {'url': TEST_URL, |
| 157 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 158 |
'bearer.auth.logical.cluster': 1, |
| 159 |
'bearer.auth.identity.pool.id': TEST_POOL} |
| 160 |
|
| 161 |
with pytest.raises(TypeError, match=r"logical cluster must be a str, not (.*)"): |
| 162 |
AsyncSchemaRegistryClient(conf) |
| 163 |
|
| 164 |
conf = {'url': TEST_URL, |
| 165 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 166 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 167 |
'bearer.auth.identity.pool.id': TEST_POOL, |
| 168 |
'bearer.auth.client.id': 1, |
| 169 |
'bearer.auth.client.secret': TEST_USER_PASSWORD, |
| 170 |
'bearer.auth.scope': TEST_SCOPE, |
| 171 |
'bearer.auth.issuer.endpoint.url': TEST_ENDPOINT} |
| 172 |
|
| 173 |
with pytest.raises(TypeError, match=r"bearer.auth.client.id must be a str, not (.*)"): |
| 174 |
AsyncSchemaRegistryClient(conf) |
| 175 |
|
| 176 |
conf = {'url': TEST_URL, |
| 177 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 178 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 179 |
'bearer.auth.identity.pool.id': TEST_POOL, |
| 180 |
'bearer.auth.client.id': TEST_USERNAME, |
| 181 |
'bearer.auth.client.secret': 1, |
| 182 |
'bearer.auth.scope': TEST_SCOPE, |
| 183 |
'bearer.auth.issuer.endpoint.url': TEST_ENDPOINT} |
| 184 |
|
| 185 |
with pytest.raises(TypeError, match=r"bearer.auth.client.secret must be a str, not (.*)"): |
| 186 |
AsyncSchemaRegistryClient(conf) |
| 187 |
|
| 188 |
conf = {'url': TEST_URL, |
| 189 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 190 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 191 |
'bearer.auth.identity.pool.id': TEST_POOL, |
| 192 |
'bearer.auth.client.id': TEST_USERNAME, |
| 193 |
'bearer.auth.client.secret': TEST_USER_PASSWORD, |
| 194 |
'bearer.auth.scope': 1, |
| 195 |
'bearer.auth.issuer.endpoint.url': TEST_ENDPOINT} |
| 196 |
|
| 197 |
with pytest.raises(TypeError, match=r"bearer.auth.scope must be a str, not (.*)"): |
| 198 |
AsyncSchemaRegistryClient(conf) |
| 199 |
|
| 200 |
conf = {'url': TEST_URL, |
| 201 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 202 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 203 |
'bearer.auth.identity.pool.id': TEST_POOL, |
| 204 |
'bearer.auth.client.id': TEST_USERNAME, |
| 205 |
'bearer.auth.client.secret': TEST_USER_PASSWORD, |
| 206 |
'bearer.auth.scope': TEST_SCOPE, |
| 207 |
'bearer.auth.issuer.endpoint.url': 1} |
| 208 |
|
| 209 |
with pytest.raises(TypeError, match=r"bearer.auth.issuer.endpoint.url must be a str, not (.*)"): |
| 210 |
AsyncSchemaRegistryClient(conf) |
| 211 |
|
| 212 |
|
| 213 |
def test_oauth_bearer_config_valid(): |
| 214 |
conf = {'url': TEST_URL, |
| 215 |
'bearer.auth.credentials.source': "OAUTHBEARER", |
| 216 |
'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 217 |
'bearer.auth.identity.pool.id': TEST_POOL, |
| 218 |
'bearer.auth.client.id': TEST_USERNAME, |
| 219 |
'bearer.auth.client.secret': TEST_USER_PASSWORD, |
| 220 |
'bearer.auth.scope': TEST_SCOPE, |
| 221 |
'bearer.auth.issuer.endpoint.url': TEST_ENDPOINT} |
| 222 |
|
| 223 |
client = AsyncSchemaRegistryClient(conf) |
| 224 |
|
| 225 |
assert client._rest_client.client_id == TEST_USERNAME |
| 226 |
assert client._rest_client.client_secret == TEST_USER_PASSWORD |
| 227 |
assert client._rest_client.scope == TEST_SCOPE |
| 228 |
assert client._rest_client.token_endpoint == TEST_ENDPOINT |
| 229 |
|
| 230 |
|
| 231 |
def test_static_bearer_config(): |
| 232 |
conf = {'url': TEST_URL, |
| 233 |
'bearer.auth.credentials.source': 'STATIC_TOKEN', |
| 234 |
'bearer.auth.logical.cluster': 'lsrc', |
| 235 |
'bearer.auth.identity.pool.id': 'pool_id'} |
| 236 |
|
| 237 |
with pytest.raises(ValueError, match='Missing bearer.auth.token'): |
| 238 |
AsyncSchemaRegistryClient(conf) |
| 239 |
|
| 240 |
|
| 241 |
def test_custom_bearer_config(): |
| 242 |
conf = {'url': TEST_URL, |
| 243 |
'bearer.auth.credentials.source': 'CUSTOM'} |
| 244 |
|
| 245 |
with pytest.raises(ValueError, match='Missing required custom OAuth configuration properties:'): |
| 246 |
AsyncSchemaRegistryClient(conf) |
| 247 |
|
| 248 |
|
| 249 |
def test_custom_bearer_config_valid(): |
| 250 |
def custom_function(config: dict): |
| 251 |
return {} |
| 252 |
|
| 253 |
custom_config = {} |
| 254 |
|
| 255 |
conf = {'url': TEST_URL, |
| 256 |
'bearer.auth.credentials.source': 'CUSTOM', |
| 257 |
'bearer.auth.custom.provider.function': custom_function, |
| 258 |
'bearer.auth.custom.provider.config': custom_config} |
| 259 |
|
| 260 |
client = AsyncSchemaRegistryClient(conf) |
| 261 |
|
| 262 |
assert client._rest_client.bearer_field_provider.custom_function == custom_function |
| 263 |
assert client._rest_client.bearer_field_provider.custom_config == custom_config |
| 264 |
|
| 265 |
|
| 266 |
def test_config_unknown_prop(): |
| 267 |
conf = {'url': TEST_URL, |
| 268 |
'basic.auth.credentials.source': 'SASL_INHERIT', |
| 269 |
'sasl.username': 'user_sasl', |
| 270 |
'sasl.password': 'secret_sasl', |
| 271 |
'invalid.conf': 1, |
| 272 |
'invalid.conf2': 2} |
| 273 |
|
| 274 |
with pytest.raises(ValueError, match=r"Unrecognized properties: (.*)"): |
| 275 |
AsyncSchemaRegistryClient(conf) |
| 276 |
|
| 277 |
|
| 278 |
def test_config_encrypt_executor(): |
| 279 |
executor = FieldEncryptionExecutor() |
| 280 |
client_conf = {'url': 'mock://'} |
| 281 |
rule_conf = {'key': 'value'} |
| 282 |
executor.configure(client_conf, rule_conf) |
| 283 |
|
| 284 |
executor.configure(client_conf, rule_conf) |
| 285 |
rule_conf2 = {'key2': 'value2'} |
| 286 |
|
| 287 |
executor.configure(client_conf, rule_conf2) |
| 288 |
|
| 289 |
client_conf2 = {'url': 'mock://', |
| 290 |
'ssl.key.location': '/ssl/keys/client', |
| 291 |
'ssl.certificate.location': '/ssl/certs/client'} |
| 292 |
with pytest.raises(RuleError, match="executor already configured"): |
| 293 |
executor.configure(client_conf2, rule_conf) |
| 294 |
|
| 295 |
rule_conf3 = {'key': 'value3'} |
| 296 |
with pytest.raises(RuleError, match="rule config key already set: key"): |
| 297 |
executor.configure(client_conf, rule_conf3) |