You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@modelmat Are you missing root certificates to validate against? The certifi python package provides these if your current python installation doesn't provide them already.
@modelmat Are you passing the correct SSL context? Your system is missing the necessary root and/or intermediate certs to verify the server certificate. If the server is not using a self-signed cert, then the following should work in most cases:
We haven't encountered this problem when using psycopg2. Perhaps that's because it's built with libpq bindings?
After banging my head for a while, I was able to successfully connect by appending the contents of AWS' root certificate bundle to the CA bundle in my environment (docker/ubuntu 14.04, postgres 9.5)
@zharben I do happen to be using Amazon AWS. However, adding the certificate file in (Windows), in Internet Options > Content > Certificates > Import, then importing the pem file that was provided in the link does not seem to work. Same with the p7b file
Had the same problem trying to connect to aiven.io pg instance. After downloading the certificate from aiven project, this code worked. Don't forget to import ssl
@elprans With the example RDS setup provided I actually couldn't connect to my RDS instance - perhaps due to my org's SSL setup. However, I can connect with a psycopg based driver using my DSN. The key difference for me, I think, is that pyscopg supports sslmode and sslrootcert connect args - I wonder if it makes sense to support sslrootcert?
I wonder if it makes sense to support sslrootcert?
sslrootcert is just a connection option, which is equivalent to the above explicit context configuration. We can add the shortcut, but that'll not change the functionality. Please double check that you are specifying the correct certificate chain to cafile.
@elprans I managed to get it to work but only by disabling the hostname check (not the verify_mode). I guess this is because sslmode verify-ca doesn't verify the hostname by default, verify-full does. However the default SSL context does attempt to verify the hostname.
I guess from a user perspective it's somewhat unclear that the sslmode connection param is supported by asyncpg but it is somewhat overriden by the SSL context. Thanks for your help also, this thread was very useful to me :).
I guess from a user perspective it's somewhat unclear that the sslmode connection param is supported by asyncpg but it is somewhat overriden by the SSL context
root@raas:~# python3 try.py
Traceback (most recent call last):
File "try.py", line 18, in <module>
asyncio.get_event_loop().run_until_complete(run())
File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
return future.result()
File "try.py", line 6, in run
con = await asyncpg.connect(dsn)
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 1677, in connect
max_cacheable_statement_size=max_cacheable_statement_size)
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connect_utils.py", line 662, in _connect
raise last_error
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connect_utils.py", line 654, in _connect
connection_class=connection_class)
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connect_utils.py", line 621, in _connect_addr
connector, timeout=timeout)
File "/usr/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
return fut.result()
File "/usr/local/lib/python3.6/dist-packages/asyncpg/connect_utils.py", line 590, in _create_ssl_connection
return await conn_factory(sock=sock)
File "/usr/lib/python3.6/asyncio/base_events.py", line 820, in create_connection
sock, protocol_factory, ssl, server_hostname)
File "/usr/lib/python3.6/asyncio/base_events.py", line 846, in _create_connection_transport
yield from waiter
File "/usr/lib/python3.6/asyncio/sslproto.py", line 505, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "/usr/lib/python3.6/asyncio/sslproto.py", line 201, in feed_ssldata
self._sslobj.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)
Tried the same with sqlalchemy with psycopg2 driver. It works great.
Activity
amsb commentedon Dec 29, 2017
@modelmat Are you missing root certificates to validate against? The certifi python package provides these if your current python installation doesn't provide them already.
modelmat commentedon Dec 30, 2017
@amsb Nope. It is already installed.
elprans commentedon Dec 31, 2017
@modelmat Are you passing the correct SSL context? Your system is missing the necessary root and/or intermediate certs to verify the server certificate. If the server is not using a self-signed cert, then the following should work in most cases:
modelmat commentedon Dec 31, 2017
I still get the same error. It works with pyscopg2, but not with asyncpg, os not sure why
zharben commentedon Jan 3, 2018
@modelmat @elprans I had the same problem.
If you happen to be using AWS RDS, it looks like you need to use their root certificate (e.g. when connecting via
asyncio/asyncpg):https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html
We haven't encountered this problem when using
psycopg2. Perhaps that's because it's built withlibpqbindings?After banging my head for a while, I was able to successfully connect by appending the contents of AWS' root certificate bundle to the CA bundle in my environment (docker/ubuntu 14.04, postgres 9.5)
modelmat commentedon Jan 3, 2018
@zharben I do happen to be using Amazon AWS. However, adding the certificate file in (Windows), in Internet Options > Content > Certificates > Import, then importing the
pemfile that was provided in the link does not seem to work. Same with thep7bfilemodelmat commentedon Jan 3, 2018
Is there a way to make
asyncpgnot care about database verification?elprans commentedon Jan 3, 2018
The whole point of SSL is that it is verified.
ssl.create_default_context(capath=.modelmat commentedon Jan 3, 2018
So like this?
Because that doesn't seem to work.
elprans commentedon Jan 3, 2018
Well, that's weird. I'll try to reproduce. Meanwhile you can disable the certificate verification like this:
modelmat commentedon Jan 3, 2018
Those made it work. I can send the file if you want
elprans commentedon Jan 3, 2018
Duh. Replace
capathwithcafileand it should work.modelmat commentedon Jan 3, 2018
That doesn't work.
elprans commentedon Jan 4, 2018
Works for me. Double-check your CA file and that the intermediate cert matches your region.
12 remaining items
savv commentedon Jul 11, 2018
I'm sorry if this is a naive question, but what is the point of a private key (=pem), if it's available online?
elprans commentedon Jul 11, 2018
PEM is an encoding format. These are all certificates in pem format.
willyhakim commentedon Nov 8, 2018
Had the same problem trying to connect to aiven.io pg instance. After downloading the certificate from aiven project, this code worked. Don't forget to
import sslmodelmat commentedon Nov 17, 2018
@imbolc I think there's little point in actually using the certificate if you disable verification (
ctx.verify_mode = ssl.CERT_NOTE).lbenka commentedon Nov 21, 2018
Thanks for this #238 (comment) works for me ;)
GabrielNicolasAvellaneda commentedon Nov 28, 2018
Thought the same thing. You are right.
infnetdanpro commentedon Feb 19, 2020
Work too
Add ssl context for database connection
Added certificate by instruction MagicStack/asyncpg#238 (comment)
Can run locally, not only from server with ssl, thanks MagicStack/asy…
ZacharyThomas commentedon Dec 2, 2020
@elprans With the example RDS setup provided I actually couldn't connect to my RDS instance - perhaps due to my org's SSL setup. However, I can connect with a psycopg based driver using my DSN. The key difference for me, I think, is that pyscopg supports
sslmodeandsslrootcertconnect args - I wonder if it makes sense to support sslrootcert?elprans commentedon Dec 2, 2020
sslrootcertis just a connection option, which is equivalent to the above explicit context configuration. We can add the shortcut, but that'll not change the functionality. Please double check that you are specifying the correct certificate chain tocafile.ZacharyThomas commentedon Dec 2, 2020
@elprans I managed to get it to work but only by disabling the hostname check (not the verify_mode). I guess this is because sslmode
verify-cadoesn't verify the hostname by default,verify-fulldoes. However the default SSL context does attempt to verify the hostname.I guess from a user perspective it's somewhat unclear that the sslmode connection param is supported by asyncpg but it is somewhat overriden by the SSL context. Thanks for your help also, this thread was very useful to me :).
elprans commentedon Dec 2, 2020
Good point. I'll add a clarification.
cheburakshu commentedon Dec 9, 2020
Asyncpg doesn't work with
verify-full. Can you please suggest a solution? Thanks.Connecting with psql:
Asyncpg connect script:
Error:
Tried the same with sqlalchemy with psycopg2 driver. It works great.