-
Notifications
You must be signed in to change notification settings - Fork 31
Support credentials and SSL with SQLAlchemy via HTTP/DB URIs #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1491673
to
1f8965e
Compare
1f8965e
to
03ff23c
Compare
03ff23c
to
ccdfffd
Compare
160aa1e
to
1b1136a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9984cc4
to
dae941f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes should probably also get documented somewhere in
crate-python/docs/sqlalchemy.rst
Lines 47 to 129 in 1b1136a
An SQLAlchemy database is represented by special type of *Uniform Resource | |
Locator* (URL) called a `database URL`_. | |
The simplest database URL for CrateDB looks like this:: | |
crate://<HOST> | |
Here, ``<HOST>`` is the node *host string*. | |
A host string looks like this:: | |
<HOST_ADDR>:<PORT> | |
Here, ``<HOST_ADDR>`` is the hostname or IP address of the CrateDB node and | |
``<PORT>`` is a valid `psql.port`_ number. | |
Example host strings: | |
- ``localhost:4200`` | |
- ``crate-1.vm.example.com:4200`` | |
- ``198.51.100.1:4200`` | |
.. TIP:: | |
If ``<HOST>`` is blank (i.e. just ``crate://``) then ``localhost:4200`` will | |
be assumed. | |
Getting a connection | |
-------------------- | |
Create an engine | |
................ | |
You can connect to CrateDB using the ``create_engine`` method. This method | |
takes a `database URL`_. | |
Import the ``sa`` module, like so: | |
>>> import sqlalchemy as sa | |
To connect to ``localhost:4200``, you can do this:: | |
>>> engine = sa.create_engine('crate://') | |
To connect to ``crate-1.vm.example.com:4200``, you would do this: | |
>>> engine = sa.create_engine('crate://crate-1.vm.example.com:4200') | |
If your CrateDB cluster has multiple nodes, however, we recommend that you | |
configure all of them. You can do that by specifying the ``crate://`` database | |
URL and passing in a list of :ref:`host strings <database-urls>` passed using | |
the ``connect_args`` argument, like so:: | |
>>> engine = sa.create_engine('crate://', connect_args={ | |
... 'servers': ['198.51.100.1:4200', '198.51.100.2:4200'] | |
... }) | |
When you do this, the Database API layer will use its :ref:`round-robin | |
<multiple-nodes>` implementation. | |
The client validates `SSL server certificates`_ by default. For further | |
adjusting this behaviour, SSL verification options can be passed in by using | |
the ``connect_args`` dictionary. For example, use ``ca_cert`` for providing | |
a path to the CA certificate used for signing the server certificate:: | |
>>> engine = sa.create_engine( | |
... 'crate://', | |
... connect_args={ | |
... 'servers': ['198.51.100.1:4200', '198.51.100.2:4200'], | |
... 'ca_cert': '<PATH_TO_CA_CERT>', | |
... } | |
... ) | |
In order to disable SSL verification, use ``verify_ssl_cert = False``, like:: | |
>>> engine = sa.create_engine( | |
... 'crate://', | |
... connect_args={ | |
... 'servers': ['198.51.100.1:4200', '198.51.100.2:4200'], | |
... 'verify_ssl_cert': False, | |
... } | |
... ) |
Otherwise I think this looks good
dae941f
to
c08ecd1
Compare
ResourceWarning: unclosed <socket.socket fd=12, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 52797), raddr=('127.0.0.1', 44209)>
Occasionally, the test suite would croak like: crate.client.exceptions.ConnectionError: No more Servers available, exception from last server: HTTPConnectionPool(host='127.0.0.1', port=44209): Read timed out. (read timeout=2) By removing the "timeout=2" parameter, that error might go away on CI.
c08ecd1
to
69eea5d
Compare
Hi there,
in order to resolve crate/sqlalchemy-cratedb#116, this patch improves some bits when connecting to CrateDB, specifically in scenarios when preferring to connect using a SQLAlchemy DB URI instead of separate
connect_args
. This is convenient for programs which can be configured using a single DB URI as database connection option, like Apache Superset.http://foo:[email protected]:4200/
crate://cratedb.example.org/?ssl=true
With kind regards,
Andreas.
P.S.: 30fc28e, 93f33b9 and 160aa1e are only maintenance commits without feature significance.