Skip to content

Commit 084bac7

Browse files
authored
[confcom] Remove the dependency on OPA (#9464)
* Remove the depedency on OPA * Bump version * Organise imports
1 parent 7de6350 commit 084bac7

File tree

4 files changed

+58
-82
lines changed

4 files changed

+58
-82
lines changed

src/confcom/HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Release History
44
===============
55

6+
1.4.5
7+
++++++
8+
* Drop the dependency on OPA
9+
610
1.4.4
711
++++++
812
* Improve the package building process

src/confcom/azext_confcom/lib/opa.py

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/confcom/azext_confcom/lib/serialization.py

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
# Licensed under the MIT License. See License.txt in the project root for license information.
55
# --------------------------------------------------------------------------------------------
66

7-
from dataclasses import asdict
87
import json
9-
from pathlib import Path
8+
import re
9+
10+
from dataclasses import asdict
1011
from textwrap import dedent
1112
from typing import Union
1213

13-
from azext_confcom.lib.opa import opa_eval
1414
from azext_confcom.lib.policy import Container, FragmentReference, Fragment, Policy
15-
import re
1615

1716

1817
# This is a single entrypoint for serializing both Policy and Fragment objects
@@ -80,21 +79,58 @@ def fragment_serialize(fragment: Fragment):
8079
def policy_deserialize(file_path: str):
8180

8281
with open(file_path, 'r') as f:
83-
content = f.read()
84-
85-
package_match = re.search(r'package\s+(\S+)', content)
86-
package_name = package_match.group(1)
87-
88-
PolicyType = Policy if package_name == "policy" else Fragment
89-
90-
raw_json = opa_eval(Path(file_path), f"data.{package_name}")["result"][0]["expressions"][0]["value"]
91-
92-
raw_fragments = raw_json.pop("fragments", [])
93-
raw_containers = raw_json.pop("containers", [])
82+
content = f.readlines()
83+
84+
def _brace_delta(line: str) -> int:
85+
delta = 0
86+
for char in line:
87+
if char in ['{', '[', '(']:
88+
delta += 1
89+
elif char in ['}', ']', ')']:
90+
delta -= 1
91+
return delta
92+
93+
policy_json = {}
94+
line_idx = 0
95+
96+
while line_idx < len(content):
97+
line = content[line_idx]
98+
99+
packages_search = re.search(r'package\s+(\S+)', line)
100+
if packages_search:
101+
policy_json["package"] = packages_search.group(1)
102+
line_idx += 1
103+
continue
104+
105+
assignment = re.match(r"\s*(?P<name>[A-Za-z0-9_]+)\s*:=\s*(?P<expr>.*)", line)
106+
if assignment:
107+
name = assignment.group('name')
108+
expr = assignment.group('expr').strip()
109+
expr_parts = [expr]
110+
depth = _brace_delta(expr)
111+
112+
while depth > 0 and line_idx + 1 < len(content):
113+
line_idx += 1
114+
continuation = content[line_idx].strip()
115+
expr_parts.append(continuation)
116+
depth += _brace_delta(continuation)
117+
118+
full_expr = "\n".join(expr_parts).strip().rstrip(",")
119+
try:
120+
policy_json[name] = json.loads(full_expr)
121+
except json.JSONDecodeError:
122+
# Skip non-literal expressions (e.g. data.framework bindings)
123+
...
124+
125+
line_idx += 1
126+
127+
PolicyType = Policy if policy_json.get("package") == "policy" else Fragment
128+
129+
raw_fragments = policy_json.pop("fragments", [])
130+
raw_containers = policy_json.pop("containers", [])
94131

95132
return PolicyType(
96-
package=package_name,
133+
**policy_json,
97134
fragments=[FragmentReference(**fragment) for fragment in raw_fragments],
98135
containers=[Container(**container) for container in raw_containers],
99-
**raw_json
100136
)

src/confcom/setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from azext_confcom.rootfs_proxy import SecurityPolicyProxy
1212
from azext_confcom.kata_proxy import KataPolicyGenProxy
1313
from azext_confcom.cose_proxy import CoseSignToolProxy
14-
from azext_confcom.lib.opa import opa_get
1514

1615
try:
1716
from azure_bdist_wheel import cmdclass
@@ -20,7 +19,7 @@
2019

2120
logger.warn("Wheel is not available, disabling bdist_wheel hook")
2221

23-
VERSION = "1.4.4"
22+
VERSION = "1.4.5"
2423

2524
# The full list of classifiers is available at
2625
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
@@ -49,7 +48,6 @@
4948
SecurityPolicyProxy.download_binaries()
5049
KataPolicyGenProxy.download_binaries()
5150
CoseSignToolProxy.download_binaries()
52-
opa_get()
5351

5452
with open("README.md", "r", encoding="utf-8") as f:
5553
README = f.read()

0 commit comments

Comments
 (0)