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