https://github.com/heliconhq/opa-python
Python client library for Open Policy Framework (OPA).
https://github.com/heliconhq/opa-python
Last synced: 7 months ago
JSON representation
Python client library for Open Policy Framework (OPA).
- Host: GitHub
- URL: https://github.com/heliconhq/opa-python
- Owner: heliconhq
- License: apache-2.0
- Created: 2022-12-11T23:30:09.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-14T23:20:07.000Z (about 3 years ago)
- Last Synced: 2025-05-03T10:02:57.040Z (8 months ago)
- Language: Python
- Homepage: https://opa-python.readthedocs.io/en/latest/
- Size: 63.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-opa - OPA Python - Python client library for Open Policy Agent (Language and Platform Integrations / Python)
README
# OPA Python
Python client library for Open Policy Framework (OPA).
## Installation
python -m pip install opa-python
## Compatibility
The library has been tested with:
- Python 3.10
- OPA 0.47.3
## Usage
Create a client instance:
```python
>>> from opa import OPAClient
>>> client = OPAClient(url="http://opa-server/")
```
Verify that the OPA server is up and ready to accept requests:
```python
>>> client.check_health()
True
```
Create or update a document:
```python
>>> data = {
... "users": [
... "bilbo",
... "frodo",
... "gandalf",
... ],
... }
>>> client.save_document("my.data", data)
```
Create or update a policy:
```python
>>> policy = """
... package my.policy
...
... default allow := false
...
... allow {
... data.my.data.users[_] = input.name
... }
... """
>>> client.save_policy("policy-id", policy)
{}
```
Request decisions by evaluating input against the policy and data:
```python
>>> client.check_policy("my.policy.allow", {"name": "bilbo"})
True
>>> client.check_policy("my.policy.allow", {"name": "sauron"})
False
```
You can find the full reference in the
[documentation](https://opa-python.readthedocs.io/en/latest/).
## Local installation
Install [Poetry](https://python-poetry.org/), create new environment and
install the dependencies:
poetry install
## Running the test suite
NOTE: Each individual test that communicates with OPA creates a fresh Docker
container. This is pretty neat from a testing perspective, but means that
running the entire suite takes a bit of time.
Make sure you do not have any services listening to `8181` when you start the
tests! We might add a configuration for setting the port later, or run the
tests in Docker as well.
Run the tests:
poetry run pytest
Run specific test module:
poetry run pytest tests/test_integration.py
## Publishing
Set your credentials:
poetry config pypi-token.pypi
Build and publish:
poetry publish --build