No project description provided
Project description
Cedar Python
cedarpy
helps you use the (Rust) Cedar Policy library from Python. You can use cedarpy
to:
- check whether a request is authorized by the Cedar Policy engine
- format policies
cedarpy
packages are availble for the following platforms:
Operating System | Processor Architectures |
---|---|
Linux | x86_64, aarch64 |
Mac | x86_64, aarch64 |
Windows | x86_64 |
Note: This project is not officially supported by AWS or the Cedar Policy team.
Using the library
Releases of cedarpy
are available on PyPi. You can install the latest release with:
pip install cedarpy
(See the Developing section for how to use artifacts you've built locally.)
Authorizing access with Cedar policies in Python
Now you can use the library to authorize access with Cedar from your Python project using the is_authorized
function. Here's an example of basic use:
from cedarpy import is_authorized, AuthzResult, Decision
policies: str = "//a string containing cedar policies"
entities: list = [ # a list of Cedar entities; can also be a json-formatted string of Cedar entities
{"uid": {"__expr": "User::\"alice\""}, "attrs": {}, "parents": []}
# ...
]
request = {
"principal": "User::\"bob\"",
"action": "Action::\"view\"",
"resource": "Photo::\"1234-abcd\"",
"context": {}
}
authz_result: AuthzResult = is_authorized(request, policies, entities)
# so you can assert on the decision like:
assert Decision.Allow == authz_result.decision
# or use the 'allowed' convenience method
assert authz_result.allowed
# or even via AuthzResult's attribute subscripting support
assert authz_result['allowed']
The AuthzResult
class also provides diagnostics and metrics for the access evaluation request.
See the unit tests for more examples of use and expected behavior.
Formatting Cedar policies
You can use format_policies
to pretty-print Cedar policies according to
convention.
from cedarpy import format_policies
policies: str = """
permit(
principal,
action == Action::"edit",
resource
)
when {
resource.owner == principal
};
"""
print(format_policies(policies))
# permit (
# principal,
# action == Action::"edit",
# resource
# )
# when { resource.owner == principal };
Developing
You'll need a few things to get started:
- Python +3.9
- Rust and
cargo
This project is built on the PyO3 and maturin projects. These projects are designed to enable Python to use Rust code and vice versa.
The most common development commands are in the Makefile
Create virtual env
First create a Python virtual environment for this project with:
make venv-dev
In addition to creating a dedicated virtual environment, this will install cedar-py
's dependencies.
If this works you should be able to run the following command:
maturin --help
Build and run cedar-py
tests
Ensure the cedar-py
virtual environment is active by sourcing it in your shell:
source venv-dev/bin/activate
Now run:
make quick
The make quick
command will build the Rust source code with maturin
and run the project's tests with pytest
.
If all goes well, you should see output like:
(venv-dev) swedish-chef:cedar-py skuenzli$ make quick
Performing quick build
set -e ;\
maturin develop ;\
pytest
📦 Including license file "/path/to/cedar-py/LICENSE"
🔗 Found pyo3 bindings
🐍 Found CPython 3.9 at /path/to/cedar-py/venv-dev/bin/python
📡 Using build options features from pyproject.toml
Ignoring maturin: markers 'extra == "dev"' don't match your environment
Ignoring pip-tools: markers 'extra == "dev"' don't match your environment
Ignoring pytest: markers 'extra == "dev"' don't match your environment
💻 Using `MACOSX_DEPLOYMENT_TARGET=11.0` for aarch64-apple-darwin by default
Compiling cedarpy v0.1.0 (/path/to/cedar-py)
Finished dev [unoptimized + debuginfo] target(s) in 3.06s
📦 Built wheel for CPython 3.9 to /var/folders/k2/tnw8n1c54tv8nt4557pfx3440000gp/T/.tmpO6aj6c/cedarpy-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
🛠 Installed cedarpy-0.1.0
================================================================================================ test session starts ================================================================================================
platform darwin -- Python 3.9.12, pytest-7.4.0, pluggy-1.2.0
rootdir: /path/to/cedar-py
configfile: pyproject.toml
testpaths: tests/unit
collected 10 items
tests/unit/test_authorize.py::AuthorizeTestCase::test_authorize_basic_ALLOW PASSED [ 10%]
tests/unit/test_authorize.py::AuthorizeTestCase::test_authorize_basic_DENY PASSED [ 20%]
... snip ... # a bunch of tests passing - please write more!
tests/unit/test_import_module.py::InvokeModuleTestFunctionTestCase::test_invoke_parse_test_policy PASSED [100%]
================================================================================================ 10 passed in 0.51s =================================================================================================
Integration tests
This project supports validating correctness with official Cedar integration tests. To run those tests you'll need to retrieve the cedar-integration-tests
data with:
make submodules
Then you can run:
make integration-tests
cedar-py
currently passes 69 of the 82 tests defined in the example_use_cases_doc
, multi
, ip
, and decimal
suites. (The pass rate is actually higher, but we skip some tests that pass due to the way test suites are loaded.) See test_cedar_integration_tests.py for details.
Using locally-built artifacts
If you used make quick
above, then a development build of the cedarpy
module will already be installed in the virtual environment.
If you want to use your local cedarpy
changes in another Python environment, you'll need to build a release with:
make release
The release process will build a wheel and output it into target/wheels/
Now you can install that file with pip, e.g.:
pip install --force-reinstall /path/to/cedar-py/target/wheels/ccedarpy-*.whl
Contributing
This project is in its early stages and contributions are welcome. Please check the project's GitHub issues for work we've already identified.
Some ways to contribute are:
- Use the project and report experience and issues
- Document usage and limitations
- Enhance the library with additional functionality you need
- Add test cases, particularly those from
cedar-integration-tests
You can reach people interested in this project in the #cedar-py
channel of the Cedar Policy Slack workspace.
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
Built Distributions
Hashes for cedarpy-0.2.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15dfc07e56144a5670cb17822684f091cf2fd92ea4d71fd0cfda3f5eefcb9467 |
|
MD5 | d3922a53b065d15ec60c2e2a1f5e2b77 |
|
BLAKE2b-256 | 82111901047cb5de84d69b6b977ec6c14c692334cc6edee21b2069b7b71f75d8 |
Hashes for cedarpy-0.2.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec6b66ccfd2be04aa9ca5e8822b44f9ca89b1746cbc3a4e58200a074633b2d46 |
|
MD5 | c02bc560289d1804293de8c97ba4e700 |
|
BLAKE2b-256 | 62f347fbf55e53314d1bb56ecb945f997fa0e85a75057a5d7d9db83887b9436f |
Hashes for cedarpy-0.2.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0abc186c4b24eea099b9ff669faea38f8793c686f058118823a1a3ac71953280 |
|
MD5 | 156988abde15ee6eb2e890a85f8449d2 |
|
BLAKE2b-256 | 5e27ee1224a60b40752f0dce69d67f7aa805936f4ace9d4781640b923dd6ffd0 |
Hashes for cedarpy-0.2.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e38a2f3837943eb3f5b7623750fb7a4525b04ac5b32323159b6dcbfeb3eb044 |
|
MD5 | 87a4c6b3910c4050f28acb9ba080d47a |
|
BLAKE2b-256 | c91757c06f8fdfa9e1728364631d7c7c81526de3c9848f40621821fc2acba842 |
Hashes for cedarpy-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02c3308b8687d6871b1f066dddb9a5b3fc2c424eea1261e2e58063e3fd89196d |
|
MD5 | 5d0af41fac06a2cc7e1052d6f27a8a94 |
|
BLAKE2b-256 | 713bc772ea5847f2d92e2d3c8638112ff30884dfda7db20212a7fc69f42616b1 |
Hashes for cedarpy-0.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53d190d0f6977da0eeabc46287123aa0108d6ab61ce57b4113d85b62849a5fe0 |
|
MD5 | 3c22fdf40f8e355273aba8e22c330e65 |
|
BLAKE2b-256 | 6f416edac6aa984d3d964b5acb2c545607c811e1c65e3822282ee579780fef4b |
Hashes for cedarpy-0.2.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6cc335d367e91a72ef98524f39e1d1fee530a7a0bae7f89c0e510de80d863a8 |
|
MD5 | dd19b315d04bcc3fda4bcbb915a98a57 |
|
BLAKE2b-256 | e28b948c2a802a0f58ed4be51019966bcf2be9475d9d1099cc0c095f36a850d8 |
Hashes for cedarpy-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17c267facc763f504b40c72c29880ed21a0a3bfbf971d657e4c4ffb0b3feef4f |
|
MD5 | df796bbaabb25511cca7d52a687c80a7 |
|
BLAKE2b-256 | a53cf9a6363580959cc70480fe4046e27547565849b6505c25baac2ce2d289b8 |
Hashes for cedarpy-0.2.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef91229c5736fe9e40c2c68135d86dbf73396a8b13092751b8813e019446b932 |
|
MD5 | d19cdb42f728cc4df62db26374f4b999 |
|
BLAKE2b-256 | ef20b769d06e09cf3eae5476d45f15c0262546ee30681d5d59871771b74069fd |
Hashes for cedarpy-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87259cea002ff9ac2739d2e34504ba9926dae8c551b3cd588d64ede4c1d46b5c |
|
MD5 | 617c846be0035f3c95f2055ee3a769a1 |
|
BLAKE2b-256 | b8959ee1f9d3995f088b30c3e7daff49a4365adfe5fd35d7df45726679cdf730 |
Hashes for cedarpy-0.2.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9e2586ce4bb5a9eace92fec674aeb33fc3f14f909be260d5d9f720d2ea36da5 |
|
MD5 | 28211531fc04b154f7c32dbcc195eac0 |
|
BLAKE2b-256 | 85d3af03e24a4b77dd3116f228f69eb8e8d31cdae0e9a205496577465688d2f8 |
Hashes for cedarpy-0.2.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a66ed11db4158ae5d710886413473b97f845fa97628f31cfabd611475b02efe1 |
|
MD5 | 761beb84e434910148735eb17addfe56 |
|
BLAKE2b-256 | 74f67047eeb7e35ba9582707e67ae2f881c763b10b685a3c803ba0f9d5b3311f |
Hashes for cedarpy-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e2b39fa8ef651fcd409b807f892a366a7127c8d34b8f16e287d9e5856fe3be4 |
|
MD5 | aaf5c4ca5f14d3bf4f28088fa57aa5f5 |
|
BLAKE2b-256 | d5417df657723c75ee2f88381100111cd1eed46162680c38e9dd70763e533ecd |
Hashes for cedarpy-0.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1caa095f6bfa0904ac8ba29d416334b85631918c6e75180b2173c4b590b6d261 |
|
MD5 | 00d4e0920a347d4526398defca4f5b40 |
|
BLAKE2b-256 | cd74bb3cc1d6e410ad235803916f52faab65c84653b78e703758a58925898b43 |
Hashes for cedarpy-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 236170adcffbf8cc98164becccd424d289d20351950fe19471f466ba503ecc56 |
|
MD5 | 89b334c50d6e5f0addfbfe80576d986f |
|
BLAKE2b-256 | f832c81ef0dfdd7753a33bd4a177331417ce5b61d43e2ef8a6a6bb57b4a038be |
Hashes for cedarpy-0.2.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0f09deddac0cfdd1c31bf7b39625e8e093424fc058443db9a9f90c7b39e00f5 |
|
MD5 | 6b600852bd88e67cc8e2c2b2ba4a8dff |
|
BLAKE2b-256 | 942261c290df136929fb7adad1ef23edebaeb74f5d44a079532ba1f9cb37393b |
Hashes for cedarpy-0.2.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b19ed4ad52fb6ddb6fda0f8e81772d45104e4f58b33af2f9e9f8f4f6bfb63ae |
|
MD5 | f2b01052f231a0ae452b60c35543594f |
|
BLAKE2b-256 | 212d99bf175ed070d154066a75f1cc97bb8db2cb405334385797317d3bfa0261 |
Hashes for cedarpy-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e0fde38b74193c34ad8c15d875b4fb3f77b26122d181b05480d841eb03ae7ea |
|
MD5 | b40ce9564afcefea6433fcbe6dbda1a9 |
|
BLAKE2b-256 | 00e4cc96b961a9710b70771e989713be79ffd96846f5d58830483ed2eb54552f |
Hashes for cedarpy-0.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a22e137ff3afaf3108d70d2b363931eb21bc1176a2926a0a1b0e5ebed9ef4c7a |
|
MD5 | fca5afc4d0d14b99bdc9eadf139d7ee4 |
|
BLAKE2b-256 | cb14303d7ed1cb8bdadf162777b0ee1feb58390611f6cdd7d21477b602bc8ba2 |
Hashes for cedarpy-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66b5432f523adff0d8d32977d28a7606c3b5336ce9741d663bf6f898c2ff9628 |
|
MD5 | 4f32246ab59e38c5f24299b586e3bd7e |
|
BLAKE2b-256 | 28aeeb8af777a162893b0b398282c61a676cb043548a60fdea704e922b570e3c |
Hashes for cedarpy-0.2.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26fb376fd3cee431cb2fef9f379fc86d720fcd679d5b399ccb501e927cafd25c |
|
MD5 | 09fe6386c1e64a8ce9058899aae53295 |
|
BLAKE2b-256 | 18d2bea6213b9e93a23593af133d5201393e196287eb89b24e45c64fdd8a1e15 |