100 lines
5.6 KiB
Python
100 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a private, pinned FIPS handover candidate; never install or publish it."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import pathlib
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import urllib.request
|
|
|
|
REVISION = 'f4a5b4a8e61e340e6be7551608109a5c399f8353'
|
|
DEB_SHA256 = '6edd18e67c4f4c47d9d8c7366aacc6505d11df79c1c4b1b1d63246fa66e3e8a3'
|
|
VERSION = '0.5.3+archy.handoff1'
|
|
REPO = pathlib.Path(__file__).resolve().parents[2]
|
|
PATCH = REPO / 'docs/companion-performance/fips-authenticated-return-path.patch'
|
|
|
|
|
|
def run(*args, **kwargs):
|
|
return subprocess.run(args, check=True, **kwargs)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--image', required=True, help='immutable local Podman image ID; Rust 1.98.1, Debian bookworm, clang, cmake, pkg-config, libssl-dev, libdbus-1-dev, libudev-dev, protobuf-compiler')
|
|
parser.add_argument('--output', type=pathlib.Path, required=True)
|
|
parser.add_argument('--cargo-home', type=pathlib.Path, required=True, help='dedicated dependency cache (not a credential-bearing personal Cargo home)')
|
|
args = parser.parse_args()
|
|
if not re.fullmatch(r'(?:sha256:)?[0-9a-f]{64}', args.image):
|
|
parser.error('--image must be an immutable local image ID')
|
|
output = args.output.expanduser().resolve()
|
|
cargo_home = args.cargo_home.expanduser().resolve()
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
cargo_home.mkdir(parents=True, exist_ok=True)
|
|
(output / "target").mkdir(exist_ok=True)
|
|
artifact = output / f'fips_{VERSION}_amd64.deb'
|
|
if artifact.exists():
|
|
parser.error('candidate already exists; use a fresh output directory')
|
|
with tempfile.TemporaryDirectory(prefix='fips-handoff-', dir=output) as temporary:
|
|
work = pathlib.Path(temporary)
|
|
source = work / 'source'
|
|
source.mkdir()
|
|
run('git', 'init', '-q', str(source))
|
|
run('git', '-C', str(source), 'fetch', '--depth=1', 'https://github.com/jmcorgan/fips.git', REVISION)
|
|
run('git', '-C', str(source), 'checkout', '--detach', '-q', 'FETCH_HEAD')
|
|
assert subprocess.check_output(['git', '-C', str(source), 'rev-parse', 'HEAD'], text=True).strip() == REVISION
|
|
run('git', '-C', str(source), 'apply', '--check', str(PATCH))
|
|
run('git', '-C', str(source), 'apply', str(PATCH))
|
|
base = work / 'upstream.deb'
|
|
with urllib.request.urlopen('https://github.com/jmcorgan/fips/releases/download/v0.5.3/fips_0.5.3_amd64.deb', timeout=60) as response:
|
|
base.write_bytes(response.read())
|
|
if hashlib.sha256(base.read_bytes()).hexdigest() != DEB_SHA256:
|
|
raise RuntimeError('official base package checksum mismatch')
|
|
# No device, host wallet, service socket or host network is mounted.
|
|
# Building can fetch locked dependencies; packaging runs offline.
|
|
common = ['podman', 'run', '--rm', '--cpus=4', '--memory=8g', '--pids-limit=2048',
|
|
'--cap-drop=all', '--security-opt=no-new-privileges', '--read-only',
|
|
'--tmpfs', '/tmp:rw,size=512m', '--tmpfs', '/root:rw,size=64m',
|
|
'--volume', f'{work}:/work:rw,Z', '--volume', f'{cargo_home}:/cargo-home:rw,Z',
|
|
'--volume', f'{output / "target"}:/target:rw,Z',
|
|
'--env', 'CARGO_HOME=/cargo-home', '--env', 'CARGO_TARGET_DIR=/target',
|
|
'--env', 'RUSTUP_TOOLCHAIN=1.98.1', '--workdir', '/work/source']
|
|
run(*common, '--network=pasta', args.image, 'cargo', 'build', '--release', '--locked', '--bin', 'fips', '--bin', 'fipsctl')
|
|
run(*common, '--network=none', args.image, 'dpkg-deb', '--raw-extract', '/work/upstream.deb', '/work/package')
|
|
package = work / 'package'
|
|
for name in ('fips', 'fipsctl'):
|
|
binary = output / 'target/release' / name
|
|
destination = package / 'usr/bin' / name
|
|
destination.write_bytes(binary.read_bytes())
|
|
destination.chmod(0o755)
|
|
control = package / 'DEBIAN/control'
|
|
text = control.read_text()
|
|
if not re.search(r'^Architecture: amd64$', text, re.M):
|
|
raise RuntimeError('unexpected package architecture')
|
|
text = re.sub(r'^Version:.*$', 'Version: ' + VERSION, text, flags=re.M)
|
|
size = sum(p.stat().st_size for p in package.rglob('*') if p.is_file())
|
|
text = re.sub(r'^Installed-Size:.*$', f'Installed-Size: {(size + 1023) // 1024}', text, flags=re.M)
|
|
control.write_text(text)
|
|
sums = package / 'DEBIAN/md5sums'
|
|
if sums.exists():
|
|
rows = []
|
|
for line in sums.read_text().splitlines():
|
|
digest, name = line.split(maxsplit=1)
|
|
if name in ('usr/bin/fips', 'usr/bin/fipsctl'):
|
|
digest = hashlib.md5((package / name).read_bytes(), usedforsecurity=False).hexdigest()
|
|
rows.append(f'{digest} {name}')
|
|
sums.write_text('\n'.join(rows) + '\n')
|
|
run(*common, '--network=none', args.image, 'dpkg-deb', '--root-owner-group', '--build', '/work/package', '/work/candidate.deb')
|
|
artifact.write_bytes((work / 'candidate.deb').read_bytes())
|
|
metadata = {'version': VERSION, 'upstream_commit': REVISION, 'upstream_deb_sha256': DEB_SHA256,
|
|
'patch_sha256': hashlib.sha256(PATCH.read_bytes()).hexdigest(), 'builder_image': args.image, 'rust_toolchain': '1.98.1',
|
|
'candidate_sha256': hashlib.sha256(artifact.read_bytes()).hexdigest(),
|
|
'publication': 'private UAT candidate; no installation or public release performed'}
|
|
artifact.with_suffix('.json').write_text(json.dumps(metadata, indent=2) + '\n')
|
|
print(artifact)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|