Complete lab 7 TLS and secret management
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One-shot in-network mTLS probe used by security_runtime.py."""
|
||||
import argparse
|
||||
import base64
|
||||
import http.client
|
||||
import json
|
||||
import ssl
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
p=argparse.ArgumentParser()
|
||||
p.add_argument('--url',required=True)
|
||||
p.add_argument('--path',default='/health/ready')
|
||||
p.add_argument('--mode',choices=('trusted','no-cert','untrusted-ca','wrong-identity'),required=True)
|
||||
a=p.parse_args()
|
||||
ctx=ssl.create_default_context(cafile='/run/certs/service-ca.crt')
|
||||
if a.mode=='trusted':
|
||||
ctx.load_cert_chain('/run/certs/lb-client.crt','/run/certs/lb-client.key')
|
||||
elif a.mode=='untrusted-ca':
|
||||
ctx.load_cert_chain('/run/certs/untrusted-client.crt','/run/certs/untrusted-client.key')
|
||||
elif a.mode=='wrong-identity':
|
||||
ctx.load_cert_chain('/run/certs/foreign-client.crt','/run/certs/foreign-client.key')
|
||||
try:
|
||||
raw=sys.stdin.read() if not sys.stdin.isatty() else ''
|
||||
probe=json.loads(raw) if raw.strip() else {}
|
||||
headers=probe.get('headers',{})
|
||||
body=base64.b64decode(probe['body_b64']) if probe.get('body_b64') else None
|
||||
request=urllib.request.Request(a.url.rstrip('/')+a.path,data=body,headers=headers,method=probe.get('method','GET'))
|
||||
with urllib.request.urlopen(request,context=ctx,timeout=5) as response:
|
||||
status=response.status
|
||||
except urllib.error.HTTPError as exc:
|
||||
status=exc.code
|
||||
except http.client.RemoteDisconnected:
|
||||
if a.mode in ('no-cert','untrusted-ca'):
|
||||
print('PASS: API rejected '+a.mode+' during TLS handshake')
|
||||
raise SystemExit(0)
|
||||
raise SystemExit('FAIL: unexpected TLS peer disconnect')
|
||||
except urllib.error.URLError as exc:
|
||||
if a.mode in ('no-cert','untrusted-ca') and isinstance(exc.reason,(ssl.SSLError,ConnectionResetError,http.client.RemoteDisconnected)):
|
||||
print('PASS: API rejected '+a.mode+' during TLS handshake')
|
||||
raise SystemExit(0)
|
||||
raise SystemExit('FAIL: unexpected TLS probe error: '+type(exc.reason).__name__)
|
||||
expected=probe.get('expected',200) if 'probe' in locals() else 200
|
||||
if a.mode=='trusted' and status==expected:
|
||||
print('PASS: API returned expected status '+str(status)+' for the allowlisted LB identity')
|
||||
elif a.mode=='wrong-identity' and status==403:
|
||||
print('PASS: API rejected a CA-signed certificate with the wrong SAN identity')
|
||||
else:
|
||||
raise SystemExit('FAIL: '+a.mode+' probe returned HTTP '+str(status))
|
||||
Reference in New Issue
Block a user