Skip to content

Commit 0fb61ea

Browse files
committed
Add basic auth provider
1 parent 2ae30fc commit 0fb61ea

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

confluent_kafka/avro/cached_schema_registry_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
VALID_LEVELS = ['NONE', 'FULL', 'FORWARD', 'BACKWARD']
3333
VALID_METHODS = ['GET', 'POST', 'PUT', 'DELETE']
34+
VALID_AUTH_PROVIDERS = ['URL', 'USERINFO', 'SASL_INHERIT']
3435

3536
# Common accept header sent
3637
ACCEPT_HDR = "application/vnd.schemaregistry.v1+json, application/vnd.schemaregistry+json, application/json"
@@ -93,6 +94,7 @@ def __init__(self, url, max_schemas_per_subject=1000, ca_location=None, cert_loc
9394
s = requests.Session()
9495
s.verify = conf.get('ssl.ca.location', None)
9596
s.cert = self._configure_client_tls(conf)
97+
s.auth = self._configure_basic_auth(conf)
9698

9799
self.url = conf['url']
98100
self._session = s
@@ -109,6 +111,26 @@ def __exit__(self, *args):
109111
def close(self):
110112
self._session.close()
111113

114+
@staticmethod
115+
def _configure_basic_auth(conf):
116+
url = conf['url']
117+
auth_provider = conf.get('basic.auth.credentials.source', 'URL').upper()
118+
if auth_provider not in VALID_AUTH_PROVIDERS:
119+
raise ValueError("basic.auth.credentials.source must be one of {}"
120+
.format(auth_provider, VALID_AUTH_PROVIDERS))
121+
122+
if auth_provider == 'SASL_INHERIT':
123+
if conf.get('sasl.mechanisms', '').upper() == 'GSSAPI':
124+
raise ValueError("SASL_INHERIT supports SASL mechanisms PLAIN and SCRAM only")
125+
auth = (conf.get('sasl.username', None), conf.get('sasl.password'))
126+
elif auth_provider == 'USERINFO':
127+
auth = tuple(conf.get('basic.auth.user.info', None).split(':'))
128+
else:
129+
auth = requests.utils.get_auth_from_url(url)
130+
131+
conf['url'] = requests.utils.urldefragauth(url)
132+
return auth
133+
112134
@staticmethod
113135
def _configure_client_tls(conf):
114136
cert = conf.get('ssl.certificate.location', None), conf.get('ssl.key.location', None)

tests/avro/test_cached_client.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,42 @@ def test_init_with_dict(self):
159159

160160
def test_emptry_url(self):
161161
with self.assertRaises(ValueError):
162-
self.client = CachedSchemaRegistryClient(**{
162+
self.client = CachedSchemaRegistryClient({
163163
'url': ''
164164
})
165165

166166
def test_invalid_url(self):
167167
with self.assertRaises(ValueError):
168-
self.client = CachedSchemaRegistryClient(**{
168+
self.client = CachedSchemaRegistryClient({
169169
'url': 'example.com:65534'
170170
})
171+
172+
def test_basic_auth_url(self):
173+
self.client = CachedSchemaRegistryClient({
174+
'url': 'https://user_url:secret@127.0.0.1:65534',
175+
})
176+
self.assertTupleEqual(('user_url', 'secret'), self.client._session.auth)
177+
178+
def test_basic_auth_userinfo(self):
179+
self.client = CachedSchemaRegistryClient({
180+
'url': 'https://user_url:secret@127.0.0.1:65534',
181+
'basic.auth.credentials.source': 'userinfo',
182+
'basic.auth.user.info': 'user_userinfo:secret'
183+
})
184+
self.assertTupleEqual(('user_userinfo', 'secret'), self.client._session.auth)
185+
186+
def test_basic_auth_sasl_inherit(self):
187+
self.client = CachedSchemaRegistryClient({
188+
'url': 'https://user_url:secret@127.0.0.1:65534',
189+
'basic.auth.credentials.source': 'SASL_INHERIT',
190+
'sasl.username': 'user_sasl',
191+
'sasl.password': 'secret'
192+
})
193+
self.assertTupleEqual(('user_sasl', 'secret'), self.client._session.auth)
194+
195+
def test_basic_auth_invalid(self):
196+
with self.assertRaises(ValueError):
197+
self.client = CachedSchemaRegistryClient({
198+
'url': 'https://user_url:secret@127.0.0.1:65534',
199+
'basic.auth.credentials.source': 'VAULT',
200+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy