| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import pytest |
| 19 |
import time |
| 20 |
from unittest.mock import Mock, patch |
| 21 |
|
| 22 |
from confluent_kafka.schema_registry._sync.schema_registry_client import _OAuthClient, SchemaRegistryClient |
| 23 |
from confluent_kafka.schema_registry._sync.schema_registry_client import _CustomOAuthClient |
| 24 |
from confluent_kafka.schema_registry.common.schema_registry_client import _StaticFieldProvider |
| 25 |
from confluent_kafka.schema_registry.error import OAuthTokenError |
| 26 |
|
| 27 |
""" |
| 28 |
Tests to ensure OAuth client is set up correctly. |
| 29 |
|
| 30 |
""" |
| 31 |
|
| 32 |
|
| 33 |
def custom_oauth_function(config: dict) -> dict: |
| 34 |
return config |
| 35 |
|
| 36 |
|
| 37 |
TEST_TOKEN = 'token123' |
| 38 |
TEST_CLUSTER = 'lsrc-cluster' |
| 39 |
TEST_POOL = 'pool-id' |
| 40 |
TEST_FUNCTION = custom_oauth_function |
| 41 |
TEST_CONFIG = {'bearer.auth.token': TEST_TOKEN, 'bearer.auth.logical.cluster': TEST_CLUSTER, |
| 42 |
'bearer.auth.identity.pool.id': TEST_POOL} |
| 43 |
TEST_URL = 'http://SchemaRegistry:65534' |
| 44 |
|
| 45 |
|
| 46 |
def test_expiry(): |
| 47 |
oauth_client = _OAuthClient('id', 'secret', 'scope', 'endpoint', TEST_CLUSTER, TEST_POOL, 2, 1000, 20000) |
| 48 |
oauth_client.token = {'expires_at': time.time() + 2, 'expires_in': 1} |
| 49 |
assert not oauth_client.token_expired() |
| 50 |
time.sleep(1.5) |
| 51 |
assert oauth_client.token_expired() |
| 52 |
|
| 53 |
|
| 54 |
def test_get_token(): |
| 55 |
oauth_client = _OAuthClient('id', 'secret', 'scope', 'endpoint', TEST_CLUSTER, TEST_POOL, 2, 1000, 20000) |
| 56 |
|
| 57 |
def update_token1(): |
| 58 |
oauth_client.token = {'expires_at': 0, 'expires_in': 1, 'access_token': '123'} |
| 59 |
|
| 60 |
def update_token2(): |
| 61 |
oauth_client.token = {'expires_at': time.time() + 2, 'expires_in': 1, 'access_token': '1234'} |
| 62 |
|
| 63 |
oauth_client.generate_access_token = Mock(side_effect=update_token1) |
| 64 |
oauth_client.get_access_token() |
| 65 |
assert oauth_client.generate_access_token.call_count == 1 |
| 66 |
assert oauth_client.token['access_token'] == '123' |
| 67 |
|
| 68 |
oauth_client.generate_access_token = Mock(side_effect=update_token2) |
| 69 |
oauth_client.get_access_token() |
| 70 |
|
| 71 |
assert oauth_client.generate_access_token.call_count == 1 |
| 72 |
assert oauth_client.token['access_token'] == '1234' |
| 73 |
|
| 74 |
oauth_client.get_access_token() |
| 75 |
assert oauth_client.generate_access_token.call_count == 1 |
| 76 |
|
| 77 |
|
| 78 |
def test_generate_token_retry_logic(): |
| 79 |
oauth_client = _OAuthClient('id', 'secret', 'scope', 'endpoint', TEST_CLUSTER, TEST_POOL, 5, 1000, 20000) |
| 80 |
|
| 81 |
with (patch("confluent_kafka.schema_registry._sync.schema_registry_client.time.sleep") as mock_sleep, |
| 82 |
patch("confluent_kafka.schema_registry._sync.schema_registry_client.full_jitter") as mock_jitter): |
| 83 |
|
| 84 |
with pytest.raises(OAuthTokenError): |
| 85 |
oauth_client.generate_access_token() |
| 86 |
|
| 87 |
assert mock_sleep.call_count == 5 |
| 88 |
assert mock_jitter.call_count == 5 |
| 89 |
|
| 90 |
|
| 91 |
def test_static_field_provider(): |
| 92 |
static_field_provider = _StaticFieldProvider(TEST_TOKEN, TEST_CLUSTER, TEST_POOL) |
| 93 |
bearer_fields = static_field_provider.get_bearer_fields() |
| 94 |
|
| 95 |
assert bearer_fields == TEST_CONFIG |
| 96 |
|
| 97 |
|
| 98 |
def test_custom_oauth_client(): |
| 99 |
custom_oauth_client = _CustomOAuthClient(TEST_FUNCTION, TEST_CONFIG) |
| 100 |
|
| 101 |
assert custom_oauth_client.get_bearer_fields() == TEST_CONFIG |
| 102 |
|
| 103 |
|
| 104 |
def test_bearer_field_headers_missing(): |
| 105 |
def empty_custom(config): |
| 106 |
return {} |
| 107 |
|
| 108 |
conf = {'url': TEST_URL, |
| 109 |
'bearer.auth.credentials.source': 'CUSTOM', |
| 110 |
'bearer.auth.custom.provider.function': empty_custom, |
| 111 |
'bearer.auth.custom.provider.config': TEST_CONFIG} |
| 112 |
|
| 113 |
headers = {'Accept': "application/vnd.schemaregistry.v1+json," |
| 114 |
" application/vnd.schemaregistry+json," |
| 115 |
" application/json"} |
| 116 |
|
| 117 |
client = SchemaRegistryClient(conf) |
| 118 |
|
| 119 |
with pytest.raises(ValueError, match=r"Missing required bearer auth fields, " |
| 120 |
r"needs to be set in config or custom function: (.*)"): |
| 121 |
client._rest_client.handle_bearer_auth(headers) |
| 122 |
|
| 123 |
|
| 124 |
def test_bearer_field_headers_valid(): |
| 125 |
conf = {'url': TEST_URL, |
| 126 |
'bearer.auth.credentials.source': 'CUSTOM', |
| 127 |
'bearer.auth.custom.provider.function': TEST_FUNCTION, |
| 128 |
'bearer.auth.custom.provider.config': TEST_CONFIG} |
| 129 |
|
| 130 |
client = SchemaRegistryClient(conf) |
| 131 |
|
| 132 |
headers = {'Accept': "application/vnd.schemaregistry.v1+json," |
| 133 |
" application/vnd.schemaregistry+json," |
| 134 |
" application/json"} |
| 135 |
|
| 136 |
client._rest_client.handle_bearer_auth(headers) |
| 137 |
|
| 138 |
assert 'Authorization' in headers |
| 139 |
assert 'Confluent-Identity-Pool-Id' in headers |
| 140 |
assert 'target-sr-cluster' in headers |
| 141 |
assert headers['Authorization'] == "Bearer {}".format(TEST_CONFIG['bearer.auth.token']) |
| 142 |
assert headers['Confluent-Identity-Pool-Id'] == TEST_CONFIG['bearer.auth.identity.pool.id'] |
| 143 |
assert headers['target-sr-cluster'] == TEST_CONFIG['bearer.auth.logical.cluster'] |