A Python OPA client library
Project description
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:
>>> from opa import OPAClient
>>> client = OPAClient(url="http://opa-server/")
Verify that the OPA server is up and ready to accept requests:
>>> client.check_health()
True
Create or update a document:
>>> data = {
... "users": [
... "bilbo",
... "frodo",
... "gandalf",
... ],
... }
>>> client.save_document("my.data", data)
Create or update a policy:
>>> 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:
>>> 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.
Local installation
Install Poetry, 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 <token>
Build and publish:
poetry publish --build
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opa_python-0.0.9.tar.gz.
File metadata
- Download URL: opa_python-0.0.9.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.13.2 Linux/6.13.3-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ba59231275fb12da5d9907aaa8dd072daf0cf666b675c5d51aebf95dcfd6541
|
|
| MD5 |
e411afa8419e99446640177b6ba4fb96
|
|
| BLAKE2b-256 |
5afbdc447d3f712f23ef2a62b429fd60b11d6033dcfbf5a4a780f0d6ca6a6745
|
File details
Details for the file opa_python-0.0.9-py3-none-any.whl.
File metadata
- Download URL: opa_python-0.0.9-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.13.2 Linux/6.13.3-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dc658241ebce96dfdc1feead11ed87147bae9b36f5fbd743ee6d36c5d4aadd2
|
|
| MD5 |
7e43b497b31c39e56e7b1650377a2f19
|
|
| BLAKE2b-256 |
d58c3abfdc8ac46212eb611bc5a7730f14fac569a4be27612551127e2c22bfab
|