diff --git a/docs/RUNBOOK.md b/docs/RUNBOOK.md index 45e2561c..f495f41b 100644 --- a/docs/RUNBOOK.md +++ b/docs/RUNBOOK.md @@ -152,7 +152,8 @@ the real `/Applications`). Never judge arrow or hiding behaviour on a copy outsi ``` 4. App Store (separate lane, **Hidden Bar App Store** scheme): the MAS listing has lagged GitHub since v1.8 (issues #281/#202); decide deliberately whether - a release goes there too. + a release goes there too. If it does, follow "App Store release" below, + after the direct release is published. 5. Homebrew cask (`brew install --cask hiddenbar`) follows the GitHub release artifact; notarization matters (issue #219). 6. Sparkle appcast (direct build only, see below): sign the notarized zip, add @@ -217,6 +218,77 @@ ends in 200, and the entry's `sparkle:version` equals `CURRENT_PROJECT_VERSION` release). Because `latest` follows the newest published release, publish the release as a draft first, upload assets, then publish. +## App Store release + +Order: **the direct release first, then the App Store.** The listing copy +(`docs/appstore/listing.json`) sends macOS 27 users to the direct download and +names the direct version, so that GitHub release must be published (and +`releases/latest` must resolve) before the App Store version goes to review. +Both lanes build the same commit with the same `MARKETING_VERSION` and +`CURRENT_PROJECT_VERSION`. + +Two scripts, both offline-testable with `bash scripts/test-release-mas.sh`: + +- `scripts/release-mas.sh` builds, signs and exports the pkg. It never uploads. +- `scripts/asc-release.py` uploads, creates the version, attaches the build, + applies `docs/appstore/listing.json`, and submits. Standard library only (it + signs the API token with the system `openssl`); nothing to install. + +Steps, from a clean tree at the release commit, after the direct release is out: + +```sh +export HB_MAS_PROFILE='' # see the table below +scripts/release-mas.sh --dry-run # preflight only: builds nothing, reads no secret +scripts/release-mas.sh # archive, export, check; prints the next command +python3 -I scripts/asc-release.py release --pkg \ + --copyright ' ' --review-email --dry-run +python3 -I scripts/asc-release.py release --pkg \ + --copyright ' ' --review-email +``` + +`release --dry-run` does every offline check (pkg readable, listing within +Apple's limits, versions well formed) and every read, writes nothing to App +Store Connect, and stops before upload and submit. The real run is safe to +rerun: it skips an upload whose build is already on App Store Connect and a +version that already exists. It waits for build processing (up to +`HB_BUILD_WAIT_SECONDS`), then attaches the build, sets the review contact, +applies the listing, and submits. Review is Apple's; with release type +automatic after approval, the version goes live when approved. Single steps +are subcommands (`state`, `upload`, `create-version`, `attach-build`, +`review-contact`, `apply-listing`, `submit`); `state` shows versions, builds +and review submissions. + +| Variable | Used by | Meaning | +|---|---|---| +| `HB_MAS_PROFILE` | release-mas.sh | Required. Name or UUID of the installed `MAC_APP_STORE` provisioning profile for `com.dwarvesv.minimalbar`. `asc-release.py profile --cert-serial ` downloads or creates one and prints its name and UUID. | +| `HB_ASC_KEY_ID`, `HB_ASC_ISSUER_ID`, `HB_ASC_KEY_FILE` | both | The App Store Connect API key (id, issuer, `.p8` path). Set all three or none. With none, the key is read from 1Password: Connect first, then the ambient account. | +| `HB_OP_VAULT` | asc-release.py | 1Password vault holding the key item (default `dfoundation-prod`). | +| `HB_ASC_ITEM_SUFFIX` | asc-release.py | Title suffix of the key item (default `Hacker Bar Release`); exactly one item must match. | +| `HB_TEAM_ID` | release-mas.sh | Apple team id (default `W777S7V8TN`). | +| `HB_MAS_INSTALLER_CERT` | release-mas.sh | Installer identity that signs the pkg (default `3rd Party Mac Developer Installer`). | +| `HB_COPYRIGHT` | asc-release.py | Default for `--copyright` (the version's copyright line). | +| `HB_REVIEW_EMAIL` | asc-release.py | Default for `--review-email` (App Review contact email). | +| `HB_BUILD_WAIT_SECONDS` | asc-release.py | How long to wait for build processing (default 2700). | + +Rules the scripts encode (learned in the 1.13.1 run): + +- **Manual signing at export**: the `Apple Distribution` identity plus the + existing MAS profile. Automatic export needs cloud signing access the API + key does not have. +- **Never `umask 077`.** Apple rejects a package whose files only root can read + (altool error 90255). The scripts keep the key in a mode 700 temp dir instead, + and refuse an archived app or pkg that others cannot read. +- **Export compliance**: the build is marked "no non-exempt encryption". +- **Release type**: automatic after approval. +- The key never lands in the build directory, a log or the process output; the + temp copies are truncated when the script exits. + +If a step fails: a missing or expired profile (`profile_installed` fails in the +preflight) is fixed with `asc-release.py profile`; a 90255 on upload means files +in the app or pkg were created by a restrictive umask, so rebuild with the +default umask; a build +stuck in processing is retried with `attach-build` once it shows `VALID`. + ## Direct vs App Store targets Two targets share the sources. **Hidden Bar Direct** (scheme *Hidden Bar*, diff --git a/scripts/asc-release.py b/scripts/asc-release.py new file mode 100755 index 00000000..51ed2ccd --- /dev/null +++ b/scripts/asc-release.py @@ -0,0 +1,697 @@ +#!/usr/bin/env python3 +"""App Store Connect release steps for Hidden Bar: upload the pkg, create the version, attach the +build, apply docs/appstore/listing.json, and submit for review. Standard library only (the ES256 +token is signed with the system openssl), so there is nothing to install. + + asc-release.py release --pkg PKG --copyright TEXT --review-email EMAIL [--dry-run] + asc-release.py state | upload PKG | create-version V --copyright T | attach-build V B + asc-release.py review-contact V --email E | apply-listing V | submit V + asc-release.py profile --cert-serial SERIAL | key-files DIR + +`release` runs the steps in order and is safe to rerun: each step skips what already exists. +--dry-run does every offline check and every read (GET), writes nothing to App Store Connect, and +stops before upload and submit. Prints no key material. + +Environment (docs/RUNBOOK.md, "App Store release"): + HB_ASC_KEY_ID, HB_ASC_ISSUER_ID, HB_ASC_KEY_FILE the API key, all three or none. With none, the + key is read from 1Password (Connect first, then the ambient account). + HB_OP_VAULT, HB_ASC_ITEM_SUFFIX 1Password vault and item-title suffix (dfoundation-prod, "Hacker Bar Release"). + HB_COPYRIGHT, HB_REVIEW_EMAIL defaults for --copyright and --review-email. + HB_BUILD_WAIT_SECONDS how long attach-build waits for processing (default 2700). + +Lessons this script encodes (App Store 1.13.1 run): + - Export compliance: the build is marked "no non-exempt encryption" (usesNonExemptEncryption=false). + - Release type is automatic after approval (AFTER_APPROVAL). + - The pkg must be world-readable. A umask 077 build leaves files only root can read, which Apple + rejects (error 90255); release-mas.sh checks the archived app, this script checks the pkg. +""" +import argparse +import base64 +import json +import os +import re +import shutil +import stat +import subprocess +import sys +import tempfile +import time +import urllib.error +import urllib.request + +API = "https://api.appstoreconnect.apple.com" +BUNDLE_ID = "com.dwarvesv.minimalbar" +PLATFORM = "MAC_OS" +RELEASE_TYPE = "AFTER_APPROVAL" +DEFAULT_VAULT = "dfoundation-prod" +DEFAULT_ITEM_SUFFIX = "Hacker Bar Release" +CONNECT_HOST = "http://127.0.0.1:8090" +CONNECT_TOKEN_FILE = os.path.expanduser("~/op-connect/.connect-token") +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +DEFAULT_LISTING = os.path.join(ROOT, "docs", "appstore", "listing.json") +PROFILE_DIR = os.path.expanduser("~/Library/MobileDevice/Provisioning Profiles") +DEFAULT_WAIT = 2700 +POLL_SECONDS = 30 + +# Apple's limits: (size, unit). Keywords count UTF-8 bytes, the rest characters. +LIMITS = { + "subtitle": (30, "chars"), + "keywords": (100, "bytes"), + "promotionalText": (170, "chars"), + "description": (4000, "chars"), + "whatsNew": (4000, "chars"), +} +VERSION_FIELDS = ("description", "keywords", "promotionalText", "whatsNew", "supportUrl", "marketingUrl") +APPINFO_FIELDS = ("subtitle", "privacyPolicyUrl") +KNOWN_FIELDS = APPINFO_FIELDS + VERSION_FIELDS +EDITABLE_STATES = ("PREPARE_FOR_SUBMISSION", "DEVELOPER_REJECTED", "REJECTED", "METADATA_REJECTED", "INVALID_BINARY") +LIVE_STATES = ("READY_FOR_SALE", "READY_FOR_DISTRIBUTION") + + +class ToolError(Exception): + """A user-facing failure: one line, exit 1, never a traceback.""" + + +class DryRunWrite(ToolError): + """A write was attempted while --dry-run was on.""" + + +# ---- credentials ---------------------------------------------------------------------------- + +class Creds: + def __init__(self, key_id, issuer, key_path): + self.key_id, self.issuer, self.key_path = key_id, issuer, key_path + + +def env_credentials(env): + """Creds from HB_ASC_* when all three are set, None when none is, ToolError on a partial set.""" + names = ("HB_ASC_KEY_ID", "HB_ASC_ISSUER_ID", "HB_ASC_KEY_FILE") + got = {n: env.get(n, "") for n in names} + n_set = sum(1 for v in got.values() if v) + if n_set == 0: + return None + if n_set != 3: + raise ToolError("set all of HB_ASC_KEY_ID, HB_ASC_ISSUER_ID, HB_ASC_KEY_FILE, or none (then 1Password is used)") + if not os.path.isfile(got["HB_ASC_KEY_FILE"]): + raise ToolError("HB_ASC_KEY_FILE is not a file") + return Creds(got["HB_ASC_KEY_ID"], got["HB_ASC_ISSUER_ID"], got["HB_ASC_KEY_FILE"]) + + +def _op_env(env): + """Connect first when its heartbeat answers, else the ambient 1Password account.""" + e = dict(env) + try: + urllib.request.urlopen(CONNECT_HOST + "/heartbeat", timeout=3).read() + with open(CONNECT_TOKEN_FILE) as fh: + e["OP_CONNECT_HOST"], e["OP_CONNECT_TOKEN"] = CONNECT_HOST, fh.read().strip() + except (OSError, urllib.error.URLError): + e.pop("OP_CONNECT_HOST", None) + e.pop("OP_CONNECT_TOKEN", None) + return e + + +def write_private(path, text): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) + with os.fdopen(fd, "w") as fh: + fh.write(text) + + +def onepassword_credentials(env, workdir, runner=subprocess.run): + """Read the ASC key item from 1Password into workdir (mode 700). Prints nothing.""" + vault = env.get("HB_OP_VAULT") or DEFAULT_VAULT + suffix = env.get("HB_ASC_ITEM_SUFFIX") or DEFAULT_ITEM_SUFFIX + e = _op_env(env) + + def op(*a): + p = runner(["op", *a, "--vault", vault, "--format", "json"], capture_output=True, text=True, env=e) + if p.returncode != 0: + raise ToolError(f"op {a[0]} {a[1]} failed (vault {vault})") + return json.loads(p.stdout) + + try: + ids = [i["id"] for i in op("item", "list") if i["title"].endswith(suffix)] + if len(ids) != 1: + raise ToolError(f"expected exactly one 1Password item titled '*{suffix}' in {vault}, found {len(ids)}") + fields = op("item", "get", ids[0])["fields"] + except FileNotFoundError: + raise ToolError("no HB_ASC_* variables and no op CLI to read the key from 1Password") from None + key_id = next((f.get("value") for f in fields if re.search(r"key ?id", f.get("label", ""), re.I)), None) + issuer = next((f.get("value") for f in fields if re.search("issuer", f.get("label", ""), re.I)), None) + pem = next((f.get("value") for f in fields if "BEGIN PRIVATE KEY" in (f.get("value") or "")), None) + if not (key_id and issuer and pem): + raise ToolError("the 1Password item lacks a private key, a key id or an issuer field") + path = os.path.join(workdir, f"AuthKey_{key_id}.p8") + write_private(path, pem) + return Creds(key_id, issuer, path) + + +def resolve_credentials(env, workdir, runner=subprocess.run): + return env_credentials(env) or onepassword_credentials(env, workdir, runner) + + +def wipe(path): + try: + open(path, "w").close() + except OSError: + pass + + +# ---- the ASC client ------------------------------------------------------------------------- + +def b64url(raw): + return base64.urlsafe_b64encode(raw).rstrip(b"=").decode() + + +def der_to_raw(der, size=32): + """An ECDSA DER signature (what openssl prints) to the raw r||s a JWT carries.""" + if len(der) < 8 or der[0] != 0x30: + raise ToolError("unexpected signature encoding from openssl") + i = 2 if der[1] < 0x80 else 2 + (der[1] & 0x7F) + parts = [] + for _ in range(2): + if der[i] != 0x02: + raise ToolError("unexpected signature encoding from openssl") + n = der[i + 1] + value = der[i + 2:i + 2 + n].lstrip(b"\0") + if len(value) > size: + raise ToolError("signature integer too long") + parts.append(value.rjust(size, b"\0")) + i += 2 + n + return b"".join(parts) + + +def make_jwt(creds, now=None, runner=subprocess.run): + now = int(now if now is not None else time.time()) + head = b64url(json.dumps({"alg": "ES256", "kid": creds.key_id, "typ": "JWT"}, separators=(",", ":")).encode()) + body = b64url(json.dumps({"iss": creds.issuer, "iat": now, "exp": now + 900, "aud": "appstoreconnect-v1"}, + separators=(",", ":")).encode()) + p = runner(["openssl", "dgst", "-sha256", "-sign", creds.key_path], input=f"{head}.{body}".encode(), capture_output=True) + if p.returncode != 0: + raise ToolError("openssl could not sign with the ASC key") + return f"{head}.{body}.{b64url(der_to_raw(p.stdout))}" + + +class Client: + def __init__(self, creds): + self.creds, self._token, self._exp = creds, None, 0 + + def call(self, method, path, body=None): + """Return (status, json). Apple errors come back as {"errors": [...]}.""" + now = int(time.time()) + if self._token is None or now > self._exp - 60: + self._token, self._exp = make_jwt(self.creds, now), now + 900 + req = urllib.request.Request( + path if path.startswith("http") else API + path, + data=json.dumps(body).encode() if body is not None else None, method=method, + headers={"Authorization": "Bearer " + self._token, "Content-Type": "application/json"}) + try: + with urllib.request.urlopen(req, timeout=90) as r: + raw = r.read() + return r.status, json.loads(raw) if raw else {} + except urllib.error.HTTPError as e: + try: + errs = json.loads(e.read() or b"{}").get("errors", []) + except ValueError: + errs = [] + return e.code, {"errors": errs} + except (urllib.error.URLError, TimeoutError) as e: + raise ToolError(f"network error calling {method} {path.split('?')[0]}: {e}") from None + + +class ReadOnly: + """Wraps a client for --dry-run: GET passes, every other method raises.""" + + def __init__(self, inner): + self.inner = inner + + def call(self, method, path, body=None): + if method != "GET": + raise DryRunWrite(f"dry-run refused {method} {path.split('?')[0]}") + return self.inner.call(method, path, body) + + +def call(c, method, path, body=None): + status, data = c.call(method, path, body) + if status >= 300: + errs = "; ".join(f"{e.get('code')}: {e.get('detail') or e.get('title')}" for e in data.get("errors", [])) + raise ToolError(f"{method} {path.split('?')[0]} -> {status}: {errs[:900]}") + return data + + +# ---- App Store Connect lookups -------------------------------------------------------------- + +def state_of(attrs): + return attrs.get("appVersionState") or attrs.get("appStoreState") or "UNKNOWN" + + +def app_id(c, bundle): + d = call(c, "GET", f"/v1/apps?filter[bundleId]={bundle}")["data"] + if len(d) != 1: + raise ToolError(f"{len(d)} apps match {bundle}") + return d[0]["id"] + + +def versions(c, aid): + return call(c, "GET", f"/v1/apps/{aid}/appStoreVersions?filter[platform]={PLATFORM}&limit=50")["data"] + + +def find_version(c, aid, ver, required=True): + hits = [v for v in versions(c, aid) if v["attributes"]["versionString"] == ver] + if len(hits) > 1 or (required and not hits): + raise ToolError(f"{len(hits)} {PLATFORM} versions named {ver}") + return hits[0] if hits else None + + +def find_build(c, aid, ver, num): + d = call(c, "GET", f"/v1/builds?filter[app]={aid}&filter[version]={num}&filter[preReleaseVersion.version]={ver}")["data"] + if len(d) > 1: + raise ToolError(f"{len(d)} builds {ver} ({num})") + return d[0] if d else None + + +# ---- the pkg and the listing (offline) ------------------------------------------------------- + +def check_pkg(path): + """The pkg exists and is world-readable (the umask 077 lesson; Apple error 90255).""" + if not path.endswith(".pkg") or not os.path.isfile(path): + raise ToolError(f"{path} is not a .pkg file") + if not os.stat(path).st_mode & stat.S_IROTH: + raise ToolError(f"{path} is not world-readable; a umask 077 build causes it (Apple error 90255)") + parent = os.path.dirname(os.path.abspath(path)) + if os.stat(parent).st_mode & (stat.S_IROTH | stat.S_IXOTH) != (stat.S_IROTH | stat.S_IXOTH): + raise ToolError(f"{parent} is not world-readable (Apple error 90255)") + + +def read_listing(path): + try: + with open(path, encoding="utf-8") as fh: + listing = json.load(fh) + except (OSError, ValueError) as exc: + raise ToolError(f"cannot read {path}: {exc}") from None + if not isinstance(listing, dict): + raise ToolError("listing.json must be a JSON object") + for field in KNOWN_FIELDS: + if field in listing and not isinstance(listing[field], str): + raise ToolError(f"listing.json: {field} must be a string") + return listing + + +def select_fields(listing): + """Map listing.json to App Store Connect: (version-localization fields, app-info fields, skipped).""" + version_fields = {f: listing[f] for f in VERSION_FIELDS if f in listing} + appinfo_fields = {f: listing[f] for f in APPINFO_FIELDS if f in listing} + skipped = [f for f in listing if f not in KNOWN_FIELDS] + return version_fields, appinfo_fields, skipped + + +def over_limits(values): + problems = [] + for field, value in values.items(): + limit, unit = LIMITS.get(field, (None, "chars")) + size = len(value.encode("utf-8")) if unit == "bytes" else len(value) + if limit is not None and size > limit: + problems.append(f"{field} is {size} {unit}, Apple allows {limit}") + return problems + + +def load_checked_listing(path): + version_fields, appinfo_fields, skipped = select_fields(read_listing(path)) + problems = over_limits({**version_fields, **appinfo_fields}) + if problems: + raise ToolError("listing over Apple's limits: " + "; ".join(problems)) + return version_fields, appinfo_fields, skipped + + +# ---- steps that write ------------------------------------------------------------------------ + +def run_altool(pkg, creds, runner=subprocess.run, out=print): + """Validate, then upload, with altool. The key sits in a mode 700 temp dir for the calls only.""" + with tempfile.TemporaryDirectory(prefix="hb-altool.") as d: + os.chmod(d, 0o700) + key = os.path.join(d, f"AuthKey_{creds.key_id}.p8") + with open(creds.key_path) as fh: + write_private(key, fh.read()) + env = dict(os.environ, API_PRIVATE_KEYS_DIR=d) + try: + for flag in ("--validate-app", "--upload-app"): + p = runner(["xcrun", "altool", flag, "-f", pkg, "-t", "macos", "--apiKey", creds.key_id, + "--apiIssuer", creds.issuer], capture_output=True, text=True, env=env) + lines = [ln for ln in (p.stdout + p.stderr).splitlines() + if not re.search(r"apiKey|issuer|authenticationKey", ln, re.I)] + out("\n".join(lines[-8:])) + if p.returncode != 0: + raise ToolError(f"altool {flag} failed (exit {p.returncode})") + finally: + wipe(key) + + +def create_version(c, aid, ver, copyright_, out=print): + if find_version(c, aid, ver, required=False): + out(f"version {ver} exists already") + return + body = {"data": {"type": "appStoreVersions", + "attributes": {"platform": PLATFORM, "versionString": ver, + "releaseType": RELEASE_TYPE, "copyright": copyright_}, + "relationships": {"app": {"data": {"type": "apps", "id": aid}}}}} + a = call(c, "POST", "/v1/appStoreVersions", body)["data"]["attributes"] + out(f"created {a['versionString']} {state_of(a)} release={a['releaseType']} copyright={a['copyright']!r}") + + +def wait_for_build(c, aid, ver, num, timeout, sleep=time.sleep, out=print): + waited = 0 + while True: + b = find_build(c, aid, ver, num) + st = b["attributes"]["processingState"] if b else "NOT_LISTED_YET" + if st == "VALID": + return b + if st in ("FAILED", "INVALID"): + raise ToolError(f"build {ver} ({num}) processing {st}") + if waited >= timeout: + raise ToolError(f"build {ver} ({num}) is {st} after {waited}s; rerun attach-build later") + out(f"build {ver} ({num}) {st}, waiting") + sleep(POLL_SECONDS) + waited += POLL_SECONDS + + +def attach_build(c, aid, ver, num, timeout, sleep=time.sleep, out=print): + v = find_version(c, aid, ver) + b = wait_for_build(c, aid, ver, num, timeout, sleep, out) + if b["attributes"].get("usesNonExemptEncryption") is None: # export compliance: no non-exempt encryption + call(c, "PATCH", f"/v1/builds/{b['id']}", + {"data": {"type": "builds", "id": b["id"], "attributes": {"usesNonExemptEncryption": False}}}) + out("set usesNonExemptEncryption=false on the build") + call(c, "PATCH", f"/v1/appStoreVersions/{v['id']}/relationships/build", {"data": {"type": "builds", "id": b["id"]}}) + got = call(c, "GET", f"/v1/appStoreVersions/{v['id']}/build")["data"] + out(f"attached build {got['attributes']['version']} to {ver}") + + +def review_contact(c, aid, ver, email, out=print): + v = find_version(c, aid, ver) + cur = call(c, "GET", f"/v1/appStoreVersions/{v['id']}/appStoreReviewDetail")["data"] + if cur: + call(c, "PATCH", f"/v1/appStoreReviewDetails/{cur['id']}", + {"data": {"type": "appStoreReviewDetails", "id": cur["id"], "attributes": {"contactEmail": email}}}) + else: + prev = {} # carry the rest of the contact from the live version's review detail + for pv in versions(c, aid): + if state_of(pv["attributes"]) in LIVE_STATES: + pd = call(c, "GET", f"/v1/appStoreVersions/{pv['id']}/appStoreReviewDetail")["data"] + if pd: + prev = pd["attributes"] + break + attrs = {k: prev.get(k) for k in ("contactFirstName", "contactLastName", "contactPhone", + "demoAccountRequired", "notes") if prev.get(k) is not None} + attrs["contactEmail"] = email + attrs.setdefault("demoAccountRequired", False) + call(c, "POST", "/v1/appStoreReviewDetails", + {"data": {"type": "appStoreReviewDetails", "attributes": attrs, + "relationships": {"appStoreVersion": {"data": {"type": "appStoreVersions", "id": v["id"]}}}}}) + got = call(c, "GET", f"/v1/appStoreVersions/{v['id']}/appStoreReviewDetail")["data"]["attributes"] + out(f"review contact email={got['contactEmail']} name_set={bool(got.get('contactFirstName'))} phone_set={bool(got.get('contactPhone'))}") + + +def localization(c, parent_path, locale): + for loc in call(c, "GET", f"{parent_path}?limit=200")["data"]: + if loc["attributes"].get("locale") == locale: + return loc + return None + + +def patch_fields(c, resource_type, rid, fields, out=print): + """PATCH each field on its own so a 409 names the field, then read back. True when all matched.""" + path, ok, patched = f"/v1/{resource_type}/{rid}", True, [] + for field, value in fields.items(): + status, data = c.call("PATCH", path, {"data": {"type": resource_type, "id": rid, "attributes": {field: value}}}) + if status >= 300: + ok = False + out(f" FAIL HTTP {status} {field}: " + "; ".join(str(e.get("detail") or e.get("title")) for e in data.get("errors", []))) + else: + patched.append(field) + if patched: + got = call(c, "GET", path)["data"]["attributes"] + for field in patched: + same = (got.get(field) or "") == fields[field] + ok &= same + out(f" {'ok ' if same else 'DIFF'} {field} {'matches listing' if same else 'DIFFERS from listing'}") + return ok + + +def apply_listing(c, aid, ver, version_fields, appinfo_fields, locale, out=print): + v = find_version(c, aid, ver) + if state_of(v["attributes"]) not in EDITABLE_STATES: + raise ToolError(f"version {ver} is {state_of(v['attributes'])}, not editable") + ok = True + loc = localization(c, f"/v1/appStoreVersions/{v['id']}/appStoreVersionLocalizations", locale) + if loc is None: + raise ToolError(f"version {ver} has no {locale} localization") + out(f"version localization {locale}:") + ok &= patch_fields(c, "appStoreVersionLocalizations", loc["id"], version_fields, out) + infos = call(c, "GET", f"/v1/apps/{aid}/appInfos")["data"] + info = next((i for i in infos if (i["attributes"].get("appStoreState") or i["attributes"].get("state")) in EDITABLE_STATES), None) + if info is None: + out(" skipped app-info fields: no editable app-info") + else: + iloc = localization(c, f"/v1/appInfos/{info['id']}/appInfoLocalizations", locale) + if iloc is None: + raise ToolError(f"editable app-info has no {locale} localization") + out(f"app-info localization {locale}:") + ok &= patch_fields(c, "appInfoLocalizations", iloc["id"], appinfo_fields, out) + if not ok: + raise ToolError("the listing did not apply cleanly (see above)") + + +def submit(c, aid, ver, out=print): + v = find_version(c, aid, ver) + open_subs = call(c, "GET", f"/v1/reviewSubmissions?filter[app]={aid}&filter[platform]={PLATFORM}&filter[state]=READY_FOR_REVIEW")["data"] + sid = open_subs[0]["id"] if open_subs else call(c, "POST", "/v1/reviewSubmissions", { + "data": {"type": "reviewSubmissions", "attributes": {"platform": PLATFORM}, + "relationships": {"app": {"data": {"type": "apps", "id": aid}}}}})["data"]["id"] + if not call(c, "GET", f"/v1/reviewSubmissions/{sid}/items")["data"]: + call(c, "POST", "/v1/reviewSubmissionItems", { + "data": {"type": "reviewSubmissionItems", + "relationships": {"reviewSubmission": {"data": {"type": "reviewSubmissions", "id": sid}}, + "appStoreVersion": {"data": {"type": "appStoreVersions", "id": v["id"]}}}}}) + d = call(c, "PATCH", f"/v1/reviewSubmissions/{sid}", + {"data": {"type": "reviewSubmissions", "id": sid, "attributes": {"submitted": True}}}) + out(f"submission {sid[:8]} {d['data']['attributes'].get('state')}") + out(f"version {ver}: {state_of(find_version(c, aid, ver)['attributes'])}") + + +# ---- commands -------------------------------------------------------------------------------- + +def need(args, env, attr, var, flag): + value = getattr(args, attr, None) or env.get(var, "") + if not value: + raise ToolError(f"missing {flag} (or {var})") + return value + + +def check_version_build(ver, build=None): + if not re.fullmatch(r"\d+(\.\d+){1,2}", ver): + raise ToolError(f"version '{ver}' is not like 1.13.1") + if build is not None and not re.fullmatch(r"\d+", build): + raise ToolError(f"build '{build}' is not an integer") + + +def cmd_state(c, a, env, out=print): + aid = app_id(c, a.bundle) + out(f"app {a.bundle}") + for v in versions(c, aid)[:6]: + at = v["attributes"] + out(f" version {at['versionString']}: {state_of(at)} release={at.get('releaseType')} copyright={at.get('copyright')!r}") + d = call(c, "GET", f"/v1/builds?filter[app]={aid}&sort=-uploadedDate&limit=10&include=preReleaseVersion") + pre = {x["id"]: x["attributes"]["version"] for x in d.get("included", []) if x["type"] == "preReleaseVersions"} + for b in d["data"]: + at = b["attributes"] + pr = (b["relationships"].get("preReleaseVersion", {}).get("data") or {}).get("id") + out(f" build {pre.get(pr, '?')} ({at['version']}) {at['processingState']} uploaded {at['uploadedDate']} encryption={at.get('usesNonExemptEncryption')}") + for s in call(c, "GET", f"/v1/reviewSubmissions?filter[app]={aid}&filter[platform]={PLATFORM}&limit=5")["data"]: + out(f" reviewSubmission {s['id'][:8]} {s['attributes'].get('state')} {s['attributes'].get('submittedDate')}") + return 0 + + +def cmd_profile(c, a, env, out=print): + """Download an active MAC_APP_STORE profile for the bundle holding the cert with --cert-serial, or create one.""" + bids = [b for b in call(c, "GET", f"/v1/bundleIds?filter[identifier]={a.bundle}&limit=20")["data"] + if b["attributes"]["identifier"] == a.bundle] + if len(bids) != 1: + raise ToolError(f"{len(bids)} bundle ids equal {a.bundle}") + certs = [x for x in call(c, "GET", "/v1/certificates?limit=200")["data"] + if (x["attributes"].get("serialNumber") or "").upper() == a.cert_serial.upper()] + if len(certs) != 1: + raise ToolError(f"{len(certs)} certificates have serial {a.cert_serial}") + prof = None + for p in call(c, "GET", f"/v1/bundleIds/{bids[0]['id']}/profiles?limit=50")["data"]: + at = p["attributes"] + if at["profileType"] == "MAC_APP_STORE" and at["profileState"] == "ACTIVE": + if certs[0]["id"] in {x["id"] for x in call(c, "GET", f"/v1/profiles/{p['id']}/certificates")["data"]}: + prof = call(c, "GET", f"/v1/profiles/{p['id']}")["data"] + out("reused active profile") + break + if prof is None: + prof = call(c, "POST", "/v1/profiles", {"data": {"type": "profiles", + "attributes": {"name": a.name, "profileType": "MAC_APP_STORE"}, + "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bids[0]["id"]}}, + "certificates": {"data": [{"type": "certificates", "id": certs[0]["id"]}]}}}})["data"] + out("created profile") + at = prof["attributes"] + dest = a.out or os.path.join(PROFILE_DIR, f"{at['uuid']}.provisionprofile") + os.makedirs(os.path.dirname(dest), exist_ok=True) + with open(dest, "wb") as fh: + fh.write(base64.b64decode(at["profileContent"])) + out(f"profile name={at['name']!r} uuid={at['uuid']} expires={at.get('expirationDate')} -> {dest}") + out("set HB_MAS_PROFILE to that name or uuid") + return 0 + + +def cmd_upload(a, creds, out=print, runner=subprocess.run): + if a.dry_run: + out(f"dry-run: pkg ok, would run altool --validate-app then --upload-app on {a.pkg}") + return 0 + run_altool(a.pkg, creds, runner, out) + return 0 + + +def cmd_create_version(c, a, env, out=print): + create_version(c, app_id(c, a.bundle), a.version, need(a, env, "copyright", "HB_COPYRIGHT", "--copyright"), out) + return 0 + + +def cmd_attach_build(c, a, env, out=print, sleep=time.sleep): + attach_build(c, app_id(c, a.bundle), a.version, a.build, a.wait, sleep, out) + return 0 + + +def cmd_review_contact(c, a, env, out=print): + review_contact(c, app_id(c, a.bundle), a.version, need(a, env, "email", "HB_REVIEW_EMAIL", "--email"), out) + return 0 + + +def cmd_apply_listing(c, a, env, out=print): + vf, af, skipped = load_checked_listing(a.listing) + apply_listing(c, app_id(c, a.bundle), a.version, vf, af, a.locale, out) + out("skipped (not App Store Connect fields): " + ", ".join(skipped) if skipped else "nothing skipped") + return 0 + + +def cmd_submit(c, a, env, out=print): + submit(c, app_id(c, a.bundle), a.version, out) + return 0 + + +def cmd_release(c, a, creds, out=print, runner=subprocess.run, sleep=time.sleep): + """Every step in order. --dry-run: offline checks and GET reads only; stops before upload and submit.""" + vf, af, skipped = load_checked_listing(a.listing) + aid = app_id(c, a.bundle) + version = find_version(c, aid, a.version, required=False) + build = find_build(c, aid, a.version, a.build) + out(f"app {a.bundle}: version {a.version} " + (f"exists ({state_of(version['attributes'])})" if version else "not created yet") + + f", build {a.build} " + (f"already uploaded ({build['attributes']['processingState']})" if build else "not uploaded yet")) + if a.dry_run: + plan = [("upload", f"altool validate + upload {a.pkg}", build is not None), + ("create version", f"{a.version}, release type {RELEASE_TYPE}, copyright {a.copyright!r}", version is not None), + ("attach build", f"{a.build} once processed, export compliance: no non-exempt encryption", False), + ("review contact", a.review_email, False), + ("apply listing", f"{', '.join(list(vf) + list(af))} ({a.locale}); not fields: {', '.join(skipped) or 'none'}", False), + ("submit", f"version {a.version} for review", False)] + for name, what, done in plan: + out(f"dry-run: would {name}: {what}" + (" (already done, skipped)" if done else "")) + out("dry-run: nothing written to App Store Connect; stopped before upload and submit") + return 0 + if build is None: + run_altool(a.pkg, creds, runner, out) + else: + out(f"upload skipped: build {a.build} is already on App Store Connect") + create_version(c, aid, a.version, a.copyright, out) + attach_build(c, aid, a.version, a.build, a.wait, sleep, out) + review_contact(c, aid, a.version, a.review_email, out) + apply_listing(c, aid, a.version, vf, af, a.locale, out) + submit(c, aid, a.version, out) + return 0 + + +def build_parser(env=None): + env = os.environ if env is None else env + common = argparse.ArgumentParser(add_help=False) + common.add_argument("--bundle", default=BUNDLE_ID) + common.add_argument("--locale", default="en-US") + common.add_argument("--dry-run", action="store_true", help="read and check only; write nothing") + wait_default = int(env.get("HB_BUILD_WAIT_SECONDS") or DEFAULT_WAIT) + p = argparse.ArgumentParser(prog="asc-release.py", description=__doc__.split("\n")[0]) + sub = p.add_subparsers(dest="cmd", required=True) + + def add(name): + return sub.add_parser(name, parents=[common]) + + add("state") + s = add("key-files"); s.add_argument("dir") + s = add("profile"); s.add_argument("--cert-serial", required=True); s.add_argument("--name", default="Hidden Bar MAS"); s.add_argument("--out") + s = add("upload"); s.add_argument("pkg") + s = add("create-version"); s.add_argument("version"); s.add_argument("--copyright") + s = add("attach-build"); s.add_argument("version"); s.add_argument("build"); s.add_argument("--wait", type=int, default=wait_default) + s = add("review-contact"); s.add_argument("version"); s.add_argument("--email") + s = add("apply-listing"); s.add_argument("version"); s.add_argument("--listing", default=DEFAULT_LISTING) + s = add("submit"); s.add_argument("version") + s = add("release") + s.add_argument("version"); s.add_argument("build"); s.add_argument("--pkg", required=True) + s.add_argument("--copyright"); s.add_argument("--review-email"); s.add_argument("--listing", default=DEFAULT_LISTING) + s.add_argument("--wait", type=int, default=wait_default) + return p + + +def check_offline(a, env): + """Every check that needs no credential and no network; raises ToolError before either is touched.""" + if hasattr(a, "version"): + check_version_build(a.version, getattr(a, "build", None)) + if a.cmd in ("create-version", "release"): + a.copyright = need(a, env, "copyright", "HB_COPYRIGHT", "--copyright") + if a.cmd == "review-contact": + a.email = need(a, env, "email", "HB_REVIEW_EMAIL", "--email") + if a.cmd == "release": + a.review_email = need(a, env, "review_email", "HB_REVIEW_EMAIL", "--review-email") + if a.cmd in ("upload", "release"): + check_pkg(a.pkg) + if a.cmd in ("apply-listing", "release"): + load_checked_listing(a.listing) + + +def main(argv=None, env=None, client_factory=Client, runner=subprocess.run, out=print, sleep=time.sleep): + env = os.environ if env is None else env + a = build_parser(env).parse_args(argv) + workdir = None + try: + check_offline(a, env) + if a.cmd == "upload" and a.dry_run: + return cmd_upload(a, None, out, runner) + workdir = tempfile.mkdtemp(prefix="hb-asc.") + os.chmod(workdir, 0o700) + creds = resolve_credentials(env, workdir, runner) + if a.cmd == "key-files": + with open(creds.key_path) as fh: + write_private(os.path.join(a.dir, f"AuthKey_{creds.key_id}.p8"), fh.read()) + print(f"{creds.key_id} {creds.issuer}") + return 0 + if a.cmd == "upload": + return cmd_upload(a, creds, out, runner) + c = client_factory(creds) + if a.dry_run: + c = ReadOnly(c) + if a.cmd == "release": + return cmd_release(c, a, creds, out, runner, sleep) + if a.cmd == "attach-build": + return cmd_attach_build(c, a, env, out, sleep) + handler = {"state": cmd_state, "profile": cmd_profile, "create-version": cmd_create_version, + "review-contact": cmd_review_contact, "apply-listing": cmd_apply_listing, "submit": cmd_submit}[a.cmd] + return handler(c, a, env, out) + except ToolError as exc: + print(f"error: {exc}", file=sys.stderr) + return 1 + finally: + if workdir: + for f in os.listdir(workdir): + wipe(os.path.join(workdir, f)) + shutil.rmtree(workdir, ignore_errors=True) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/release-mas.sh b/scripts/release-mas.sh new file mode 100755 index 00000000..69931a49 --- /dev/null +++ b/scripts/release-mas.sh @@ -0,0 +1,234 @@ +#!/bin/bash +# scripts/release-mas.sh : build, sign and export the Mac App Store pkg. +# +# scripts/release-mas.sh [--out ] [--dry-run] +# scripts/release-mas.sh --help +# +# Archives the "Hidden Bar App Store" scheme (Release), exports an app-store-connect pkg with +# manual signing, and checks the pkg. Output under --out (default build/appstore-/): +# the xcarchive, logs, export.plist and export/.pkg. It never uploads or submits: that is +# scripts/asc-release.py (it prints the exact next command when the pkg is ready). +# +# --dry-run runs every preflight check (tools, clean tree, versions, env vars, signing +# identities, installed profile) and prints the plan; it builds nothing and reads no secret. +# +# Environment (full table: docs/RUNBOOK.md, "App Store release"): +# HB_MAS_PROFILE required. Name or UUID of the installed MAC_APP_STORE provisioning +# profile for the app (scripts/asc-release.py profile fetches one). +# HB_ASC_KEY_ID, HB_ASC_ISSUER_ID, HB_ASC_KEY_FILE +# optional, all three or none: the App Store Connect API key. Without +# them the key is read from 1Password (see asc-release.py). +# HB_TEAM_ID default W777S7V8TN. +# HB_MAS_INSTALLER_CERT default "3rd Party Mac Developer Installer" (signs the pkg). +# +# Lessons this script encodes (App Store 1.13.1 run): +# - Manual signing at export: the "Apple Distribution" identity plus the existing profile. +# Automatic export needs cloud signing access the ASC API key does not have. +# - Never umask 077. Apple rejects a package whose files only root can read (altool error +# 90255). The key lives in a mode 700 temp dir instead, and the archived app and the pkg +# are checked for world-readability before anything is uploaded. +# +# Exit: 0 ok (or dry-run plan printed) | 1 build or export failed | 2 usage or preflight error. +# +# Test seam: RELEASE_MAS_SOURCE_ONLY=1 source scripts/release-mas.sh defines the functions and +# runs nothing (scripts/test-release-mas.sh). Bash 3.2 compatible (macOS /bin/bash). + +case "$-" in *x*) echo "release-mas.sh: refusing to run with xtrace on (it would print secrets)" >&2; exit 2 ;; esac +case ":${SHELLOPTS:-}:" in *:xtrace:*) echo "release-mas.sh: refusing to run with SHELLOPTS=xtrace" >&2; exit 2 ;; esac + +set -u -o pipefail +umask 022 + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +PBXPROJ="$ROOT/Hidden Bar.xcodeproj/project.pbxproj" +PROJECT="Hidden Bar.xcodeproj" +SCHEME="Hidden Bar App Store" +BUNDLE_ID="com.dwarvesv.minimalbar" +TEAM_ID="${HB_TEAM_ID:-W777S7V8TN}" +INSTALLER_CERT="${HB_MAS_INSTALLER_CERT:-3rd Party Mac Developer Installer}" +PROFILE_DIRS=("$HOME/Library/MobileDevice/Provisioning Profiles" "$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles") + +WORK="" +usage() { sed -n '2,30p' "$0" | sed 's/^# \{0,1\}//'; } +die() { echo "release-mas.sh: $*" >&2; exit 2; } +# Hide anything that names the API key from build output. +filter() { grep -v -i -E 'apiKey|issuer|authenticationKey'; } + +read_versions() { # pbxproj : prints " ", rc 1 with the distinct values when they disagree + local mv cv + mv="$(grep -o 'MARKETING_VERSION = [^;]*;' "$1" | sed 's/.*= //; s/;$//' | sort | uniq -c)" + cv="$(grep -o 'CURRENT_PROJECT_VERSION = [^;]*;' "$1" | sed 's/.*= //; s/;$//' | sort | uniq -c)" + if [ "$(printf '%s\n' "$mv" | grep -c .)" != 1 ] || [ "$(printf '%s\n' "$cv" | grep -c .)" != 1 ]; then + echo "versions disagree: MARKETING_VERSION [$(echo $mv)], CURRENT_PROJECT_VERSION [$(echo $cv)]"; return 1 + fi + mv="$(echo $mv | awk '{print $2}')"; cv="$(echo $cv | awk '{print $2}')" + case "$mv" in *[!0-9.]*|"") echo "MARKETING_VERSION '$mv' is not numeric"; return 1 ;; esac + case "$cv" in *[!0-9]*|"") echo "CURRENT_PROJECT_VERSION '$cv' is not an integer"; return 1 ;; esac + echo "$mv $cv" +} + +check_env() { # prints the problem and returns 1 when the environment cannot work + local set_n=0 v + [ -n "${HB_MAS_PROFILE:-}" ] || { echo "HB_MAS_PROFILE is not set (name or UUID of the MAC_APP_STORE profile)"; return 1; } + case "$HB_MAS_PROFILE" in *[!A-Za-z0-9._\ -]*) echo "HB_MAS_PROFILE has characters outside A-Z a-z 0-9 . _ - and space"; return 1 ;; esac + for v in HB_ASC_KEY_ID HB_ASC_ISSUER_ID HB_ASC_KEY_FILE; do + [ -z "${!v:-}" ] || set_n=$((set_n + 1)) + done + case "$set_n" in + 0) command -v op >/dev/null 2>&1 || { echo "no HB_ASC_KEY_ID/HB_ASC_ISSUER_ID/HB_ASC_KEY_FILE and no op CLI to read the key from 1Password"; return 1; } ;; + 3) [ -f "$HB_ASC_KEY_FILE" ] || { echo "HB_ASC_KEY_FILE is not a file"; return 1; } ;; + *) echo "set all of HB_ASC_KEY_ID, HB_ASC_ISSUER_ID, HB_ASC_KEY_FILE, or none (then 1Password is used)"; return 1 ;; + esac + return 0 +} + +check_tools() { # prints the missing tools and returns 1 + local t missing="" + for t in xcodebuild xcrun security pkgutil git python3 plutil stat; do + command -v "$t" >/dev/null 2>&1 || missing="$missing $t" + done + [ -z "$missing" ] || { echo "missing:$missing"; return 1; } +} + +check_identities() { # "Apple Distribution" and the installer cert, both for the team + local ids + ids="$(security find-identity -v 2>/dev/null)" + printf '%s\n' "$ids" | grep -q "\"Apple Distribution: .*($TEAM_ID)\"" || { echo "no Apple Distribution identity for $TEAM_ID"; return 1; } + printf '%s\n' "$ids" | grep -qF "\"$INSTALLER_CERT: " || { echo "no '$INSTALLER_CERT' identity"; return 1; } +} + +profile_installed() { # name-or-uuid : rc 0 when an installed profile has that Name or UUID + local dir f name + for dir in "${PROFILE_DIRS[@]}"; do + [ -d "$dir" ] || continue + for f in "$dir"/*.provisionprofile; do + [ -f "$f" ] || continue + [ "$(basename "$f" .provisionprofile)" = "$1" ] && return 0 + name="$(security cms -D -i "$f" 2>/dev/null | plutil -extract Name raw -o - - 2>/dev/null)" + [ "$name" = "$1" ] && return 0 + done + done + return 1 +} + +check_tree_clean() { # rc 1 and the dirty paths when dirty + local s + s="$(git -C "$ROOT" status --porcelain)" || { echo "git status failed"; return 1; } + [ -z "$s" ] || { echo "$s"; return 1; } +} + +write_export_plist() { # file : the manual-signing export options + cat > "$1" < + + + methodapp-store-connect + destinationexport + teamID$TEAM_ID + signingStylemanual + signingCertificateApple Distribution + installerSigningCertificate$INSTALLER_CERT + provisioningProfiles$BUNDLE_ID$HB_MAS_PROFILE + uploadSymbols + +EOF +} + +mode_of() { stat -f %Lp "$1"; } +world_readable() { # path : rc 0 when others can read it (and, for a directory, enter it) + local m + m="$(mode_of "$1")" || return 1 + if [ -d "$1" ]; then [ $((8#$m & 5)) = 5 ]; else [ $((8#$m & 4)) = 4 ]; fi +} + +check_payload() { # dir : rc 1 and the first offenders when a file or folder under it is not world-readable + local bad + bad="$(find "$1" \( -type d ! -perm -o=rx \) -o \( ! -type d ! -type l ! -perm -o=r \) 2>/dev/null | head -3)" + [ -z "$bad" ] || { echo "not world-readable under $1 (first: $(echo $bad)): Apple rejects files only root can read with error 90255; a umask 077 build causes it"; return 1; } +} + +check_pkg() { # export-dir : prints the pkg path; rc 1 with the reason when it is unusable + local n pkg + n="$(ls "$1"/*.pkg 2>/dev/null | grep -c .)" + [ "$n" = 1 ] || { echo "expected exactly one pkg in $1, found $n"; return 1; } + pkg="$(ls "$1"/*.pkg)" + world_readable "$1" && world_readable "$pkg" || { echo "pkg or its folder is not world-readable (mode $(mode_of "$pkg")); a umask 077 build causes it (Apple error 90255)"; return 1; } + pkgutil --check-signature "$pkg" >/dev/null 2>&1 || { echo "pkgutil --check-signature failed for $pkg"; return 1; } + echo "$pkg" +} + +key_wipe() { + local f + [ -n "$WORK" ] && [ -d "$WORK" ] || return 0 + for f in "$WORK"/* "$WORK"/.[!.]*; do [ -f "$f" ] && : > "$f"; done + command -v trash >/dev/null 2>&1 && trash "$WORK" >/dev/null 2>&1 + return 0 +} + +fetch_key() { # sets AUTH; the key goes to a mode 700 dir via asc-release.py, never to the build dir + local ids + WORK="$(mktemp -d "${TMPDIR:-/tmp}/hb-mas.XXXXXX")" || die "mktemp failed" + chmod 700 "$WORK"; trap key_wipe EXIT + ids="$(python3 -I "$ROOT/scripts/asc-release.py" key-files "$WORK")" || die "could not read the App Store Connect key" + KEY_ID="${ids%% *}"; ISSUER="${ids#* }" + [ -n "$KEY_ID" ] && [ -n "$ISSUER" ] && [ -s "$WORK/AuthKey_$KEY_ID.p8" ] || die "key fetch gave no key id, issuer or key file" + echo "ASC key: in a mode 700 temp dir, key file mode 600" + AUTH=(-allowProvisioningUpdates -authenticationKeyPath "$WORK/AuthKey_$KEY_ID.p8" -authenticationKeyID "$KEY_ID" -authenticationKeyIssuerID "$ISSUER") +} + +stage_build() { # out-dir + local out="$1" r + mkdir -p "$out"; cd "$ROOT" || die "cannot cd to $ROOT" + echo "HEAD $(git rev-parse HEAD)" + xcodebuild archive -project "$PROJECT" -scheme "$SCHEME" -configuration Release \ + -archivePath "$out/HiddenBar.xcarchive" -derivedDataPath "$out/dd" \ + DEVELOPMENT_TEAM="$TEAM_ID" CODE_SIGN_STYLE=Automatic "${AUTH[@]}" > "$out/archive.log" 2>&1 \ + || { tail -30 "$out/archive.log" | filter; return 1; } + tail -2 "$out/archive.log" | filter + r="$(check_payload "$out/HiddenBar.xcarchive/Products")" || { echo "$r" >&2; return 1; } + write_export_plist "$out/export.plist" + xcodebuild -exportArchive -archivePath "$out/HiddenBar.xcarchive" -exportPath "$out/export" \ + -exportOptionsPlist "$out/export.plist" "${AUTH[@]}" > "$out/export.log" 2>&1 \ + || { tail -30 "$out/export.log" | filter; return 1; } + tail -2 "$out/export.log" | filter + check_tree_clean >/dev/null || { echo "tree changed during build" >&2; git status --porcelain >&2; return 1; } +} + +main() { + local out_arg="" dry=0 r vb ver build out pkg + while [ $# -gt 0 ]; do + case "$1" in + --out) [ $# -ge 2 ] || { usage >&2; exit 2; }; out_arg="$2"; shift ;; + --dry-run) dry=1 ;; + -h|--help) usage; exit 0 ;; + *) echo "release-mas.sh: unknown argument $1" >&2; usage >&2; exit 2 ;; + esac + shift + done + + r="$(check_tools)" || die "$r" + r="$(check_env)" || die "$r" + vb="$(read_versions "$PBXPROJ")" || die "$vb" + ver="${vb% *}"; build="${vb#* }" + r="$(check_tree_clean)" || die "dirty tree: $r" + r="$(check_identities)" || die "$r" + profile_installed "$HB_MAS_PROFILE" || die "no installed provisioning profile named or numbered '$HB_MAS_PROFILE' (scripts/asc-release.py profile fetches one)" + out="${out_arg:-$ROOT/build/appstore-$ver}" + [ -L "$out" ] && die "--out $out is a symlink" + if [ -d "$out" ] && [ -n "$(ls -A "$out" 2>/dev/null)" ]; then die "--out $out exists and is not empty; use a new dir"; fi + + echo "preflight ok: Hidden Bar $ver ($build), team $TEAM_ID, profile '$HB_MAS_PROFILE', out $out" + if [ "$dry" = 1 ]; then + echo "dry-run: would archive scheme '$SCHEME' (Release), export app-store-connect with manual signing, check the pkg" + echo "dry-run: built nothing, read no secret" + exit 0 + fi + + fetch_key + stage_build "$out" || exit 1 + pkg="$(check_pkg "$out/export")" || { echo "release-mas.sh: $pkg" >&2; exit 1; } + echo "pkg: $pkg" + echo "next: python3 scripts/asc-release.py release $ver $build --pkg '$pkg' --copyright ' ' --review-email --dry-run" +} + +if [ -z "${RELEASE_MAS_SOURCE_ONLY:-}" ]; then main "$@"; fi diff --git a/scripts/test-asc-release.py b/scripts/test-asc-release.py new file mode 100755 index 00000000..ffeeb5ad --- /dev/null +++ b/scripts/test-asc-release.py @@ -0,0 +1,427 @@ +#!/usr/bin/env python3 +"""Offline tests for scripts/asc-release.py: argument parsing, the env-var checks, the listing.json +mapping, the ES256 token, and the dry-run write guard. No network, no 1Password, no Apple. +Run through scripts/test-release-mas.sh, or: python3 -I -B scripts/test-asc-release.py +One PASS/FAIL line per case; exit 0 only when no case failed.""" +import base64 +import contextlib +import importlib.util +import io +import json +import os +import stat +import subprocess +import sys +import tempfile + +sys.dont_write_bytecode = True +HERE = os.path.dirname(os.path.abspath(__file__)) +spec = importlib.util.spec_from_file_location("asc_release", os.path.join(HERE, "asc-release.py")) +R = importlib.util.module_from_spec(spec) +spec.loader.exec_module(R) + +PASSED, FAILED = 0, [] +TMP = tempfile.mkdtemp(prefix="hb-asc-test.") +os.chmod(TMP, 0o755) + + +def case(fn): + global PASSED + try: + fn() + print("PASS " + fn.__name__) + PASSED += 1 + except Exception as exc: # noqa: BLE001 : a failing case must not stop the run + print(f"FAIL {fn.__name__}: {type(exc).__name__}: {exc}") + FAILED.append(fn.__name__) + + +def raises(exc_type, fn, *needles): + try: + fn() + except exc_type as exc: + for n in needles: + assert n in str(exc), f"'{n}' not in '{exc}'" + return + raise AssertionError(f"{exc_type.__name__} not raised") + + +def make_key(name="k.p8"): + path = os.path.join(TMP, name) + ec = subprocess.run(["openssl", "ecparam", "-name", "prime256v1", "-genkey", "-noout"], capture_output=True, check=True).stdout + p8 = subprocess.run(["openssl", "pkcs8", "-topk8", "-nocrypt"], input=ec, capture_output=True, check=True).stdout + with open(path, "wb") as fh: + fh.write(p8) + return path + + +def make_pkg(mode=0o644, name="Hidden Bar.pkg"): + d = tempfile.mkdtemp(prefix="pkg.", dir=TMP) + os.chmod(d, 0o755) + path = os.path.join(d, name) + with open(path, "wb") as fh: + fh.write(b"pkg") + os.chmod(path, mode) + return path + + +KEY = make_key() +ENV = {"HB_ASC_KEY_ID": "KEYID12345", "HB_ASC_ISSUER_ID": "issuer-uuid", "HB_ASC_KEY_FILE": KEY, + "HB_COPYRIGHT": "2026 Test Holder", "HB_REVIEW_EMAIL": "review@example.test"} + + +class FakeASC: + """A small stateful App Store Connect: enough routes for `release`, every call recorded.""" + + def __init__(self, version=False, build=False, polls=("PROCESSING", "VALID")): + self.calls, self.versions, self.uploaded, self.polls = [], [], build, list(polls) + self.attrs, self.contact, self.build_attrs = {}, None, {"version": "22", "usesNonExemptEncryption": None} + if version: + self.versions.append(self._version("1.13.1")) + + @staticmethod + def _version(ver): + return {"id": "V1", "attributes": {"versionString": ver, "appVersionState": "PREPARE_FOR_SUBMISSION", + "releaseType": "AFTER_APPROVAL", "copyright": "c"}} + + def call(self, method, path, body=None): + self.calls.append((method, path, body)) + p, m = path.split("?")[0], method + attrs = (body or {}).get("data", {}).get("attributes", {}) + if m == "GET" and p == "/v1/apps": + return 200, {"data": [{"id": "A1"}]} + if m == "GET" and p == "/v1/apps/A1/appStoreVersions": + return 200, {"data": self.versions} + if m == "GET" and p == "/v1/builds": + if not self.uploaded: + return 200, {"data": []} + st = self.polls.pop(0) if len(self.polls) > 1 else self.polls[0] + return 200, {"data": [{"id": "B1", "attributes": dict(self.build_attrs, processingState=st)}]} + if m == "POST" and p == "/v1/appStoreVersions": + self.versions.append(self._version(attrs["versionString"])) + self.versions[-1]["attributes"].update(attrs) + return 201, {"data": self.versions[-1]} + if m == "PATCH" and p == "/v1/builds/B1": + self.build_attrs.update(attrs) + return 200, {} + if p == "/v1/appStoreVersions/V1/relationships/build": + return 200, {} + if m == "GET" and p == "/v1/appStoreVersions/V1/build": + return 200, {"data": {"attributes": {"version": "22"}}} + if m == "GET" and p == "/v1/appStoreVersions/V1/appStoreReviewDetail": + return 200, {"data": {"id": "D1", "attributes": self.contact} if self.contact else None} + if m == "POST" and p == "/v1/appStoreReviewDetails": + self.contact = attrs + return 201, {} + if m == "GET" and p == "/v1/appStoreVersions/V1/appStoreVersionLocalizations": + return 200, {"data": [{"id": "L1", "attributes": {"locale": "en-US"}}]} + if m == "GET" and p == "/v1/apps/A1/appInfos": + return 200, {"data": [{"id": "I1", "attributes": {"appStoreState": "PREPARE_FOR_SUBMISSION"}}]} + if m == "GET" and p == "/v1/appInfos/I1/appInfoLocalizations": + return 200, {"data": [{"id": "L2", "attributes": {"locale": "en-US"}}]} + if p in ("/v1/appStoreVersionLocalizations/L1", "/v1/appInfoLocalizations/L2"): + if m == "PATCH": + self.attrs.setdefault(p, {}).update(attrs) + return 200, {} + return 200, {"data": {"attributes": self.attrs.get(p, {})}} + if m == "GET" and p == "/v1/reviewSubmissions": + return 200, {"data": []} + if m == "POST" and p == "/v1/reviewSubmissions": + return 201, {"data": {"id": "S1234567"}} + if m == "GET" and p == "/v1/reviewSubmissions/S1234567/items": + return 200, {"data": []} + if m == "POST" and p == "/v1/reviewSubmissionItems": + return 201, {} + if m == "PATCH" and p == "/v1/reviewSubmissions/S1234567": + return 200, {"data": {"attributes": {"state": "WAITING_FOR_REVIEW"}}} + raise AssertionError(f"FakeASC: unexpected {m} {path}") + + def writes(self): + return [(m, p) for m, p, _ in self.calls if m != "GET"] + + +class FakeAltool: + def __init__(self, asc=None, rc=0): + self.calls, self.asc, self.rc, self.seen = [], asc, rc, [] + + def __call__(self, argv, **kw): + self.calls.append(argv) + if argv[0] == "xcrun": + d = kw["env"]["API_PRIVATE_KEYS_DIR"] + f = os.path.join(d, "AuthKey_KEYID12345.p8") + self.seen.append((stat.S_IMODE(os.stat(d).st_mode), stat.S_IMODE(os.stat(f).st_mode))) + if self.asc and argv[2] == "--upload-app": + self.asc.uploaded = True + return subprocess.CompletedProcess(argv, self.rc, "UPLOAD SUCCEEDED\napiKey line\nissuer line", "") + raise AssertionError(f"unexpected command {argv}") + + +def run_main(argv, env=None, asc=None, runner=None): + out, err = io.StringIO(), io.StringIO() + lines = [] + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + rc = R.main(argv, env=ENV if env is None else env, client_factory=lambda creds: asc, + runner=runner or FakeAltool(asc), out=lines.append, sleep=lambda s: None) + return rc, "\n".join(lines) + out.getvalue(), err.getvalue() + + +def release_args(pkg, *extra): + return ["release", "1.13.1", "22", "--pkg", pkg, *extra] + + +# ---- argument parsing ------------------------------------------------------------------------- + +def parse_release_flags(): + a = R.build_parser({}).parse_args(release_args("x.pkg", "--dry-run", "--copyright", "c", "--review-email", "e@x.test")) + assert (a.cmd, a.version, a.build, a.pkg, a.dry_run, a.bundle, a.locale) == ("release", "1.13.1", "22", "x.pkg", True, "com.dwarvesv.minimalbar", "en-US") + assert a.wait == 2700 + + +def parse_wait_from_env(): + a = R.build_parser({"HB_BUILD_WAIT_SECONDS": "60"}).parse_args(["attach-build", "1.13.1", "22"]) + assert a.wait == 60 + + +def parse_requires_pkg(): + with contextlib.redirect_stderr(io.StringIO()): + raises(SystemExit, lambda: R.build_parser({}).parse_args(["release", "1.13.1", "22"])) + + +def parse_unknown_command(): + with contextlib.redirect_stderr(io.StringIO()): + raises(SystemExit, lambda: R.build_parser({}).parse_args(["nope"])) + + +def version_and_build_shape(): + R.check_version_build("1.13.1", "22") + raises(R.ToolError, lambda: R.check_version_build("1.13.1; rm", "22"), "not like") + raises(R.ToolError, lambda: R.check_version_build("1.13.1", "2x"), "not an integer") + + +# ---- env-var checks --------------------------------------------------------------------------- + +def creds_none_set(): + assert R.env_credentials({}) is None + + +def creds_all_set(): + c = R.env_credentials(ENV) + assert (c.key_id, c.issuer, c.key_path) == ("KEYID12345", "issuer-uuid", KEY) + + +def creds_partial_refused(): + raises(R.ToolError, lambda: R.env_credentials({"HB_ASC_KEY_ID": "x"}), "all of HB_ASC_KEY_ID") + + +def creds_missing_key_file(): + raises(R.ToolError, lambda: R.env_credentials(dict(ENV, HB_ASC_KEY_FILE=os.path.join(TMP, "nope.p8"))), "not a file") + + +def missing_copyright_named_before_any_network(): + asc = FakeASC() + rc, _, err = run_main(release_args(make_pkg()), env={k: v for k, v in ENV.items() if k != "HB_COPYRIGHT"}, asc=asc) + assert rc == 1 and "HB_COPYRIGHT" in err and asc.calls == [], (rc, err, asc.calls) + + +def missing_review_email_named(): + rc, _, err = run_main(release_args(make_pkg()), env={k: v for k, v in ENV.items() if k != "HB_REVIEW_EMAIL"}, asc=FakeASC()) + assert rc == 1 and "HB_REVIEW_EMAIL" in err, (rc, err) + + +def flag_beats_env(): + a = R.build_parser({}).parse_args(["create-version", "1.13.1", "--copyright", "from flag"]) + assert R.need(a, {"HB_COPYRIGHT": "from env"}, "copyright", "HB_COPYRIGHT", "--copyright") == "from flag" + + +def partial_asc_env_stops_main_before_network(): + asc = FakeASC() + rc, _, err = run_main(release_args(make_pkg(), "--dry-run"), env={"HB_ASC_KEY_ID": "x", "HB_COPYRIGHT": "c", "HB_REVIEW_EMAIL": "e"}, asc=asc) + assert rc == 1 and "all of HB_ASC_KEY_ID" in err and asc.calls == [], (rc, err) + + +def root_only_pkg_refused(): + asc = FakeASC() + rc, _, err = run_main(release_args(make_pkg(0o600)), asc=asc) + assert rc == 1 and "90255" in err and asc.calls == [], (rc, err) + + +def world_readable_pkg_accepted(): + R.check_pkg(make_pkg(0o644)) + + +def non_pkg_refused(): + raises(R.ToolError, lambda: R.check_pkg(KEY), "not a .pkg") + + +# ---- listing.json mapping --------------------------------------------------------------------- + +LISTING = os.path.join(os.path.dirname(HERE), "docs", "appstore", "listing.json") + + +def listing_maps_to_asc_fields(): + vf, af, skipped = R.load_checked_listing(LISTING) + assert set(vf) == {"description", "keywords", "promotionalText", "whatsNew", "supportUrl", "marketingUrl"}, set(vf) + assert set(af) == {"subtitle", "privacyPolicyUrl"}, set(af) + assert skipped == ["note"], skipped + with open(LISTING) as fh: + raw = json.load(fh) + assert all(vf[k] == raw[k] for k in vf) and all(af[k] == raw[k] for k in af) + + +def listing_limits_count_bytes_for_keywords(): + assert R.over_limits({"keywords": "a" * 100}) == [] + assert R.over_limits({"keywords": "é" * 51}), "51 two-byte chars are 102 bytes" + assert R.over_limits({"promotionalText": "x" * 171}) and not R.over_limits({"promotionalText": "x" * 170}) + assert R.over_limits({"subtitle": "x" * 31}) and R.over_limits({"description": "x" * 4001}) + + +def listing_over_limit_refused_before_network(): + path = os.path.join(TMP, "long.json") + with open(path, "w") as fh: + json.dump({"subtitle": "x" * 31}, fh) + asc = FakeASC() + rc, _, err = run_main(release_args(make_pkg(), "--listing", path, "--dry-run"), asc=asc) + assert rc == 1 and "subtitle is 31" in err and asc.calls == [], (rc, err) + + +def listing_non_string_refused(): + path = os.path.join(TMP, "bad.json") + with open(path, "w") as fh: + json.dump({"subtitle": 3}, fh) + raises(R.ToolError, lambda: R.read_listing(path), "must be a string") + + +# ---- the ES256 token -------------------------------------------------------------------------- + +def raw_to_der(raw): + def i(b): + b = b.lstrip(b"\0") or b"\0" + b = b"\0" + b if b[0] & 0x80 else b + return bytes([2, len(b)]) + b + body = i(raw[:32]) + i(raw[32:]) + return bytes([0x30, len(body)]) + body + + +def der_vectors(): + r, s = b"\x01" * 29, b"\xff" * 32 + der = bytes([0x30, 0x42, 2, 0x1D]) + r + bytes([2, 0x21, 0]) + s + assert R.der_to_raw(der) == b"\0" * 3 + r + s + raises(R.ToolError, lambda: R.der_to_raw(b"\x31" + b"\0" * 10), "unexpected") + + +def jwt_verifies_with_openssl(): + creds = R.Creds("KEYID12345", "issuer-uuid", KEY) + token = R.make_jwt(creds, now=1_000_000) + head, body, sig = token.split(".") + + def pad(t): + return t + "=" * (-len(t) % 4) + + assert json.loads(base64.urlsafe_b64decode(pad(head))) == {"alg": "ES256", "kid": "KEYID12345", "typ": "JWT"} + claims = json.loads(base64.urlsafe_b64decode(pad(body))) + assert claims == {"iss": "issuer-uuid", "iat": 1_000_000, "exp": 1_000_900, "aud": "appstoreconnect-v1"}, claims + raw = base64.urlsafe_b64decode(pad(sig)) + assert len(raw) == 64 + pub = subprocess.run(["openssl", "pkey", "-in", KEY, "-pubout"], capture_output=True, check=True).stdout + pubf, sigf = os.path.join(TMP, "pub.pem"), os.path.join(TMP, "sig.der") + with open(pubf, "wb") as fh: + fh.write(pub) + with open(sigf, "wb") as fh: + fh.write(raw_to_der(raw)) + verify = ["openssl", "dgst", "-sha256", "-verify", pubf, "-signature", sigf] + ok = subprocess.run(verify, input=f"{head}.{body}".encode(), capture_output=True) + assert ok.returncode == 0, ok.stdout + ok.stderr + bad = subprocess.run(verify, input=b"tampered", capture_output=True) + assert bad.returncode != 0, "negative control: a tampered message must not verify" + + +# ---- dry-run and the full flow ---------------------------------------------------------------- + +def dry_run_writes_nothing_and_skips_altool(): + asc, altool = FakeASC(), FakeAltool() + rc, out, err = run_main(release_args(make_pkg(), "--dry-run"), asc=asc, runner=altool) + assert rc == 0, (rc, err) + assert asc.writes() == [] and altool.calls == [], (asc.writes(), altool.calls) + assert "would upload" in out and "would submit" in out and "stopped before upload and submit" in out, out + + +def readonly_guard_refuses_writes(): + ro = R.ReadOnly(FakeASC()) + assert ro.call("GET", "/v1/apps")[0] == 200 + for m in ("POST", "PATCH", "DELETE"): + raises(R.DryRunWrite, lambda m=m: ro.call(m, "/v1/appStoreVersions", {}), "dry-run refused") + + +def dry_run_flag_is_what_protects(): + asc = FakeASC() + altool = FakeAltool(asc) + rc, out, err = run_main(release_args(make_pkg()), asc=asc, runner=altool) + assert rc == 0 and altool.calls and asc.writes(), "negative control: without --dry-run the same call writes" + + +def full_flow_applies_the_lessons(): + asc = FakeASC() + altool = FakeAltool(asc) + rc, out, err = run_main(release_args(make_pkg()), asc=asc, runner=altool) + assert rc == 0, (rc, out, err) + flags = [c[2] for c in altool.calls] + assert flags == ["--validate-app", "--upload-app"], flags + assert all(d == 0o700 and f == 0o600 for d, f in altool.seen), altool.seen + created = next(b for m, p, b in asc.calls if (m, p) == ("POST", "/v1/appStoreVersions"))["data"]["attributes"] + assert created["releaseType"] == "AFTER_APPROVAL" and created["copyright"] == "2026 Test Holder", created + assert asc.build_attrs["usesNonExemptEncryption"] is False + assert asc.contact["contactEmail"] == "review@example.test" + vf, af, _ = R.load_checked_listing(LISTING) + assert all(asc.attrs["/v1/appStoreVersionLocalizations/L1"][k] == v for k, v in vf.items()) + assert all(asc.attrs["/v1/appInfoLocalizations/L2"][k] == v for k, v in af.items()) + submitted = [b for m, p, b in asc.calls if (m, p) == ("PATCH", "/v1/reviewSubmissions/S1234567")] + assert len(submitted) == 1 and submitted[0]["data"]["attributes"] == {"submitted": True}, submitted + assert "apiKey" not in out and "issuer line" not in out, "altool lines naming the key are filtered" + + +def rerun_skips_upload_and_existing_version(): + asc = FakeASC(version=True, build=True, polls=("VALID",)) + altool = FakeAltool(asc) + rc, out, err = run_main(release_args(make_pkg()), asc=asc, runner=altool) + assert rc == 0 and altool.calls == [], (rc, err) + assert ("POST", "/v1/appStoreVersions") not in asc.writes() + assert "upload skipped" in out and "exists already" in out, out + + +def altool_failure_stops_the_flow(): + asc = FakeASC() + rc, out, err = run_main(release_args(make_pkg()), asc=asc, runner=FakeAltool(asc, rc=1)) + assert rc == 1 and "altool --validate-app failed" in err and asc.writes() == [], (rc, err, asc.writes()) + + +def wait_for_build_paths(): + waits = [] + b = R.wait_for_build(FakeASC(build=True, polls=("PROCESSING", "PROCESSING", "VALID")), "A1", "1.13.1", "22", 600, waits.append, lambda s: None) + assert b["attributes"]["processingState"] == "VALID" and waits == [30, 30] + raises(R.ToolError, lambda: R.wait_for_build(FakeASC(build=True, polls=("INVALID",)), "A1", "1.13.1", "22", 600, lambda s: None, lambda s: None), "INVALID") + raises(R.ToolError, lambda: R.wait_for_build(FakeASC(build=True, polls=("PROCESSING",)), "A1", "1.13.1", "22", 60, lambda s: None, lambda s: None), "after 60s") + + +def key_files_writes_a_private_key_and_prints_ids(): + d = tempfile.mkdtemp(prefix="kf.", dir=TMP) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + rc = R.main(["key-files", d], env=ENV) + f = os.path.join(d, "AuthKey_KEYID12345.p8") + assert rc == 0 and buf.getvalue().strip() == "KEYID12345 issuer-uuid" and stat.S_IMODE(os.stat(f).st_mode) == 0o600 + + +CASES = [parse_release_flags, parse_wait_from_env, parse_requires_pkg, parse_unknown_command, version_and_build_shape, + creds_none_set, creds_all_set, creds_partial_refused, creds_missing_key_file, + missing_copyright_named_before_any_network, missing_review_email_named, flag_beats_env, + partial_asc_env_stops_main_before_network, root_only_pkg_refused, world_readable_pkg_accepted, non_pkg_refused, + listing_maps_to_asc_fields, listing_limits_count_bytes_for_keywords, listing_over_limit_refused_before_network, + listing_non_string_refused, der_vectors, jwt_verifies_with_openssl, + dry_run_writes_nothing_and_skips_altool, readonly_guard_refuses_writes, dry_run_flag_is_what_protects, + full_flow_applies_the_lessons, rerun_skips_upload_and_existing_version, altool_failure_stops_the_flow, + wait_for_build_paths, key_files_writes_a_private_key_and_prints_ids] +for fn in CASES: + case(fn) + +print(f"\ntest-asc-release: {PASSED} passed, {len(FAILED)} failed" + (f" (failed: {' '.join(FAILED)})" if FAILED else "")) +sys.exit(1 if FAILED else 0) diff --git a/scripts/test-release-mas.sh b/scripts/test-release-mas.sh new file mode 100755 index 00000000..d4f80bcc --- /dev/null +++ b/scripts/test-release-mas.sh @@ -0,0 +1,194 @@ +#!/bin/bash +# scripts/test-release-mas.sh : offline tests for scripts/release-mas.sh and scripts/asc-release.py. +# +# bash scripts/test-release-mas.sh +# +# No network, no secrets, no Xcode build, no Apple, no 1Password: xcodebuild, security, pkgutil +# and op are PATH stubs, and release-mas.sh runs inside a throwaway git repo built under $TMPDIR +# (its ROOT follows its own path). Covers argument parsing, the env-var checks, the dry-run +# (builds nothing, reads no secret), the umask lesson (a root-only pkg is refused), and that no +# scratch path or secret-shaped value is hardcoded. scripts/test-asc-release.py covers the Python +# side (listing.json mapping, token, dry-run write guard) and runs at the end. +# One PASS/FAIL line per case; exit 0 only when no case failed. + +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" +WORK="$(mktemp -d "${TMPDIR:-/tmp}/hb-mas-test.XXXXXX")" +WORK="$(cd "$WORK" && pwd -P)" +chmod 755 "$WORK" +PASSED=0; FAILED_N=0; FAILED_NAMES="" + +result() { # name rc [reason] + if [ "$2" = 0 ]; then echo "PASS $1"; PASSED=$((PASSED + 1)) + else echo "FAIL $1${3:+: $3}"; FAILED_N=$((FAILED_N + 1)); FAILED_NAMES="$FAILED_NAMES $1"; fi +} +t() { local name="$1"; shift; "$@" >"$WORK/case.log" 2>&1; result "$name" $? "$(tail -1 "$WORK/case.log")"; } + +STUBS="$WORK/stubs"; mkdir -p "$STUBS" +stub() { cat > "$STUBS/$1"; chmod +x "$STUBS/$1"; } +stub security <<'EOF' +#!/bin/bash +# find-identity lists both identities unless STUB_NO_IDENTITY is set +if [ "$1 $2" = "find-identity -v" ]; then + [ -n "${STUB_NO_IDENTITY:-}" ] && { echo " 0 valid identities found"; exit 0; } + echo ' 1) AAAA "Apple Distribution: Test Co (W777S7V8TN)"' + echo ' 2) BBBB "3rd Party Mac Developer Installer: Test Co (W777S7V8TN)"' + echo ' 2 valid identities found' + exit 0 +fi +exit 1 +EOF +stub xcodebuild <<'EOF' +#!/bin/bash +# records its arguments; creates an archive and an exported pkg the way Xcode would, with this process's umask +echo "$*" >> "$STUB_MARKS/xcodebuild.args" +[ -z "${STUB_XCODE_FAIL:-}" ] || { echo "stub xcodebuild failure"; exit 1; } +for a in "$@"; do case "$a" in -archivePath) next=arch ;; -exportPath) next=exp ;; *) case "${next:-}" in arch) mkdir -p "$a/Products/Applications/Hidden Bar.app/Contents"; : > "$a/Products/Applications/Hidden Bar.app/Contents/Info.plist"; [ -z "${STUB_PAYLOAD_MODE:-}" ] || chmod "$STUB_PAYLOAD_MODE" "$a/Products/Applications/Hidden Bar.app/Contents/Info.plist"; next= ;; exp) mkdir -p "$a"; : > "$a/Hidden Bar.pkg"; [ -z "${STUB_PKG_MODE:-}" ] || chmod "$STUB_PKG_MODE" "$a/Hidden Bar.pkg"; next= ;; esac ;; esac; done +echo "** ARCHIVE SUCCEEDED **" +EOF +stub pkgutil <<'EOF' +#!/bin/bash +exit "${STUB_PKGUTIL_RC:-0}" +EOF +stub op <<'EOF' +#!/bin/bash +echo "stub op called: $*" >> "$STUB_MARKS/op.calls"; exit 1 +EOF +stub trash <<'EOF2' +#!/bin/bash +mkdir -p "$TMPDIR/trashed" && mv "$1" "$TMPDIR/trashed/" 2>/dev/null +EOF2 +export PATH="$STUBS:$PATH" +for tool in security xcodebuild pkgutil op trash; do + [ "$(command -v "$tool")" = "$STUBS/$tool" ] || { echo "FAIL stub-shadowing ($tool resolves to $(command -v "$tool"))"; exit 1; } +done + +# A fixture repo: scripts copied in, a project file with one marketing and one build number, +# a fake HOME holding an installed profile named by UUID, a fake ASC key file. +UUID="11111111-2222-3333-4444-555555555555" +fixture() { # name : prints the fixture dir + local d="$WORK/$1" + mkdir -p "$d/scripts" "$d/Hidden Bar.xcodeproj" "$d/home/Library/MobileDevice/Provisioning Profiles" "$d.marks" + cp "$ROOT/scripts/release-mas.sh" "$ROOT/scripts/asc-release.py" "$d/scripts/" + printf 'MARKETING_VERSION = 1.13.1;\nCURRENT_PROJECT_VERSION = 22;\nMARKETING_VERSION = 1.13.1;\n' > "$d/Hidden Bar.xcodeproj/project.pbxproj" + : > "$d/home/Library/MobileDevice/Provisioning Profiles/$UUID.provisionprofile" + printf 'fake key\n' > "$d/AuthKey.p8" + printf 'build/\n' > "$d/.gitignore" + ( cd "$d" && git init -q --template= . && git add -A && git -c user.name=t -c user.email=t@example.test commit -q -m fixture ) + echo "$d" +} +mas() { # fixture-dir args... : runs the fixture's release-mas.sh with a clean, fully set environment + local d="$1"; shift + ( cd "$d" && env -i PATH="$PATH" HOME="$d/home" TMPDIR="$WORK" STUB_MARKS="$d.marks" \ + HB_MAS_PROFILE="${HB_MAS_PROFILE-$UUID}" ${ASC_ENV:-HB_ASC_KEY_ID=KEYID12345 HB_ASC_ISSUER_ID=issuer-uuid HB_ASC_KEY_FILE=$d/AuthKey.p8} \ + ${EXTRA_ENV:-} bash scripts/release-mas.sh "$@" ) +} +rc_of() { local want="$1"; shift; "$@"; [ $? = "$want" ]; } +out_has() { local d="$1" needle="$2"; shift 2; local o; o="$(mas "$d" "$@" 2>&1)"; printf '%s' "$o" | grep -qF -- "$needle" || { echo "no '$needle' in: $(printf '%s' "$o" | tail -2)"; return 1; }; } + +# ---- argument parsing ------------------------------------------------------------------------ + +D="$(fixture args)" +help_ok() { local o; o="$(bash "$ROOT/scripts/release-mas.sh" --help)"; [ $? = 0 ] && for v in HB_MAS_PROFILE HB_ASC_KEY_ID HB_ASC_ISSUER_ID HB_ASC_KEY_FILE HB_TEAM_ID --dry-run; do printf '%s' "$o" | grep -qF -- "$v" || { echo "help lacks $v"; return 1; }; done; } +t help-exits-0-and-documents-env help_ok +t unknown-argument-exits-2 rc_of 2 mas "$D" --frobnicate +t out-without-value-exits-2 rc_of 2 mas "$D" --out +xtrace_refused() { bash -x "$ROOT/scripts/release-mas.sh" --dry-run 2>&1 | grep -q 'refusing to run with xtrace'; } +t xtrace-refused xtrace_refused + +# ---- env-var checks -------------------------------------------------------------------------- + +missing_profile() { local o; o="$(HB_MAS_PROFILE= mas "$D" --dry-run 2>&1)"; local rc=$?; [ $rc = 2 ] && printf '%s' "$o" | grep -q 'HB_MAS_PROFILE is not set'; } +t missing-profile-env-exits-2 missing_profile +bad_profile_chars() { local o; o="$(HB_MAS_PROFILE='xy' mas "$D" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'characters outside'; } +t profile-with-xml-chars-refused bad_profile_chars +partial_asc() { local o; o="$(ASC_ENV='HB_ASC_KEY_ID=x' mas "$D" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'all of HB_ASC_KEY_ID'; } +t partial-asc-env-exits-2 partial_asc +missing_key_file() { local o; o="$(ASC_ENV="HB_ASC_KEY_ID=x HB_ASC_ISSUER_ID=y HB_ASC_KEY_FILE=$D/nope.p8" mas "$D" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'HB_ASC_KEY_FILE is not a file'; } +t missing-key-file-exits-2 missing_key_file +profile_not_installed() { local o; o="$(HB_MAS_PROFILE=no-such-profile mas "$D" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'no installed provisioning profile'; } +t profile-not-installed-exits-2 profile_not_installed +no_identity() { local o; o="$(EXTRA_ENV=STUB_NO_IDENTITY=1 mas "$D" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'no Apple Distribution identity'; } +t missing-signing-identity-exits-2 no_identity +dirty_tree() { local d; d="$(fixture dirty)"; : > "$d/stray.txt"; local o; o="$(mas "$d" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'dirty tree'; } +t dirty-tree-exits-2 dirty_tree +disagreeing_versions() { local d; d="$(fixture skew)"; printf 'MARKETING_VERSION = 1.13.1;\nMARKETING_VERSION = 1.13.2;\nCURRENT_PROJECT_VERSION = 22;\n' > "$d/Hidden Bar.xcodeproj/project.pbxproj"; ( cd "$d" && git add -A && git -c user.name=t -c user.email=t@example.test commit -q -m skew ); local o; o="$(mas "$d" --dry-run 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'versions disagree'; } +t disagreeing-versions-exit-2 disagreeing_versions +nonempty_out() { local d; d="$(fixture out)"; mkdir "$d.o"; : > "$d.o/x"; local o; o="$(mas "$d" --dry-run --out "$d.o" 2>&1)"; [ $? = 2 ] && printf '%s' "$o" | grep -q 'not empty'; } +t nonempty-out-exits-2 nonempty_out + +# ---- dry-run --------------------------------------------------------------------------------- + +dry_run() { + local d o rc; d="$(fixture dry)" + o="$(mas "$d" --dry-run 2>&1)"; rc=$? + [ $rc = 0 ] && printf '%s' "$o" | grep -q 'preflight ok: Hidden Bar 1.13.1 (22)' && printf '%s' "$o" | grep -q 'built nothing, read no secret' && + [ ! -e "$d.marks/xcodebuild.args" ] && [ ! -e "$d.marks/op.calls" ] && [ ! -e "$d/build" ] || { echo "rc $rc: $(printf '%s' "$o" | tail -2)"; return 1; } +} +t dry-run-builds-nothing-and-reads-no-secret dry_run +dry_run_op_fallback() { # no HB_ASC_* and an op CLI on PATH: preflight passes, op is still never called + local d o rc; d="$(fixture dryop)" + o="$(ASC_ENV=HB_UNRELATED=1 mas "$d" --dry-run 2>&1)"; rc=$? + [ $rc = 0 ] && [ ! -e "$d.marks/op.calls" ] || { echo "rc $rc: $(printf '%s' "$o" | tail -2)"; return 1; } +} +t dry-run-with-1password-path-never-calls-op dry_run_op_fallback + +# ---- a real run through the stubs ------------------------------------------------------------ + +real_run() { + local d o rc plist; d="$(fixture real)" + o="$(mas "$d" 2>&1)"; rc=$? + plist="$d/build/appstore-1.13.1/export.plist" + [ $rc = 0 ] || { echo "rc $rc: $(printf '%s' "$o" | tail -3)"; return 1; } + printf '%s' "$o" | grep -q '^pkg: .*Hidden Bar.pkg$' && printf '%s' "$o" | grep -q '^next: python3 scripts/asc-release.py release 1.13.1 22 ' || { echo "no pkg/next line"; return 1; } + grep -q 'methodapp-store-connect' "$plist" && grep -q 'signingStylemanual' "$plist" && + grep -q 'signingCertificateApple Distribution' "$plist" && + grep -q "com.dwarvesv.minimalbar$UUID" "$plist" && + grep -q 'teamIDW777S7V8TN' "$plist" || { echo "export.plist wrong"; return 1; } + grep -q 'archive .*-scheme Hidden Bar App Store -configuration Release' "$d.marks/xcodebuild.args" && + grep -q -- '-exportArchive' "$d.marks/xcodebuild.args" && grep -q -- '-authenticationKeyID KEYID12345' "$d.marks/xcodebuild.args" || { echo "xcodebuild args wrong"; return 1; } + [ -z "$(find "$d/build" -name '*.p8' -o -name 'AuthKey*')" ] || { echo "key file left in the build dir"; return 1; } + printf '%s' "$o" | grep -qi 'authenticationKey\|issuer-uuid' && { echo "key flags leaked into output"; return 1; } + [ "$(stat -f %Lp "$d/build/appstore-1.13.1/export/Hidden Bar.pkg" | cut -c3)" -ge 4 ] || { echo "pkg not world-readable"; return 1; } + return 0 +} +t real-run-exports-with-manual-signing-and-no-key-left real_run +root_only_pkg() { # negative control for the umask lesson: a root-only pkg stops the run with the 90255 explanation + local d o rc; d="$(fixture rootonly)" + o="$(EXTRA_ENV=STUB_PKG_MODE=600 mas "$d" 2>&1)"; rc=$? + [ $rc = 1 ] && printf '%s' "$o" | grep -q '90255' +} +t root-only-pkg-refused-with-90255 root_only_pkg +root_only_payload() { # the file Apple actually rejects: an app file only root can read, caught before export + local d o rc; d="$(fixture rootpayload)" + o="$(EXTRA_ENV=STUB_PAYLOAD_MODE=600 mas "$d" 2>&1)"; rc=$? + [ $rc = 1 ] && printf '%s' "$o" | grep -q '90255' && printf '%s' "$o" | grep -q 'Info.plist' && ! grep -q -- '-exportArchive' "$d.marks/xcodebuild.args" +} +t root-only-app-file-refused-before-export root_only_payload +xcode_failure() { local d o rc; d="$(fixture xfail)"; o="$(EXTRA_ENV=STUB_XCODE_FAIL=1 mas "$d" 2>&1)"; rc=$?; [ $rc = 1 ] && printf '%s' "$o" | grep -q 'stub xcodebuild failure'; } +t build-failure-exits-1 xcode_failure +bad_signature() { local d o rc; d="$(fixture badsig)"; o="$(EXTRA_ENV=STUB_PKGUTIL_RC=1 mas "$d" 2>&1)"; rc=$?; [ $rc = 1 ] && printf '%s' "$o" | grep -q 'check-signature failed'; } +t unsigned-pkg-exits-1 bad_signature + +# ---- the scripts hold no scratch path, secret or umask 077 ----------------------------------- + +code_of() { grep -v '^[[:space:]]*#' "$1"; } +no_umask_077() { ! code_of "$ROOT/scripts/release-mas.sh" | grep -qE '^[[:space:]]*umask[[:space:]]+0?77' && ! code_of "$ROOT/scripts/asc-release.py" | grep -q 'os.umask' && code_of "$ROOT/scripts/release-mas.sh" | grep -q '^umask 022$'; } +t no-umask-077-in-the-scripts no_umask_077 +no_scratch_paths() { ! grep -nE '/private/tmp|scratchpad|/Users/|/home/|ops-toolkit' "$ROOT/scripts/release-mas.sh" "$ROOT/scripts/asc-release.py"; } +t no-scratch-or-home-paths-hardcoded no_scratch_paths +no_secret_shapes() { ! grep -nE '[A-Za-z0-9+/=]{64,}|AuthKey_[A-Z0-9]{10}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|op://|eyJ[A-Za-z0-9_-]{10}' "$ROOT/scripts/release-mas.sh" "$ROOT/scripts/asc-release.py"; } +t no-secret-shaped-values-hardcoded no_secret_shapes +lessons_present() { + grep -q 'signingStylemanual' "$ROOT/scripts/release-mas.sh" && grep -q 'Apple Distribution' "$ROOT/scripts/release-mas.sh" && + grep -q 'RELEASE_TYPE = "AFTER_APPROVAL"' "$ROOT/scripts/asc-release.py" && grep -q '"usesNonExemptEncryption": False' "$ROOT/scripts/asc-release.py" +} +t lessons-encoded-manual-signing-export-compliance-release-type lessons_present + +# ---- the Python side ------------------------------------------------------------------------- + +python_side() { python3 -I -B "$ROOT/scripts/test-asc-release.py" > "$WORK/py.log" 2>&1; local rc=$?; grep -E '^(PASS|FAIL)' "$WORK/py.log" | sed 's/^/py: /'; tail -1 "$WORK/py.log" | grep -q 'test-asc-release: [0-9]* passed, 0 failed' && return $rc; return 1; } +if python_side; then result python-side-all-cases 0; else result python-side-all-cases 1 "see $WORK/py.log"; fi + +echo +echo "test-release-mas: $PASSED passed, $FAILED_N failed${FAILED_NAMES:+ (failed:$FAILED_NAMES)}" +[ "$FAILED_N" = 0 ]