43 lines
4.7 KiB
Python
43 lines
4.7 KiB
Python
import pathlib,subprocess,secrets,json,time,tempfile
|
|
import os
|
|
root=pathlib.Path(__file__).resolve().parents[2]
|
|
bundle=pathlib.Path(os.environ['HEALTHAPP_RELEASE']).resolve()
|
|
identity=secrets.token_hex(4);name='healthapp-b-remote-app-'+identity;db='healthapp-b-remote-db-'+identity;network='healthapp-b-remote-net-'+identity;volume='healthapp-b-remote-cert-'+identity
|
|
password=secrets.token_hex(32);temporary=pathlib.Path(tempfile.mkdtemp(prefix='healthapp-b-remote-'))
|
|
evidence=root/'ImplementationPlan/evidence'
|
|
def run(args,check=True):
|
|
r=subprocess.run(args,capture_output=True,text=True)
|
|
if check and r.returncode:raise RuntimeError('Command failed: '+str(args[:3])+'\n'+(r.stdout+r.stderr).replace(password,'[redacted]')[-3500:])
|
|
return r
|
|
run(['openssl','req','-x509','-newkey','rsa:2048','-nodes','-keyout',str(temporary/'server.key'),'-out',str(temporary/'server.crt'),'-subj','/CN=b-database','-days','2','-addext','subjectAltName=DNS:b-database'])
|
|
run(['docker','network','create',network]);run(['docker','volume','create',volume])
|
|
try:
|
|
run(['docker','run','--rm','--user','0','--entrypoint','sh','-v',str(temporary)+':/input:ro','-v',volume+':/certs','postgres:18.6','-c','cp /input/server.key /input/server.crt /certs/ && chown 999:999 /certs/server.key /certs/server.crt && chmod 600 /certs/server.key'])
|
|
run(['docker','run','--rm','-d','--name',db,'--network',network,'--network-alias','b-database','--network-alias','wrong-b-database','-v',volume+':/certs:ro','-e','POSTGRES_PASSWORD='+password,'postgres:18.6','postgres','-c','ssl=on','-c','ssl_cert_file=/certs/server.crt','-c','ssl_key_file=/certs/server.key'])
|
|
for _ in range(60):
|
|
if run(['docker','exec',db,'pg_isready','-U','postgres'],False).returncode==0:break
|
|
time.sleep(.5)
|
|
else:raise RuntimeError('TLS database did not start')
|
|
run(['docker','run','--rm','-d','--privileged','--cgroupns=private','--tmpfs','/run','--tmpfs','/run/lock','--name',name,'--network',network,'healthapp-b-systemd-test:debian12'])
|
|
for _ in range(60):
|
|
if run(['docker','exec',name,'systemctl','is-system-running'],False).stdout.strip() in ('running','degraded'):break
|
|
time.sleep(.5)
|
|
run(['docker','cp',str(bundle),name+':/release']);run(['docker','cp',str(temporary/'server.crt'),name+':/root/db-ca.crt'])
|
|
admin=temporary/'admin.connection';admin.write_text('Host=b-database;Database=postgres;Username=postgres;Password='+password+';SslMode=VerifyFull;RootCertificate=/root/db-ca.crt');admin.chmod(0o600)
|
|
run(['docker','cp',str(admin),name+':/root/admin.connection']);run(['docker','exec',name,'chmod','600','/root/admin.connection'])
|
|
r=run(['docker','exec',name,'bash','/release/install.sh','--mode','remote','--host','localhost','--postgres-admin-file','/root/admin.connection'],False)
|
|
(evidence/'native-debian12-remote.log').write_text((r.stdout+r.stderr).replace(password,'[redacted]'));print((r.stdout+r.stderr).replace(password,'[redacted]')[-4500:])
|
|
if r.returncode:
|
|
print(run(['docker','exec',name,'journalctl','-u','healthapp-b','--no-pager','-n','30'],False).stdout[-3000:]);raise RuntimeError('remote installation failed')
|
|
config_mode=run(['docker','exec',name,'stat','-c','%a %U %G','/etc/healthapp-b/appsettings.json']).stdout.strip()
|
|
if config_mode!='640 root healthapp-b':raise RuntimeError('runtime configuration permissions are incorrect')
|
|
noServer=run(['docker','exec',name,'sh','-c','! command -v postgres && ! command -v pg_isready'],False).returncode==0
|
|
if not noServer:raise RuntimeError('remote mode installed local PostgreSQL')
|
|
run(['docker','exec',name,'sh','-c',"mkdir /root/wrong-name && sed 's/Host=b-database/Host=wrong-b-database/g' /var/lib/healthapp-b-installation/deployment/installation.json > /root/wrong-name/installation.json"])
|
|
negative=run(['docker','exec',name,'dotnet','/release/provisioner/HealthApp.Provisioning.dll','verify-database','--plan','/root/wrong-name'],False)
|
|
if negative.returncode==0:raise RuntimeError('wrong TLS hostname was accepted')
|
|
summary={'environment':run(['docker','exec',name,'cat','/etc/os-release']).stdout,'runtime':run(['docker','exec',name,'dotnet','--list-runtimes']).stdout,'database':'PostgreSQL 18.6 separate container/host namespace; TLS certificate SAN b-database','sslMode':'VerifyFull','wrongHostnameRejected':True,'localPostgresInstalled':False,'result':'passed','runtimeConfigMode':config_mode,'isolation':'Separate disposable systemd app and TLS DB containers; no Architecture A database access; not measurement-host evidence'}
|
|
(evidence/'native-debian12-remote-summary.json').write_text(json.dumps(summary,indent=2)+'\n');print('Remote Debian 12 native installation passed; local PostgreSQL absent.')
|
|
finally:
|
|
run(['docker','stop',name],False);run(['docker','stop',db],False);run(['docker','volume','rm',volume],False);run(['docker','network','rm',network],False)
|