Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add driver option to disable TLS certificate verification #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions postgresql/documentation/driver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ interfaces:
``sslrootcrlfile``
Revocation list file path. [Currently not checked.]

``sslnoverify``
Optionally disable certificate verification.

Connections
===========
Expand Down
2 changes: 2 additions & 0 deletions postgresql/driver/pq3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2796,11 +2796,13 @@ def _security(self, parameters):
self.sslcrtfile = parameters.get('sslcrtfile') or None
self.sslrootcrtfile = parameters.get('sslrootcrtfile') or None
self.sslrootcrlfile = parameters.get('sslrootcrlfile') or None
self.sslnoverify = parameters.get('sslnoverify') or None

self._socket_secure = {
'keyfile': self.sslkeyfile,
'certfile': self.sslcrtfile,
'ca_certs': self.sslrootcrtfile,
'noverify': self.sslnoverify,
}

if self.sslrootcrlfile is not None:
Expand Down
7 changes: 6 additions & 1 deletion postgresql/python/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ def fatal_exception_message(typ, err) -> (str, None):
@property
def _security_context(self):
if self._security_context_ii is None:
from ssl import SSLContext, PROTOCOL_TLS_CLIENT
from ssl import SSLContext, PROTOCOL_TLS_CLIENT, CERT_NONE
ctx = self._security_context_ii = SSLContext(PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False

if self.socket_secure.get('noverify') is not None:
ctx.verify_mode = CERT_NONE

cf = self.socket_secure.get('certfile')
kf = self.socket_secure.get('keyfile')
if cf is not None:
Expand All @@ -63,6 +66,8 @@ def _security_context(self):
ca = self.socket_secure.get('ca_certs')
if ca is not None:
self._security_context_ii.load_verify_locations(ca)
else:
self._security_context_ii.load_default_certs()

return self._security_context_ii

Expand Down