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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_permission_audit.py · 3.0 KB · 107 lines
| 1 | """Permission decision audit log.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | from collections import deque |
| 7 | from pathlib import Path |
| 8 | from typing import Any |
| 9 | |
| 10 | from object_versions import DEFAULT_DATA_DIR |
| 11 | |
| 12 | PERMISSIONS_DIR = "permissions" |
| 13 | AUDIT_FILE = "audit.jsonl" |
| 14 | |
| 15 | |
| 16 | def audit_path(base_dir: Path | str = DEFAULT_DATA_DIR) -> Path: |
| 17 | """Return the permission audit file path.""" |
| 18 | root = Path(base_dir) / PERMISSIONS_DIR |
| 19 | path = root / AUDIT_FILE |
| 20 | resolved_root = root.resolve(strict=False) |
| 21 | resolved_path = path.resolve(strict=False) |
| 22 | |
| 23 | try: |
| 24 | resolved_path.relative_to(resolved_root) |
| 25 | except ValueError as exc: |
| 26 | raise ValueError("Permission audit path escapes permissions directory") from exc |
| 27 | |
| 28 | return path |
| 29 | |
| 30 | |
| 31 | def append_permission_audit( |
| 32 | entry: dict[str, Any], |
| 33 | base_dir: Path | str = DEFAULT_DATA_DIR, |
| 34 | ) -> Path: |
| 35 | """Append one JSON-line permission audit entry.""" |
| 36 | path = audit_path(base_dir) |
| 37 | path.parent.mkdir(parents=True, exist_ok=True) |
| 38 | |
| 39 | with path.open("a", encoding="utf-8") as handle: |
| 40 | handle.write(json.dumps(entry, sort_keys=True, separators=(",", ":")) + "\n") |
| 41 | |
| 42 | return path |
| 43 | |
| 44 | |
| 45 | def get_permission_audit( |
| 46 | base_dir: Path | str = DEFAULT_DATA_DIR, |
| 47 | *, |
| 48 | limit: int = 100, |
| 49 | action: str | None = None, |
| 50 | object_id: str | None = None, |
| 51 | collection: str | None = None, |
| 52 | allowed: bool | None = None, |
| 53 | enforced: bool | None = None, |
| 54 | ) -> list[dict[str, Any]]: |
| 55 | """Read recent permission audit entries.""" |
| 56 | if limit < 1: |
| 57 | raise ValueError("Permission audit limit must be at least 1") |
| 58 | |
| 59 | path = audit_path(base_dir) |
| 60 | if not path.exists(): |
| 61 | return [] |
| 62 | |
| 63 | entries: deque[dict[str, Any]] = deque(maxlen=limit) |
| 64 | with path.open(encoding="utf-8") as handle: |
| 65 | for line in handle: |
| 66 | if not line.strip(): |
| 67 | continue |
| 68 | try: |
| 69 | entry = json.loads(line) |
| 70 | except json.JSONDecodeError: |
| 71 | continue |
| 72 | if _matches_filters( |
| 73 | entry, |
| 74 | action=action, |
| 75 | object_id=object_id, |
| 76 | collection=collection, |
| 77 | allowed=allowed, |
| 78 | enforced=enforced, |
| 79 | ): |
| 80 | entries.append(entry) |
| 81 | |
| 82 | return list(entries) |
| 83 | |
| 84 | |
| 85 | def _matches_filters( |
| 86 | entry: dict[str, Any], |
| 87 | *, |
| 88 | action: str | None, |
| 89 | object_id: str | None, |
| 90 | collection: str | None, |
| 91 | allowed: bool | None, |
| 92 | enforced: bool | None, |
| 93 | ) -> bool: |
| 94 | if action is not None and entry.get("action") != action: |
| 95 | return False |
| 96 | if object_id is not None and entry.get("object_id") != object_id: |
| 97 | return False |
| 98 | if collection is not None and entry.get("collection") != collection: |
| 99 | return False |
| 100 | if enforced is not None and entry.get("enforced") is not enforced: |
| 101 | return False |
| 102 | if allowed is not None: |
| 103 | decision = entry.get("decision") |
| 104 | if not isinstance(decision, dict) or decision.get("allowed") is not allowed: |
| 105 | return False |
| 106 | |
| 107 | return True |