"""Run in the verifier profile. Real TLS, identity and authorization probes.""" import json import socket import ssl import urllib.request import urllib.error import uuid import time from urllib.parse import urlparse import boto3 import pika import psycopg from botocore.exceptions import ClientError def context(identity=None): ctx=ssl.create_default_context(cafile='/probe/lb/ca.crt') if identity: ctx.load_cert_chain('/probe/'+identity+'/cert.crt','/probe/'+identity+'/key.key') return ctx def http(host,port,path,identity=None,method='GET',body=None,token=None): headers={'Content-Type':'application/json'} if token:headers['X-Vault-Token']=token req=urllib.request.Request(f'https://{host}:{port}'+path,data=json.dumps(body).encode() if body is not None else None, headers=headers,method=method) try: with urllib.request.urlopen(req,context=context(identity),timeout=5) as response: return response.status,response.read() except urllib.error.HTTPError as error: return error.code,error.read() def denied_tls(host,port,path,identity): try: status,_=http(host,port,path,identity) except (ssl.SSLError, ConnectionResetError, __import__('http.client',fromlist=['RemoteDisconnected']).RemoteDisconnected): return except urllib.error.URLError as error: assert isinstance(error.reason,(ssl.SSLError,ConnectionResetError)),type(error.reason).__name__ return raise AssertionError('TLS authorization unexpectedly returned HTTP '+str(status)) def plaintext_closed(host,port): try: with socket.create_connection((host,port),timeout=2):pass except ConnectionRefusedError:return raise AssertionError('Plaintext bypass reachable '+host) def wait_http(host,port,path,identity): deadline=time.monotonic()+60 while True: status,_=http(host,port,path,identity) if status==200:return assert time.monotonic() all backends mTLS') # Assert hostname rejection, distinct from network timeout/refusal. with socket.create_connection(('s3',9000),timeout=3) as raw: try:context().wrap_socket(raw,server_hostname='wrong.invalid') except ssl.SSLCertVerificationError:pass else:raise AssertionError('wrong hostname accepted') print('PASS internal wrong hostname rejected by certificate verifier') if __name__=='__main__': main()