Repository navigation
Expand file tree
/
Copy pathrelease_plugin.py
More file actions
112 lines (96 loc) · 5.33 KB
/
Copy pathrelease_plugin.py
File metadata and controls
112 lines (96 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""Build already passed without secrets. Sign only in the protected release job.
Existing release assets are reused, checked against the build, and never overwritten.
"""
import base64
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import zipfile
def run(*args, **kwargs):
return subprocess.run(args, check=True, **kwargs)
def gh(*args):
return json.loads(subprocess.check_output(['gh', 'api', *args]))
def manifest(jar):
with zipfile.ZipFile(jar) as archive:
text = re.sub(r'\r?\n ', '', archive.read('META-INF/MANIFEST.MF').decode())
return dict(line.split(': ', 1) for line in text.splitlines() if ': ' in line)
def content(jar):
# Signing changes manifest digests and adds signature files; all other entries must match.
with zipfile.ZipFile(jar) as archive:
return {name: archive.read(name) for name in archive.namelist()
if not name.endswith('/') and name.upper() != 'META-INF/MANIFEST.MF'
and not re.fullmatch(r'META-INF/[^/]+\.(SF|RSA|DSA|EC)', name, re.I)}
def main():
repository = os.environ['GITHUB_REPOSITORY']
tag = os.environ['GITHUB_REF_NAME']
assert repository.startswith('gameyfin/') and os.environ['GITHUB_REF_PROTECTED'] == 'true'
publish(repository, tag)
def checked_artifact(repository, tag):
assert repository.startswith('gameyfin/plugin-')
jars = list(Path('unsigned').glob('*.jar'))
assert len(jars) == 1, 'Expected exactly one plugin JAR'
unsigned = jars[0]
attrs = manifest(unsigned)
version = attrs['Plugin-Version']
assert re.fullmatch(r'\d+\.\d+\.\d+', version) and tag == f'v{version}', 'Tag/manifest version mismatch'
assert attrs.get('Plugin-Requires') and attrs['Plugin-Requires'] != '*', 'Compatibility baseline required'
assert attrs['Plugin-Url'] == f'https://github.com/{repository}'
assert attrs['Plugin-Id'].startswith('org.gameyfin.')
return unsigned, attrs, version
def publish(repository, tag):
unsigned, attrs, version = checked_artifact(repository, tag)
output = Path('signed'); output.mkdir()
signed = output / unsigned.name
result = subprocess.run(['gh', 'api', f'repos/{repository}/releases/tags/{tag}'], capture_output=True, text=True)
if result.returncode and 'HTTP 404' not in result.stderr:
raise RuntimeError(result.stderr)
release = json.loads(result.stdout) if result.returncode == 0 else None
# A tag is protected by the repository ruleset; publishing never modifies it.
if release:
existing = next((item for item in release['assets'] if item['name'] == signed.name), None)
if existing:
run('gh', 'release', 'download', tag, '--repo', repository, '--pattern', signed.name, '--dir', str(output))
assert content(unsigned) == content(signed), 'Immutable release differs from this build'
previous = manifest(signed)
assert all(previous.get(k) == v for k, v in attrs.items()), 'Immutable manifest differs from this build'
with tempfile.TemporaryDirectory() as temporary:
temporary = Path(temporary)
truststore = temporary / 'verify.p12'
run('keytool', '-importcert', '-noprompt', '-alias', 'gameyfin-plugins', '-file',
str(Path(__file__).resolve().parent.parent / 'certs' / 'gameyfin-plugins.pem'), '-keystore', str(truststore), '-storepass', 'changeit')
if not signed.exists():
shutil.copyfile(unsigned, signed)
keystore = temporary / 'signing.jks'
keystore.write_bytes(base64.b64decode(os.environ['GAMEYFIN_KEYSTORE_BASE64'], validate=True))
keystore.chmod(0o600)
run('jarsigner', '-keystore', str(keystore), '-storepass:env', 'GAMEYFIN_KEYSTORE_PASSWORD',
str(signed), 'gameyfin-plugins')
run('jarsigner', '-verify', '-strict', '-keystore', str(truststore), '-storepass', 'changeit', str(signed))
# Must be computed after signing (or after downloading the immutable signed asset).
with signed.open('rb') as stream:
digest = hashlib.file_digest(stream, 'sha512').hexdigest()
checksum = signed.with_suffix(signed.suffix + '.sha512')
checksum.write_text(f'{digest} {signed.name}\n')
if not release:
run('gh', 'release', 'create', tag, '--repo', repository, '--verify-tag', '--title', tag, '--generate-notes')
release = gh(f'repos/{repository}/releases/tags/{tag}')
for asset in (signed, checksum):
if any(item['name'] == asset.name for item in release['assets']):
if asset == checksum:
with tempfile.TemporaryDirectory() as directory:
run('gh', 'release', 'download', tag, '--repo', repository, '--pattern', asset.name, '--dir', directory)
assert (Path(directory) / asset.name).read_bytes() == asset.read_bytes(), 'Immutable checksum differs'
continue
run('gh', 'release', 'upload', tag, str(asset), '--repo', repository)
Path('plugin-release.json').write_text(json.dumps({
'attributes': attrs, 'release': {'version': version, 'requires': attrs['Plugin-Requires'],
'url': f'https://github.com/{repository}/releases/download/{tag}/{signed.name}',
'sha512sum': digest, 'date': gh(f'repos/{repository}/releases/tags/{tag}')['published_at']}
}))
if __name__ == '__main__':
main()