-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapability_access.py
More file actions
363 lines (296 loc) · 12.5 KB
/
Copy pathcapability_access.py
File metadata and controls
363 lines (296 loc) · 12.5 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
"""
Capability-Based Access Control System
This module implements a capability-based access control system where
capabilities are unforgeable tokens that grant specific permissions to
principals for accessing resources.
"""
import uuid
from typing import Dict, List, Set, Optional, Any
from enum import Enum
from dataclasses import dataclass
class Permission(Enum):
"""Enumeration of possible permissions."""
READ = "read"
WRITE = "write"
EXECUTE = "execute"
DELETE = "delete"
@dataclass(frozen=True)
class Resource:
"""Represents a resource that can be accessed."""
id: str
name: str
metadata: Dict[str, Any]
def __hash__(self) -> int:
return hash(self.id)
class Principal:
"""Represents an entity that can hold capabilities."""
def __init__(self, name: str) -> None:
self.name = name
self.capabilities: Set['Capability'] = set()
def __str__(self) -> str:
return f"Principal({self.name})"
def __repr__(self) -> str:
return self.__str__()
class Capability:
"""Represents an unforgeable token granting access to a resource."""
def __init__(
self,
principal: Principal,
resource: Resource,
permissions: Set[Permission],
delegable: bool = True,
attenuated: bool = False,
parent: Optional['Capability'] = None
) -> None:
self.id = str(uuid.uuid4())
self.principal = principal
self.resource = resource
self.permissions = frozenset(permissions)
self.delegable = delegable
self.attenuated = attenuated
self.parent = parent
self.children: Set['Capability'] = set()
if parent:
parent.children.add(self)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Capability):
return False
return self.id == other.id
def __hash__(self) -> int:
return hash(self.id)
def __str__(self) -> str:
return f"Capability(id={self.id[:8]}, resource={self.resource.name}, " \
f"permissions={self.permissions}, delegable={self.delegable})"
def __repr__(self) -> str:
return self.__str__()
class CapabilitySystem:
"""Main system for managing capabilities."""
def __init__(self) -> None:
self.principals: Dict[str, Principal] = {}
self.resources: Dict[str, Resource] = {}
self.capabilities: Dict[str, Capability] = {}
def create_principal(self, name: str) -> Principal:
"""Create a new principal."""
if name in self.principals:
raise ValueError(f"Principal {name} already exists")
principal = Principal(name)
self.principals[name] = principal
return principal
def create_resource(
self,
name: str,
metadata: Optional[Dict[str, Any]] = None
) -> Resource:
"""Create a new resource."""
resource_id = str(uuid.uuid4())
if metadata is None:
metadata = {}
resource = Resource(id=resource_id, name=name, metadata=metadata)
self.resources[resource_id] = resource
return resource
def grant(
self,
principal: Principal,
resource: Resource,
permissions: Set[Permission],
delegable: bool = True
) -> Capability:
"""
Grant a new capability to a principal for a resource.
Args:
principal: The principal to grant the capability to
resource: The resource to grant access to
permissions: Set of permissions to grant
delegable: Whether the capability can be delegated to others
Returns:
The newly created capability
"""
if not permissions:
raise ValueError("At least one permission must be granted")
capability = Capability(
principal=principal,
resource=resource,
permissions=permissions,
delegable=delegable
)
principal.capabilities.add(capability)
self.capabilities[capability.id] = capability
return capability
def revoke(self, capability: Capability) -> None:
"""
Revoke a capability and all its descendants.
Args:
capability: The capability to revoke
"""
if capability.id not in self.capabilities:
raise ValueError("Capability does not exist in system")
# Recursively revoke all children
for child in list(capability.children):
self.revoke(child)
# Remove from principal
capability.principal.capabilities.discard(capability)
# Remove from system
del self.capabilities[capability.id]
# Remove from parent if exists
if capability.parent:
capability.parent.children.discard(capability)
def delegate(
self,
from_principal: Principal,
to_principal: Principal,
capability: Capability,
permissions: Optional[Set[Permission]] = None
) -> Capability:
"""
Delegate a capability from one principal to another.
Args:
from_principal: The principal delegating the capability
to_principal: The principal receiving the capability
capability: The capability to delegate
permissions: Optional subset of permissions to delegate
Returns:
The new delegated capability
"""
if capability not in from_principal.capabilities:
raise ValueError("Principal does not hold the specified capability")
if not capability.delegable:
raise ValueError("Capability is not delegable")
# If no permissions specified, delegate all permissions
if permissions is None:
permissions = set(capability.permissions)
# Check that requested permissions are a subset of available permissions
if not permissions.issubset(capability.permissions):
raise ValueError("Requested permissions exceed available permissions")
# Create new delegated capability
delegated_capability = Capability(
principal=to_principal,
resource=capability.resource,
permissions=permissions,
delegable=capability.delegable,
parent=capability
)
to_principal.capabilities.add(delegated_capability)
self.capabilities[delegated_capability.id] = delegated_capability
return delegated_capability
def attenuate(
self,
principal: Principal,
capability: Capability,
permissions: Set[Permission]
) -> Capability:
"""
Create an attenuated (restricted) version of a capability.
Args:
principal: The principal to create the attenuated capability for
capability: The capability to attenuate
permissions: The restricted set of permissions
Returns:
The new attenuated capability
"""
if capability not in principal.capabilities:
raise ValueError("Principal does not hold the specified capability")
# Check that requested permissions are a subset of available permissions
if not permissions.issubset(capability.permissions):
raise ValueError("Requested permissions exceed available permissions")
# Create new attenuated capability
attenuated_capability = Capability(
principal=principal,
resource=capability.resource,
permissions=permissions,
delegable=capability.delegable,
attenuated=True,
parent=capability
)
principal.capabilities.add(attenuated_capability)
self.capabilities[attenuated_capability.id] = attenuated_capability
return attenuated_capability
def check_access(
self,
principal: Principal,
resource: Resource,
permission: Permission
) -> bool:
"""
Check if a principal has a specific permission for a resource.
Args:
principal: The principal to check
resource: The resource to check access to
permission: The permission to check for
Returns:
True if access is granted, False otherwise
"""
for capability in principal.capabilities:
if (capability.resource == resource and
permission in capability.permissions):
return True
return False
def main() -> None:
"""Self-test: THE SECURITY LAW — every forbidden action is attempted and
must be denied; grants/delegation/attenuation/revocation exact."""
system = CapabilitySystem()
admin = system.create_principal("admin")
user1 = system.create_principal("user1")
user2 = system.create_principal("user2")
file1 = system.create_resource("confidential.txt", {"sensitivity": "high"})
file2 = system.create_resource("public.txt", {"sensitivity": "low"})
admin_cap = system.grant(admin, file1,
{Permission.READ, Permission.WRITE, Permission.DELETE},
delegable=True)
user1_cap = system.grant(user1, file2, {Permission.READ}, delegable=False)
live_perms = sum([system.check_access(admin, file1, Permission.READ),
system.check_access(admin, file1, Permission.WRITE),
system.check_access(admin, file1, Permission.DELETE)])
assert live_perms == 3, f"admin grant must confer exactly 3 permissions, got {live_perms}"
# Grants confer exactly the named permissions — nothing more.
assert system.check_access(admin, file1, Permission.READ) is True
assert system.check_access(admin, file1, Permission.DELETE) is True
assert system.check_access(user1, file2, Permission.READ) is True
assert system.check_access(user1, file2, Permission.WRITE) is False, \
"user1 granted READ but can WRITE"
assert system.check_access(user2, file1, Permission.READ) is False, \
"user2 has NO capability but can read the confidential file"
assert system.check_access(user1, file1, Permission.READ) is False
# Delegation: admin passes READ (a subset) to user1.
delegated = system.delegate(admin, user1, admin_cap, {Permission.READ})
assert system.check_access(user1, file1, Permission.READ) is True, \
"delegation did not confer READ"
assert system.check_access(user1, file1, Permission.WRITE) is False, \
"delegating READ leaked WRITE"
# FORBIDDEN: delegating a non-delegable capability.
try:
system.delegate(user1, user2, user1_cap)
assert False, "non-delegable capability was delegated"
except ValueError:
pass
# FORBIDDEN: delegating permissions the delegator does not hold.
try:
system.delegate(admin, user2, admin_cap,
{Permission.READ, Permission.EXECUTE})
assert False, "delegation AMPLIFIED permissions (EXECUTE never granted)"
except ValueError:
pass
assert system.check_access(user2, file1, Permission.READ) is False, \
"failed delegation still granted access"
# Attenuation narrows; it must never widen.
attenuated = system.attenuate(user1, delegated, {Permission.READ})
assert Permission.READ in attenuated.permissions
assert len(attenuated.permissions) == 1
try:
system.attenuate(user1, attenuated, {Permission.READ, Permission.WRITE})
assert False, "attenuation WIDENED permissions"
except ValueError:
pass
# Revocation of the root capability cuts the whole delegation chain.
assert system.check_access(user1, file1, Permission.READ) is True
system.revoke(admin_cap)
assert system.check_access(admin, file1, Permission.READ) is False, \
"revoked capability still grants its holder access"
assert system.check_access(user1, file1, Permission.READ) is False, \
"revoking the root left the DELEGATED capability alive"
# Unrelated capability untouched.
assert system.check_access(user1, file2, Permission.READ) is True, \
"revocation of file1's chain damaged file2 access"
print("capability_access: exact grants, no ambient authority, delegation "
"subset-only, attenuation narrows, revocation kills the chain — PASS")
if __name__ == "__main__":
main()