Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/object_permission_audit.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3from __future__ import annotations
4
5import json
6from collections import deque
7from pathlib import Path
8from typing import Any
9
10from object_versions import DEFAULT_DATA_DIR
11
12PERMISSIONS_DIR = "permissions"
13AUDIT_FILE = "audit.jsonl"
14
15
16def 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
31def 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
45def 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
85def _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