-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.py
More file actions
195 lines (141 loc) · 5.06 KB
/
updater.py
File metadata and controls
195 lines (141 loc) · 5.06 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import base64
import os
import re
import subprocess
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from trustifi import __version__
API_URL = "https://api.github.com/repos/chromium/chromium/contents/{path}?ref=main"
TEXTPROTO_PATH = "net/data/ssl/chrome_root_store/root_store.textproto"
CERTS_PATH = "net/data/ssl/chrome_root_store/root_store.certs"
VERSION_RE = re.compile(r"version_major\s*:\s*(\d+)")
PEM_RE = re.compile(r"-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----")
def run(cmd: list[str]) -> None:
subprocess.run(cmd, check=True, timeout=30)
def github_request(path: str) -> bytes:
path = path.lstrip("/")
url = API_URL.format(path=path)
headers = {
"Accept": "application/vnd.github+json",
}
token = os.environ.get("GH_TOKEN")
if token:
headers["Authorization"] = f"Bearer {token}"
req = Request(url, headers=headers)
try:
with urlopen(req, timeout=30) as r:
import json
data = json.loads(r.read().decode("utf-8"))
if data.get("encoding") != "base64":
raise ValueError("Unexpected encoding from GitHub API")
return base64.b64decode(data["content"])
except HTTPError as e:
raise RuntimeError(f"GitHub API request failed: {e}") from e
def get_file(path: str) -> str:
return github_request(path).decode("utf-8")
def extract_version(text: str) -> str:
match = VERSION_RE.search(text)
if not match:
raise ValueError("Could not find version_major")
return match.group(1)
def extract_pems(text: str) -> list[str]:
pems = PEM_RE.findall(text)
if not pems:
raise ValueError("No certificates found")
return pems
def write_certs(pems: list[str], path: str) -> None:
with open(path, "w", encoding="utf-8") as f:
f.write("\n\n".join(pems))
print(f"Wrote {len(pems)} certificates to {path}")
def update_version_file(path: str, new_version: str) -> None:
with open(path, "r+", encoding="utf-8") as f:
content = f.read()
content = re.sub(
r'__version__\s*=\s*["\']\d+["\']',
f'__version__ = "{new_version}"',
content,
)
f.seek(0)
f.truncate()
f.write(content)
print(f"Updated trustifi version to {new_version}")
def git_commit_and_tag(version: str) -> None:
run(["git", "config", "user.name", "github-actions[bot]"])
run(
[
"git",
"config",
"user.email",
"41898282+github-actions[bot]@users.noreply.github.com",
]
)
run(["git", "add", "trustifi/cacert.pem", "trustifi/__init__.py"])
run(
[
"git",
"commit",
"--author=AYMENJD <53928879+AYMENJD@users.noreply.github.com>",
"-m",
f"Update version to {version}.",
]
)
run(["git", "push"])
run(["git", "tag", f"v{version}"])
run(["git", "push", "--tags"])
def test_cacert() -> None:
print("Testing cacert.pem...\n")
import socket
import ssl
from trustifi import contents, where
assert "-----BEGIN CERTIFICATE-----" in contents(), "cacert.pem seems invalid"
cafile = where()
ctx = ssl.create_default_context(cafile=cafile)
tests = [
# (host, should_pass)
("expired.badssl.com", False),
("self-signed.badssl.com", False),
("wrong.host.badssl.com", False),
("untrusted-root.badssl.com", False),
("google.com", True),
("cloudflare.com", True),
("github.com", True),
]
failures = 0
for host, should_pass in tests:
try:
with socket.create_connection((host, 443), timeout=10) as sock:
with ctx.wrap_socket(sock, server_hostname=host):
if not should_pass:
print(f"FAIL: {host} (unexpected PASS)")
failures += 1
else:
print(f"PASS: {host}")
except ssl.SSLError:
if should_pass:
print(f"FAIL: {host} (unexpected FAIL)")
failures += 1
else:
print(f"FAIL: {host} (expected)")
except OSError as exc:
print(f"SKIP: {host} (network error: {exc})")
if failures:
print(f"\nCA test finished with {failures} failure(s)\n")
return
print("All CA tests passed.\n")
def main() -> None:
textproto = get_file(TEXTPROTO_PATH)
remote_version = extract_version(textproto)
if remote_version == __version__:
print("Already up to date.")
return
certs_content = get_file(CERTS_PATH)
pems = extract_pems(certs_content)
write_certs(pems, "trustifi/cacert.pem")
test_cacert()
update_version_file("trustifi/__init__.py", remote_version)
git_commit_and_tag(remote_version)
if "GITHUB_ENV" in os.environ:
with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as env_file:
print("TRUSTIFI_UPDATED=1", file=env_file)
if __name__ == "__main__":
main()