Skip to content

Commit 24a11a1

Browse files
danielballannmaytan
authored andcommitted
Type access policy methods, and introduce aliases.
1 parent 950a475 commit 24a11a1

File tree

5 files changed

+145
-62
lines changed

5 files changed

+145
-62
lines changed

tiled/_tests/test_protocols.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from ..structures.core import Spec, StructureFamily
2929
from ..structures.sparse import COOStructure
3030
from ..structures.table import TableStructure
31-
from ..type_aliases import JSON, Filters, Scopes
31+
from ..type_aliases import JSON, AccessBlob, AccessTags, Filters, Scopes
3232

3333

3434
class CustomArrayAdapter:
@@ -379,11 +379,30 @@ def __init__(self, scopes: Optional[Scopes] = None) -> None:
379379
def _get_id(self, principal: Principal) -> None:
380380
return None
381381

382+
async def init_node(
383+
self,
384+
principal: Principal,
385+
authn_access_tags: Optional[AccessTags],
386+
authn_scopes: Scopes,
387+
access_blob: Optional[AccessBlob] = None,
388+
) -> Tuple[bool, Optional[AccessBlob]]:
389+
return (False, access_blob)
390+
391+
async def modify_node(
392+
self,
393+
node: BaseAdapter,
394+
principal: Principal,
395+
authn_access_tags: Optional[AccessTags],
396+
authn_scopes: Scopes,
397+
access_blob: Optional[AccessBlob] = None,
398+
) -> Tuple[bool, Optional[AccessBlob]]:
399+
return (False, access_blob)
400+
382401
async def allowed_scopes(
383402
self,
384403
node: BaseAdapter,
385404
principal: Principal,
386-
authn_access_tags: Optional[Set[str]],
405+
authn_access_tags: Optional[AccessTags],
387406
authn_scopes: Scopes,
388407
) -> Scopes:
389408
allowed = self.scopes
@@ -394,7 +413,7 @@ async def filters(
394413
self,
395414
node: BaseAdapter,
396415
principal: Principal,
397-
authn_access_tags: Optional[Set[str]],
416+
authn_access_tags: Optional[AccessTags],
398417
authn_scopes: Scopes,
399418
scopes: Scopes,
400419
) -> Filters:

tiled/access_control/access_policies.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import logging
22
import os
3+
from types import Optional, Tuple
34

5+
from ..adapters.protocols import BaseAdapter
46
from ..queries import AccessBlobFilter
7+
from ..server.schemas import Principal
8+
from ..type_aliases import AccessBlob, AccessTags, Filters, Scopes
59
from ..utils import Sentinel, import_object
610
from .protocols import AccessPolicy
711
from .scopes import ALL_SCOPES, PUBLIC_SCOPES
@@ -24,10 +28,35 @@
2428
class DummyAccessPolicy(AccessPolicy):
2529
"Impose no access restrictions."
2630

27-
async def allowed_scopes(self, node, principal, authn_access_tags, authn_scopes):
31+
async def init_node(
32+
self,
33+
principal: Principal,
34+
authn_access_tags: Optional[AccessTags],
35+
authn_scopes: Scopes,
36+
access_blob: Optional[AccessBlob] = None,
37+
) -> Tuple[bool, AccessBlob]:
38+
"Do nothing; there is no persistent state to initialize."
39+
return (False, access_blob)
40+
41+
async def allowed_scopes(
42+
self,
43+
node: BaseAdapter,
44+
principal: Principal,
45+
authn_access_tags: Optional[AccessTags],
46+
authn_scopes: Scopes,
47+
) -> Scopes:
48+
"Always allow all scopes."
2849
return ALL_SCOPES
2950

30-
async def filters(self, node, principal, authn_access_tags, authn_scopes, scopes):
51+
async def filters(
52+
self,
53+
node: BaseAdapter,
54+
principal: Principal,
55+
authn_access_tags: Optional[AccessTags],
56+
authn_scopes: Scopes,
57+
scopes: Scopes,
58+
) -> Filters:
59+
"Always impose no filtering on results."
3160
return []
3261

3362

@@ -74,8 +103,12 @@ def _is_admin(self, authn_scopes):
74103
return False
75104

