-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_example.py
More file actions
580 lines (537 loc) · 16.8 KB
/
Copy pathfull_example.py
File metadata and controls
580 lines (537 loc) · 16.8 KB
1
2
3
4
5
6
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
"""Comprehensive example demonstrating ALL methods in the Authzee class.
This file is runnable as-is. Destructive or unnecessary calls are commented out.
"""
import datetime
import json
from uuid import uuid4
from authzee import (
AuditResultPage,
Authzee,
BatchAuditResultPage,
DictStorage,
InProcessCompute,
authzee_specification_version,
jmespath_execute,
paginator
)
# The version of the authzee specification that this library implements.
print(f"Authzee Specification Version: {authzee_specification_version}")
# Create an Authzee instance with the execute function, compute module, and storage module.
# DictStorage uses an in-memory dict. InProcessCompute runs evaluation in the current process.
storage_dict = {}
authz = Authzee(
execute=jmespath_execute, # JSON query function (JMESPath)
compute_type=InProcessCompute, # Compute module type
compute_kwargs={}, # KWArgs for compute module instances
storage_type=DictStorage, # Storage module type
storage_kwargs={ # KWArgs for storage module instances
"storage_dict": storage_dict
},
config={ # Optional AuthzeeConfigOverride
"authzee": {
"raise_errors": True # raise exceptions on errors
}
# "method_name": {<method config>} # per-method config overrides
}
)
# One time setup for the life of storage and compute. Creates DB tables, storage setup, etc.
# Should only be run once per storage/compute lifecycle.
result = authz.construct()
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Initialize the authzee app. Must be run once for every Authzee instance.
result = authz.start()
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Validate a context definition without storing it.
# Context is used to pass structured data to authorization requests.
result = authz.validate_context_def(
context_def={
"context_type": "NONE", # unique identifier for this context type
"schema": { # JSON Schema
"type": "object",
"additionalProperties": False
}
}
)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Create or update a context definition.
result = authz.put_context_def(
context_def={
"context_type": "NONE",
"schema": {
"type": "object",
"additionalProperties": False
}
}
)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Retrieve a context definition by its context_type.
result = authz.get_context_def(context_type="NONE")
print(json.dumps(result, indent=4))
# {
# "context_def": {
# "context_type": "NONE",
# "schema": {
# "type": "object",
# "additionalProperties": false
# }
# },
# "error": null
# }
# Retrieve all context definitions. Use paginator for full iteration.
# For AuthzeeAsync use: async for page in paginator_async(authz.list_context_defs):
for page in paginator(authz.list_context_defs):
for context_def in page['context_defs']:
print(json.dumps(context_def, indent=4))
# {
# "context_type": "NONE",
# "schema": {
# "type": "object",
# "additionalProperties": false
# }
# }
# Delete a context definition by its context_type.
# Commented out so NONE context remains available for the rest of the example.
# result = authz.delete_context_def(context_type="NONE")
# print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Validate an identity definition without storing it.
# Identities describe who is being authorized.
result = authz.validate_identity_def(
identity_def={
"identity_type": "user", # unique identifier for this identity type
"schema": {
"type": "object",
"required": [
"username",
"department"
],
"additionalProperties": False,
"properties": {
"username": {
"type": "string"
},
"department": {
"type": "string"
}
}
}
}
)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Create or update an identity definition.
result = authz.put_identity_def(
identity_def={
"identity_type": "user",
"schema": {
"type": "object",
"required": [
"username",
"department"
],
"additionalProperties": False,
"properties": {
"username": {
"type": "string"
},
"department": {
"type": "string"
}
}
}
}
)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Retrieve an identity definition by its identity_type.
result = authz.get_identity_def(identity_type="user")
print(json.dumps(result, indent=4))
# {
# "identity_def": {
# "identity_type": "user",
# "schema": {
# "type": "object",
# "required": [
# "username",
# "department"
# ],
# "additionalProperties": false,
# "properties": {
# "username": {
# "type": "string"
# },
# "department": {
# "type": "string"
# }
# }
# }
# },
# "error": null
# }
# Retrieve all identity definitions. Use paginator for full iteration.
# For AuthzeeAsync use: async for page in paginator_async(authz.list_identity_defs):
for page in paginator(authz.list_identity_defs):
for identity_def in page['identity_defs']:
print(json.dumps(identity_def, indent=4))
# Delete an identity definition by its identity_type.
# Commented out so user identity remains available for the rest of the example.
# result = authz.delete_identity_def(identity_type="user")
# print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Validate a resource definition without storing it.
# Resources define resource types and the actions that can be taken on them.
result = authz.validate_resource_def(
resource_def={
"resource_type": "balloon", # unique identifier for this resource type
"actions": [ # actions that can be taken on this resource
"balloon:read",
"balloon:inflate",
"balloon:pop"
],
"schema": {
"type": "object",
"required": [
"color",
"is_inflated"
],
"additionalProperties": False,
"properties": {
"color": {
"type": "string"
},
"is_inflated": {
"type": "boolean"
}
}
}
}
)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Create or update a resource definition.
result = authz.put_resource_def(
resource_def={
"resource_type": "balloon",
"actions": [
"balloon:read",
"balloon:inflate",
"balloon:pop"
],
"schema": {
"type": "object",
"required": [
"color",
"is_inflated"
],
"additionalProperties": False,
"properties": {
"color": {
"type": "string"
},
"is_inflated": {
"type": "boolean"
}
}
}
}
)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Retrieve a resource definition by its resource_type.
result = authz.get_resource_def(resource_type="balloon")
print(json.dumps(result, indent=4))
# {
# "resource_def": {
# "resource_type": "balloon",
# "actions": [
# "balloon:read",
# "balloon:inflate",
# "balloon:pop"
# ],
# "schema": {
# "type": "object",
# "required": [
# "color",
# "is_inflated"
# ],
# "additionalProperties": false,
# "properties": {
# "color": {
# "type": "string"
# },
# "is_inflated": {
# "type": "boolean"
# }
# }
# }
# },
# "error": null
# }
# Retrieve all resource definitions. Use paginator for full iteration.
# For AuthzeeAsync use: async for page in paginator_async(authz.list_resource_defs):
for page in paginator(authz.list_resource_defs):
for resource_def in page['resource_defs']:
print(json.dumps(resource_def, indent=4))
# Delete a resource definition by its resource_type.
# Commented out so balloon resource remains available for the rest of the example.
# result = authz.delete_resource_def(resource_type="balloon")
# print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Validate a grant without storing it.
# Grants are authorization rules that allow or deny actions.
grant_uuid = str(uuid4())
grant = {
"grant_uuid": grant_uuid,
"name": "Allow inflate for balloon department",
"description": "Balloon department people are allowed to read and inflate all balloons.",
"tags": {
"team": "balloon"
},
"effect": "allow",
"actions": [
"balloon:read",
"balloon:inflate"
],
"query": "length(request.identities.user[?department == 'Balloon Dept']) > `0`",
"equality": True,
"applicable_on_failure": False,
"data": {}
}
result = authz.validate_grant(grant=grant)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Enact (store) a grant to create an authorization rule.
# Grants should only ever be created or destroyed. It is not guaranteed to have an error you try to update an existing grant.
result = authz.enact(grant=grant)
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Retrieve a grant by its UUID.
result = authz.get_grant(grant_uuid=grant_uuid)
print(json.dumps(result, indent=4))
# {
# "grant": {
# "grant_uuid": "<uuid>",
# "name": "Allow inflate for balloon department",
# "description": "Balloon department people are allowed to read and inflate all balloons.",
# "tags": {
# "team": "balloon"
# },
# "effect": "allow",
# "actions": [
# "balloon:read",
# "balloon:inflate"
# ],
# "query": "length(request.identities.user[?department == 'Balloon Dept']) > `0`",
# "equality": true,
# "applicable_on_failure": false,
# "data": {}
# },
# "error": null
# }
# Retrieve grants with optional effect and action filtering. Use paginator for full iteration.
# For AuthzeeAsync use: async for page in paginator_async(authz.list_grants, effect="allow"):
for page in paginator(authz.list_grants, effect="allow"):
for g in page['grants']:
print(json.dumps(g, indent=4))
# {
# "grant_uuid": "<uuid>",
# "name": "Allow inflate for balloon department",
# "description": "Balloon department people are allowed to read and inflate all balloons.",
# "tags": {
# "team": "balloon"
# },
# "effect": "allow",
# "actions": [
# "balloon:read",
# "balloon:inflate"
# ],
# "query": "length(request.identities.user[?department == 'Balloon Dept']) > `0`",
# "equality": true,
# "applicable_on_failure": false,
# "data": {}
# }
# Retrieve grant page references for parallel pagination.
# Useful for distributing grant evaluation across workers.
# For AuthzeeAsync use: async for page in paginator_async(authz.list_grant_refs, effect="allow"):
for page in paginator(authz.list_grant_refs, effect="allow"):
for ref in page['page_refs']:
print(f" Page ref: {ref}")
# Repeal (remove) a grant by its UUID.
# purge=True scans all partitions to completely remove (useful if corruption suspected).
# Commented out so the grant remains available for authorize/audit below.
# result = authz.repeal(grant_uuid=grant_uuid, purge=False)
# print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Define the authorization request used for authorize, audit, and batch methods.
request = {
"identities": {
"user": [
{
"username": "balloon_person",
"department": "Balloon Dept"
}
]
},
"action": "balloon:inflate",
"resource_type": "balloon",
"resource": {
"color": "blue",
"is_inflated": False
},
"context_type": "NONE",
"context": {}
}
# Determine if a request is authorized. Returns is_authorized, the matching grant, and a message.
result = authz.authorize(request=request)
print(json.dumps(result, indent=4))
# {
# "is_authorized": true,
# "grant": {
# "grant_uuid": "<uuid>",
# "name": "Allow inflate for balloon department",
# "description": "Balloon department people are allowed to read and inflate all balloons.",
# "tags": {
# "team": "balloon"
# },
# "effect": "allow",
# "actions": [
# "balloon:read",
# "balloon:inflate"
# ],
# "query": "length(request.identities.user[?department == 'Balloon Dept']) > `0`",
# "equality": true,
# "applicable_on_failure": false,
# "data": {}
# },
# "message": "An allow grant is applicable to the request, and there are no deny grants that are applicable to the request. Therefore, the request is authorized.",
# "error": null
# }
# Audit how each grant evaluates against the request. Returns per-grant results.
# Use paginator for full iteration over all grants.
# For AuthzeeAsync use: async for page in paginator_async(authz.audit, request=request):
for page in paginator(authz.audit, request=request):
page: AuditResultPage
for r in page['results']:
print(f"Grant: {r['grant']['name']}")
print(f" is_applicable: {r['is_applicable']}")
print(f" query_result: {r['query_result']}")
# Grant: Allow inflate for balloon department
# is_applicable: True
# query_result: True
# Batch requests evaluate multiple resource variations against the same base request.
batch_request = {
"identities": {
"user": [
{
"username": "balloon_person",
"department": "Balloon Dept"
}
]
},
"action": "balloon:inflate",
"resource_type": "balloon",
"resource": {
"color": "blue",
"is_inflated": False
},
"context_type": "NONE",
"context": {},
"batch": [ # each batch item overrides the specified root fields
{
"resource": {
"color": "red",
"is_inflated": True
}
},
{
"resource": {
"color": "green",
"is_inflated": False
}
}
]
}
# Determine if each item in the batch request is authorized.
result = authz.batch_authorize(batch_request=batch_request)
print(json.dumps(result, indent=4))
# {
# "batch": [
# {
# "is_authorized": true,
# "grant": {
# "grant_uuid": "<uuid>",
# "name": "Allow inflate for balloon department",
# ...
# },
# "message": "An allow grant is applicable to the request...",
# "error": null
# },
# ...
# ],
# "error": null
# }
# Audit how each grant evaluates against each item in the batch request.
# Use paginator for full iteration over all grants.
# For AuthzeeAsync use: async for page in paginator_async(authz.batch_audit, batch_request=batch_request):
for page in paginator(authz.batch_audit, batch_request=batch_request):
page: BatchAuditResultPage
for g in page['grants']:
print(f" Grant: {g['name']}")
for batch_result in page['batch']:
for r in batch_result['results']:
print(f" is_applicable: {r['is_applicable']}, query_result: {r['query_result']}")
# Grant: Allow inflate for balloon department
# is_applicable: True, query_result: True
# is_applicable: True, query_result: True
# Clean up storage latches created before a given datetime.
# Operations should clean up their own latches, but this handles zombie latches from failures.
result = authz.cleanup_latches(before=datetime.datetime(2026, 1, 1))
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Shutdown the authzee app. Should be run before exit for every Authzee instance.
result = authz.shutdown()
print(json.dumps(result, indent=4))
# {
# "error": null
# }
# Tear down everything that construct set up. Deletes DB tables, storage, etc.
# DESTRUCTIVE - only run if you want to completely remove storage.
# result = authz.destroy()
# print(json.dumps(result, indent=4))
# {
# "error": null
# }