SPB Git

Repository Public

provision.py 3.6 KB 45 lines python

1#!/usr/bin/env python32# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: scripts/provision.py4# Description: Registers the bundle id, its Associated Domains capability and the Developer ID provisioning profile with the EXISTING App Store Connect API key and Developer ID certificate.5#6# Idempotent: existing resources are reused; no certificate is ever created. Nothing secret is printed.7import base64, hashlib, json, os, subprocess, sys8sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))9import asc1011BID, NAME = "ai.spboucher.spbrowser", "SP Browser Developer ID"12OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "build", "SPBrowser_Developer_ID.provisionprofile")1314s, d = asc.call("GET", f"/v1/bundleIds?filter[identifier]={BID}&limit=5")15ids = [b for b in d.get("data", []) if b["attributes"]["identifier"] == BID]16if ids: bid = ids[0]["id"]; print("bundle id exists:", BID)17else:18    s, d = asc.call("POST", "/v1/bundleIds", {"data": {"type": "bundleIds", "attributes": {"identifier": BID, "name": "SP Browser", "platform": "UNIVERSAL"}}})19    if s >= 300: sys.exit(f"cannot create bundle id: HTTP {s}")20    bid = d["data"]["id"]; print("bundle id created:", BID)21s, d = asc.call("GET", f"/v1/bundleIds/{bid}/bundleIdCapabilities")22if "ASSOCIATED_DOMAINS" not in [c["attributes"]["capabilityType"] for c in d.get("data", [])]:23    s, d = asc.call("POST", "/v1/bundleIdCapabilities", {"data": {"type": "bundleIdCapabilities", "attributes": {"capabilityType": "ASSOCIATED_DOMAINS"}, "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}}}})24    if s >= 300: sys.exit(f"cannot enable Associated Domains: HTTP {s}")25    print("capability enabled: ASSOCIATED_DOMAINS")26out = subprocess.run(["security", "find-certificate", "-c", "Developer ID Application", "-Z"], capture_output=True, text=True).stdout27local = [l.split(":")[1].strip() for l in out.splitlines() if l.startswith("SHA-1")]28if not local: sys.exit("no Developer ID Application certificate in the keychain")29s, d = asc.call("GET", "/v1/certificates?filter[certificateType]=DEVELOPER_ID_APPLICATION,DEVELOPER_ID_APPLICATION_G2&limit=100")30cert = next((c["id"] for c in d.get("data", []) if hashlib.sha1(base64.b64decode(c["attributes"]["certificateContent"])).hexdigest().upper() in local), None)31if not cert: sys.exit("the keychain's Developer ID certificate is not visible through the API")32s, d = asc.call("GET", "/v1/profiles?filter[profileType]=MAC_APP_DIRECT&limit=200")33active = [p for p in d.get("data", []) if p["attributes"]["name"] == NAME and p["attributes"]["profileState"] == "ACTIVE"]34if active: p = active[0]; print("profile exists:", NAME)35else:36    # a capability change on the App ID invalidates its profiles: the stale one is replaced37    for stale in [x for x in d.get("data", []) if x["attributes"]["name"] == NAME]:38        asc.call("DELETE", f"/v1/profiles/{stale['id']}"); print("stale profile removed:", stale["attributes"]["profileState"])39    s, d = asc.call("POST", "/v1/profiles", {"data": {"type": "profiles", "attributes": {"name": NAME, "profileType": "MAC_APP_DIRECT"}, "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}, "certificates": {"data": [{"type": "certificates", "id": cert}]}}}})40    if s >= 300: sys.exit(f"cannot create the profile: HTTP {s}")41    p = d["data"]; print("profile created:", NAME)42os.makedirs(os.path.dirname(OUT), exist_ok=True)43open(OUT, "wb").write(base64.b64decode(p["attributes"]["profileContent"]))44print("written:", os.path.relpath(OUT), "| uuid", p["attributes"]["uuid"], "| expires", p["attributes"]["expirationDate"][:10])45
Files main
Menu

Theme