Conversation

jdobes

Allows to set sslcert, sslkey and sslrootcert connection parameters. Fixes #238 as it's not possible to set custom SSLContext object with loaded CA certificate in current version.

Example:

conn = await asyncpg.connect(user='user', password='password',
                             database='database', host='some.rds.aws.com',
                             ssl='verify-full', sslrootcert='/etc/rds.pem')

@@ -442,6 +458,21 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
else:
sslmode = SSLMode.disable

if sslcert is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An explicit SSLContext passed to connect() is considered to be of higher priority than the environment, so let's move this block inside if isinstance(ssl, (str, SSLMode)): above.

if sslcert:
ssl.load_cert_chain(sslcert, keyfile=sslkey)
if sslrootcert:
ssl.load_verify_locations(cafile=sslrootcert)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_verify_locations also supports CRL (just call it again), so let's add that while we are here.

@jdobesjdobes requested a review from elprans June 8, 2021 16:57
@Peder2911

You are a hero! This works great.

@svajipay

Can this PR be approved and merged please ? Most deployments use SSL settings.

@@ -1755,6 +1755,10 @@ async def connect(dsn=None, *,
max_cacheable_statement_size=1024 * 15,
command_timeout=None,
ssl=None,
sslcert=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for all the extra arguments in connect(). If you need to customize the certs, you are supposed to pass an appropriate SSLContext in the ssl argument.

@elprans

I removed the redundant ssl* arguments from connect(), added examples on how to properly configure SSLContext to achieve the same result and wrote tests for client certificate validation. @fantix, please take a quick look.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested to call SSLContext.load_verify_locations() twice - as far as the second is CRL, the CA cert set in the first call is not overwritten, so I think this PR is good.

@elpranselprans merged commit c674e86 into MagicStack:master Aug 10, 2021
elprans added a commit that referenced this pull request Aug 10, 2021
Changes
-------

* Drop support for Python 3.5 (#777)
  (by @and-semakin in da58cd2 for #777)

* Add support for Python 3.10 (#795)
  (by @elprans in abf5569 for #795)

* Add support for asynchronous iterables to copy_records_to_table() (#713)
  (by @elprans in 1d33ff6 for #713)

* Add support for coroutine functions as listener callbacks (#802)
  (by @elprans in 41da093 for #802)

* Add support for sslcert, sslkey and sslrootcert parameters to DSN (#768)
  (by @jdobes and @elprans in c674e86 for #768)

* Add copy_ wrappers to Pool (#661)
  (by @elprans in a6b0f28 for #661)

* Add issubset and issuperset methods to the Range type (#563)
  (by @kdorsel in de07d0a for #563)

Fixes
-----

* Break connection internal circular reference (#774)
  (by @fantix in d08a9b8 for #774)

* Make Server Version Extraction More Flexible (#778)
  (by @Natrinicle in d076169 for #778)
@elpranselprans mentioned this pull request Aug 10, 2021
elprans added a commit that referenced this pull request Aug 10, 2021
Changes
-------

* Drop support for Python 3.5 (#777)
  (by @and-semakin in da58cd2 for #777)

* Add support for Python 3.10 (#795)
  (by @elprans in abf5569 for #795)

* Add support for asynchronous iterables to copy_records_to_table() (#713)
  (by @elprans in 1d33ff6 for #713)

* Add support for coroutine functions as listener callbacks (#802)
  (by @elprans in 41da093 for #802)

* Add support for sslcert, sslkey and sslrootcert parameters to DSN (#768)
  (by @jdobes and @elprans in c674e86 for #768)

* Add copy_ wrappers to Pool (#661)
  (by @elprans in a6b0f28 for #661)

* Add issubset and issuperset methods to the Range type (#563)
  (by @kdorsel in de07d0a for #563)

Fixes
-----

* Break connection internal circular reference (#774)
  (by @fantix in d08a9b8 for #774)

* Make Server Version Extraction More Flexible (#778)
  (by @Natrinicle in d076169 for #778)
elprans added a commit that referenced this pull request Aug 10, 2021
Changes
-------

* Drop support for Python 3.5 (#777)
  (by @and-semakin in da58cd2 for #777)

* Add support for Python 3.10 (#795)
  (by @elprans in abf5569 for #795)

* Add support for asynchronous iterables to copy_records_to_table() (#713)
  (by @elprans in 1d33ff6 for #713)

* Add support for coroutine functions as listener callbacks (#802)
  (by @elprans in 41da093 for #802)

* Add support for sslcert, sslkey and sslrootcert parameters to DSN (#768)
  (by @jdobes and @elprans in c674e86 for #768)

* Add copy_ wrappers to Pool (#661)
  (by @elprans in a6b0f28 for #661)

* Add issubset and issuperset methods to the Range type (#563)
  (by @kdorsel in de07d0a for #563)

Fixes
-----

* Break connection internal circular reference (#774)
  (by @fantix in d08a9b8 for #774)

* Make Server Version Extraction More Flexible (#778)
  (by @Natrinicle in d076169 for #778)
@fantixfantix mentioned this pull request Sep 13, 2021
2 tasks
Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

SSL Certificate Verify Failed