askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_access_grant_equivalence.py · 8.4 KB · 178 lines
| 1 | """object_access.Access answers what the record API answers, grants included. |
| 2 | |
| 3 | The server resolves a subject's project grants (``project_access``, |
| 4 | ``projects.owner_id``) and record shares (``record_shares``) before it asks |
| 5 | the permission engine; Access did not, so a project member or a sharee |
| 6 | the record API allows was refused by every object that asked Access, and |
| 7 | an object could not ask ``Access.relation_refusal`` without refusing them |
| 8 | too. The resolution now lives in object_access (``with_grants``) and the |
| 9 | server calls it, so there is one implementation. |
| 10 | |
| 11 | The matrix asks both the same questions -- owner, project member (read and |
| 12 | write grants), sharee (read and write shares), stranger, admin role; read |
| 13 | and update; on a task in a project, a privately shared task and the |
| 14 | project itself -- and asserts the same answers: |
| 15 | |
| 16 | - enforcement on: the record API's status (GET, and a PUT that changes |
| 17 | nothing) against ``Access.can`` asked from inside an object the same |
| 18 | caller runs, so Access sees the ``_identity`` the server really passes; |
| 19 | - enforcement off: unenforced, the record API answers a session with 401 |
| 20 | before it decides anything (only the admin token is served), so there |
| 21 | is no HTTP answer to compare. The server's own subject resolution for |
| 22 | the same credentials and the engine calls an enforced request makes are |
| 23 | compared instead. Access is the policy's answer in every mode, which is |
| 24 | why the holes in test_object_pointer_grants were open with enforcement |
| 25 | off too. Access does not read the enforcement flag, so its answers in both |
| 26 | modes are also asserted identical. |
| 27 | """ |
| 28 | |
| 29 | from __future__ import annotations |
| 30 | |
| 31 | import json |
| 32 | |
| 33 | import pytest |
| 34 | from test_findings_access_control import _json, _text, make_site # noqa: F401 |
| 35 | |
| 36 | import object_permission_store |
| 37 | import object_permissions |
| 38 | import object_server |
| 39 | |
| 40 | PROBE = ''' |
| 41 | import os |
| 42 | import object_access |
| 43 | |
| 44 | |
| 45 | def POST(request): |
| 46 | access = object_access.Access(request.get("_identity"), |
| 47 | base_dir=os.environ["DBBASIC_DATA_DIR"]) |
| 48 | answers = [] |
| 49 | for action, collection, record_id in request["questions"]: |
| 50 | import object_records |
| 51 | record = object_records.get_collection_record( |
| 52 | collection, record_id, base_dir=os.environ["DBBASIC_DATA_DIR"]) |
| 53 | answers.append(access.can(action, collection, record)) |
| 54 | return {"answers": answers} |
| 55 | ''' |
| 56 | |
| 57 | USERS = { |
| 58 | "alice": (), # owns the project and both tasks |
| 59 | "carol": (), # project member, read |
| 60 | "wendy": (), # project member, write |
| 61 | "dave": (), # t-private shared with him, read |
| 62 | "sam": (), # t-private shared with him, write |
| 63 | "bob": (), # stranger |
| 64 | "root": ("admin",), # admin role |
| 65 | } |
| 66 | RECORDS = { |
| 67 | ("tasks", "t-proj"): {"title": "In the project"}, |
| 68 | ("tasks", "t-private"): {"title": "Private"}, |
| 69 | ("tasks", "t-assigned"): {"title": "Assigned"}, |
| 70 | ("projects", "p1"): {"name": "P1"}, |
| 71 | } |
| 72 | QUESTIONS = [(action, collection, record_id) |
| 73 | for (collection, record_id) in RECORDS for action in ("read", "update")] |
| 74 | |
| 75 | |
| 76 | def _box(make_site, monkeypatch): |
| 77 | site = make_site("app-tasks", "app-share") |
| 78 | headers = {name: site.user(name, roles=list(roles)) for name, roles in USERS.items()} |
| 79 | site.create("projects", {"id": "p1", "name": "P1", "owner_id": "alice"}) |
| 80 | site.create("tasks", {"id": "t-proj", "title": "In the project", "owner_id": "alice", |
| 81 | "project_id": "p1"}) |
| 82 | site.create("tasks", {"id": "t-private", "title": "Private", "owner_id": "alice"}) |
| 83 | site.create("tasks", {"id": "t-assigned", "title": "Assigned", "owner_id": "alice", |
| 84 | "assigned_to": "bob"}) |
| 85 | site.create("project_access", {"project_id": "p1", "user_id": "carol", "permission": "read"}) |
| 86 | site.create("project_access", {"project_id": "p1", "user_id": "wendy", "permission": "write"}) |
| 87 | for user, permission in (("dave", "read"), ("sam", "write")): |
| 88 | site.create("record_shares", {"collection": "tasks", "record_id": "t-private", |
| 89 | "user_id": user, "permission": permission, |
| 90 | "granted_by": "alice"}) |
| 91 | (site.root / "action").mkdir(exist_ok=True) |
| 92 | (site.root / "action" / "access_probe.py").write_text(PROBE) |
| 93 | path = object_permission_store.policy_path(site.data) |
| 94 | policy = json.loads(path.read_text()) |
| 95 | policy["rules"].append({"effect": "allow", "principal": "registered", "actions": ["execute"], |
| 96 | "object_id": "action_access_probe", "reason": "test probe"}) |
| 97 | path.write_text(json.dumps(policy)) |
| 98 | return site, headers |
| 99 | |
| 100 | |
| 101 | def _access_answers(site, headers): |
| 102 | response = site.execute("action_access_probe", {"questions": QUESTIONS}, headers) |
| 103 | assert response.status == 200, _text(response) |
| 104 | return _json(response)["answers"] |
| 105 | |
| 106 | |
| 107 | def _api_call(site, headers, action, collection, record_id): |
| 108 | path = f"/collections/{collection}/records/{record_id}" |
| 109 | if action == "read": |
| 110 | return site.call(path, headers=headers) |
| 111 | return site.call(path, "PUT", RECORDS[(collection, record_id)], headers) |
| 112 | |
| 113 | |
| 114 | def _api_statuses(site, headers): |
| 115 | return [_api_call(site, headers, *question).status for question in QUESTIONS] |
| 116 | |
| 117 | |
| 118 | def _server_answers(site, headers): |
| 119 | """Enforcement off: the question the record API would enforce, asked of |
| 120 | the server's own code -- the subject it resolves for these credentials |
| 121 | (object_server._permission_subject, grants included) and the two engine |
| 122 | calls an enforced GET or PUT makes (collection level, then the record). |
| 123 | Unenforced, the HTTP answer to a session is a 401 before any decision is |
| 124 | made (_admin_token_gate_error), so there is no HTTP answer to compare.""" |
| 125 | subject = object_server._permission_subject({name: value for name, value in headers}) |
| 126 | policy = object_permission_store.load_policy(site.data) |
| 127 | answers = [] |
| 128 | for action, collection, record_id in QUESTIONS: |
| 129 | assert _api_call(site, headers, action, collection, record_id).status == 401 |
| 130 | record = site.row(collection, record_id) |
| 131 | answers.append(all( |
| 132 | object_permissions.check_permission(subject, action, policy=policy, |
| 133 | collection=collection, record=asked).allowed |
| 134 | for asked in (None, record))) |
| 135 | return answers |
| 136 | |
| 137 | |
| 138 | EXPECTED = { |
| 139 | # (read, update) per record, in RECORDS order: t-proj, t-private, t-assigned, p1 |
| 140 | "alice": [True, True, True, True, True, True, True, True], |
| 141 | "carol": [True, False, False, False, False, False, True, False], |
| 142 | "wendy": [True, False, False, False, False, False, True, False], |
| 143 | "dave": [False, False, True, False, False, False, False, False], |
| 144 | "sam": [False, False, True, False, False, False, False, False], |
| 145 | "bob": [False, False, False, False, True, True, False, False], |
| 146 | "root": [True, True, True, True, True, True, True, True], |
| 147 | } |
| 148 | |
| 149 | |
| 150 | def test_access_and_the_record_api_agree_with_enforcement_on(make_site, monkeypatch): |
| 151 | site, headers = _box(make_site, monkeypatch) |
| 152 | for user, caller in headers.items(): |
| 153 | access = _access_answers(site, caller) |
| 154 | api = [status == 200 for status in _api_statuses(site, caller)] |
| 155 | assert access == api, (user, list(zip(QUESTIONS, access, api))) |
| 156 | assert access == EXPECTED[user], (user, list(zip(QUESTIONS, access))) |
| 157 | |
| 158 | |
| 159 | def test_access_and_the_servers_decisions_agree_with_enforcement_off(make_site, monkeypatch): |
| 160 | site, headers = _box(make_site, monkeypatch) |
| 161 | enforced = {user: _access_answers(site, caller) for user, caller in headers.items()} |
| 162 | monkeypatch.delenv("DBBASIC_ENABLE_PERMISSION_ENFORCEMENT") |
| 163 | for user, caller in headers.items(): |
| 164 | access = _access_answers(site, caller) |
| 165 | assert access == enforced[user], user # Access does not read the flag |
| 166 | server = _server_answers(site, caller) |
| 167 | assert access == server, (user, list(zip(QUESTIONS, access, server))) |
| 168 | |
| 169 | |
| 170 | def test_a_grant_taken_away_is_gone_for_access_on_the_next_request(make_site, monkeypatch): |
| 171 | """Fresh reads, as for the server: no grant outlives its row.""" |
| 172 | site, headers = _box(make_site, monkeypatch) |
| 173 | assert _access_answers(site, headers["carol"])[0] is True |
| 174 | import object_records |
| 175 | grant = next(row for row in site.rows("project_access") if row["user_id"] == "carol") |
| 176 | object_records.delete_collection_record("project_access", grant["id"], base_dir=site.data) |
| 177 | assert _access_answers(site, headers["carol"])[0] is False |
| 178 | assert site.call("/collections/tasks/records/t-proj", headers=headers["carol"]).status == 404 |