PlotDirector/Tools/Deployment/sandbox_build.py

56 lines
2.9 KiB
Python

"""Isolate repository-controlled build code; signing and activation stay outside.
Only OS tools, the source, scratch build output and public NuGet packages are
visible. No host home, signing keys, controller records, inbox or runtime config.
"""
from pathlib import Path
import subprocess
import tempfile
def build_command(command, source, writable, home):
source, writable = Path(source).resolve(), Path(writable).resolve()
if command[0] != 'dotnet' or command[1] not in ('publish', 'run'):
raise ValueError('Only existing dotnet release gates are supported')
if not (source / '.git').is_dir():
raise ValueError('Isolated builds require a standalone committed checkout')
args = ['/usr/bin/bwrap', '--die-with-parent', '--unshare-all', '--share-net',
'--new-session', '--cap-drop', 'ALL', '--clearenv',
'--ro-bind', '/usr', '/usr', '--symlink', 'usr/bin', '/bin',
'--symlink', 'usr/lib', '/lib', '--symlink', 'usr/lib64', '/lib64',
'--proc', '/proc', '--dev', '/dev', '--tmpfs', '/tmp',
'--dir', '/etc', '--ro-bind', '/etc/ssl', '/etc/ssl',
'--ro-bind', '/etc/resolv.conf', '/etc/resolv.conf',
'--ro-bind', '/etc/hosts', '/etc/hosts',
'--bind', str(home), '/build-home',
'--bind', str(source), str(source),
'--ro-bind', str(source / '.git'), str(source / '.git'),
'--bind', str(writable), str(writable),
'--chdir', str(source), '--setenv', 'HOME', '/build-home',
'--setenv', 'DOTNET_CLI_HOME', '/build-home',
'--setenv', 'DOTNET_CLI_TELEMETRY_OPTOUT', '1',
'--setenv', 'DOTNET_SKIP_FIRST_TIME_EXPERIENCE', '1',
'--setenv', 'PATH', '/usr/bin:/bin', '--setenv', 'TMPDIR', '/tmp']
# NuGet/.NET use NSS identity lookups even when HOME is explicit. These
# world-readable OS metadata files contain no deployment credentials.
for filename in ('passwd', 'group', 'nsswitch.conf', 'os-release', 'localtime'):
path = Path('/etc')/filename
if path.exists():
args += ['--ro-bind', str(path), str(path)]
packages = Path.home() / '.nuget/packages'
if packages.is_dir():
args += ['--ro-bind', str(packages), '/nuget/packages',
'--setenv', 'NUGET_PACKAGES', '/nuget/packages']
return args + ['--', *command]
def run_build(command, source, writable, log):
# A fresh home per invocation has no NuGet credentials or host configuration.
with tempfile.TemporaryDirectory(prefix='pd-build-home-') as home:
args = build_command(command, source, writable, home)
with Path(log).open('w') as stream:
result = subprocess.run(args, stdout=stream, stderr=subprocess.STDOUT,
env={'PATH': '/usr/bin:/bin'}, timeout=1800)
if result.returncode:
raise ValueError('Isolated release gate failed; inspect '+str(log))