Skip to content

Commit 064e341

Browse files
committed
Adjust connect arguments to accept credentials within the HTTP URI
1 parent d3d33fe commit 064e341

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Unreleased
1111
HTTP by default. Previously, this setting defaulted to false. This setting
1212
can be changed via the ``verify_ssl_cert`` connection parameter.
1313

14+
- Adjusted connect arguments to accept credentials within the HTTP URI.
15+
1416
2020/09/28 0.26.0
1517
=================
1618

src/crate/client/doctests/client.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,36 @@ connect::
4747

4848
>>> connection = client.connect([crate_host],
4949
... username='trusted_me')
50+
>>> connection.client.username
51+
'trusted_me'
52+
>>> connection.client.password
53+
54+
The username for trusted users can also be provided in the URL::
55+
56+
>>> connection = client.connect(['http://trusted_me@' + crate_host])
57+
>>> connection.client.username
58+
'trusted_me'
59+
>>> connection.client.password
5060

5161
To connect to CrateDB with as a user that requires password authentication, you
5262
also need to provide ``password`` as argument for the ``connect()`` call::
5363

5464
>>> connection = client.connect([crate_host],
5565
... username='me',
5666
... password='my_secret_pw')
67+
>>> connection.client.username
68+
'me'
69+
>>> connection.client.password
70+
'my_secret_pw'
71+
72+
The authentication credentials can also be provided in the URL::
73+
74+
>>> connection = client.connect(['http://me:my_secret_pw@' + crate_host])
75+
>>> connection.client.username
76+
'me'
77+
>>> connection.client.password
78+
'my_secret_pw'
79+
5780

5881
Default Schema
5982
--------------

src/crate/client/http.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,22 @@ def __init__(self,
346346
servers = [self.default_server]
347347
else:
348348
servers = _to_server_list(servers)
349+
350+
# Try to derive credentials from first server argument if not
351+
# explicitly given.
352+
if servers and not username:
353+
try:
354+
url = urlparse(servers[0])
355+
if url.username is not None:
356+
username = url.username
357+
if url.password is not None:
358+
password = url.password
359+
except Exception as ex:
360+
logger.warning("Unable to decode credentials from database "
361+
"URI, so connecting to CrateDB without "
362+
"authentication: {ex}"
363+
.format(ex=ex))
364+
349365
self._active_servers = servers
350366
self._inactive_servers = []
351367
pool_kw = _pool_kw_args(

0 commit comments

Comments
 (0)