Package: cedarpy-conversor
CedarPy Conversorcedarpy-conversorhelps you use the (Rust)Cedar Policylibrary from Python. You can usecedarpy-conversorto:convert cedar policies to JSON representationconvert JSON representation to cedar policiescedarpy-conversorpackages are availble for the following platforms:Operating SystemProcessor ArchitecturesLinuxx86_64, aarch64Macx86_64, aarch64Windowsx86_64Note: This project isnotofficially supported by AWS or the Cedar Policy team.Using the libraryReleases ofcedarpy-conversorare available on PyPi.  You can install the latest release with:pipinstallcedarpy-conversor(See the Developing section for how to use artifacts you've built locally.)Convert cedar policies to JSON representationNow you can use the library to convert cedar policies to JSON representation:importjsonfromcedarpy_conversorimportconvert_cedar_policies_to_jsonpolicies:str="""permit(principal == User::"12UA45",action == Action::"view",resource in Folder::"abc") when {(context["tls_version"]) == "1.3"};"""json_representation=json.loads(convert_cedar_policies_to_json(policies))json_representation:str=json.dumps(json_representation,indent=2)expected_json_representation:str="""{"effect": "permit","principal": {"op": "==","entity": {"type": "User","id": "12UA45"}},"action": {"op": "==","entity": {"type": "Action","id": "view"}},"resource": {"op": "in","entity": {"type": "Folder","id": "abc"}},"conditions": [{"kind": "when","body": {"==": {"left": {".": {"left": {"Var": "context"},"attr": "tls_version"}},"right": {"Value": "1.3"}}}}]}"""# so you can assert that the json representation is correctassertjson_representation.strip()==expected_json_representation.strip()Convert JSON representation to cedar policiesYou can also use the library to convert JSON representation to cedar policies:importjsonfromcedarpy_conversorimportconvert_json_to_cedar_policiesexpected_policies:str="""permit(principal == User::"12UA45",action == Action::"view",resource in Folder::"abc") when {(context["tls_version"]) == "1.3"};"""json_representation:str="""{"effect": "permit","principal": {"op": "==","entity": {"type": "User","id": "12UA45"}},"action": {"op": "==","entity": {"type": "Action","id": "view"}},"resource": {"op": "in","entity": {"type": "Folder","id": "abc"}},"conditions": [{"kind": "when","body": {"==": {"left": {".": {"left": {"Var": "context"},"attr": "tls_version"}},"right": {"Value": "1.3"}}}}]}"""policies:str=convert_json_to_cedar_policies(json_representation)# so you can assert that the json representation is correctassertpolicies.strip()==expected_policies.strip()DevelopingYou'll need a few things to get started:Python +3.9Rust andcargoThis project is built on thePyO3andmaturinprojects.  These projects are designed to enable Python to use Rust code and vice versa.The most common development commands are in theMakefileCreate virtual envFirst create a Python virtual environment for this project with:make venv-devIn addition to creating a dedicated virtual environment, this will installcedarpy-conversor's dependencies.If this works you should be able to run the following command:maturin--helpBuild and runcedarpy-conversortestsEnsure thecedarpy-conversorvirtual environment is active by sourcing it in your shell:sourcevenv-dev/bin/activateNow run:makequickThemake quickcommand will build the Rust source code withmaturinand run the project's tests withpytest.If all goes well, you should see output like:(venv-dev)totvs:cedarpy-conversortotvs$makequick
Performingquickbuildset-e;\maturindevelop;\pytest
📦Includinglicensefile"/path/to/cedarpy-conversor/LICENSE"🔗Foundpyo3bindings
🐍FoundCPython3.9at/path/to/cedarpy-conversor/venv-dev/bin/python
📡Usingbuildoptionsfeaturesfrompyproject.toml
Ignoringmaturin:markers'extra == "dev"'don't match your environmentIgnoring pip-tools: markers 'extra=="dev"' don'tmatchyourenvironment
Ignoringpytest:markers'extra == "dev"'don'tmatchyourenvironment
💻Using`MACOSX_DEPLOYMENT_TARGET=11.0`foraarch64-apple-darwinbydefaultCompilingcedarpy_conversorv0.1.0(/path/to/cedarpy-conversor)Finisheddev[unoptimized+debuginfo]target(s)in3.06s
📦BuiltwheelforCPython3.9to/var/folders/k2/tnw8n1c54tv8nt4557pfx3440000gp/T/.tmpO6aj6c/cedarpy-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
🛠Installedcedarpy-0.1.0================================================================================================testsessionstarts================================================================================================platformdarwin--Python3.9.12,pytest-7.4.0,pluggy-1.2.0
rootdir:/path/to/cedarpy-conversor
configfile:pyproject.toml
testpaths:tests/unit
collected4items

tests/unit/test_convert_json_to_policy.py::ConvertJsonToPolicyTestCase::test_policy_json_to_cedarPASSED[25%]tests/unit/test_convert_policy_to_json.py::ConvertPolicyToJsonTestCase::test_policy_json_to_cedarPASSED[50%]tests/unit/test_import_module.py::ImportModuleTestCase::test_cedarpy_conversor_module_importsPASSED[75%]tests/unit/test_import_module.py::InvokeModuleTestFunctionTestCase::test_invoke_echoPASSED[100%]================================================================================================4passedin0.32s=================================================================================================Using locally-built artifactsIf you usedmake quickabove, then a development build of thecedarpy-conversormodule will already be installed in the virtual environment.If you want to use your localcedarpy-conversorchanges in another Python environment, you'll need to build a release with:makereleaseThe release process will build a wheel and output it intotarget/wheels/Now you can install that file with pip, e.g.:pipinstall--force-reinstall/path/to/cedarpy-conversor/target/wheels/cedarpy_*.whlContributingThis project is in its early stages and contributions are welcome. Please check the project's GitHubissuesfor work we've already identified.Some ways to contribute are:Use the project and report experience and issuesDocument usage and limitationsEnhance the library with additional functionality you need