@@ -222,6 +222,10 @@ def _parse_hostlist(hostlist, port, *, unquote=False):
222222
223223
224224def _parse_tls_version (tls_version ):
225+ if not hasattr (ssl_module , 'TLSVersion' ):
226+ raise ValueError (
227+ "TLSVersion is not supported in this version of Python"
228+ )
225229 if tls_version .startswith ('SSL' ):
226230 raise ValueError (
227231 f"Unsupported TLS version: { tls_version } "
@@ -234,6 +238,10 @@ def _parse_tls_version(tls_version):
234238 )
235239
236240
241+ def _dot_postgresql_path (filename ) -> pathlib .Path :
242+ return (pathlib .Path .home () / '.postgresql' / filename ).resolve ()
243+
244+
237245def _parse_connect_dsn_and_args (* , dsn , host , port , user ,
238246 password , passfile , database , ssl ,
239247 connect_timeout , server_settings ):
@@ -485,7 +493,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
485493 ssl .load_verify_locations (cafile = sslrootcert )
486494 ssl .verify_mode = ssl_module .CERT_REQUIRED
487495 else :
488- sslrootcert = os . path . expanduser ( '~/.postgresql/ root.crt' )
496+ sslrootcert = _dot_postgresql_path ( ' root.crt' )
489497 try :
490498 ssl .load_verify_locations (cafile = sslrootcert )
491499 except FileNotFoundError :
@@ -509,7 +517,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
509517 ssl .load_verify_locations (cafile = sslcrl )
510518 ssl .verify_flags |= ssl_module .VERIFY_CRL_CHECK_CHAIN
511519 else :
512- sslcrl = os . path . expanduser ( '~/.postgresql/ root.crl' )
520+ sslcrl = _dot_postgresql_path ( ' root.crl' )
513521 try :
514522 ssl .load_verify_locations (cafile = sslcrl )
515523 except FileNotFoundError :
@@ -520,8 +528,8 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
520528 if sslkey is None :
521529 sslkey = os .getenv ('PGSSLKEY' )
522530 if not sslkey :
523- sslkey = os . path . expanduser ( '~/.postgresql/ postgresql.key' )
524- if not os . path . exists (sslkey ):
531+ sslkey = _dot_postgresql_path ( ' postgresql.key' )
532+ if not sslkey . exists ():
525533 sslkey = None
526534 if not sslpassword :
527535 sslpassword = ''
@@ -532,7 +540,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
532540 sslcert , keyfile = sslkey , password = lambda : sslpassword
533541 )
534542 else :
535- sslcert = os . path . expanduser ( '~/.postgresql/ postgresql.crt' )
543+ sslcert = _dot_postgresql_path ( ' postgresql.crt' )
536544 try :
537545 ssl .load_cert_chain (
538546 sslcert , keyfile = sslkey , password = lambda : sslpassword
@@ -552,13 +560,17 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
552560 ssl .options &= ~ ssl_module .OP_NO_COMPRESSION
553561
554562 if ssl_min_protocol_version is None :
555- ssl_min_protocol_version = os .getenv (
556- 'PGSSLMINPROTOCOLVERSION' , 'TLSv1.2'
557- )
563+ ssl_min_protocol_version = os .getenv ('PGSSLMINPROTOCOLVERSION' )
558564 if ssl_min_protocol_version :
559565 ssl .minimum_version = _parse_tls_version (
560566 ssl_min_protocol_version
561567 )
568+ else :
569+ try :
570+ ssl .minimum_version = _parse_tls_version ('TLSv1.2' )
571+ except ValueError :
572+ # Python 3.6 does not have ssl.TLSVersion
573+ pass
562574
563575 if ssl_max_protocol_version is None :
564576 ssl_max_protocol_version = os .getenv ('PGSSLMAXPROTOCOLVERSION' )
0 commit comments