|
1 | 1 | import logging |
2 | 2 | import os |
| 3 | +from typing import Optional, Tuple |
3 | 4 |
|
| 5 | +from ..adapters.protocols import BaseAdapter |
4 | 6 | from ..queries import AccessBlobFilter |
| 7 | +from ..server.schemas import Principal |
| 8 | +from ..type_aliases import AccessBlob, AccessTags, Filters, Scopes |
5 | 9 | from ..utils import Sentinel, import_object |
| 10 | +from .protocols import AccessPolicy |
6 | 11 | from .scopes import ALL_SCOPES, PUBLIC_SCOPES |
7 | 12 |
|
8 | 13 | ALL_ACCESS = Sentinel("ALL_ACCESS") |
|
20 | 25 | logger.setLevel(log_level.upper()) |
21 | 26 |
|
22 | 27 |
|
23 | | -class DummyAccessPolicy: |
| 28 | +class DummyAccessPolicy(AccessPolicy): |
24 | 29 | "Impose no access restrictions." |
25 | 30 |
|
26 | | - 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." |
27 | 49 | return ALL_SCOPES |
28 | 50 |
|
29 | | - 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." |
30 | 60 | return [] |
31 | 61 |
|
32 | 62 |
|
33 | | -class TagBasedAccessPolicy: |
| 63 | +class TagBasedAccessPolicy(AccessPolicy): |
34 | 64 | def __init__( |
35 | 65 | self, |
36 | 66 | *, |
@@ -73,8 +103,12 @@ def _is_admin(self, authn_scopes): |
73 | 103 | return False |
74 | 104 |
|
75 | 105 | async def init_node( |
76 | | - self, principal, authn_access_tags, authn_scopes, access_blob=None |
77 | | - ): |
| 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]: |
78 | 112 | if principal.type == "service": |
79 | 113 | identifier = str(principal.uuid) |
80 | 114 | else: |
@@ -156,8 +190,13 @@ async def init_node( |
156 | 190 | return access_blob_modified, access_blob_from_policy |
157 | 191 |
|
158 | 192 | async def modify_node( |
159 | | - self, node, principal, authn_access_tags, authn_scopes, access_blob |
160 | | - ): |
| 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]: |
161 | 200 | if principal.type == "service": |
162 | 201 | identifier = str(principal.uuid) |
163 | 202 | else: |
@@ -278,7 +317,13 @@ async def modify_node( |
278 | 317 | # modified means the blob to-be-used was changed in comparison to the user input |
279 | 318 | return access_blob_modified, access_blob_from_policy |
280 | 319 |
|
281 | | - 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: |
282 | 327 | # If this is being called, filter_for_access has let us get this far. |
283 | 328 | # However, filters and allowed_scopes should always be implemented to |
284 | 329 | # give answers consistent with each other. |
@@ -317,7 +362,14 @@ async def allowed_scopes(self, node, principal, authn_access_tags, authn_scopes) |
317 | 362 |
|
318 | 363 | return allowed |
319 | 364 |
|
320 | | - 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: |
321 | 373 | queries = [] |
322 | 374 | query_filter = AccessBlobFilter |
323 | 375 |
|
|
0 commit comments