-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
executable file
·165 lines (142 loc) · 4.88 KB
/
main.py
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
# Copyright 2021-2022 Zenauth Ltd.
# SPDX-License-Identifier: Apache-2.0
import dataclasses
import os
import emoji
from cerbos.sdk.client import CerbosClient
from cerbos.sdk.container import CerbosContainer
from cerbos.sdk.model import *
# Define the principals and resources we are going to use in the demo.
# Harry is an employee in the UK working in the marketing department's design team
harry = Principal(
id="harry",
roles={"employee"},
attr={"department": "marketing", "geography": "GB", "team": "design"},
)
# Maggie is Harry's direct manager
maggie = Principal(
id="maggie",
roles={"employee", "manager"},
attr={
"department": "marketing",
"geography": "GB",
"managed_geographies": "GB",
"team": "design",
},
)
# Amanda is Maggie's counterpart in the US
amanda = Principal(
id="amanda",
roles={"employee", "manager"},
attr={
"department": "marketing",
"geography": "US",
"managed_geographies": ["US"],
"team": "design",
},
)
# Pedro is an employee in a different team and geography
pedro = Principal(
id="pedro",
roles={"employee"},
attr={"department": "shipping", "geography": "FR", "team": "Pod 1"},
)
# A leave request created by Harry that is still in DRAFT status
harrys_draft_holiday_request = Resource(
id="XX125",
kind="leave_request",
attr={
"department": "marketing",
"geography": "GB",
"id": "XX125",
"owner": "harry",
"status": "DRAFT",
"team": "design",
},
)
# A leave request created by Maggie that is still in PENDING_APPROVAL status
maggies_pending_holiday_request = Resource(
id="XX226",
kind="leave_request",
attr={
"department": "marketing",
"geography": "GB",
"id": "XX226",
"owner": "maggie",
"status": "PENDING_APPROVAL",
"team": "design",
},
)
# Harry's leave request that is now in PENDING_APPROVAL state
harrys_pending_holiday_request = Resource(
**dataclasses.asdict(harrys_draft_holiday_request)
)
harrys_pending_holiday_request.attr["status"] = "PENDING_APPROVAL"
# Harry's leave request that is now in APPROVED state
harrys_approved_holiday_request = Resource(
**dataclasses.asdict(harrys_draft_holiday_request)
)
harrys_approved_holiday_request.attr["status"] = "APPROVED"
# Check whether the principal is allowed to perform a specific action on the given resource
def check(
client: CerbosClient,
principal: Principal,
action: str,
resource: Resource,
):
try:
effect = "_cannot_"
icon = emoji.emojize(":cross_mark:")
if client.is_allowed(action=action, principal=principal, resource=resource):
effect = "_can_"
icon = emoji.emojize(":thumbs_up:")
print(
f"{icon} {principal.id} {effect} {action} {resource.attr['owner']}'s "
f"{resource.attr['status']} holiday request"
)
except Exception as e:
print(f"Request failed: {e}")
if __name__ == "__main__":
# Use the Cerbos TestContainer to start Cerbos automatically. This is for demonstration purposes only.
# In production scenarios Cerbos would be deployed as a separate service.
container = CerbosContainer()
policy_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "policies")
container.with_volume_mapping(policy_dir, "/policies")
with container:
container.wait_until_ready()
host = container.http_host()
# Create a Cerbos client
with CerbosClient(host) as client:
# Check access to Harry's draft holiday request
for principal, action in [
(harry, "create"),
(harry, "view"),
(pedro, "view"),
(maggie, "view"),
(amanda, "view"),
(maggie, "approve"),
(amanda, "approve"),
(harry, "submit"),
]:
check(client, principal, action, harrys_draft_holiday_request)
# Check access to Harry's pending holiday request
print("\nHarry submits his holiday request\n")
for principal, action in [
(harry, "view"),
(harry, "approve"),
(pedro, "view"),
(amanda, "view"),
(maggie, "view"),
(amanda, "approve"),
(maggie, "approve"),
]:
check(client, principal, action, harrys_pending_holiday_request)
# Check access to Harry's approved holiday request
print("\nMaggie approves Harry's holiday request\n")
for principal, action in [
(harry, "view"),
(pedro, "view"),
(amanda, "view"),
(maggie, "view"),
]:
check(client, principal, action, harrys_approved_holiday_request)