CI runs fmt, clippy and tests. Tag releases build Linux and Windows (mingw cross) artifacts, SHA256SUMS and an Ed25519 signed manifest, then publish via the Gitea API. Helper scripts live in scripts/release, docs in docs/release.md.
146 lines
4.9 KiB
Python
Executable file
146 lines
4.9 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Build, sign and verify the VNOX release manifest.
|
|
|
|
Canonical form: UTF-8 JSON, keys sorted, no whitespace (separators "," and ":"),
|
|
ensure_ascii=False, no trailing newline. manifest.json is written in exactly this
|
|
form, and manifest.json.sig is the base64 Ed25519 signature over those exact bytes.
|
|
A verifier checks the signature over the raw file bytes first, then parses.
|
|
|
|
Subcommands:
|
|
build --version V --channel C --min-version M --base-url URL [--published-at TS]
|
|
--dir DIR --out manifest.json NAME=PLATFORM [NAME=PLATFORM ...]
|
|
sign --manifest manifest.json --out manifest.json.sig
|
|
(key from env VNOX_RELEASE_SIGNING_KEY: base64 raw 32 byte seed)
|
|
verify --manifest manifest.json --sig manifest.json.sig --public-key BASE64
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import datetime
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from cryptography.exceptions import InvalidSignature
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey
|
|
|
|
SCHEMA = 1
|
|
|
|
|
|
def canonical(obj) -> bytes:
|
|
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
|
|
|
|
|
def sha256_file(path: str):
|
|
h = hashlib.sha256()
|
|
n = 0
|
|
with open(path, "rb") as f:
|
|
for chunk in iter(lambda: f.read(1 << 20), b""):
|
|
h.update(chunk)
|
|
n += len(chunk)
|
|
return h.hexdigest(), n
|
|
|
|
|
|
def cmd_build(a) -> int:
|
|
artifacts = []
|
|
for spec in a.artifacts:
|
|
name, _, platform = spec.partition("=")
|
|
if not name or not platform:
|
|
sys.exit(f"bad artifact spec {spec!r}, expected NAME=PLATFORM")
|
|
digest, size = sha256_file(os.path.join(a.dir, name))
|
|
artifacts.append(
|
|
{
|
|
"name": name,
|
|
"platform": platform,
|
|
"sha256": digest,
|
|
"size": size,
|
|
"url": f"{a.base_url.rstrip('/')}/{name}",
|
|
}
|
|
)
|
|
published = a.published_at or datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
manifest = {
|
|
"schema": SCHEMA,
|
|
"product": "vnox-client",
|
|
"version": a.version,
|
|
"channel": a.channel,
|
|
"min_version": a.min_version,
|
|
"published_at": published,
|
|
"artifacts": sorted(artifacts, key=lambda x: x["platform"]),
|
|
}
|
|
with open(a.out, "wb") as f:
|
|
f.write(canonical(manifest))
|
|
print(f"wrote {a.out}")
|
|
return 0
|
|
|
|
|
|
def load_private() -> Ed25519PrivateKey:
|
|
raw = os.environ.get("VNOX_RELEASE_SIGNING_KEY", "").strip()
|
|
if not raw:
|
|
sys.exit("VNOX_RELEASE_SIGNING_KEY is empty or unset, refusing to sign")
|
|
try:
|
|
seed = base64.b64decode(raw, validate=True)
|
|
except Exception:
|
|
sys.exit("VNOX_RELEASE_SIGNING_KEY is not valid base64")
|
|
if len(seed) != 32:
|
|
sys.exit(f"VNOX_RELEASE_SIGNING_KEY must decode to 32 bytes, got {len(seed)}")
|
|
return Ed25519PrivateKey.from_private_bytes(seed)
|
|
|
|
|
|
def cmd_sign(a) -> int:
|
|
with open(a.manifest, "rb") as f:
|
|
data = f.read()
|
|
# The signed bytes must be canonical, so that re-serialisation can never change them.
|
|
if canonical(json.loads(data)) != data:
|
|
sys.exit("manifest is not in canonical form, refusing to sign")
|
|
key = load_private()
|
|
sig = key.sign(data)
|
|
key.public_key().verify(sig, data)
|
|
with open(a.out, "w") as f:
|
|
f.write(base64.b64encode(sig).decode() + "\n")
|
|
print(f"wrote {a.out}")
|
|
return 0
|
|
|
|
|
|
def cmd_verify(a) -> int:
|
|
with open(a.manifest, "rb") as f:
|
|
data = f.read()
|
|
with open(a.sig) as f:
|
|
sig = base64.b64decode(f.read().strip(), validate=True)
|
|
pub = Ed25519PublicKey.from_public_bytes(base64.b64decode(a.public_key, validate=True))
|
|
try:
|
|
pub.verify(sig, data)
|
|
except InvalidSignature:
|
|
print("signature INVALID", file=sys.stderr)
|
|
return 1
|
|
print("signature ok")
|
|
return 0
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
b = sub.add_parser("build")
|
|
b.add_argument("--version", required=True)
|
|
b.add_argument("--channel", required=True, choices=["stable", "beta"])
|
|
b.add_argument("--min-version", default="0.0.0")
|
|
b.add_argument("--base-url", required=True)
|
|
b.add_argument("--published-at")
|
|
b.add_argument("--dir", default=".")
|
|
b.add_argument("--out", required=True)
|
|
b.add_argument("artifacts", nargs="+")
|
|
b.set_defaults(fn=cmd_build)
|
|
s = sub.add_parser("sign")
|
|
s.add_argument("--manifest", required=True)
|
|
s.add_argument("--out", required=True)
|
|
s.set_defaults(fn=cmd_sign)
|
|
v = sub.add_parser("verify")
|
|
v.add_argument("--manifest", required=True)
|
|
v.add_argument("--sig", required=True)
|
|
v.add_argument("--public-key", required=True)
|
|
v.set_defaults(fn=cmd_verify)
|
|
a = ap.parse_args()
|
|
return a.fn(a)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|