Repository Public
1#!/usr/bin/env python32"""Idempotent Apple Developer provisioning for MacLustr Command Center (macOS), team 3YM54G49SN.34 ASC_KEY_ID=L379BDKR69 python3 infra/apple/provision.py apply (creates only what is missing or stale)5 ASC_KEY_ID=L379BDKR69 python3 infra/apple/provision.py --check report only, change nothing (exit 1 if drift)67What it manages (and nothing else):8 - bundle ids ai.spboucher.clustr and ai.spboucher.clustr.debug (MAC_OS)9 - capabilities ASSOCIATED_DOMAINS and PUSH_NOTIFICATIONS on both10 - profile "MCC macOS Developer ID" (MAC_APP_DIRECT) for ai.spboucher.clustr11 - profile "MCC macOS Debug Development" (MAC_APP_DEVELOPMENT) for ai.spboucher.clustr.debug, this Mac as device12It never touches any other bundle id, profile, certificate or device. Only the two profiles named above may be13deleted and recreated, and only when stale (inactive, expired, wrong certificates/device, missing entitlements).1415iOS mode (added for apps/ios; the macOS behaviour above is unchanged):16 ASC_KEY_ID=L379BDKR69 ASC_ISSUER_ID=… python3 infra/apple/provision.py --ios [--check]17 - bundle ids ai.spboucher.clustr.ios and ai.spboucher.clustr.ios.debug (IOS)18 - capabilities ASSOCIATED_DOMAINS and PUSH_NOTIFICATIONS on both19 - profile "MCC iOS App Store" (IOS_APP_STORE) for ai.spboucher.clustr.ios — only when the keychain holds an20 Apple / iPhone Distribution identity; otherwise nothing is created and apps/ios/Scripts/release.sh signs with21 Xcode automatic (cloud-managed) signing through the same API key22 - profile "MCC iOS Debug Development" (IOS_APP_DEVELOPMENT) for ai.spboucher.clustr.ios.debug — only when the23 team has registered iOS devices (a development profile needs at least one)24 - reports whether the App Store Connect app record exists (it cannot be created through the public API)2526ASC_ISSUER_ID is read from the environment, otherwise from the default of the ISSUER_ID variable in27~/Desktop/Cluster/apple/SPBrowser/scripts/ios-release.sh. Neither the issuer id nor key material is ever printed.28"""29import base64, datetime, hashlib, json, os, plistlib, re, subprocess, sys3031HERE = os.path.dirname(os.path.abspath(__file__))32sys.path.insert(0, HERE)3334TEAM = "3YM54G49SN"35ISSUER_FALLBACK = os.path.expanduser("~/Desktop/Cluster/apple/SPBrowser/scripts/ios-release.sh")36CAPABILITIES = ["ASSOCIATED_DOMAINS", "PUSH_NOTIFICATIONS"]37BUNDLES = [38 {"identifier": "ai.spboucher.clustr", "name": "MacLustr Command Center"},39 {"identifier": "ai.spboucher.clustr.debug", "name": "MacLustr Command Center Debug"},40]41PROFILES = [42 {"name": "MCC macOS Developer ID", "type": "MAC_APP_DIRECT", "bundle": "ai.spboucher.clustr",43 "file": "MCC_macOS_Developer_ID.provisionprofile", "identity_prefix": "Developer ID Application:", "device": False},44 {"name": "MCC macOS Debug Development", "type": "MAC_APP_DEVELOPMENT", "bundle": "ai.spboucher.clustr.debug",45 "file": "MCC_macOS_Debug_Development.provisionprofile", "identity_prefix": "Apple Development:", "device": True},46]47DEVICE_NAME = "MCC dev Mac"48REQUIRED_ENTITLEMENTS = ["com.apple.developer.associated-domains", "com.apple.developer.aps-environment",49 "keychain-access-groups", "com.apple.application-identifier"]50INSTALL_DIRS = [os.path.expanduser("~/Library/Developer/Xcode/UserData/Provisioning Profiles"),51 os.path.expanduser("~/Library/MobileDevice/Provisioning Profiles")]5253CHECK = "--check" in sys.argv[1:]54changes, pending, errors = [], [], []555657def ensure_credentials():58 os.environ.setdefault("ASC_KEY_ID", "L379BDKR69")59 if not os.environ.get("ASC_ISSUER_ID"):60 try:61 for line in open(ISSUER_FALLBACK, encoding="utf-8", errors="replace"):62 if "ISSUER_ID" in line:63 m = re.search(r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", line)64 if m:65 os.environ["ASC_ISSUER_ID"] = m.group(0)66 break67 except OSError:68 pass69 if not os.environ.get("ASC_ISSUER_ID"):70 sys.exit("ASC_ISSUER_ID is not set and could not be found in the fallback script.")717273ensure_credentials()74import asc # noqa: E402 (needs the environment above)757677def api(method, path, body=None, ok=(200, 201, 204)):78 status, data = asc.call(method, path, body)79 if status not in ok:80 errs = [f"{e.get('code')}: {e.get('detail') or e.get('title')}" for e in data.get("errors", [])] if isinstance(data, dict) else []81 raise RuntimeError(f"{method} {path.split('?')[0]} -> HTTP {status} {'; '.join(errs) or str(data)[:300]}")82 return data838485def get_all(path):86 """GET with pagination; returns (items, included)."""87 items, included = [], []88 while path:89 d = api("GET", path)90 items += d.get("data", [])91 included += d.get("included", [])92 nxt = d.get("links", {}).get("next")93 path = nxt.replace("https://api.appstoreconnect.apple.com", "") if nxt else None94 return items, included959697def mutate(description, fn):98 """Runs a change (or records it as pending in --check mode)."""99 if CHECK:100 pending.append(description)101 return None102 out = fn()103 changes.append(description)104 return out105106107# ── Local machine ──────────────────────────────────────────────────────────108109def keychain_identities():110 out = subprocess.run(["security", "find-identity", "-v", "-p", "codesigning"], capture_output=True, text=True).stdout111 return {m.group(1).upper(): m.group(2) for m in re.finditer(r'\)\s+([0-9A-Fa-f]{40})\s+"([^"]+)"', out)}112113114def provisioning_udid():115 out = subprocess.run(["system_profiler", "SPHardwareDataType"], capture_output=True, text=True).stdout116 m = re.search(r"Provisioning UDID:\s*(\S+)", out)117 return m.group(1) if m else None118119120def decode_profile(raw):121 p = subprocess.run(["security", "cms", "-D"], input=raw, capture_output=True)122 if p.returncode != 0:123 raise RuntimeError("security cms -D failed: " + p.stderr.decode(errors="replace")[:200])124 return plistlib.loads(p.stdout)125126127def sha1(b):128 return hashlib.sha1(b).hexdigest().upper()129130131# ── Steps ──────────────────────────────────────────────────────────────────132133def ensure_bundle(spec):134 items, _ = get_all(f"/v1/bundleIds?filter[identifier]={spec['identifier']}&limit=200")135 found = [b for b in items if b["attributes"]["identifier"] == spec["identifier"]] # the filter is a prefix match136 created = False137 if not found:138 res = mutate(f"register bundle id {spec['identifier']}", lambda: api("POST", "/v1/bundleIds", {"data": {139 "type": "bundleIds", "attributes": {"identifier": spec["identifier"], "name": spec["name"], "platform": spec.get("platform", "MAC_OS")}}}))140 if res is None:141 return {"identifier": spec["identifier"], "exists": False, "capabilities": []}142 b, created = res["data"], True143 else:144 b = found[0]145 caps, _ = get_all(f"/v1/bundleIds/{b['id']}/bundleIdCapabilities")146 have = sorted(c["attributes"]["capabilityType"] for c in caps)147 for cap in CAPABILITIES:148 if cap not in have:149 r = mutate(f"enable {cap} on {spec['identifier']}", lambda cap=cap: api("POST", "/v1/bundleIdCapabilities", {"data": {150 "type": "bundleIdCapabilities", "attributes": {"capabilityType": cap, "settings": []},151 "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": b["id"]}}}}}))152 if r is not None:153 have = sorted(have + [cap])154 return {"identifier": spec["identifier"], "exists": True, "created": created, "asc_id": b["id"], "name": b["attributes"]["name"],155 "platform": b["attributes"]["platform"], "seed_id": b["attributes"].get("seedId"), "capabilities": have,156 "required_capabilities_enabled": all(c in have for c in CAPABILITIES)}157158159def matching_certificates(identities, prefix):160 """ASC certificates whose DER SHA-1 equals a keychain identity with the given name prefix."""161 wanted = {h: n for h, n in identities.items() if n.startswith(prefix)}162 certs, _ = get_all("/v1/certificates?limit=200")163 out = []164 for c in certs:165 content = c["attributes"].get("certificateContent")166 if not content:167 continue168 h = sha1(base64.b64decode(content))169 if h in wanted:170 out.append({"asc_id": c["id"], "sha1": h, "keychain_identity": wanted[h], "type": c["attributes"].get("certificateType"),171 "serial": c["attributes"].get("serialNumber"), "expires": c["attributes"].get("expirationDate")})172 return out173174175def ensure_device(udid):176 devices, _ = get_all("/v1/devices?limit=200")177 for d in devices:178 if d["attributes"]["udid"].upper() == udid.upper():179 return {"asc_id": d["id"], "name": d["attributes"]["name"], "platform": d["attributes"]["platform"],180 "status": d["attributes"]["status"], "registered_now": False}181 res = mutate("register this Mac as device '%s'" % DEVICE_NAME, lambda: api("POST", "/v1/devices", {"data": {182 "type": "devices", "attributes": {"name": DEVICE_NAME, "platform": "MAC_OS", "udid": udid}}}))183 if res is None:184 return None185 d = res["data"]186 return {"asc_id": d["id"], "name": d["attributes"]["name"], "platform": d["attributes"]["platform"],187 "status": d["attributes"]["status"], "registered_now": True}188189190def profile_summary(raw, identities):191 pl = decode_profile(raw)192 ent = pl.get("Entitlements", {})193 return {"uuid": pl.get("UUID"), "name": pl.get("Name"), "team": pl.get("TeamIdentifier"),194 "expires": pl["ExpirationDate"].strftime("%Y-%m-%dT%H:%M:%SZ") if pl.get("ExpirationDate") else None,195 "platform": pl.get("Platform"), "provisions_all_devices": pl.get("ProvisionsAllDevices", False),196 "provisioned_devices": len(pl.get("ProvisionedDevices", []) or []),197 "entitlements": ent,198 "certificates": [{"sha1": sha1(c), "keychain_identity": identities.get(sha1(c), "(not in keychain)")}199 for c in pl.get("DeveloperCertificates", [])],200 "_plist": pl}201202203def stale_reasons(p, summary, cert_sha1s, udid, app_id):204 r = []205 if p["attributes"].get("profileState") != "ACTIVE":206 r.append("state " + str(p["attributes"].get("profileState")))207 exp = summary["_plist"].get("ExpirationDate")208 if exp and exp < datetime.datetime.utcnow() + datetime.timedelta(days=14):209 r.append("expired or expiring within 14 days")210 ent = summary["entitlements"]211 for k in REQUIRED_ENTITLEMENTS:212 if k not in ent:213 r.append("missing entitlement " + k)214 if ent.get("com.apple.application-identifier") != app_id:215 r.append("application identifier mismatch")216 if {c["sha1"] for c in summary["certificates"]} != set(cert_sha1s):217 r.append("certificate set differs from the keychain identities")218 if udid and udid not in (summary["_plist"].get("ProvisionedDevices") or []):219 r.append("this Mac is not in the profile")220 return r221222223def write_if_changed(path, raw):224 try:225 if open(path, "rb").read() == raw:226 return False227 except OSError:228 pass229 os.makedirs(os.path.dirname(path), exist_ok=True)230 with open(path, "wb") as f:231 f.write(raw)232 return True233234235def ensure_profile(spec, bundle, certs, device, udid, identities):236 app_id = f"{TEAM}.{spec['bundle']}"237 out = {"name": spec["name"], "type": spec["type"], "bundle": spec["bundle"]}238 if not bundle.get("exists"):239 return {**out, "status": "bundle id missing"}240 if not certs:241 errors.append(f"{spec['name']}: no ASC certificate matches a keychain identity '{spec['identity_prefix']}'")242 return {**out, "status": "no matching certificate"}243 if spec["device"] and not device:244 return {**out, "status": "device not registered"}245 items, _ = get_all("/v1/profiles?filter[name]=%s&limit=200" % spec["name"].replace(" ", "%20"))246 found = [p for p in items if p["attributes"]["name"] == spec["name"]]247 raw, action = None, "kept"248 cert_sha1s = [c["sha1"] for c in certs]249 for p in found:250 content = p["attributes"].get("profileContent")251 s = profile_summary(base64.b64decode(content), identities) if content else None252 reasons = ["no content"] if s is None else stale_reasons(p, s, cert_sha1s, udid if spec["device"] else None, app_id)253 if reasons or raw is not None: # stale, or a duplicate of the managed name254 why = "; ".join(reasons) or "duplicate"255 mutate(f"delete stale profile '{spec['name']}' ({why})", lambda p=p: api("DELETE", f"/v1/profiles/{p['id']}"))256 if CHECK:257 out["stale"] = why258 else:259 raw, out["asc_id"] = base64.b64decode(content), p["id"]260 if raw is None:261 rel = {"bundleId": {"data": {"type": "bundleIds", "id": bundle["asc_id"]}},262 "certificates": {"data": [{"type": "certificates", "id": c["asc_id"]} for c in certs]}}263 if spec["device"]:264 rel["devices"] = {"data": [{"type": "devices", "id": device["asc_id"]}]}265 res = mutate(f"create profile '{spec['name']}' ({spec['type']})", lambda: api("POST", "/v1/profiles", {"data": {266 "type": "profiles", "attributes": {"name": spec["name"], "profileType": spec["type"]}, "relationships": rel}}))267 if res is None:268 return {**out, "status": "to be created"}269 raw, out["asc_id"], action = base64.b64decode(res["data"]["attributes"]["profileContent"]), res["data"]["id"], "created"270 s = profile_summary(raw, identities)271 s.pop("_plist")272 missing = [k for k in REQUIRED_ENTITLEMENTS if k not in s["entitlements"]]273 if missing:274 errors.append(f"{spec['name']}: profile lacks {missing}")275 files = [os.path.join(HERE, "profiles", spec["file"])] + [os.path.join(d, s["uuid"] + ".provisionprofile") for d in INSTALL_DIRS]276 written = []277 for f in files:278 if CHECK:279 try:280 same = open(f, "rb").read() == raw281 except OSError:282 same = False283 if not same:284 pending.append("write " + f.replace(os.path.expanduser("~"), "~"))285 elif write_if_changed(f, raw):286 written.append(f.replace(os.path.expanduser("~"), "~"))287 changes.append("write " + written[-1])288 return {**out, "status": action, **s, "files": [f.replace(os.path.expanduser("~"), "~") for f in files], "files_written": written}289290291def main():292 identities = keychain_identities()293 udid = provisioning_udid()294 summary = {"mode": "check" if CHECK else "apply", "team": TEAM}295 bundles = {b["identifier"]: ensure_bundle(b) for b in BUNDLES}296 summary["bundle_ids"] = list(bundles.values())297 device = ensure_device(udid) if udid else None298 if not udid:299 errors.append("Provisioning UDID of this Mac not found")300 summary["device"] = {"udid_suffix": udid[-6:] if udid else None, **(device or {"registered": False})}301 summary["profiles"] = []302 for spec in PROFILES:303 certs = matching_certificates(identities, spec["identity_prefix"])304 prof = ensure_profile(spec, bundles[spec["bundle"]], certs, device, udid, identities)305 prof["bound_certificates"] = certs306 summary["profiles"].append(prof)307 summary["changes"], summary["pending"], summary["errors"] = changes, pending, errors308 print(json.dumps(summary, indent=1, default=str))309 sys.exit(1 if errors or (CHECK and pending) else 0)310311312# ── iOS mode (--ios) ───────────────────────────────────────────────────────313314IOS_BUNDLES = [315 {"identifier": "ai.spboucher.clustr.ios", "name": "MacLustr CC", "platform": "IOS"},316 {"identifier": "ai.spboucher.clustr.ios.debug", "name": "MacLustr CC Debug", "platform": "IOS"},317]318IOS_PROFILES = [319 {"name": "MCC iOS App Store", "type": "IOS_APP_STORE", "bundle": "ai.spboucher.clustr.ios",320 "file": "MCC_iOS_App_Store.mobileprovision", "identity_prefixes": ("Apple Distribution:", "iPhone Distribution:"), "devices": False},321 {"name": "MCC iOS Debug Development", "type": "IOS_APP_DEVELOPMENT", "bundle": "ai.spboucher.clustr.ios.debug",322 "file": "MCC_iOS_Debug_Development.mobileprovision", "identity_prefixes": ("Apple Development:", "iPhone Developer:"), "devices": True},323]324IOS_REQUIRED_ENTITLEMENTS = ["application-identifier", "aps-environment", "com.apple.developer.associated-domains", "keychain-access-groups"]325IOS_APP_RECORD = {"name": "MacLustr CC", "bundle_id": "ai.spboucher.clustr.ios", "sku": "maclustr-cc-ios-001", "primary_locale": "fr-CA"}326327328def ios_devices():329 devices, _ = get_all("/v1/devices?filter[platform]=IOS&filter[status]=ENABLED&limit=200")330 return [{"asc_id": d["id"], "udid": d["attributes"]["udid"], "class": d["attributes"].get("deviceClass")} for d in devices]331332333def ensure_ios_profile(spec, bundle, identities, devices):334 """Same policy as ensure_profile: only the profile carrying exactly this name is ever replaced, only when stale."""335 app_id = f"{TEAM}.{spec['bundle']}"336 out = {"name": spec["name"], "type": spec["type"], "bundle": spec["bundle"]}337 if not bundle.get("exists"):338 return {**out, "status": "bundle id missing"}339 certs = [c for prefix in spec["identity_prefixes"] for c in matching_certificates(identities, prefix)]340 out["bound_certificates"] = certs341 if not certs:342 return {**out, "status": "skipped: no keychain identity %s — use Xcode automatic signing (release.sh does)" % " / ".join(spec["identity_prefixes"])}343 if spec["devices"] and not devices:344 return {**out, "status": "skipped: no iOS device is registered on the team (a development profile needs one)"}345 items, _ = get_all("/v1/profiles?filter[name]=%s&limit=200" % spec["name"].replace(" ", "%20"))346 found = [p for p in items if p["attributes"]["name"] == spec["name"]]347 cert_sha1s = {c["sha1"] for c in certs}348 udids = {d["udid"] for d in devices} if spec["devices"] else set()349 raw, action = None, "kept"350 for p in found:351 content = p["attributes"].get("profileContent")352 reasons = []353 if not content:354 reasons.append("no content")355 else:356 s = profile_summary(base64.b64decode(content), identities)357 pl, ent = s["_plist"], s["entitlements"]358 if p["attributes"].get("profileState") != "ACTIVE":359 reasons.append("state " + str(p["attributes"].get("profileState")))360 exp = pl.get("ExpirationDate")361 if exp and exp < datetime.datetime.utcnow() + datetime.timedelta(days=14):362 reasons.append("expired or expiring within 14 days")363 reasons += ["missing entitlement " + k for k in IOS_REQUIRED_ENTITLEMENTS if k not in ent]364 if ent.get("application-identifier") != app_id:365 reasons.append("application identifier mismatch")366 if {c["sha1"] for c in s["certificates"]} != cert_sha1s:367 reasons.append("certificate set differs from the keychain identities")368 if udids and udids != set(pl.get("ProvisionedDevices") or []):369 reasons.append("device set differs from the registered iOS devices")370 if reasons or raw is not None:371 why = "; ".join(reasons) or "duplicate"372 mutate(f"delete stale profile '{spec['name']}' ({why})", lambda p=p: api("DELETE", f"/v1/profiles/{p['id']}"))373 if CHECK:374 out["stale"] = why375 else:376 raw, out["asc_id"] = base64.b64decode(content), p["id"]377 if raw is None:378 rel = {"bundleId": {"data": {"type": "bundleIds", "id": bundle["asc_id"]}},379 "certificates": {"data": [{"type": "certificates", "id": c["asc_id"]} for c in certs]}}380 if spec["devices"]:381 rel["devices"] = {"data": [{"type": "devices", "id": d["asc_id"]} for d in devices]}382 res = mutate(f"create profile '{spec['name']}' ({spec['type']})", lambda: api("POST", "/v1/profiles", {"data": {383 "type": "profiles", "attributes": {"name": spec["name"], "profileType": spec["type"]}, "relationships": rel}}))384 if res is None:385 return {**out, "status": "to be created"}386 raw, out["asc_id"], action = base64.b64decode(res["data"]["attributes"]["profileContent"]), res["data"]["id"], "created"387 s = profile_summary(raw, identities)388 s.pop("_plist")389 missing = [k for k in IOS_REQUIRED_ENTITLEMENTS if k not in s["entitlements"]]390 if missing:391 errors.append(f"{spec['name']}: profile lacks {missing}")392 files = [os.path.join(HERE, "profiles", spec["file"])] + [os.path.join(d, s["uuid"] + ".mobileprovision") for d in INSTALL_DIRS]393 written = []394 for f in files:395 if CHECK:396 try:397 same = open(f, "rb").read() == raw398 except OSError:399 same = False400 if not same:401 pending.append("write " + f.replace(os.path.expanduser("~"), "~"))402 elif write_if_changed(f, raw):403 written.append(f.replace(os.path.expanduser("~"), "~"))404 changes.append("write " + written[-1])405 return {**out, "status": action, **s, "files": [f.replace(os.path.expanduser("~"), "~") for f in files], "files_written": written}406407408def ios_app_record():409 """The App Store Connect app record. Read-only: the public API cannot create it."""410 items, _ = get_all("/v1/apps?filter[bundleId]=%s&limit=200" % IOS_APP_RECORD["bundle_id"])411 found = [a for a in items if a["attributes"].get("bundleId") == IOS_APP_RECORD["bundle_id"]]412 if not found:413 return {"exists": False, "to_create_in_app_store_connect": IOS_APP_RECORD}414 a = found[0]415 return {"exists": True, "asc_id": a["id"], "name": a["attributes"].get("name"), "sku": a["attributes"].get("sku"),416 "primary_locale": a["attributes"].get("primaryLocale")}417418419def main_ios():420 identities = keychain_identities()421 summary = {"mode": ("check" if CHECK else "apply") + " (ios)", "team": TEAM}422 bundles = {b["identifier"]: ensure_bundle(b) for b in IOS_BUNDLES}423 summary["bundle_ids"] = list(bundles.values())424 devices = ios_devices()425 summary["ios_devices_registered"] = len(devices)426 summary["profiles"] = [ensure_ios_profile(spec, bundles[spec["bundle"]], identities, devices) for spec in IOS_PROFILES]427 summary["app_record"] = ios_app_record()428 summary["changes"], summary["pending"], summary["errors"] = changes, pending, errors429 print(json.dumps(summary, indent=1, default=str))430 sys.exit(1 if errors or (CHECK and pending) else 0)431432433if __name__ == "__main__":434 try:435 main_ios() if "--ios" in sys.argv[1:] else main()436 except RuntimeError as e:437 print(json.dumps({"fatal": str(e), "changes": changes}, indent=1))438 sys.exit(2)439