@@ -1758,10 +1758,6 @@ async def connect(dsn=None, *,
1758
1758
max_cacheable_statement_size = 1024 * 15 ,
1759
1759
command_timeout = None ,
1760
1760
ssl = None ,
1761
- sslcert = None ,
1762
- sslkey = None ,
1763
- sslrootcert = None ,
1764
- sslcrl = None ,
1765
1761
connection_class = Connection ,
1766
1762
record_class = protocol .Record ,
1767
1763
server_settings = None ):
@@ -1780,10 +1776,11 @@ async def connect(dsn=None, *,
1780
1776
Connection arguments specified using as a single string in the
1781
1777
`libpq connection URI format`_:
1782
1778
``postgres://user:password@host:port/database?option=value``.
1783
- The following options are recognized by asyncpg: host, port,
1784
- user, database (or dbname), password, passfile, sslmode.
1785
- Unlike libpq, asyncpg will treat unrecognized options
1786
- as `server settings`_ to be used for the connection.
1779
+ The following options are recognized by asyncpg: ``host``,
1780
+ ``port``, ``user``, ``database`` (or ``dbname``), ``password``,
1781
+ ``passfile``, ``sslmode``, ``sslcert``, ``sslkey``, ``sslrootcert``,
1782
+ and ``sslcrl``. Unlike libpq, asyncpg will treat unrecognized
1783
+ options as `server settings`_ to be used for the connection.
1787
1784
1788
1785
.. note::
1789
1786
@@ -1904,21 +1901,51 @@ async def connect(dsn=None, *,
1904
1901
.. note::
1905
1902
1906
1903
*ssl* is ignored for Unix domain socket communication.
1907
-
1908
- :param sslcert:
1909
- This parameter specifies the file name of the client SSL certificate.
1910
1904
1911
- :param sslkey:
1912
- This parameter specifies the location for the secret key used for
1913
- the client certificate.
1905
+ Example of programmatic SSL context configuration that is equivalent
1906
+ to ``sslmode=verify-full&sslcert=..&sslkey=..&sslrootcert=..``:
1914
1907
1915
- :param sslrootcert:
1916
- This parameter specifies the name of a file containing SSL certificate
1917
- authority (CA) certificate(s).
1908
+ .. code-block:: pycon
1918
1909
1919
- :param sslcrl
1920
- This parameter specifies the file name of the SSL certificate
1921
- revocation list (CRL).
1910
+ >>> import asyncpg
1911
+ >>> import asyncio
1912
+ >>> import ssl
1913
+ >>> async def main():
1914
+ ... # Load CA bundle for server certificate verification,
1915
+ ... # equivalent to sslrootcert= in DSN.
1916
+ ... sslctx = ssl.create_default_context(
1917
+ ... ssl.Purpose.SERVER_AUTH,
1918
+ ... cafile="path/to/ca_bundle.pem")
1919
+ ... # If True, equivalent to sslmode=verify-full, if False:
1920
+ ... # sslmode=verify-ca.
1921
+ ... sslctx.check_hostname = True
1922
+ ... # Load client certificate and private key for client
1923
+ ... # authentication, equivalent to sslcert= and sslkey= in
1924
+ ... # DSN.
1925
+ ... sslctx.load_cert_chain(
1926
+ ... "path/to/client.cert",
1927
+ ... keyfile="path/to/client.key",
1928
+ ... )
1929
+ ... con = await asyncpg.connect(user='postgres', ssl=sslctx)
1930
+ ... await con.close()
1931
+ >>> asyncio.run(run())
1932
+
1933
+ Example of programmatic SSL context configuration that is equivalent
1934
+ to ``sslmode=require`` (no server certificate or host verification):
1935
+
1936
+ .. code-block:: pycon
1937
+
1938
+ >>> import asyncpg
1939
+ >>> import asyncio
1940
+ >>> import ssl
1941
+ >>> async def main():
1942
+ ... sslctx = ssl.create_default_context(
1943
+ ... ssl.Purpose.SERVER_AUTH)
1944
+ ... sslctx.check_hostname = False
1945
+ ... sslctx.verify_mode = ssl.CERT_NONE
1946
+ ... con = await asyncpg.connect(user='postgres', ssl=sslctx)
1947
+ ... await con.close()
1948
+ >>> asyncio.run(run())
1922
1949
1923
1950
:param dict server_settings:
1924
1951
An optional dict of server runtime parameters. Refer to
@@ -1978,6 +2005,10 @@ async def connect(dsn=None, *,
1978
2005
.. versionchanged:: 0.22.0
1979
2006
The *ssl* argument now defaults to ``'prefer'``.
1980
2007
2008
+ .. versionchanged:: 0.24.0
2009
+ The ``sslcert``, ``sslkey``, ``sslrootcert``, and ``sslcrl`` options
2010
+ are supported in the *dsn* argument.
2011
+
1981
2012
.. _SSLContext: https://docs.python.org/3/library/ssl.html#ssl.SSLContext
1982
2013
.. _create_default_context:
1983
2014
https://docs.python.org/3/library/ssl.html#ssl.create_default_context
@@ -2012,10 +2043,6 @@ async def connect(dsn=None, *,
2012
2043
password = password ,
2013
2044
passfile = passfile ,
2014
2045
ssl = ssl ,
2015
- sslcert = sslcert ,
2016
- sslkey = sslkey ,
2017
- sslrootcert = sslrootcert ,
2018
- sslcrl = sslcrl ,
2019
2046
database = database ,
2020
2047
server_settings = server_settings ,
2021
2048
command_timeout = command_timeout ,
0 commit comments