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_identity_source_readiness.py · 4.5 KB · 111 lines
| 1 | """A configured identity source counts as a user sign-in path for enforcement. |
| 2 | |
| 3 | Users who sign in only through an external verifier (DBBASIC_IDENTITY_VERIFY_URL) |
| 4 | have no local users, sessions or API keys. The readiness check used to call |
| 5 | that "No non-admin identity path". A verify URL that is set but invalid does |
| 6 | not count, and nothing here contacts the verifier. |
| 7 | """ |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | import object_identity_sources |
| 13 | import object_server |
| 14 | from test_object_server import request, save_permission_policy |
| 15 | |
| 16 | TOKEN = "t" * 32 |
| 17 | NO_PATH = ( |
| 18 | "No non-admin identity path is available; enable trusted headers, " |
| 19 | "guarded session login, password login, or create an active session " |
| 20 | "before enforcement." |
| 21 | ) |
| 22 | VERIFY = object_identity_sources.IDENTITY_VERIFY_URL_ENV |
| 23 | |
| 24 | |
| 25 | @pytest.fixture |
| 26 | def enforced(tmp_path, monkeypatch): |
| 27 | data_dir = tmp_path / "data" |
| 28 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 29 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(tmp_path / "objects")) |
| 30 | monkeypatch.setenv("DBBASIC_ADMIN_TOKEN", TOKEN) |
| 31 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 32 | for name in ( |
| 33 | object_server.ALLOW_BLOCKED_ENFORCEMENT_START_ENV, |
| 34 | object_server.PERMISSION_UNREADY_ENFORCEMENT_ENV, |
| 35 | object_server.PERMISSION_TRUST_HEADERS_ENV, |
| 36 | object_server.SESSION_LOGIN_ENV, |
| 37 | object_server.PASSWORD_LOGIN_ENV, |
| 38 | VERIFY, |
| 39 | ): |
| 40 | monkeypatch.delenv(name, raising=False) |
| 41 | monkeypatch.setenv(object_identity_sources.IDENTITY_VERIFY_SECRET_ENV, "test-shared-secret-0123") |
| 42 | monkeypatch.setenv(object_identity_sources.IDENTITY_AUDIENCE_ENV, "box-test.example") |
| 43 | save_permission_policy( |
| 44 | data_dir, |
| 45 | { |
| 46 | "access_mode": "registered", |
| 47 | "rules": [ |
| 48 | {"effect": "allow", "principal": "registered", "actions": ["read"], |
| 49 | "collection": "contacts"} |
| 50 | ], |
| 51 | }, |
| 52 | ) |
| 53 | return data_dir |
| 54 | |
| 55 | |
| 56 | def _blockers(): |
| 57 | """Readiness blockers: advisory now (enforcement stays on); see test_enforcement_fail_closed.""" |
| 58 | return object_server.object_permission_status.enforcement_readiness( |
| 59 | base_dir=object_server._data_dir(), permissions=object_server._permission_readiness_inputs() |
| 60 | )["blockers"] |
| 61 | |
| 62 | |
| 63 | def _problems(): |
| 64 | return object_server._permission_enforcement_problems() |
| 65 | |
| 66 | |
| 67 | def test_without_an_identity_source_the_old_blocker_stands_as_a_problem(enforced): |
| 68 | assert _blockers() == [NO_PATH] |
| 69 | assert _problems() == [object_server.object_permission_status.NO_SIGN_IN_PATH_PROBLEM] |
| 70 | # a problem is reported, never acted on by opening |
| 71 | assert object_server._permission_enforcement_enabled() is True |
| 72 | assert object_server._permission_enforcement_blocked() is False |
| 73 | |
| 74 | |
| 75 | def test_identity_source_counts_as_a_sign_in_path(enforced, monkeypatch): |
| 76 | monkeypatch.setenv(VERIFY, "https://auth.example.com/verify/") |
| 77 | assert _blockers() == [] |
| 78 | assert _problems() == [] |
| 79 | assert object_server._permission_enforcement_problem() is None |
| 80 | |
| 81 | |
| 82 | @pytest.mark.parametrize( |
| 83 | "bad", |
| 84 | ["ftp://auth.example.com/verify/", "auth.example.com/verify", " ", |
| 85 | "http://auth.example.com/verify/"], |
| 86 | ) |
| 87 | def test_invalid_verify_url_does_not_count(enforced, monkeypatch, bad): |
| 88 | monkeypatch.setenv(VERIFY, bad) |
| 89 | assert _blockers() == [NO_PATH] |
| 90 | assert _problems() == [object_server.object_permission_status.NO_SIGN_IN_PATH_PROBLEM] |
| 91 | |
| 92 | |
| 93 | def test_status_lists_the_identity_source_and_drops_the_no_sessions_warning(enforced, monkeypatch): |
| 94 | headers = [("authorization", f"Token {TOKEN}")] |
| 95 | _, _, before = request("/permissions/status", headers=headers) |
| 96 | assert before["permissions"]["identity_source_configured"] is False |
| 97 | assert any("no active DBBASIC sessions" in w for w in before["warnings"]) |
| 98 | monkeypatch.setenv(VERIFY, "https://auth.example.com/verify/") |
| 99 | _, _, after = request("/permissions/status", headers=headers) |
| 100 | assert after["permissions"]["identity_source_configured"] is True |
| 101 | assert after["readiness"] == {"can_enable_enforcement": True, "blockers": []} |
| 102 | assert not any("no active DBBASIC sessions" in w for w in after["warnings"]) |
| 103 | |
| 104 | |
| 105 | def test_a_remote_source_without_its_secret_or_audience_does_not_count(enforced, monkeypatch): |
| 106 | monkeypatch.setenv(VERIFY, "https://auth.example.com/verify/") |
| 107 | monkeypatch.delenv(object_identity_sources.IDENTITY_VERIFY_SECRET_ENV) |
| 108 | assert _blockers() == [NO_PATH] |
| 109 | monkeypatch.setenv(object_identity_sources.IDENTITY_VERIFY_SECRET_ENV, "test-shared-secret-0123") |
| 110 | monkeypatch.delenv(object_identity_sources.IDENTITY_AUDIENCE_ENV) |
| 111 | assert _blockers() == [NO_PATH] |