76105
async def init_node(
77-
self, principal, authn_access_tags, authn_scopes, access_blob=None
78-
):
106+
self,
107+
principal: Principal,
108+
authn_access_tags: Optional[AccessTags],
109+
authn_scopes: Scopes,
110+
access_blob: Optional[AccessBlob] = None,
111+
) -> Tuple[bool, AccessBlob]:
79112
if principal.type == "service":
80113
identifier = str(principal.uuid)
81114
else:
@@ -157,8 +190,13 @@ async def init_node(
157190
return access_blob_modified, access_blob_from_policy
158191

159192
async def modify_node(
160-
self, node, principal, authn_access_tags, authn_scopes, access_blob
161-
):
193+
self,
194+
node: BaseAdapter,
195+
principal: Principal,
196+
authn_access_tags: Optional[AccessTags],
197+
authn_scopes: Scopes,
198+
access_blob: Optional[AccessBlob],
199+
) -> Tuple[bool, AccessBlob]:
162200
if principal.type == "service":
163201
identifier = str(principal.uuid)
164202
else:
@@ -279,7 +317,13 @@ async def modify_node(
279317
# modified means the blob to-be-used was changed in comparison to the user input
280318
return access_blob_modified, access_blob_from_policy
281319

282-
async def allowed_scopes(self, node, principal, authn_access_tags, authn_scopes):
320+
async def allowed_scopes(
321+
self,
322+
node: BaseAdapter,
323+
principal: Principal,
324+
authn_access_tags: Optional[AccessTags],
325+
authn_scopes: Scopes,
326+
) -> Scopes:
283327
# If this is being called, filter_for_access has let us get this far.
284328
# However, filters and allowed_scopes should always be implemented to
285329
# give answers consistent with each other.
@@ -318,7 +362,14 @@ async def allowed_scopes(self, node, principal, authn_access_tags, authn_scopes)
318362

319363
return allowed
320364

321-
async def filters(self, node, principal, authn_access_tags, authn_scopes, scopes):
365+
async def filters(
366+
self,
367+
node: BaseAdapter,
368+
principal: Principal,
369+
authn_access_tags: Optional[AccessTags],
370+
authn_scopes: Scopes,
371+
scopes: Scopes,
372+
) -> Filters:
322373
queries = []
323374
query_filter = AccessBlobFilter
324375

tiled/access_control/protocols.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
from abc import ABC, abstractmethod
2-
from typing import Any, Optional, Set
2+
from typing import Optional, Tuple
33

44
from ..adapters.protocols import BaseAdapter
55
from ..server.schemas import Principal
6-
from ..type_aliases import Filters, Scopes
6+
from ..type_aliases import AccessBlob, AccessTags, Filters, Scopes
77

88

99
class AccessPolicy(ABC):
10+
@abstractmethod
11+
async def init_node(
12+
self,
13+
principal: Principal,
14+
authn_access_tags: Optional[AccessTags],
15+
authn_scopes: Scopes,
16+
access_blob: Optional[AccessBlob] = None,
17+
) -> Tuple[bool, Optional[AccessBlob]]:
18+
pass
19+
20+
async def modify_node(
21+
self,
22+
node: BaseAdapter,
23+
principal: Principal,
24+
authn_access_tags: Optional[AccessTags],
25+
authn_scopes: Scopes,
26+
access_blob: Optional[AccessBlob],
27+
) -> Tuple[bool, Optional[AccessBlob]]:
28+
return (False, access_blob)
29+
1030
@abstractmethod
1131
async def allowed_scopes(
1232
self,
1333
node: BaseAdapter,
1434
principal: Principal,
15-
authn_access_tags: Optional[Set[str]],
35+
authn_access_tags: Optional[AccessTags],
1636
authn_scopes: Scopes,
1737
) -> Scopes:
1838
pass
@@ -22,17 +42,8 @@ async def filters(
2242
self,
2343
node: BaseAdapter,
2444
principal: Principal,
25-
authn_access_tags: Optional[Set[str]],
45+
authn_access_tags: Optional[AccessTags],
2646
authn_scopes: Scopes,
2747
scopes: Scopes,
2848
) -> Filters:
2949
pass
30-
31-
async def modify_node(
32-
self,
33-
node: BaseAdapter,
34-
principal: Principal,
35-
authn_scopes: Scopes,
36-
access_blob: Optional[dict[str, Any]],
37-
) -> tuple[bool, Optional[dict[str, Any]]]:
38-
return (False, access_blob)

0 commit comments

Comments
 (0)