Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opa_python-0.0.9.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opa_python-0.0.9-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

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

Hashes for opa_python-0.0.9.tar.gz
Algorithm Hash digest
SHA256 5ba59231275fb12da5d9907aaa8dd072daf0cf666b675c5d51aebf95dcfd6541
MD5 e411afa8419e99446640177b6ba4fb96
BLAKE2b-256 5afbdc447d3f712f23ef2a62b429fd60b11d6033dcfbf5a4a780f0d6ca6a6745

See more details on using hashes here.

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

Hashes for opa_python-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0dc658241ebce96dfdc1feead11ed87147bae9b36f5fbd743ee6d36c5d4aadd2
MD5 7e43b497b31c39e56e7b1650377a2f19
BLAKE2b-256 d58c3abfdc8ac46212eb611bc5a7730f14fac569a4be27612551127e2c22bfab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page