Skip to content

Commit 3b449e6

Browse files
committed
upgrade opa version
1 parent ebc6028 commit 3b449e6

File tree

5 files changed

+133
-4
lines changed

5 files changed

+133
-4
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.analysis.typeCheckingMode": "basic"
3+
}

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2121

2222
http_archive(
2323
name = "rules_opa",
24-
sha256 = "510c9e0a2f556ea443a7da567d84e76b3ebc7aea48665109f35c7029d9a6d56e",
25-
strip_prefix = "rules_opa-0.2.0",
26-
url = "https://github.com/ticketmaster/rules_opa/archive/refs/tags/v0.2.0.tar.gz",
24+
sha256 = "<SHA256>",
25+
strip_prefix = "rules_opa-<VERSION>",
26+
url = "https://github.com/ticketmaster/rules_opa/archive/refs/tags/<VERSION>.tar.gz",
2727
)
2828

2929
load("@rules_opa//opa:deps.bzl", "opa_register_toolchains", "opa_rules_dependencies")
@@ -54,3 +54,11 @@ opa_test(
5454
bundle = ":simple",
5555
)
5656
```
57+
58+
## Upgrade
59+
60+
To upgrade the opa version, run the following command
61+
62+
```shell
63+
bazel run -- //tools:opa_upgrade --version <version>
64+
```

opa/private/opa_rules_dependencies.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
22
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
33

4+
DEFAULT_VERSION = "0.57.1"
5+
46
_OPA_SHA256 = {
7+
"0.57.1": {
8+
"opa_darwin_amd64": "54a2d229638baddb0ac6f7c283295e547e6f491ab2ddcaf714fa182427e8421d",
9+
"opa_darwin_arm64_static": "367adba9c1380297c87a83019965a28bb0f33fe7c0854ff6beedb4aa563e4b4f",
10+
"opa_linux_amd64": "5212d513dad9bd90bc67743d7812e5ec7019b2a994f30c0d8dbb2b2c6772f094",
11+
"opa_linux_amd64_static": "59e8c6ef9ae2f95b76aa79344eb81ca6f3950a0fd7a23534c4d7065f42fda99f",
12+
"opa_linux_arm64_static": "6d581ef6f9a066c0d2a36f3cb7ee605ec8195e49631121d1707248549758806b",
13+
"opa_windows_amd64": "9a6d3ef2279760efbcead6a7095393e04adaa1be3c7458eb62a2b79d93df4bc3",
14+
"opa_capabilities_json": "3a99b25a99f484b9de2037ef459af50cfa3c2da01219d38ddb049ad0c2384411",
15+
"opa_builtin_metadata_json": "5e9aeb1048a49e9fc04c694be53a4eead8a1b5f4acff71d14b1610d84fb347b4",
16+
},
517
"0.54.0": {
618
"opa_darwin_amd64": "a33e829306cd2210ed743da7f4f957588ea350a184bb6ecbb7cbfd77ae7ca401",
719
"opa_darwin_arm64_static": "74500746e5faf0deb60863f1a3d1e3eed96006ff8183940f1c13f1a47969059d",
@@ -24,7 +36,7 @@ _SUPPORTED_PLATFORMS = [
2436
]
2537

2638
def opa_rules_dependencies(
27-
version = "0.54.0"):
39+
version = DEFAULT_VERSION):
2840
"""Install the opa dependencies
2941
3042
Args:

tools/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ py_binary(
1313
visibility = ["//visibility:public"],
1414
)
1515

16+
py_binary(
17+
name = "opa_upgrade",
18+
srcs = ["opa_upgrade.py"],
19+
)
20+
1621
toolchain_type(name = "toolchain_type")
1722

1823
opa_toolchain(

tools/opa_upgrade.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import re
3+
from typing import TypeVar, Optional
4+
from argparse import ArgumentParser, Namespace
5+
from urllib.request import urlopen
6+
from pathlib import Path
7+
from hashlib import sha256
8+
9+
ARTIFACTS = [
10+
"opa_darwin_amd64",
11+
"opa_darwin_arm64_static",
12+
"opa_linux_amd64",
13+
"opa_linux_amd64_static",
14+
"opa_linux_arm64_static",
15+
"opa_windows_amd64",
16+
]
17+
18+
T = TypeVar('T')
19+
20+
21+
def required(value: Optional[T], name: str) -> T:
22+
if value is None:
23+
raise ValueError(f"Expected value for {name}")
24+
25+
return value
26+
27+
28+
def get_sha256(artifact_name: str, version: str) -> str:
29+
ext = ".exe" if "windows" in artifact_name else ""
30+
31+
with urlopen(f"https://github.com/open-policy-agent/opa/releases/download/v{version}/{artifact_name}{ext}.sha256") as res:
32+
return res.read().decode().split(' ')[0]
33+
34+
35+
def get_sha256_file(file: str, version: str) -> str:
36+
file_url = f"https://raw.githubusercontent.com/open-policy-agent/opa/v{version}/{file}"
37+
38+
with urlopen(file_url) as res:
39+
return sha256(res.read()).hexdigest()
40+
41+
42+
def main(args: Namespace):
43+
d = dict([
44+
(artifact_name, get_sha256(artifact_name, args.version))
45+
for artifact_name in ARTIFACTS
46+
])
47+
48+
bzl_path = WORKSPACE_ROOT.joinpath(
49+
'opa', 'private', 'opa_rules_dependencies.bzl')
50+
51+
with open(bzl_path) as bzl_file:
52+
bzl_content = bzl_file.read()
53+
54+
version_match = re.search(
55+
r"DEFAULT_VERSION\s*=\s*[\"'](.*?)[\"']", bzl_content)
56+
57+
if version_match is None:
58+
raise ValueError(f"Could not find DEFAULT_VERSION in file {bzl_path}")
59+
60+
start_version = version_match.start(1)
61+
end_version = version_match.end(1)
62+
63+
bzl_content = bzl_content[:start_version] + \
64+
args.version + bzl_content[end_version:]
65+
66+
dict_match = re.search(
67+
r"^_OPA_SHA256\s*=\s*{\s*$", bzl_content, re.MULTILINE)
68+
69+
if dict_match is None:
70+
raise ValueError(f"Could not find _OPA_SHA256 in file {bzl_path}")
71+
72+
bzl_content = bzl_content[:dict_match.end(
73+
)] + f"\n \"{args.version}\": {{\n" + '\n'.join([
74+
f" \"{artifact_name}\": \"{sha256}\","
75+
for artifact_name, sha256 in d.items()
76+
] + [
77+
f" \"opa_capabilities_json\": \"{get_sha256_file('capabilities.json', args.version)}\",",
78+
f" \"opa_builtin_metadata_json\": \"{get_sha256_file('builtin_metadata.json', args.version)}\",",
79+
]) + "\n }," + bzl_content[dict_match.end():]
80+
81+
with open(bzl_path, "w", encoding="utf-8") as bzl_file:
82+
bzl_file.write(bzl_content)
83+
84+
85+
def get_workspace_root(wd: Path) -> Path:
86+
while not wd.joinpath("WORKSPACE").exists():
87+
wd = wd.parent
88+
89+
return wd
90+
91+
92+
BUILD_WORKING_DIRECTORY = Path(
93+
required(os.getenv("BUILD_WORKING_DIRECTORY"), "BUILD_WORKING_DIRECTORY"))
94+
WORKSPACE_ROOT = get_workspace_root(BUILD_WORKING_DIRECTORY)
95+
96+
if __name__ == '__main__':
97+
98+
parser = ArgumentParser()
99+
parser.add_argument('--version', '-v', required=True)
100+
101+
main(parser.parse_args())

0 commit comments

Comments
 (0)