opa-py-wasm 1.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Intuit Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,212 @@
1
+ Metadata-Version: 2.4
2
+ Name: opa-py-wasm
3
+ Version: 1.2.1
4
+ Summary: In-process, thread-safe Python SDK for evaluating OPA policies compiled to WebAssembly, built on wasmtime.py
5
+ Keywords: opa,open-policy-agent,wasm,webassembly,policy,authorization
6
+ Author: Sanjana Goel, Ravi Chauhan
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Topic :: Security
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Typing :: Typed
21
+ Requires-Dist: wasmtime>=46.0.0,<47.0.0
22
+ Requires-Dist: pyyaml>=6.0 ; extra == 'yaml'
23
+ Requires-Python: >=3.10, <4.0
24
+ Project-URL: Homepage, https://github.com/intuit/opa-py-wasm
25
+ Project-URL: Repository, https://github.com/intuit/opa-py-wasm
26
+ Project-URL: Issues, https://github.com/intuit/opa-py-wasm/issues
27
+ Project-URL: Changelog, https://github.com/intuit/opa-py-wasm/blob/main/CHANGELOG.md
28
+ Provides-Extra: yaml
29
+ Description-Content-Type: text/markdown
30
+
31
+ # opa-py-wasm
32
+
33
+ ![Supported Python versions](https://shields.io/badge/python-3.10_|_3.11_|_3.12_|_3.13_|_3.14-green?logo=python)
34
+ [![CI](https://github.com/intuit/opa-py-wasm/actions/workflows/ci.yml/badge.svg)](https://github.com/intuit/opa-py-wasm/actions/workflows/ci.yml)
35
+ ![Coverage](https://img.shields.io/badge/coverage-99%25-brightgreen)
36
+ [![PyPI](https://img.shields.io/pypi/v/opa-py-wasm.svg)](https://pypi.org/project/opa-py-wasm/)
37
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/intuit/opa-py-wasm/blob/main/LICENSE)
38
+
39
+ `opa-py-wasm` is an in-process, thread-safe Python SDK for evaluating
40
+ [Open Policy Agent](https://www.openpolicyagent.org/) policies compiled to
41
+ WebAssembly, built on [`wasmtime.py`](https://github.com/bytecodealliance/wasmtime-py).
42
+ It requires no OPA server, no OPA binary, no subprocess, and no network at
43
+ runtime. It is functionally comparable to StyraOSS `opa-java-wasm`, with a
44
+ Pythonic API and explicit thread-safety guarantees.
45
+
46
+ > **Import name:** the package installs as `opa-py-wasm` and is imported as
47
+ > `opapywasm`.
48
+
49
+ ## Installation
50
+
51
+ ```shell
52
+ pip install opa-py-wasm
53
+ ```
54
+
55
+ Or with [uv](https://docs.astral.sh/uv/):
56
+
57
+ ```shell
58
+ uv add opa-py-wasm
59
+ ```
60
+
61
+ Requires Python 3.10 or newer. The only runtime dependency is
62
+ [`wasmtime`](https://pypi.org/project/wasmtime/).
63
+
64
+ ## Usage
65
+
66
+ ```python
67
+ from opapywasm import UNDEFINED, OpaWasmPolicy, PolicyConfig
68
+
69
+ policy = OpaWasmPolicy.from_wasm_file(
70
+ "policy.wasm",
71
+ config=PolicyConfig(
72
+ pool_size=8,
73
+ default_entrypoint="authz/allow",
74
+ eval_timeout_seconds=30.0, # per-eval deadline (epoch interruption)
75
+ max_memory_pages=4096, # per-instance linear-memory cap (256 MiB)
76
+ ),
77
+ )
78
+
79
+ policy.set_data({"roles": {"alice": "admin", "bob": "viewer"}})
80
+
81
+ decision = policy.evaluate({"user": "alice", "action": "delete"}) # simplified decision
82
+ raw = policy.evaluate_raw({"user": "alice", "action": "delete"}) # full OPA result set
83
+
84
+ # `evaluate` distinguishes an undefined decision from a defined JSON null:
85
+ if decision is UNDEFINED:
86
+ ... # policy produced no result
87
+ elif decision is None:
88
+ ... # policy result was JSON null
89
+
90
+ # Custom host builtins (receive decoded Python args, return JSON-compatible values):
91
+ policy.register_builtin("my.custom", lambda x: {"ok": True})
92
+ ```
93
+
94
+ `OpaWasmPolicy` is a bounded, thread-safe pool — construct it once and call
95
+ `evaluate` from many threads. Runnable scripts are in [`examples/`](https://github.com/intuit/opa-py-wasm/tree/main/examples):
96
+ `basic_authz.py`, `pooled_eval.py`, `custom_builtin.py`, `yaml_builtin.py`.
97
+
98
+ For the YAML default builtins, install the optional extra:
99
+
100
+ ```shell
101
+ uv add "opa-py-wasm[yaml]"
102
+ ```
103
+
104
+ Start with the [**user guide**](https://github.com/intuit/opa-py-wasm/blob/main/docs/guide.md) — an end-to-end walkthrough
105
+ covering compiling a policy, result semantics, concurrency, data updates, memory
106
+ behaviour, host builtins, and a production adoption checklist.
107
+
108
+ See [`docs/`](https://github.com/intuit/opa-py-wasm/tree/main/docs) for the focused references: architecture, thread-safety,
109
+ the OPA Wasm ABI, the `opa-java-wasm` parity table, and
110
+ [capacity-planning guidance](https://github.com/intuit/opa-py-wasm/blob/main/docs/capacity_planning.md) for sizing pool size
111
+ and concurrency.
112
+
113
+ ### Test fixtures (dev only)
114
+
115
+ Compiled `.wasm` fixtures are committed under `tests/fixtures/wasm`, so the test
116
+ suite needs no OPA CLI. To regenerate them from the Rego sources in
117
+ `tests/fixtures/rego`, install the [OPA CLI](https://www.openpolicyagent.org/docs/latest/#running-opa)
118
+ and run:
119
+
120
+ ```shell
121
+ python scripts/build_fixtures.py # all fixtures
122
+ python scripts/build_fixtures.py allow_true # a single fixture
123
+ ```
124
+
125
+ ## Local Development
126
+
127
+ ### uv
128
+
129
+ This library uses [uv to manage Python dependencies](https://docs.astral.sh/uv/getting-started/features/#projects).
130
+ `brew` is The easiest way to install on macOS:
131
+
132
+ ```shell
133
+ brew install uv
134
+ ```
135
+
136
+ For additional installation options (e.g. setting the PATH, installing a specific version, etc),
137
+ see the installation docs:
138
+ https://docs.astral.sh/uv/getting-started/installation/
139
+
140
+ ## Python Versions Supported
141
+
142
+ Python 3.10 through 3.14 are supported and tested in CI.
143
+
144
+ To change which versions are supported and tested:
145
+ - Update "envlist" in "tox" section of [tox.ini](/tox.ini)
146
+ - Update "Supported Python versions" badge in [README.md](/README.md)
147
+ - Update "project.requires-python" in [pyproject.toml](/pyproject.toml) (if needed)
148
+ - Update "tool.black.target-version" in [pyproject.toml](/pyproject.toml) (optional)
149
+ - Update the matrix in [.github/workflows/ci.yml](/.github/workflows/ci.yml)
150
+
151
+ ### Virtual Environment
152
+
153
+ Create by running:
154
+
155
+ ```shell
156
+ uv sync --all-extras
157
+ ```
158
+
159
+ Run a command from the virtual environment, like code formatting:
160
+
161
+ ```shell
162
+ uv run black .
163
+ ```
164
+
165
+ To activate the virtual environment:
166
+
167
+ ```shell
168
+ source .venv/bin/activate
169
+ ```
170
+
171
+ For more information, refer to [uv's documentation](https://docs.astral.sh/uv/pip/environments/#using-a-virtual-environment).
172
+
173
+ ### Type checking (mypy)
174
+
175
+ This library supports Python type [annotation](https://peps.python.org/pep-0484/).
176
+ Types will be checked as part of the test suite (see below).
177
+ For more information, see the [mypy documentation](https://mypy.readthedocs.io/en/stable/getting_started.html#dynamic-vs-static-typing).
178
+
179
+ ### Testing
180
+
181
+ To run the test suite locally:
182
+ ```shell
183
+ uv run tox
184
+ ```
185
+
186
+ Coverage is enforced on every CI run by `fail_under = 90` in
187
+ [`tool.coverage.report`](https://github.com/intuit/opa-py-wasm/blob/main/pyproject.toml); the suite currently sits at 99%. The
188
+ coverage badge at the top of this file is static — if you change coverage
189
+ materially, update the percentage in that badge along with your change.
190
+
191
+ ## Releasing
192
+
193
+ Releases are cut by maintainers by pushing a `vX.Y.Z` tag, which triggers an
194
+ automated build and publish to PyPI. See [`RELEASING.md`](https://github.com/intuit/opa-py-wasm/blob/main/RELEASING.md) for
195
+ the process and the versioning policy. Published versions are listed on
196
+ [PyPI](https://pypi.org/project/opa-py-wasm/), and each release's changes are in
197
+ [`CHANGELOG.md`](https://github.com/intuit/opa-py-wasm/blob/main/CHANGELOG.md).
198
+
199
+ ## Contributing
200
+
201
+ Contributions are welcome — see the [Contribution Guidelines](https://github.com/intuit/opa-py-wasm/blob/main/CONTRIBUTING.md)
202
+ and our [Code of Conduct](https://github.com/intuit/opa-py-wasm/blob/main/CODE_OF_CONDUCT.md).
203
+
204
+ ## Support
205
+
206
+ Please open a [GitHub issue](https://github.com/intuit/opa-py-wasm/issues) for
207
+ bugs and feature requests, or start a
208
+ [discussion](https://github.com/intuit/opa-py-wasm/discussions) for questions.
209
+
210
+ ## License
211
+
212
+ Released under the [MIT License](https://github.com/intuit/opa-py-wasm/blob/main/LICENSE).
@@ -0,0 +1,182 @@
1
+ # opa-py-wasm
2
+
3
+ ![Supported Python versions](https://shields.io/badge/python-3.10_|_3.11_|_3.12_|_3.13_|_3.14-green?logo=python)
4
+ [![CI](https://github.com/intuit/opa-py-wasm/actions/workflows/ci.yml/badge.svg)](https://github.com/intuit/opa-py-wasm/actions/workflows/ci.yml)
5
+ ![Coverage](https://img.shields.io/badge/coverage-99%25-brightgreen)
6
+ [![PyPI](https://img.shields.io/pypi/v/opa-py-wasm.svg)](https://pypi.org/project/opa-py-wasm/)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/intuit/opa-py-wasm/blob/main/LICENSE)
8
+
9
+ `opa-py-wasm` is an in-process, thread-safe Python SDK for evaluating
10
+ [Open Policy Agent](https://www.openpolicyagent.org/) policies compiled to
11
+ WebAssembly, built on [`wasmtime.py`](https://github.com/bytecodealliance/wasmtime-py).
12
+ It requires no OPA server, no OPA binary, no subprocess, and no network at
13
+ runtime. It is functionally comparable to StyraOSS `opa-java-wasm`, with a
14
+ Pythonic API and explicit thread-safety guarantees.
15
+
16
+ > **Import name:** the package installs as `opa-py-wasm` and is imported as
17
+ > `opapywasm`.
18
+
19
+ ## Installation
20
+
21
+ ```shell
22
+ pip install opa-py-wasm
23
+ ```
24
+
25
+ Or with [uv](https://docs.astral.sh/uv/):
26
+
27
+ ```shell
28
+ uv add opa-py-wasm
29
+ ```
30
+
31
+ Requires Python 3.10 or newer. The only runtime dependency is
32
+ [`wasmtime`](https://pypi.org/project/wasmtime/).
33
+
34
+ ## Usage
35
+
36
+ ```python
37
+ from opapywasm import UNDEFINED, OpaWasmPolicy, PolicyConfig
38
+
39
+ policy = OpaWasmPolicy.from_wasm_file(
40
+ "policy.wasm",
41
+ config=PolicyConfig(
42
+ pool_size=8,
43
+ default_entrypoint="authz/allow",
44
+ eval_timeout_seconds=30.0, # per-eval deadline (epoch interruption)
45
+ max_memory_pages=4096, # per-instance linear-memory cap (256 MiB)
46
+ ),
47
+ )
48
+
49
+ policy.set_data({"roles": {"alice": "admin", "bob": "viewer"}})
50
+
51
+ decision = policy.evaluate({"user": "alice", "action": "delete"}) # simplified decision
52
+ raw = policy.evaluate_raw({"user": "alice", "action": "delete"}) # full OPA result set
53
+
54
+ # `evaluate` distinguishes an undefined decision from a defined JSON null:
55
+ if decision is UNDEFINED:
56
+ ... # policy produced no result
57
+ elif decision is None:
58
+ ... # policy result was JSON null
59
+
60
+ # Custom host builtins (receive decoded Python args, return JSON-compatible values):
61
+ policy.register_builtin("my.custom", lambda x: {"ok": True})
62
+ ```
63
+
64
+ `OpaWasmPolicy` is a bounded, thread-safe pool — construct it once and call
65
+ `evaluate` from many threads. Runnable scripts are in [`examples/`](https://github.com/intuit/opa-py-wasm/tree/main/examples):
66
+ `basic_authz.py`, `pooled_eval.py`, `custom_builtin.py`, `yaml_builtin.py`.
67
+
68
+ For the YAML default builtins, install the optional extra:
69
+
70
+ ```shell
71
+ uv add "opa-py-wasm[yaml]"
72
+ ```
73
+
74
+ Start with the [**user guide**](https://github.com/intuit/opa-py-wasm/blob/main/docs/guide.md) — an end-to-end walkthrough
75
+ covering compiling a policy, result semantics, concurrency, data updates, memory
76
+ behaviour, host builtins, and a production adoption checklist.
77
+
78
+ See [`docs/`](https://github.com/intuit/opa-py-wasm/tree/main/docs) for the focused references: architecture, thread-safety,
79
+ the OPA Wasm ABI, the `opa-java-wasm` parity table, and
80
+ [capacity-planning guidance](https://github.com/intuit/opa-py-wasm/blob/main/docs/capacity_planning.md) for sizing pool size
81
+ and concurrency.
82
+
83
+ ### Test fixtures (dev only)
84
+
85
+ Compiled `.wasm` fixtures are committed under `tests/fixtures/wasm`, so the test
86
+ suite needs no OPA CLI. To regenerate them from the Rego sources in
87
+ `tests/fixtures/rego`, install the [OPA CLI](https://www.openpolicyagent.org/docs/latest/#running-opa)
88
+ and run:
89
+
90
+ ```shell
91
+ python scripts/build_fixtures.py # all fixtures
92
+ python scripts/build_fixtures.py allow_true # a single fixture
93
+ ```
94
+
95
+ ## Local Development
96
+
97
+ ### uv
98
+
99
+ This library uses [uv to manage Python dependencies](https://docs.astral.sh/uv/getting-started/features/#projects).
100
+ `brew` is The easiest way to install on macOS:
101
+
102
+ ```shell
103
+ brew install uv
104
+ ```
105
+
106
+ For additional installation options (e.g. setting the PATH, installing a specific version, etc),
107
+ see the installation docs:
108
+ https://docs.astral.sh/uv/getting-started/installation/
109
+
110
+ ## Python Versions Supported
111
+
112
+ Python 3.10 through 3.14 are supported and tested in CI.
113
+
114
+ To change which versions are supported and tested:
115
+ - Update "envlist" in "tox" section of [tox.ini](/tox.ini)
116
+ - Update "Supported Python versions" badge in [README.md](/README.md)
117
+ - Update "project.requires-python" in [pyproject.toml](/pyproject.toml) (if needed)
118
+ - Update "tool.black.target-version" in [pyproject.toml](/pyproject.toml) (optional)
119
+ - Update the matrix in [.github/workflows/ci.yml](/.github/workflows/ci.yml)
120
+
121
+ ### Virtual Environment
122
+
123
+ Create by running:
124
+
125
+ ```shell
126
+ uv sync --all-extras
127
+ ```
128
+
129
+ Run a command from the virtual environment, like code formatting:
130
+
131
+ ```shell
132
+ uv run black .
133
+ ```
134
+
135
+ To activate the virtual environment:
136
+
137
+ ```shell
138
+ source .venv/bin/activate
139
+ ```
140
+
141
+ For more information, refer to [uv's documentation](https://docs.astral.sh/uv/pip/environments/#using-a-virtual-environment).
142
+
143
+ ### Type checking (mypy)
144
+
145
+ This library supports Python type [annotation](https://peps.python.org/pep-0484/).
146
+ Types will be checked as part of the test suite (see below).
147
+ For more information, see the [mypy documentation](https://mypy.readthedocs.io/en/stable/getting_started.html#dynamic-vs-static-typing).
148
+
149
+ ### Testing
150
+
151
+ To run the test suite locally:
152
+ ```shell
153
+ uv run tox
154
+ ```
155
+
156
+ Coverage is enforced on every CI run by `fail_under = 90` in
157
+ [`tool.coverage.report`](https://github.com/intuit/opa-py-wasm/blob/main/pyproject.toml); the suite currently sits at 99%. The
158
+ coverage badge at the top of this file is static — if you change coverage
159
+ materially, update the percentage in that badge along with your change.
160
+
161
+ ## Releasing
162
+
163
+ Releases are cut by maintainers by pushing a `vX.Y.Z` tag, which triggers an
164
+ automated build and publish to PyPI. See [`RELEASING.md`](https://github.com/intuit/opa-py-wasm/blob/main/RELEASING.md) for
165
+ the process and the versioning policy. Published versions are listed on
166
+ [PyPI](https://pypi.org/project/opa-py-wasm/), and each release's changes are in
167
+ [`CHANGELOG.md`](https://github.com/intuit/opa-py-wasm/blob/main/CHANGELOG.md).
168
+
169
+ ## Contributing
170
+
171
+ Contributions are welcome — see the [Contribution Guidelines](https://github.com/intuit/opa-py-wasm/blob/main/CONTRIBUTING.md)
172
+ and our [Code of Conduct](https://github.com/intuit/opa-py-wasm/blob/main/CODE_OF_CONDUCT.md).
173
+
174
+ ## Support
175
+
176
+ Please open a [GitHub issue](https://github.com/intuit/opa-py-wasm/issues) for
177
+ bugs and feature requests, or start a
178
+ [discussion](https://github.com/intuit/opa-py-wasm/discussions) for questions.
179
+
180
+ ## License
181
+
182
+ Released under the [MIT License](https://github.com/intuit/opa-py-wasm/blob/main/LICENSE).
@@ -0,0 +1,74 @@
1
+ """opapywasm — in-process, thread-safe Python SDK for evaluating OPA policies
2
+ compiled to WebAssembly, built on ``wasmtime.py``.
3
+
4
+ Public API (see :class:`~opapywasm.policy.OpaWasmPolicy`)::
5
+
6
+ from opapywasm import OpaWasmPolicy, PolicyConfig
7
+
8
+ policy = OpaWasmPolicy.from_wasm_file("policy.wasm")
9
+ policy.set_data({"roles": {"alice": "admin"}})
10
+ decision = policy.evaluate({"user": "alice", "action": "delete"})
11
+
12
+ The ``OpaWasmPolicy`` facade owns a bounded, thread-safe pool of single-use
13
+ Wasm instances; application threads own request-level concurrency.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ from .builtins import BuiltinRegistry
19
+ from .errors import (
20
+ OpaAbiError,
21
+ OpaAbortError,
22
+ OpaBuiltinError,
23
+ OpaDataError,
24
+ OpaEntrypointError,
25
+ OpaEvaluationError,
26
+ OpaInvalidInputError,
27
+ OpaInvalidPolicyError,
28
+ OpaMemoryError,
29
+ OpaPolicyClosedError,
30
+ OpaPoolTimeoutError,
31
+ OpaWasmError,
32
+ )
33
+ from .policy import OpaWasmPolicy
34
+ from .types import UNDEFINED, BuiltinCallable, JSONInput, JSONValue, PolicyConfig, Undefined
35
+
36
+ __all__ = [
37
+ # Facade — a bounded, thread-safe pool of policy instances.
38
+ "OpaWasmPolicy",
39
+ "BuiltinRegistry",
40
+ # Configuration & types
41
+ "PolicyConfig",
42
+ "JSONValue",
43
+ "JSONInput",
44
+ "BuiltinCallable",
45
+ "Undefined",
46
+ "UNDEFINED",
47
+ # Errors
48
+ "OpaWasmError",
49
+ "OpaInvalidPolicyError",
50
+ "OpaAbiError",
51
+ "OpaAbortError",
52
+ "OpaEvaluationError",
53
+ "OpaBuiltinError",
54
+ "OpaMemoryError",
55
+ "OpaPoolTimeoutError",
56
+ "OpaPolicyClosedError",
57
+ "OpaInvalidInputError",
58
+ "OpaEntrypointError",
59
+ "OpaDataError",
60
+ ]
61
+
62
+ # Sourced from installed package metadata so it can never drift from
63
+ # pyproject.toml. Falls back to "0.0.0+unknown" when running from a source tree
64
+ # that was never installed (e.g. some editable/CI layouts).
65
+ try:
66
+ from importlib.metadata import PackageNotFoundError
67
+ from importlib.metadata import version as _pkg_version
68
+
69
+ try:
70
+ __version__ = _pkg_version("opa-py-wasm")
71
+ except PackageNotFoundError: # pragma: no cover - only in an uninstalled tree
72
+ __version__ = "0.0.0+unknown"
73
+ except ImportError: # pragma: no cover - importlib.metadata always present on 3.10+
74
+ __version__ = "0.0.0+unknown"