Python SDK
Eunomia offers a Python client that enables users to interact with the Eunomia server.
The client allows you to register resources and principals with their metadata to the Eunomia server, as well as verify permissions of principals performing actions on resources. These features simplify the integration of the Eunomia server into your Python applications.
Installation
Install the eunomia-sdk
package via pip:
Usage
Standard API
Use the standard API for authorization checks in your application:
from eunomia_sdk import EunomiaClient
client = EunomiaClient(endpoint="http://localhost:8000")
# Check if a principal has permissions to perform an action on a resource
response = client.check(
principal_uri="user:123",
resource_uri="document:456",
action="read",
)
print(f"Is allowed: {response.allowed}")
Admin API Usage
Use the admin API for server configuration and entity management:
from eunomia_sdk import EunomiaClient
# For admin API usage authentication via API key might be required
client = EunomiaClient(
endpoint="http://localhost:8000",
api_key="your-admin-api-key" # or set WAY_API_KEY environment variable
)
# Register a resource with metadata
resource = client.register_entity(
type="resource",
uri="document:456",
attributes={
"name": "sensitive_document",
"type": "document",
"classification": "confidential",
},
)
# Register a principal with metadata
principal = client.register_entity(
type="principal",
uri="user:123",
attributes={
"name": "user_123",
"role": "analyst",
"department": "research",
},
)
Docs
eunomia_sdk.client.EunomiaClient
A client for interacting with the Eunomia server.
This client provides methods to register resources and principals, and to check permissions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
endpoint
|
str
|
The base URL endpoint of the Eunomia server. Defaults to "http://localhost:8000" if not provided. |
None
|
api_key
|
str
|
The API key for authenticating with the server. Defaults to the environment variable "WAY_API_KEY" if not provided. |
None
|
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
bulk_check(check_requests)
Perform a set of permission checks in a single request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
check_requests
|
list[CheckRequest]
|
The list of check requests to perform. |
required |
Returns:
Type | Description |
---|---|
list[CheckResponse]
|
The list of results of the check requests. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
check(principal_uri=None, resource_uri=None, principal_attributes={}, resource_attributes={}, action='access')
Check whether a principal has permissions to perform an action on a specific resource.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
principal_uri
|
str
|
The identifier of the principal. Can be provided for registered principals to automatically retrieve attributes. |
None
|
resource_uri
|
str
|
The identifier of the resource. Can be provided for registered resources to automatically retrieve attributes. |
None
|
principal_attributes
|
dict
|
The attributes of the principal. Shall be provided if the principal is not registered. |
{}
|
resource_attributes
|
dict
|
The attributes of the resource. Shall be provided if the resource is not registered. |
{}
|
action
|
str
|
The action to check permissions for. Defaults to "access". |
'access'
|
Returns:
Type | Description |
---|---|
CheckResponse
|
The response containing the allowed flag and the reason for the decision. |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
create_policy(request)
Create a new policy and store it in the Eunomia server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Policy
|
The policy to create. |
required |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
create_simple_policy(request, name)
Create a new simple policy with a single rule and store it in the Eunomia server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
CheckRequest
|
The request to create the policy from. |
required |
name
|
str
|
The name of the policy. |
required |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
delete_entity(uri)
Delete an entity from the Eunomia server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
str
|
The uri of the entity to delete. |
required |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
delete_policy(name)
Delete a policy from the Eunomia server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the policy to delete. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the policy was successfully deleted. |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
get_policies()
Get all policies from the Eunomia server.
Returns:
Type | Description |
---|---|
list[Policy]
|
The list of all policies. |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
register_entity(type, attributes, uri=None)
Register a new entity with the Eunomia server.
This method registers a new entity with its attributes to the Eunomia server. If no uri identifier is provided, the server will generate a random UUID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type
|
EntityType
|
The type of entity to register. Either "resource" or "principal". |
required |
attributes
|
dict
|
The attributes to associate with the entity. |
required |
uri
|
str | None
|
The uri for the entity. If not provided, the server will generate a random UUID. |
None
|
Returns:
Type | Description |
---|---|
EntityInDb
|
The newly registered entity. |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |
Source code in pkgs/sdks/python/src/eunomia_sdk/client.py
update_entity(uri, attributes, override=False)
Update the attributes of an existing entity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
str
|
The uri of the entity to update. |
required |
attributes
|
dict
|
The attributes to update. |
required |
override
|
bool
|
If True, the existing attributes are deleted and the new attributes are added. If False, the existing attributes are maintaned or updated in case of overlap, and the additional new attributes are added. |
False
|
Returns:
Type | Description |
---|---|
EntityInDb
|
The updated entity. |
Raises:
Type | Description |
---|---|
HTTPStatusError
|
If the HTTP request returns an unsuccessful status code. |