Minor refactor of get_server_cert

This commit is contained in:
Tarjei Husøy 2018-04-21 12:37:54 -07:00
parent ac01be70cb
commit dc38e1fd1b

View file

@ -21,7 +21,7 @@ def main():
certificate_as_pem = get_certificate_from_socket(sock) certificate_as_pem = get_certificate_from_socket(sock)
print(certificate_as_pem.decode('utf-8')) print(certificate_as_pem.decode('utf-8'))
except Exception as exc: except Exception as exc:
sys.stderr.write('Something failed while fetching certificate: {0}'.format(exc)) sys.stderr.write('Something failed while fetching certificate: {0}\n'.format(exc))
sys.exit(1) sys.exit(1)
finally: finally:
sock.close() sock.close()
@ -55,11 +55,12 @@ def request_ssl(sock):
# 1234.5679 is the magic protocol version used to request TLS, defined # 1234.5679 is the magic protocol version used to request TLS, defined
# in pgcomm.h) # in pgcomm.h)
version_ssl = postgres_protocol_version_to_binary(1234, 5679) version_ssl = postgres_protocol_version_to_binary(1234, 5679)
packet = struct.pack('!I', 8) + version_ssl length = struct.pack('!I', 8)
packet = length + version_ssl
sock.sendall(packet) sock.sendall(packet)
data = read_n_bytes_from_socket(sock, 1) data = read_n_bytes_from_socket(sock, 1)
if data != bytearray('S'.encode('utf-8')): if data != b'S':
raise Exception('Backend does not support TLS') raise Exception('Backend does not support TLS')