Hi, I'm trying to load policies into Cerbos using ...
# help
j
Hi, I'm trying to load policies into Cerbos using the admin API with the Python SDK. Is there a way to load the policies from a file similar to the cli command
cerbosctl put policy ./path/to/policy.yaml
? Looks like the
add_or_update_policy
function takes schema objects...
d
Hi Jonah, You may want to deserialise the policy object first and then use it in the
add_or_update_policy
function as per this example:
Copy code
from cerbos.policy.v1 import policy_pb2
from cerbos.sdk.grpc.client import AdminCredentials, AsyncCerbosAdminClient

admin_credentials = AdminCredentials(username="admin", password="some_password")
async with AsyncCerbosAdminClient("localhost:3593", admin_credentials=admin_credentials) as c:
    await c.add_or_update_policy(
        [
            policy_pb2.Policy(
                api_version="api.cerbos.dev/v1",
                principal_policy=policy_pb2.PrincipalPolicy(
                    principal="terry", version="default"
                ),
            )
        ]
    )
j
Hi Dennis, I would like to keep the YAML as the source of the policy. Are there any tools to help convert the YAML to the Policy object?
d
I am not a Python programmer, but there should be a way to read a YAML file into the policy object (deserialisation).
c
Hey, adding the utility functions to help with this is one of the things in our backlog (https://github.com/cerbos/cerbos-sdk-python/issues/32). In the mean time, you can use json_format.Parse to obtain a
policy_pb2.Policy
object from JSON or use
yaml.Load
and
json_format.ParseDict
to go from YAML to proto.
j
got it, thank you!