Python Authorizer API Client
Overview
This package provides a high-level interface for interacting with the Aserto Authorizer API.
Installation
Using pip:
pip install aserto
Using Poetry:
poetry add aserto
Usage
Creating a client
The AuthorizerClient
class provides the methods for interacting with the API. The constructor takes
two arguments:
-
identity
(required): AnIdentity
instance that represents a user. To create an Identity you need to provide atype
and avalue
. Example:from aserto import Identity, IdentityType
identitySub = Identity(type=IdentityType.IDENTITY_TYPE_SUB, value=identity)
idenitytNone = Identity(IdentityType.IDENTITY_TYPE_NONE) -
options
(required): An [AuthorizerOptions
] that contains the necessary information to create an Authorizer client. The options that can be provided are:api_key
(optional): An Aserto Authorizer API Keytenant_id
(optional): An Aserto Tenant IDcert_file_path
(optional): Path to Authorizer certs if the authtorizer runs locally.url
(optional):hostname:port
of Aserto Authorizer. Defaults to"authorizer.prod.aserto.com:8443"
.
Creating an AuthorizerClient
example:
from aserto import Identity, IdentityType
from aserto.client.authorizer import AuthorizerClient, AuthorizerOptions
client = AuthorizerClient(
identity=Identity(IdentityType.IDENTITY_TYPE_NONE),
options=AuthorizerOptions(
api_key=YOUR_ASERTO_API_KEY,
tenant_id=YOUR_ASERTO_TENANT_ID,
),
)
Client methods
decisions
Arguments
decisions
(required): A list of decision values to request, e.g.["allowed"]
policy_path
(required): The path of the policy module, including the policy root.policy_instance_name
(optional): The policy instance name run by the authorizer.policy_instance_label
(optional): The policy instance label run by the authorizer.resource_context
(optional): The resource context provided to the Authorizer as a serializabledict
deadline
(optional): How long to wait for the request to time-out. Either a Pythontimedelta
object representing the duration to wait or adatetime
object representing when the request should time-out
Example
decisions = client.decisions(
decisions=["allowed", "enabled"],
policy_instance_name=ASERTO_POLICY_INSTANCE_NAME,
policy_instance_label=ASERTO_POLICY_INSTANCE_LABEL,
policy_path="my_policy_root.GET.user.__id",
)
assert decisions == {
"enabled": True,
"allowed": False,
}
decision_tree
Arguments
decisions
(required): A list of decision values to request, e.g.["allowed"]
policy_path_root
(required): The root path of all the policy modulespolicy_instance_name
(optional): The policy instance name run by the authorizer.policy_instance_label
(optional): The policy instance label run by the authorizer.resource_context
(optional): The resource context provided to the Authorizer as a serializabledict
policy_path_separator
(optional): Either"DOT"
or"SLASH"
, the delimiter to use in the returned policy path keysdeadline
(optional): How long to wait for the request to time-out. Either a Pythontimedelta
object representing the duration to wait or adatetime
object representing when the request should time-out.
Example
result = await client.decision_tree(
decisions=["visible", "enabled", "allowed"],
policy_instance_name=ASERTO_POLICY_INSTANCE_NAME,
policy_instance_label=ASERTO_POLICY_INSTANCE_LABEL,
policy_path_root=ASERTO_POLICY_PATH_ROOT,
policy_path_separator="DOT",
)
assert result == {
"GET.your.policy.path": {
"visible": True,
"enabled": True,
"allowed": False,
},
}
query
Arguments
-
query
(required): A rego query that is evaluated over the policy. Example:x = input
(bind theinput
to thex
variable). -
input
(required): A string that encodes a JSON document. -
policy_path
(required): The path of the policy module, including the policy root. -
decisions
(required): A list of decision values to request, e.g.["allowed"]
. -
policy_instance_name
(optional): The policy instance name run by the authorizer. -
policy_instance_label
(optional): The policy instance label run by the authorizer. -
resource_context
(optional): The resource context provided to the Authorizer as a serializabledict
. -
options
(optional):QueryOptions
that can indicate the trace level, if the result should include the metrics, etc.metrics: bool
instrument: bool
trace: TraceLevel
trace_summary: boolThe trace levels can be one of the following values:
- TRACE_LEVEL_OFF
- TRACE_LEVEL_FULL
- TRACE_LEVEL_NOTES
- TRACE_LEVEL_FAIL
-
deadline
(optional): How long to wait for the request to time-out. Either a Pythontimedelta
object representing the duration to wait or adatetime
object representing when the request should time-out.
Github
This package is open source and can be found on GitHub.