interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / scripts / test-public-key-integration.sh

146 lines · 12409 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Public-key integration test (D3, sc#385 and sc#118). Proves, from ONLY the public folder a ceremony produces
18# (allowed_signers line + ceremony-record.txt), that the post-ceremony trust-config pull request is correct:
19# (a) a release signed by the matching key installs with the real install.sh as signature_check: release, and the
20# publish gate passes it
21# (b) a release signed by a different key is refused by install.sh ("signature is NOT valid") and by the publish gate
22# (c) a tampered archive (checksum) and a tampered checksum list (signature) are refused and install nothing
23# (d) the trust-config change is a reviewable diff: one allowed_signers key line, one AIEN_RELEASE_SIGNER line,
24# one new public record file, no private material
25# (e) the record's fingerprint matches the pinned line, and each kind of mismatch is refused (mutants)
26# The "ceremony" is the --rehearsal mode of scripts/key-ceremony.sh: a throwaway key made and deleted inside this test.
27# No production key is made, read or needed.
28set -euo pipefail
29ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
30W="$(mktemp -d "${TMPDIR:-/tmp}/aien-test-pubkey.XXXXXX")"
31trap 'rm -rf "$W"' EXIT
32fail() { echo "FAIL: $*" >&2; exit 1; }
33pass() { echo "ok: $*"; }
34case "$(uname -s)" in Linux) OS=linux ;; Darwin) OS=macos ;; *) fail "unsupported OS" ;; esac
35case "$(uname -m)" in x86_64|amd64) ARCH=x86_64 ;; aarch64|arm64) ARCH=aarch64 ;; *) fail "unsupported arch" ;; esac
36[[ "$OS-$ARCH" != macos-aarch64 ]] || ARCH=arm64
37ASSET="sovereign-$OS-$ARCH.tar.gz"
38KC=(env AIEN_CEREMONY_ASSUME_YES=1 bash "$ROOT/scripts/key-ceremony.sh")
39
40# The rehearsal ceremony. From here on only $PUB (public) is used to configure trust; the private key only signs.
41"${KC[@]}" ceremony --rehearsal --out "$W/offline" --backup "$W/u1" --backup "$W/u2" > "$W/cer.log" 2>&1 || { tail -20 "$W/cer.log" >&2; fail "rehearsal ceremony failed"; }
42PUB="$W/offline/public"; KEY="$W/offline/REHEARSAL-aien_release_ed25519"
43ssh-keygen -q -t ed25519 -N '' -C other -f "$W/other"
44
45# A scratch copy of the repository files that matter, under git, so the change is a real diff.
46T="$W/tree"; mkdir -p "$T/docs/release" "$T/scripts"
47cp "$ROOT/install.sh" "$T/"; cp "$ROOT/docs/release/allowed_signers" "$ROOT/docs/release/internal_test_signers" "$T/docs/release/"
48cp "$ROOT"/scripts/*.sh "$T/scripts/"
49git -C "$T" init -q; git -C "$T" add -A; git -C "$T" -c user.name=t -c user.email=t@t commit -qm base
50bash "$T/scripts/check-release-trust-config.sh" > "$W/o.log" || fail "baseline (placeholder) rejected"
51grep -q PLACEHOLDER "$W/o.log" || fail "baseline not reported as placeholder"
52pass "baseline: placeholder state is consistent"
53
54# Negative control: the rehearsal record must not be accepted as a real ceremony.
55if bash "$T/scripts/apply-ceremony-public-record.sh" "$PUB" --root "$T" > "$W/o.log" 2>&1; then fail "rehearsal record applied without --allow-rehearsal"; fi
56grep -q 'REHEARSAL' "$W/o.log" || fail "rehearsal refusal not named"
57git -C "$T" diff --quiet && [[ -z "$(git -C "$T" status --porcelain)" ]] || fail "refused apply still changed the tree"
58pass "a REHEARSAL record is refused as a release key (negative control), tree untouched"
59mkdir "$W/stray"; cp "$PUB"/* "$W/stray/"; touch "$W/stray/aien_release_ed25519"
60if bash "$T/scripts/apply-ceremony-public-record.sh" "$W/stray" --root "$T" --allow-rehearsal > "$W/o.log" 2>&1; then fail "apply accepted a public folder holding an extra file"; fi
61pass "a public folder with any extra file (for example a key file) is refused"
62
63# (d) the post-ceremony change
64bash "$T/scripts/apply-ceremony-public-record.sh" "$PUB" --root "$T" --allow-rehearsal > "$W/o.log" 2>&1 || { cat "$W/o.log" >&2; fail "apply failed"; }
65git -C "$T" add -A
66[[ "$(git -C "$T" diff --cached --name-only | sort | tr '\n' ' ')" == "docs/release/allowed_signers docs/release/ceremony-record.txt install.sh " ]] || fail "change touches files other than the three expected"
67[[ "$(git -C "$T" diff --cached -U0 install.sh | grep -c '^[-+][^-+]')" -eq 2 ]] || fail "install.sh change is not exactly one line replaced"
68git -C "$T" diff --cached -U0 install.sh | grep -q "^+AIEN_RELEASE_SIGNER='aien-release ssh-ed25519 " || fail "install.sh change is not the AIEN_RELEASE_SIGNER line"
69[[ "$(git -C "$T" diff --cached -U0 docs/release/allowed_signers | grep '^+[^+]' | grep -vc '^+#')" -eq 1 ]] || fail "allowed_signers change is not exactly one key line"
70[[ "$(git -C "$T" diff --cached -U0 docs/release/allowed_signers | grep '^-[^-]' | grep -vc '^-#')" -eq 0 ]] || fail "allowed_signers change removes a key line"
71! git -C "$T" diff --cached | grep -q 'PRIVATE KEY' || fail "private key material in the diff"
72cmp -s "$PUB/ceremony-record.txt" "$T/docs/release/ceremony-record.txt" || fail "record in the tree is not the ceremony's public record"
73pass "(d) trust-config change = one allowed_signers key line + one AIEN_RELEASE_SIGNER value + the public record, no private material"
74
75# (e) fingerprint match and mutants
76CHK=(bash "$T/scripts/check-release-trust-config.sh" --root "$T" --allow-rehearsal)
77"${CHK[@]}" > "$W/o.log" 2>&1 || { cat "$W/o.log" >&2; fail "consistent tree rejected"; }
78FP="$(sed -n 's/^fingerprint: //p' "$PUB/ceremony-record.txt")"
79[[ "$FP" == "$(awk '{ print $2" "$3 }' "$T/docs/release/allowed_signers" | grep -v '^#' | ssh-keygen -lf - | awk '{ print $2 }' | head -1)" ]] || fail "record fingerprint is not the pinned key's fingerprint"
80if bash "$T/scripts/check-release-trust-config.sh" --root "$T" > "$W/o.log" 2>&1; then fail "rehearsal tree passed the strict check"; fi
81pass "(e) record fingerprint $FP equals the pinned line; strict mode refuses the rehearsal tree"
82mutant() { # name, then commands run in a fresh copy of the pinned tree; the copy must differ; the checker must refuse
83 local name="$1" M="$W/m-$1"; shift
84 rm -rf "$M"; cp -r "$T" "$M"; ( cd "$M"; "$@" ) || fail "mutant $name could not be built"
85 # A mutant that leaves the tree byte-identical is accepted by the checker for the right reason and would report
86 # a false failure (sc#568). A no-op mutant is a bug in this test, so refuse to run it at all.
87 if diff -rq -x .git "$T" "$M" > /dev/null; then fail "mutant $name is a no-op: the mutated tree is identical to the pinned tree"; fi
88 if bash "$M/scripts/check-release-trust-config.sh" --root "$M" --allow-rehearsal > "$W/o.log" 2>&1; then fail "mutant refused? no: $name passed the checker"; fi
89 pass "mutant refused: $name ($(sed -n 's/.*REFUSED: //p' "$W/o.log" | cut -c1-70))"
90}
91OTHERLINE="aien-release $(cut -d' ' -f1,2 "$W/other.pub")"
92# The fingerprint is random per run, so replacing its first character with a fixed one was a no-op about 1 in 64
93# runs (sc#568). Swap it to X unless it already is X, then to Y: the value always changes.
94mutant wrong-fingerprint-in-record sed -i -E 's/^(fingerprint: SHA256:)X/\1Y/; t; s/^(fingerprint: SHA256:)./\1X/' docs/release/ceremony-record.txt
95mutant other-key-in-allowed_signers sed -i "s|^aien-release ssh-ed25519 .*|$OTHERLINE|" docs/release/allowed_signers
96mutant installer-pin-differs sed -i "s|^AIEN_RELEASE_SIGNER=.*|AIEN_RELEASE_SIGNER='$OTHERLINE'|" install.sh
97mutant installer-pin-empty sed -i "s|^AIEN_RELEASE_SIGNER=.*|AIEN_RELEASE_SIGNER=''|" install.sh
98mutant record-missing rm docs/release/ceremony-record.txt
99mutant two-keys-pinned sh -c "echo '$OTHERLINE' >> docs/release/allowed_signers"
100mutant internal-test-key-pinned sh -c "l=\$(grep -v '^#' docs/release/internal_test_signers); sed -i \"s|^aien-release ssh-ed25519 .*|\$l|\" docs/release/allowed_signers; sed -i \"s|^AIEN_RELEASE_SIGNER=.*|AIEN_RELEASE_SIGNER='\$l'|\" install.sh"
101mutant private-material-in-record sh -c "echo '-----BEGIN OPENSSH PRIVATE KEY-----' >> docs/release/ceremony-record.txt"
102mutant record-says-one-backup sed -i '0,/signs and verifies yes; altered file refused yes/s//signs and verifies no/' docs/release/ceremony-record.txt
103
104# (a)(b)(c) the real installer and publish gate, configured only from the public record
105make_release() { # dir signing-key
106 local d="$1" k="$2" p="$W/pkg" f aien
107 rm -rf "$p"; mkdir -p "$d" "$p/bin"
108 printf '#!/bin/sh\necho aien\nexit 0\n' > "$p/bin/aien"; chmod 755 "$p/bin/aien"
109 aien="$(sha256sum "$p/bin/aien" | cut -d' ' -f1)"
110 { printf 'schema = "AienReleaseV1"\ncandidate = "CAND-1"\naien-cli-sha256 = "%s"\n\n[files]\n' "$aien"
111 printf '"bin/aien" = "%s"\n' "$aien"; } > "$p/release.toml"
112 tar -czf "$d/$ASSET" -C "$p" .
113 (cd "$d" && sha256sum "$ASSET" > SHA256SUMS.txt)
114 ssh-keygen -q -Y sign -f "$k" -n aien-release "$d/SHA256SUMS.txt"
115}
116inst() { # release-dir tag-suffix
117 env HOME="$W/home" AIEN_RELEASE_TAG=v0.0.0-test AIEN_RELEASE_BASE_URL="file://$1" AIEN_BIN_DIR="$W/bin-$2" \
118 AIEN_CONFIG_DIR="$W/cfg-$2" AIEN_INSTALL_NO_PROFILE=1 bash "$T/install.sh" > "$W/log" 2>&1
119}
120mkdir -p "$W/home"
121GATE=(bash "$T/scripts/release-publish-gate.sh")
122make_release "$W/good" "$KEY"
123inst "$W/good" good || { cat "$W/log" >&2; fail "release signed by the matching key did not install"; }
124grep -q '"signature_check": "release"' "$W/cfg-good/install-receipt.json" || fail "receipt does not say signature_check release"
125grep -q "\"signer_fingerprint\": \"$FP\"" "$W/cfg-good/install-receipt.json" || fail "receipt signer_fingerprint is not the ceremony record's fingerprint"
126"${GATE[@]}" "$W/good" "$T/docs/release/allowed_signers" > "$W/g.log" 2>&1 || { cat "$W/g.log" >&2; fail "publish gate refused the matching-key release"; }
127pass "(a) signed by the matching key: installs as signature_check release, receipt fingerprint = record fingerprint, publish gate passes"
128make_release "$W/wrongkey" "$W/other"
129if inst "$W/wrongkey" wk; then fail "release signed by a different key was installed"; fi
130grep -q "signature is NOT valid" "$W/log" || fail "different key not refused by name"
131[[ ! -e "$W/cfg-wk/install-receipt.json" && ! -e "$W/bin-wk/aien" ]] || fail "refusal left a receipt or binary"
132if "${GATE[@]}" "$W/wrongkey" "$T/docs/release/allowed_signers" > "$W/g.log" 2>&1; then fail "publish gate passed a different key"; fi
133grep -q "NOT valid" "$W/g.log" || fail "publish gate did not name the signature"
134pass "(b) signed by a different key: installer refuses ('signature is NOT valid'), nothing installed; publish gate refuses"
135cp -r "$W/good" "$W/flip"; A="$W/flip/$ASSET"; SZ=$(wc -c < "$A"); OFF=$((SZ / 2))
136B="$(dd if="$A" bs=1 skip="$OFF" count=1 2>/dev/null | od -An -tu1 | tr -d ' ')"
137printf "$(printf '\\%03o' $(( (B + 1) % 256 )))" | dd of="$A" bs=1 seek="$OFF" count=1 conv=notrunc 2>/dev/null
138if inst "$W/flip" flip; then fail "tampered archive installed"; fi
139grep -qi checksum "$W/log" || fail "tampered archive not refused as a checksum failure"
140[[ ! -e "$W/cfg-flip/install-receipt.json" ]] || fail "tampered archive left a receipt"
141if "${GATE[@]}" "$W/flip" "$T/docs/release/allowed_signers" > "$W/g.log" 2>&1; then fail "publish gate passed a tampered archive"; fi
142cp -r "$W/good" "$W/flipsum"; C1="$(head -c1 "$W/flipsum/SHA256SUMS.txt")"; [[ "$C1" == 0 ]] && R=1 || R=0; sed -i "1s/^./$R/" "$W/flipsum/SHA256SUMS.txt"
143if inst "$W/flipsum" fs; then fail "tampered checksum list installed"; fi
144grep -q "signature is NOT valid" "$W/log" || fail "tampered checksum list not refused as a signature failure"
145pass "(c) tampered archive refused (checksum), tampered checksum list refused (signature), nothing installed"
146echo "PASS: public-key integration: from only the public ceremony record, the trust-config change is a 3-file reviewable diff, the record fingerprint equals the pinned key (9 mismatch mutants refused), a release signed by the matching key installs as release, a different key, a tampered archive and a tampered checksum list are refused"