@@ -222,6 +222,10 @@ def _parse_hostlist(hostlist, port, *, unquote=False):
222
222
223
223
224
224
def _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
+ )
225
229
if tls_version .startswith ('SSL' ):
226
230
raise ValueError (
227
231
f"Unsupported TLS version: { tls_version } "
@@ -234,6 +238,10 @@ def _parse_tls_version(tls_version):
234
238
)
235
239
236
240
241
+ def _dot_postgresql_path (filename ) -> pathlib .Path :
242
+ return (pathlib .Path .home () / '.postgresql' / filename ).resolve ()
243
+
244
+
237
245
def _parse_connect_dsn_and_args (* , dsn , host , port , user ,
238
246
password , passfile , database , ssl ,
239
247
connect_timeout , server_settings ):
@@ -485,7 +493,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
485
493
ssl .load_verify_locations (cafile = sslrootcert )
486
494
ssl .verify_mode = ssl_module .CERT_REQUIRED
487
495
else :
488
- sslrootcert = os . path . expanduser ( '~/.postgresql/ root.crt' )
496
+ sslrootcert = _dot_postgresql_path ( ' root.crt' )
489
497
try :
490
498
ssl .load_verify_locations (cafile = sslrootcert )
491
499
except FileNotFoundError :
@@ -509,7 +517,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
509
517
ssl .load_verify_locations (cafile = sslcrl )
510
518
ssl .verify_flags |= ssl_module .VERIFY_CRL_CHECK_CHAIN
511
519
else :
512
- sslcrl = os . path . expanduser ( '~/.postgresql/ root.crl' )
520
+ sslcrl = _dot_postgresql_path ( ' root.crl' )
513
521
try :
514
522
ssl .load_verify_locations (cafile = sslcrl )
515
523
except FileNotFoundError :
@@ -520,8 +528,8 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
520
528
if sslkey is None :
521
529
sslkey = os .getenv ('PGSSLKEY' )
522
530
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 ():
525
533
sslkey = None
526
534
if not sslpassword :
527
535
sslpassword = ''
@@ -532,7 +540,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
532
540
sslcert , keyfile = sslkey , password = lambda : sslpassword
533
541
)
534
542
else :
535
- sslcert = os . path . expanduser ( '~/.postgresql/ postgresql.crt' )
543
+ sslcert = _dot_postgresql_path ( ' postgresql.crt' )
536
544
try :
537
545
ssl .load_cert_chain (
538
546
sslcert , keyfile = sslkey , password = lambda : sslpassword
@@ -552,13 +560,17 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
552
560
ssl .options &= ~ ssl_module .OP_NO_COMPRESSION
553
561
554
562
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' )
558
564
if ssl_min_protocol_version :
559
565
ssl .minimum_version = _parse_tls_version (
560
566
ssl_min_protocol_version
561
567
)
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
562
574
563
575
if ssl_max_protocol_version is None :
564
576
ssl_max_protocol_version = os .getenv ('PGSSLMAXPROTOCOLVERSION' )
0 commit comments