fix(kernel): authenticate the DriverKit header listing when a token is set
prepare-xnu-source.py lists Apple's xnu tree through the GitHub API without credentials. Hosted runners share the anonymous per-address quota and the bootstrap pass stops with HTTP 403 rate limit exceeded. Send GITHUB_TOKEN or GH_TOKEN, when set, to api.github.com and raw.githubusercontent.com only.
This commit is contained in:
1 parent
dc21175e5a
commit
d12c96bf2c
1 file changed
+16
-4
@@ -5,6 +5,7 @@ import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
# Tags of the Apple sources that still carry the headers newer xnu omits; feeds.toml sets them for the unixnu driver.
|
||||
@@ -13,6 +14,17 @@ TRUST_TAG = os.environ.get('UNIXNU_TRUSTCACHE_TAG', 'xnu-8796.101.5')
|
||||
DESTINATION = Path('.cache/xnu-work/iokit/DriverKit')
|
||||
CACHE = Path(f'.cache/sources/xnu-driverkit-{TAG.removeprefix("xnu-")}.json')
|
||||
URL = f'https://api.github.com/repos/apple-oss-distributions/xnu/contents/iokit/DriverKit?ref={TAG}'
|
||||
# Anonymous GitHub API calls share a small per-address quota that hosted CI runners exhaust (HTTP 403);
|
||||
# a token in GITHUB_TOKEN or GH_TOKEN is sent to GitHub hosts only.
|
||||
TOKEN = os.environ.get('GITHUB_TOKEN') or os.environ.get('GH_TOKEN')
|
||||
|
||||
|
||||
def fetch(url):
|
||||
request = urllib.request.Request(url, headers={'User-Agent': 'unixnu-prepare-xnu-source'})
|
||||
if TOKEN and urllib.parse.urlsplit(url).hostname in ('api.github.com', 'raw.githubusercontent.com'):
|
||||
request.add_header('Authorization', f'Bearer {TOKEN}')
|
||||
with urllib.request.urlopen(request, timeout=30) as response:
|
||||
return response.read()
|
||||
|
||||
|
||||
def restore(entry, destination=DESTINATION, tag=TAG):
|
||||
@@ -22,7 +34,7 @@ def restore(entry, destination=DESTINATION, tag=TAG):
|
||||
target = destination / name
|
||||
if target.exists():
|
||||
return None
|
||||
data = urllib.request.urlopen(entry['download_url'], timeout=30).read()
|
||||
data = fetch(entry['download_url'])
|
||||
digest = hashlib.sha1(b'blob ' + str(len(data)).encode() + b'\0' + data).hexdigest()
|
||||
if digest != entry['sha']:
|
||||
raise ValueError(f'Git blob hash mismatch: {entry["name"]}')
|
||||
@@ -37,12 +49,12 @@ if __name__ == '__main__':
|
||||
raise SystemExit('Create the local XNU work source before preparing headers')
|
||||
if not CACHE.exists():
|
||||
CACHE.parent.mkdir(parents=True, exist_ok=True)
|
||||
CACHE.write_bytes(urllib.request.urlopen(URL, timeout=30).read())
|
||||
CACHE.write_bytes(fetch(URL))
|
||||
entries = json.loads(CACHE.read_text())
|
||||
crypto_cache = CACHE.with_name(f'xnu-driverkit-crypto-{TAG.removeprefix("xnu-")}.json')
|
||||
if not crypto_cache.exists():
|
||||
crypto_url = URL.replace('/DriverKit?', '/DriverKit/crypto?')
|
||||
crypto_cache.write_bytes(urllib.request.urlopen(crypto_url, timeout=30).read())
|
||||
crypto_cache.write_bytes(fetch(crypto_url))
|
||||
for entry in json.loads(crypto_cache.read_text()):
|
||||
entry['relative'] = 'crypto/' + entry['name']
|
||||
entries.append(entry)
|
||||
@@ -53,7 +65,7 @@ if __name__ == '__main__':
|
||||
if not trust_cache.exists():
|
||||
url = ('https://api.github.com/repos/apple-oss-distributions/xnu/'
|
||||
f'contents/EXTERNAL_HEADERS/TrustCache?ref={trust_tag}')
|
||||
trust_cache.write_bytes(urllib.request.urlopen(url, timeout=30).read())
|
||||
trust_cache.write_bytes(fetch(url))
|
||||
trust_destination = Path('.cache/xnu-work/EXTERNAL_HEADERS/TrustCache')
|
||||
def restore_trust(entry):
|
||||
return restore(entry, trust_destination, trust_tag)
|
||||
|
||||
Reference in new issue
Block a user