Repository navigation
Expand file tree
/
Copy pathghwrap.py
More file actions
382 lines (309 loc) · 13.5 KB
/
Copy pathghwrap.py
File metadata and controls
382 lines (309 loc) · 13.5 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
"""Schemas and dataclasses for GitHub releases."""
import dataclasses
import hashlib
import io
import json
import shutil
import time
from dataclasses import dataclass, field
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from zipfile import ZipFile
import click
import requests
from github import Github
from github.GitRelease import GitRelease
from github.GitReleaseAsset import GitReleaseAsset
from github.Repository import Repository
from requests import Response
from .schemas import definitions
def get_gh_release(repo: Repository, release_name: str | None = None, tag_name: str | None = None) -> GitRelease | None:
"""Get a list of GitHub releases by repo."""
if not release_name and not tag_name:
raise ValueError("Must specify a release_name or tag_name")
releases = repo.get_releases()
for release in releases:
if (release_name and release_name == release.name) or (tag_name and tag_name == release.tag_name):
return release
return None
def load_zipped_gh_assets_with_metadata(url: str) -> tuple[str, dict[str, Any]]:
"""Download and unzip a GitHub assets."""
response = requests.get(url, timeout=30)
zipped_asset = ZipFile(io.BytesIO(response.content))
zipped_sha256 = hashlib.sha256(response.content).hexdigest()
assets: dict[str, Any] = {}
for zipped in zipped_asset.filelist:
if zipped.is_dir():
continue
contents = zipped_asset.read(zipped.filename)
sha256 = hashlib.sha256(contents).hexdigest()
assets[zipped.filename] = {
"contents": contents,
"metadata": {
"compress_size": zipped.compress_size,
# zipfile provides only a 6 tuple datetime; -1 means DST is unknown; 0's set tm_wday and tm_yday
"created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", (*zipped.date_time, 0, 0, -1)),
"sha256": sha256,
"size": zipped.file_size,
},
}
return zipped_sha256, assets
def load_json_gh_asset(url: str) -> dict[str, Any]:
"""Load and return the contents of a json asset file."""
response = requests.get(url, timeout=30)
response.raise_for_status()
return response.json()
def download_gh_asset(url: str, path: str, overwrite: bool = False) -> None:
"""Download and unzip a GitHub asset."""
zipped = requests.get(url, timeout=30)
z = ZipFile(io.BytesIO(zipped.content))
Path(path).mkdir(exist_ok=True)
if overwrite:
shutil.rmtree(path, ignore_errors=True)
z.extractall(path)
click.echo(f"files saved to {path}")
z.close()
_GIST_PATCH_FILE_LIMIT = 50
def _patch_gist_files(url: str, headers: dict[str, str], body: dict[str, Any], files: dict[str, Any]) -> Response:
"""PATCH gist files and include GitHub's response body on failure."""
response = requests.patch(url, headers=headers, json={**body, "files": files}, timeout=60)
_raise_for_status_with_body(response)
return response
def update_gist( # noqa: PLR0913, PLR0917
token: str,
file_map: dict[Path, str],
description: str,
gist_id: str,
public: bool = False,
pre_purge: bool = False,
) -> Response:
"""Update existing gist."""
url = f"https://api.github.com/gists/{gist_id}"
headers = {"accept": "application/vnd.github.v3+json", "Authorization": f"token {token}"}
body: dict[str, Any] = {
"description": description,
"public": public,
}
uploads = {path.name: {"content": contents} for path, contents in file_map.items()}
if not uploads:
raise ValueError("Cannot update gist with an empty file map")
deletions: dict[str, None] = {}
existing_keep = False
if pre_purge:
# GitHub deletes a gist file when its PATCH value is JSON null, not an empty object.
# Deleting every remaining file 422s, so seed a keep file first when none exist yet.
get_response = requests.get(url, headers=headers, timeout=30)
_raise_for_status_with_body(get_response)
existing_names = {str(name) for name in get_response.json()["files"]}
keep_names = set(uploads)
deletions = {name: None for name in existing_names if name not in keep_names}
existing_keep = bool(keep_names & existing_names)
remaining_uploads = dict(uploads)
response: Response | None = None
if deletions and not existing_keep:
seed_name, seed_body = next(iter(uploads.items()))
response = _patch_gist_files(url, headers, body, {seed_name: seed_body})
remaining_uploads = {name: payload for name, payload in uploads.items() if name != seed_name}
for chunk in batch_gist_files(deletions, _GIST_PATCH_FILE_LIMIT):
response = _patch_gist_files(url, headers, body, chunk)
for chunk in batch_gist_files(remaining_uploads, _GIST_PATCH_FILE_LIMIT):
response = _patch_gist_files(url, headers, body, chunk)
if response is None:
raise ValueError("Cannot update gist with an empty file map")
return response
def batch_gist_files(mapping: dict[str, Any], size: int) -> list[dict[str, Any]]:
"""Split a mapping into chunks so gist PATCH payloads stay under GitHub limits."""
items = list(mapping.items())
return [dict(items[index : index + size]) for index in range(0, len(items), size)]
def _raise_for_status_with_body(response: Response) -> None:
"""Raise HTTPError including the response body (GitHub 422 details live there)."""
try:
response.raise_for_status()
except requests.HTTPError as exc:
detail = (response.text or "").strip()
if detail:
raise requests.HTTPError(f"{exc} {detail}", response=response) from exc
raise
class GithubClient:
"""GitHub client wrapper."""
def __init__(self, token: str | None = None) -> None:
"""Get an unauthenticated client, verified authenticated client, or a default client."""
self.assert_github()
self.client = Github(token)
self.unauthenticated_client = Github()
self.__token = token
self.__authenticated_client = None
@classmethod
def assert_github(cls) -> None:
if not Github:
raise ModuleNotFoundError("Missing PyGithub - try running `pip3 install .[dev]`")
@property
def authenticated_client(self) -> Github:
if not self.__token:
raise ValueError("Token not defined! Re-instantiate with a token or use add_token method")
if not self.__authenticated_client:
self.__authenticated_client = Github(self.__token)
return self.__authenticated_client
def add_token(self, token: str) -> None:
self.__token = token
@dataclass
class AssetManifestEntry:
compress_size: int
created_at: datetime
name: str
sha256: str
size: int
@dataclass
class AssetManifestMetadata:
relative_url: str
entries: dict[str, AssetManifestEntry]
zipped_sha256: definitions.Sha256
created_at: datetime = field(default_factory=lambda: datetime.now(UTC))
description: str | None = None # populated by GitHub release asset label
@dataclass
class ReleaseManifest:
assets: dict[str, AssetManifestMetadata]
assets_url: str
author: str # parsed from GitHub release metadata as: author[login]
created_at: str
html_url: str
id: int
name: str
published_at: str
url: str
zipball_url: str
tag_name: str | None = None
description: str | None = None # parsed from GitHub release metadata as: body
class ManifestManager:
"""Manifest handler for GitHub releases."""
def __init__(
self,
repo: str = "elastic/detection-rules",
release_name: str | None = None,
tag_name: str | None = None,
token: str | None = None,
) -> None:
self.repo_name = repo
self.release_name = release_name
self.tag_name = tag_name
self.gh_client = GithubClient(token)
self.has_token = token is not None
self.repo: Repository = self.gh_client.client.get_repo(repo)
release = get_gh_release(self.repo, release_name, tag_name)
if not release:
raise ValueError("No release info found")
self.release = release
if not self.release:
raise ValueError(f"No release found for {tag_name or release_name}")
if not self.release_name:
self.release_name = self.release.name
self.manifest_name = f"manifest-{self.release_name}.json"
self.assets = self._get_enriched_assets_from_release()
self.release_manifest = self._create()
self.__release_manifest_dict = dataclasses.asdict(self.release_manifest)
self.manifest_size = len(json.dumps(self.__release_manifest_dict))
@property
def release_manifest_fl(self) -> io.BytesIO:
return io.BytesIO(json.dumps(self.__release_manifest_dict, sort_keys=True).encode("utf-8"))
def _create(self) -> ReleaseManifest:
"""Create the manifest from GitHub asset metadata and file contents."""
assets = {}
for asset_name, asset_data in self.assets.items():
entries: dict[str, AssetManifestEntry] = {}
data = asset_data["data"]
metadata = asset_data["metadata"]
for file_name, file_data in data.items():
file_metadata = file_data["metadata"]
name = Path(file_name).name
file_metadata.update(name=name)
entry = AssetManifestEntry(**file_metadata)
entries[name] = entry
assets[asset_name] = AssetManifestMetadata(
metadata["browser_download_url"],
entries,
metadata["zipped_sha256"],
metadata["created_at"],
metadata["label"],
)
release_metadata = self._parse_release_metadata()
release_metadata.update(assets=assets)
return ReleaseManifest(**release_metadata)
def _parse_release_metadata(self) -> dict[str, Any]:
"""Parse relevant info from GitHub metadata for release manifest."""
ignore = ["assets"]
manual_set_keys = ["author", "description"]
keys = [f.name for f in dataclasses.fields(ReleaseManifest) if f.name not in ignore + manual_set_keys]
parsed = {k: self.release.raw_data[k] for k in keys}
parsed.update(description=self.release.raw_data["body"], author=self.release.raw_data["author"]["login"])
return parsed
def save(self) -> GitReleaseAsset:
"""Save manifest files."""
if not self.has_token:
raise ValueError("You must provide a token to save a manifest to a GitHub release")
asset = self.release.upload_asset_from_memory(self.release_manifest_fl, self.manifest_size, self.manifest_name)
click.echo(f"Manifest saved as {self.manifest_name} to {self.release.html_url}")
return asset
@classmethod
def load(
cls,
name: str,
repo_name: str = "elastic/detection-rules",
token: str | None = None,
) -> dict[str, Any] | None:
"""Load a manifest."""
gh_client = GithubClient(token)
repo = gh_client.client.get_repo(repo_name)
release = get_gh_release(repo, tag_name=name)
if not release:
raise ValueError("No release info found")
for asset in release.get_assets():
if asset.name == f"manifest-{name}.json":
return load_json_gh_asset(asset.browser_download_url)
return None
@classmethod
def load_all(
cls,
repo_name: str = "elastic/detection-rules",
token: str | None = None,
) -> tuple[dict[str, dict[str, Any]], list[str]]:
"""Load a consolidated manifest."""
gh_client = GithubClient(token)
repo = gh_client.client.get_repo(repo_name)
consolidated: dict[str, dict[str, Any]] = {}
missing: set[str] = set()
for release in repo.get_releases():
name = release.tag_name
asset = next((a for a in release.get_assets() if a.name == f"manifest-{name}.json"), None)
if not asset:
missing.add(name)
else:
consolidated[name] = load_json_gh_asset(asset.browser_download_url)
return consolidated, list(missing)
@classmethod
def get_existing_asset_hashes(
cls,
repo: str = "elastic/detection-rules",
token: str | None = None,
) -> dict[str, Any]:
"""Load all assets with their hashes, by release."""
flat: dict[str, Any] = {}
consolidated, _ = cls.load_all(repo_name=repo, token=token)
for release, data in consolidated.items():
for asset in data["assets"].values():
flat_release = flat[release] = {}
for asset_name, asset_data in asset["entries"].items():
flat_release[asset_name] = asset_data["sha256"]
return flat
def _get_enriched_assets_from_release(self) -> dict[str, Any]:
"""Get assets and metadata from a GitHub release."""
assets: dict[str, Any] = {}
for asset in [a.raw_data for a in self.release.get_assets()]:
zipped_sha256, data = load_zipped_gh_assets_with_metadata(asset["browser_download_url"])
asset.update(zipped_sha256=zipped_sha256)
assets[asset["name"]] = {"metadata": asset, "data": data}
return assets