|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.12" |
| 3 | +# dependencies = ["boto3"] |
| 4 | +# |
| 5 | +# [tool.uv] |
| 6 | +# no-build = true |
| 7 | +# /// |
| 8 | +"""Submit signed macOS executables to Apple's notarization service. |
| 9 | +
|
| 10 | +Apple scans software for malware and signing issues. On acceptance, it publishes |
| 11 | +tickets that Gatekeeper can retrieve online when users run the binaries. |
| 12 | +Standalone executables cannot have tickets stapled to them, so uv relies on |
| 13 | +that online lookup even when the binaries are distributed in wheels or archives. |
| 14 | +
|
| 15 | +This script uploads all signed macOS targets and waits for Apple's result. |
| 16 | +It authenticates to the Notary API using an App Store Connect key in Azure Key |
| 17 | +Vault, which signs API tokens without exposing the private key. |
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import base64 |
| 22 | +import hashlib |
| 23 | +import json |
| 24 | +import os |
| 25 | +import subprocess |
| 26 | +import sys |
| 27 | +import tempfile |
| 28 | +import time |
| 29 | +import urllib.error |
| 30 | +import urllib.request |
| 31 | +from dataclasses import dataclass |
| 32 | +from pathlib import Path |
| 33 | +from zipfile import ZIP_DEFLATED, ZipFile |
| 34 | + |
| 35 | +import boto3 |
| 36 | +from botocore.config import Config |
| 37 | +from botocore.exceptions import BotoCoreError, ClientError |
| 38 | + |
| 39 | +NOTARY_URL = "https://appstoreconnect.apple.com/notary/v2/submissions" |
| 40 | + |
| 41 | + |
| 42 | +@dataclass(frozen=True, kw_only=True) |
| 43 | +class NotarizationKey: |
| 44 | + """An App Store Connect key in Azure Key Vault.""" |
| 45 | + |
| 46 | + # Versioned Azure Key Vault URL used for signing. Azure CLI authenticates |
| 47 | + # requests using the workflow's existing Azure login. |
| 48 | + url: str |
| 49 | + key_id: str |
| 50 | + issuer: str |
| 51 | + |
| 52 | + |
| 53 | +def base64url(value: bytes) -> str: |
| 54 | + """Encode a JWT component without padding.""" |
| 55 | + return base64.urlsafe_b64encode(value).decode("ascii").rstrip("=") |
| 56 | + |
| 57 | + |
| 58 | +def azure_json(*arguments: str) -> dict: |
| 59 | + """Run an Azure CLI request without printing key-vault details to CI logs.""" |
| 60 | + try: |
| 61 | + result = subprocess.run( |
| 62 | + ["az", *arguments, "--output", "json", "--only-show-errors"], |
| 63 | + check=True, |
| 64 | + capture_output=True, |
| 65 | + text=True, |
| 66 | + ) |
| 67 | + except subprocess.CalledProcessError: |
| 68 | + raise RuntimeError("Azure Key Vault request failed") from None |
| 69 | + return json.loads(result.stdout) |
| 70 | + |
| 71 | + |
| 72 | +def notarization_key() -> NotarizationKey: |
| 73 | + """Resolve the current key version and its Apple key and issuer IDs.""" |
| 74 | + vault = os.environ["AZURE_KEYVAULT_NAME"] |
| 75 | + name = os.environ["APPLE_NOTARIZATION_AKV_KEY_NAME"] |
| 76 | + key = azure_json( |
| 77 | + "keyvault", |
| 78 | + "key", |
| 79 | + "show", |
| 80 | + "--vault-name", |
| 81 | + vault, |
| 82 | + "--name", |
| 83 | + name, |
| 84 | + "--query", |
| 85 | + '{url:key.kid,key_id:tags."apple-key-id",issuer:tags."apple-issuer-id"}', |
| 86 | + ) |
| 87 | + return NotarizationKey(url=key["url"], key_id=key["key_id"], issuer=key["issuer"]) |
| 88 | + |
| 89 | + |
| 90 | +def apple_token(key: NotarizationKey) -> str: |
| 91 | + """Sign a short-lived Notary API token without exporting the Apple private key.""" |
| 92 | + now = int(time.time()) |
| 93 | + header = {"alg": "ES256", "kid": key.key_id, "typ": "JWT"} |
| 94 | + claims = { |
| 95 | + "iss": key.issuer, |
| 96 | + "iat": now, |
| 97 | + "exp": now + 15 * 60, |
| 98 | + "aud": "appstoreconnect-v1", |
| 99 | + "scope": ["/notary/v2"], |
| 100 | + } |
| 101 | + signing_input = ".".join( |
| 102 | + base64url(json.dumps(value, separators=(",", ":")).encode()) |
| 103 | + for value in (header, claims) |
| 104 | + ) |
| 105 | + signature = azure_json( |
| 106 | + "rest", |
| 107 | + "--method", |
| 108 | + "post", |
| 109 | + "--url", |
| 110 | + f"{key.url}/sign?api-version=7.4", |
| 111 | + "--resource", |
| 112 | + "https://vault.azure.net", |
| 113 | + "--headers", |
| 114 | + "Content-Type=application/json", |
| 115 | + "--body", |
| 116 | + json.dumps( |
| 117 | + { |
| 118 | + "alg": "ES256", |
| 119 | + "value": base64url( |
| 120 | + hashlib.sha256(signing_input.encode("ascii")).digest() |
| 121 | + ), |
| 122 | + } |
| 123 | + ), |
| 124 | + ) |
| 125 | + if signature["kid"] != key.url: |
| 126 | + raise ValueError("Azure signed with an unexpected notarization key") |
| 127 | + if len(base64.urlsafe_b64decode(signature["value"] + "==")) != 64: |
| 128 | + raise ValueError("Azure returned an invalid ES256 signature") |
| 129 | + return f"{signing_input}.{signature['value']}" |
| 130 | + |
| 131 | + |
| 132 | +def apple_json(token: str, suffix: str = "", body: dict | None = None) -> dict: |
| 133 | + """Send a JSON request to Apple's notary service.""" |
| 134 | + request = urllib.request.Request( |
| 135 | + NOTARY_URL + suffix, |
| 136 | + data=json.dumps(body).encode() if body is not None else None, |
| 137 | + headers={ |
| 138 | + "Authorization": f"Bearer {token}", |
| 139 | + "Content-Type": "application/json", |
| 140 | + }, |
| 141 | + method="POST" if body is not None else "GET", |
| 142 | + ) |
| 143 | + try: |
| 144 | + with urllib.request.urlopen(request, timeout=60) as response: |
| 145 | + return json.load(response) |
| 146 | + except urllib.error.HTTPError as error: |
| 147 | + raise RuntimeError(f"Apple Notary API returned HTTP {error.code}") from None |
| 148 | + |
| 149 | + |
| 150 | +def notarize(signed: Path) -> None: |
| 151 | + """Submit all targets' signed binaries together and wait for Apple's acceptance.""" |
| 152 | + key = notarization_key() |
| 153 | + with tempfile.TemporaryDirectory(dir=os.environ.get("RUNNER_TEMP")) as temporary: |
| 154 | + # Apple requires a supported container: ZIP, disk image, or signed flat |
| 155 | + # installer package. This ZIP is only for submission; distribution uses |
| 156 | + # the wheels and release archives assembled later. |
| 157 | + # https://developer.apple.com/documentation/security/customizing-the-notarization-workflow |
| 158 | + archive = Path(temporary) / "uv-notarization.zip" |
| 159 | + with ZipFile(archive, "w", compression=ZIP_DEFLATED) as output: |
| 160 | + for target in sorted(signed.iterdir()): |
| 161 | + for path in sorted(target.iterdir()): |
| 162 | + path.chmod(0o755) |
| 163 | + output.write(path, path.relative_to(signed)) |
| 164 | + |
| 165 | + # Include upload time in the budget so polling uses an unexpired token. |
| 166 | + deadline = time.monotonic() + 600 |
| 167 | + token = apple_token(key) |
| 168 | + submission = apple_json( |
| 169 | + token, |
| 170 | + body={ |
| 171 | + "submissionName": archive.name, |
| 172 | + "sha256": hashlib.sha256(archive.read_bytes()).hexdigest(), |
| 173 | + }, |
| 174 | + )["data"] |
| 175 | + submission_id = submission["id"] |
| 176 | + upload = submission["attributes"] |
| 177 | + print(f"Uploading Apple notarization submission {submission_id}", flush=True) |
| 178 | + s3 = boto3.client( |
| 179 | + "s3", |
| 180 | + aws_access_key_id=upload["awsAccessKeyId"], |
| 181 | + aws_secret_access_key=upload["awsSecretAccessKey"], |
| 182 | + aws_session_token=upload["awsSessionToken"], |
| 183 | + config=Config(s3={"use_accelerate_endpoint": True}), |
| 184 | + ) |
| 185 | + try: |
| 186 | + s3.upload_file(str(archive), upload["bucket"], upload["object"]) |
| 187 | + except (BotoCoreError, ClientError): |
| 188 | + raise RuntimeError("Apple notarization upload failed") from None |
| 189 | + |
| 190 | + while True: |
| 191 | + if time.monotonic() >= deadline: |
| 192 | + raise TimeoutError(f"Apple notarization timed out: {submission_id}") |
| 193 | + status = apple_json(token, f"/{submission_id}")["data"]["attributes"][ |
| 194 | + "status" |
| 195 | + ] |
| 196 | + if status != "In Progress": |
| 197 | + log_url = apple_json(token, f"/{submission_id}/logs")["data"][ |
| 198 | + "attributes" |
| 199 | + ]["developerLogUrl"] |
| 200 | + if not log_url.startswith("https://"): |
| 201 | + raise ValueError("Apple returned an invalid notarization log URL") |
| 202 | + with urllib.request.urlopen(log_url, timeout=60) as response: |
| 203 | + log = json.load(response) |
| 204 | + for issue in log.get("issues") or []: |
| 205 | + print( |
| 206 | + f"Apple notarization {issue['severity']}: " |
| 207 | + f"{issue['path']}: {issue['message']}", |
| 208 | + file=sys.stderr, |
| 209 | + ) |
| 210 | + if status != "Accepted": |
| 211 | + raise ValueError(f"Apple notarization {status}: {submission_id}") |
| 212 | + print(f"Apple notarization accepted: {submission_id}") |
| 213 | + return |
| 214 | + time.sleep(10) |
| 215 | + |
| 216 | + |
| 217 | +def main() -> None: |
| 218 | + """Notarize the signed executables from uv's protected release job.""" |
| 219 | + parser = argparse.ArgumentParser(description=__doc__) |
| 220 | + parser.add_argument("signed", type=Path) |
| 221 | + args = parser.parse_args() |
| 222 | + try: |
| 223 | + notarize(args.signed) |
| 224 | + except (KeyError, OSError, ValueError, RuntimeError, TimeoutError) as error: |
| 225 | + parser.exit(1, f"{error}\n") |
| 226 | + |
| 227 | + |
| 228 | +if __name__ == "__main__": |
| 229 | + main() |
0 commit comments