Qualify tenant-scoped bootstrap rotation and fresh Windows scripting
This commit is contained in:
+90
-3
@@ -2,6 +2,7 @@
|
||||
"""Prepare an Ed25519 agent release on the signing host and publish through the catalog API."""
|
||||
import argparse
|
||||
import base64
|
||||
import fcntl
|
||||
import getpass
|
||||
import hashlib
|
||||
import http.client
|
||||
@@ -50,6 +51,22 @@ def fingerprint(path):
|
||||
return digest.hexdigest(), size
|
||||
|
||||
|
||||
def verify_native_image(path, target, size):
|
||||
with path.open('rb') as binary:
|
||||
header = binary.read(64)
|
||||
check(len(header) == 64, 'truncated binary header: ' + path.name)
|
||||
if target == LINUX:
|
||||
check(header[:6] == b'\x7fELF\x02\x01' and header[16:18] in (b'\x02\x00', b'\x03\x00')
|
||||
and header[18:20] == b'\x3e\x00', 'Linux x86-64 ELF image required: ' + path.name)
|
||||
else:
|
||||
pe = int.from_bytes(header[60:64], 'little')
|
||||
check(header[:2] == b'MZ' and 64 <= pe <= size - 26, 'Windows PE image required: ' + path.name)
|
||||
binary.seek(pe)
|
||||
coff = binary.read(26)
|
||||
check(coff[:6] == b'PE\0\0\x64\x86' and coff[24:26] == b'\x0b\x02',
|
||||
'Windows x86-64 PE32+ image required: ' + path.name)
|
||||
|
||||
|
||||
def binaries(folder, receipt):
|
||||
record = json.loads(receipt.read_text(encoding='utf-8-sig'))
|
||||
if 'target' in record and 'sha256' in record:
|
||||
@@ -65,6 +82,7 @@ def binaries(folder, receipt):
|
||||
for name in FILES[target]:
|
||||
path = folder / name
|
||||
digest, size = fingerprint(path)
|
||||
verify_native_image(path, target, size)
|
||||
check(digest == hashes[name], 'file differs from build receipt: ' + name)
|
||||
result.append({'artifact_id': str(uuid.uuid5(uuid.NAMESPACE_OID, target + '/' + name + '/' + digest)),
|
||||
'sha256': digest, 'bytes': size})
|
||||
@@ -108,11 +126,22 @@ def https(value):
|
||||
|
||||
|
||||
def prepare(args):
|
||||
provision_folder(args.state)
|
||||
provision_folder(args.state/'private')
|
||||
with (args.state/'private'/'signing.lock').open('a') as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
prepare_locked(args)
|
||||
|
||||
|
||||
def prepare_locked(args):
|
||||
https(args.gateway)
|
||||
check(0 < args.sequence < 2**63 and re.fullmatch('[A-Za-z0-9.+_-]{1,64}', args.version), 'invalid release version/sequence')
|
||||
minimum = getattr(args, 'minimum_updater_version', 1)
|
||||
check(minimum in (1,2), 'unknown minimum updater version')
|
||||
check(0 < args.days <= 3650, 'invalid validity in days')
|
||||
platform, blobs = binaries(args.artifacts, args.build_receipt)
|
||||
label = {LINUX:'linux', WINDOWS:'windows', WINDOWS_GNU:'windows-gnu'}[platform]
|
||||
check(minimum == 1 or platform == WINDOWS_GNU, 'version 2 currently applies to the GNU Windows worker')
|
||||
provision_folder(args.state)
|
||||
config, private = args.state / 'config', args.state / 'private'
|
||||
provision_folder(config, args.service_uid)
|
||||
@@ -146,13 +175,15 @@ def prepare(args):
|
||||
prepared = args.state / 'prepared' / label / str(args.sequence)
|
||||
provision_folder(prepared.parent)
|
||||
if prepared.exists():
|
||||
check(json.loads((prepared / 'inputs.json').read_bytes()) == {'version': args.version, 'blobs': blobs}, 'sequence already bound')
|
||||
prior = json.loads((prepared / 'inputs.json').read_bytes())
|
||||
check(prior.get('version') == args.version and prior.get('blobs') == blobs
|
||||
and prior.get('minimum_updater_version', 1) == minimum, 'sequence already bound')
|
||||
print(prepared / 'envelope.json')
|
||||
return
|
||||
now = int(time.time())
|
||||
manifest = {'schema_version': 1, 'release_id': str(uuid.uuid4()), 'release_sequence': args.sequence,
|
||||
'version': args.version, 'platform': platform, 'published_at': now, 'expires_at': now + args.days * 86400,
|
||||
'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': 1,
|
||||
'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': minimum,
|
||||
'artifacts': [dict(role=role, **blob) for role, blob in zip(('agent', 'helper'), blobs[:2])]}
|
||||
raw = encoded(manifest)
|
||||
with tempfile.TemporaryDirectory(dir=prepared.parent) as tmpdir:
|
||||
@@ -168,7 +199,7 @@ def prepare(args):
|
||||
output.mkdir()
|
||||
env = {'schema_version': 1, 'key_id': hashlib.sha256(pub).hexdigest(), 'payload_bytes': b64(raw), 'signature': b64(sig)}
|
||||
save_once(output / 'envelope.json', encoded(env))
|
||||
save_once(output / 'inputs.json', encoded({'version': args.version, 'blobs': blobs}))
|
||||
save_once(output / 'inputs.json', encoded({'version': args.version, 'blobs': blobs, 'minimum_updater_version': minimum}))
|
||||
output.rename(prepared)
|
||||
print(prepared / 'envelope.json')
|
||||
|
||||
@@ -197,12 +228,59 @@ class Publisher:
|
||||
conn.close()
|
||||
|
||||
|
||||
def rotate_pin(state, label, tenant, expected, new_pin):
|
||||
tenant = str(uuid.UUID(tenant))
|
||||
check(re.fullmatch('[0-9a-f]{64}', expected), 'expected predecessor SHA-256 required')
|
||||
directory = state/'config'
|
||||
default = directory/(label + '-bootstrap.json')
|
||||
path = default.with_suffix('.' + tenant + '.json')
|
||||
# The already completed upload attests the API read and stored the object.
|
||||
# Independently reopen the actual tenant object before changing the pin.
|
||||
sha, size = fingerprint(state/'objects'/tenant/new_pin['sha256'])
|
||||
check(new_pin == {'sha256':sha,'bytes':size}, 'uploaded stable updater differs')
|
||||
with (state/'private'/'bootstrap-rotation.lock').open('a') as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
if path.exists():
|
||||
pins = json.loads(path.read_bytes())
|
||||
else:
|
||||
baseline = json.loads(default.read_bytes())
|
||||
pins = {'current':{'sha256':baseline['updater_sha256'],'bytes':baseline['updater_bytes']},'previous':[]}
|
||||
now = int(time.time())
|
||||
if pins['current'] == new_pin:
|
||||
prior = pins['previous'][-1]['pin']['sha256'] if pins['previous'] else None
|
||||
check(expected in (prior, new_pin['sha256']), 'bootstrap predecessor changed')
|
||||
return
|
||||
check(pins['current']['sha256'] == expected, 'bootstrap predecessor changed')
|
||||
previous = [item for item in pins['previous'] if item['expires_at'] > now
|
||||
and item['pin'] not in (new_pin,pins['current'])]
|
||||
previous.append({'pin':pins['current'],'expires_at':now+900})
|
||||
raw = encoded({'current':new_pin,'previous':previous})
|
||||
check(len(raw) <= 32768, 'too many concurrent bootstrap rotations')
|
||||
with tempfile.NamedTemporaryFile(dir=directory,delete=False) as f:
|
||||
try:
|
||||
f.write(raw)
|
||||
os.fchmod(f.fileno(),0o600)
|
||||
os.fchown(f.fileno(),default.stat().st_uid,default.stat().st_gid)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.replace(f.name,path)
|
||||
finally:
|
||||
Path(f.name).unlink(missing_ok=True)
|
||||
fd = os.open(directory,os.O_DIRECTORY)
|
||||
try:
|
||||
os.fsync(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def publish(args):
|
||||
platform, blobs = binaries(args.artifacts, args.build_receipt)
|
||||
env = json.loads(args.envelope.read_bytes())
|
||||
manifest = json.loads(base64.urlsafe_b64decode(env['payload_bytes'] + '==='))
|
||||
check(manifest['platform'] == platform and manifest['artifacts'] == [dict(role=role, **blob)
|
||||
for role, blob in zip(('agent', 'helper'), blobs[:2])], 'input files differ from release envelope')
|
||||
check(bool(args.rotate_bootstrap_from) == bool(args.bootstrap_state), 'rotation needs previous SHA and local signing state')
|
||||
check(not (args.rotate_bootstrap_from and args.bootstrap_worker), 'a rotation uses the new receipt-bound stable worker')
|
||||
client = Publisher(args.origin, args.ca)
|
||||
if args.dev_login_file:
|
||||
path = args.dev_login_file
|
||||
@@ -222,6 +300,7 @@ def publish(args):
|
||||
if args.bootstrap_worker:
|
||||
paths[2] = args.bootstrap_worker
|
||||
sha, size = fingerprint(paths[2])
|
||||
verify_native_image(paths[2], platform, size)
|
||||
name = FILES[platform][2]
|
||||
blobs[2] = {'artifact_id': str(uuid.uuid5(uuid.NAMESPACE_OID, platform + '/' + name + '/' + sha)),
|
||||
'sha256': sha, 'bytes': size}
|
||||
@@ -231,6 +310,11 @@ def publish(args):
|
||||
result = client.request('POST', path + '/agent-update-artifacts?' + query, data,
|
||||
{'Content-Type': 'application/octet-stream', 'Content-Length': str(blob['bytes'])})
|
||||
check(result['sha256'] == blob['sha256'] and result['size_bytes'] == blob['bytes'], 'artifact receipt differs')
|
||||
if args.rotate_bootstrap_from:
|
||||
stable = blobs[2]
|
||||
label = {LINUX:'linux', WINDOWS:'windows', WINDOWS_GNU:'windows-gnu'}[platform]
|
||||
rotate_pin(args.bootstrap_state,label,tenant,args.rotate_bootstrap_from,
|
||||
{'sha256':stable['sha256'],'bytes':stable['bytes']})
|
||||
result = client.request('POST', path + '/agent-releases', encoded(env), {'Content-Type': 'application/json'})
|
||||
check(result['release_envelope'] == env, 'published envelope changed')
|
||||
print(json.dumps({'release_id': result['release_id'], 'version': manifest['version'], 'platform': platform,
|
||||
@@ -253,9 +337,12 @@ def main():
|
||||
cmd.add_argument('--sequence', type=int, required=True)
|
||||
cmd.add_argument('--version', required=True)
|
||||
cmd.add_argument('--days', type=int, default=90)
|
||||
cmd.add_argument('--minimum-updater-version',type=int,default=1)
|
||||
cmd.add_argument('--service-uid', type=int, default=os.getuid())
|
||||
else:
|
||||
cmd.add_argument('--origin', required=True)
|
||||
cmd.add_argument('--bootstrap-state',type=Path,help='Local signing host state for an explicit stable updater rotation')
|
||||
cmd.add_argument('--rotate-bootstrap-from',help='Compare-and-swap the tenant pin from this SHA-256 after verified upload')
|
||||
cmd.add_argument('--bootstrap-worker', type=Path, help='Pinned initial stable updater when publishing subsequent code pairs')
|
||||
cmd.add_argument('--ca', type=Path)
|
||||
cmd.add_argument('--tenant', required=True)
|
||||
|
||||
Reference in New Issue
Block a user