|
3 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 |
|
| 6 | +import hashlib |
6 | 7 | import os |
7 | 8 | import platform |
8 | 9 | import stat |
|
12 | 13 | import requests |
13 | 14 | from azext_confcom.config import DATA_FOLDER |
14 | 15 | from azext_confcom.errors import eprint |
| 16 | +from azext_confcom.lib.paths import get_binaries_dir, get_data_dir |
15 | 17 |
|
16 | 18 | host_os = platform.system() |
17 | 19 | machine = platform.machine() |
18 | 20 |
|
19 | 21 |
|
| 22 | +_binaries_dir = get_binaries_dir() |
| 23 | +_kata_binaries = { |
| 24 | + "Linux": { |
| 25 | + "path": _binaries_dir / "genpolicy-linux", |
| 26 | + "url": "https://github.com/microsoft/kata-containers/releases/download/3.2.0.azl3.genpolicy3/genpolicy", |
| 27 | + "sha256": "4cd497ca5e995ddacb53af4da47449c16291aea62e9f8b8ee0fe36ca8d41fe66", |
| 28 | + }, |
| 29 | + "Windows": { |
| 30 | + "path": _binaries_dir / "genpolicy-windows.exe", |
| 31 | + "url": "https://github.com/microsoft/kata-containers/releases/download/3.2.0.azl1.genpolicy0/genpolicy.exe", |
| 32 | + "sha256": "caa9d8ee21b5819cc42b5c0967b14e166c715f6d4c87b574edabeaaeebf3573c", |
| 33 | + }, |
| 34 | +} |
| 35 | +_data_dir = get_data_dir() |
| 36 | +_kata_data = [ |
| 37 | + { |
| 38 | + "path": _data_dir / "genpolicy-settings.json", |
| 39 | + "url": "https://github.com/microsoft/kata-containers/releases/download/3.2.0.azl3.genpolicy3/genpolicy-settings.json", # pylint: disable=line-too-long |
| 40 | + "sha256": "c38be1474b133d49800a43bd30c40e7585b5f302179a307f9c6d89f195daee94", |
| 41 | + }, |
| 42 | + { |
| 43 | + "path": _data_dir / "rules.rego", |
| 44 | + "url": "https://github.com/microsoft/kata-containers/releases/download/3.2.0.azl3.genpolicy3/rules.rego", |
| 45 | + "sha256": "2ca6c0e9617f97a922724112bd738fd73881d35b9ae5d31d573f0871d1ecf897", |
| 46 | + }, |
| 47 | +] |
| 48 | + |
| 49 | + |
20 | 50 | class KataPolicyGenProxy: # pylint: disable=too-few-public-methods |
21 | 51 | # static variable to cache layer hashes between container groups |
22 | 52 | layer_cache = {} |
23 | 53 |
|
24 | 54 | @staticmethod |
25 | 55 | def download_binaries(): |
26 | | - dir_path = os.path.dirname(os.path.realpath(__file__)) |
27 | | - |
28 | | - bin_folder = os.path.join(dir_path, "bin") |
29 | | - if not os.path.exists(bin_folder): |
30 | | - os.makedirs(bin_folder) |
31 | | - |
32 | | - data_folder = os.path.join(dir_path, "data") |
33 | | - if not os.path.exists(data_folder): |
34 | | - os.makedirs(data_folder) |
35 | | - |
36 | | - # get the most recent release artifacts from github |
37 | | - r = requests.get("https://api.github.com/repos/microsoft/kata-containers/releases") |
38 | | - r.raise_for_status() |
39 | | - bin_flag = False |
40 | | - needed_assets = ["genpolicy", "genpolicy.exe"] |
41 | | - # search for genpolicy in the assets from kata-container releases |
42 | | - for release in r.json(): |
43 | | - is_target = ( |
44 | | - "genpolicy" in release.get("tag_name") and |
45 | | - not release.get("draft") and |
46 | | - not release.get("prerelease") |
47 | | - ) |
48 | | - if is_target: |
49 | | - # these should be newest to oldest |
50 | | - for asset in release["assets"]: |
51 | | - # download the file if it contains genpolicy |
52 | | - if asset["name"] in needed_assets: |
53 | | - # say which version we're downloading |
54 | | - print(f"Downloading genpolicy version {release['tag_name']}") |
55 | | - save_name = "" |
56 | | - if ".exe" in asset["name"]: |
57 | | - save_name = "genpolicy-windows.exe" |
58 | | - else: |
59 | | - save_name = "genpolicy-linux" |
60 | | - bin_flag = True |
61 | | - # get the download url for the genpolicy file |
62 | | - exe_url = asset["browser_download_url"] |
63 | | - # download the file |
64 | | - r = requests.get(exe_url) |
65 | | - r.raise_for_status() |
66 | | - # save the file to the bin folder |
67 | | - with open(os.path.join(bin_folder, save_name), "wb") as f: |
68 | | - f.write(r.content) |
69 | | - |
70 | | - # download the rules.rego and genpolicy-settings.json files |
71 | | - if asset["name"] == "rules.rego" or asset["name"] == "genpolicy-settings.json": |
72 | | - # download the rules.rego file |
73 | | - exe_url = asset["browser_download_url"] |
74 | | - # download the file |
75 | | - r = requests.get(exe_url) |
76 | | - # save the file to the data folder |
77 | | - with open(os.path.join(data_folder, asset["name"]), "wb") as f: |
78 | | - f.write(r.content) |
79 | | - if bin_flag: |
80 | | - break |
| 56 | + |
| 57 | + for binary_info in list(_kata_binaries.values()) + _kata_data: |
| 58 | + kata_fetch_resp = requests.get(binary_info["url"], verify=True) |
| 59 | + kata_fetch_resp.raise_for_status() |
| 60 | + |
| 61 | + assert hashlib.sha256(kata_fetch_resp.content).hexdigest() == binary_info["sha256"] |
| 62 | + |
| 63 | + with open(binary_info["path"], "wb") as f: |
| 64 | + f.write(kata_fetch_resp.content) |
81 | 65 |
|
82 | 66 | def __init__(self): |
83 | 67 | script_directory = os.path.dirname(os.path.realpath(__file__)) |
|
0 commit comments