dbctl 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dbctl/__init__.py +5 -0
- dbctl/__main__.py +6 -0
- dbctl/audit.py +74 -0
- dbctl/cli.py +880 -0
- dbctl/config.py +263 -0
- dbctl/connections.py +52 -0
- dbctl/db.py +117 -0
- dbctl/execute.py +117 -0
- dbctl/init.py +159 -0
- dbctl/multi.py +53 -0
- dbctl/operations.py +40 -0
- dbctl/reports.py +136 -0
- dbctl/runtime.py +110 -0
- dbctl/tunnels/__init__.py +20 -0
- dbctl/tunnels/base.py +71 -0
- dbctl/tunnels/direct.py +28 -0
- dbctl/tunnels/ssh.py +73 -0
- dbctl/tunnels/ssm.py +142 -0
- dbctl-0.1.1.dist-info/METADATA +323 -0
- dbctl-0.1.1.dist-info/RECORD +22 -0
- dbctl-0.1.1.dist-info/WHEEL +4 -0
- dbctl-0.1.1.dist-info/entry_points.txt +2 -0
dbctl/__init__.py
ADDED
dbctl/__main__.py
ADDED
dbctl/audit.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Append-only JSONL audit log at ~/.dbctl/history.jsonl."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import time
|
|
7
|
+
import uuid
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from dbctl.config import history_path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def append(
|
|
14
|
+
*,
|
|
15
|
+
profile: str | None,
|
|
16
|
+
connection: str,
|
|
17
|
+
operation: str | None,
|
|
18
|
+
params: dict | None,
|
|
19
|
+
mode: str,
|
|
20
|
+
status: str,
|
|
21
|
+
rows_affected: int | None = None,
|
|
22
|
+
duration_ms: float = 0.0,
|
|
23
|
+
actor: str | None = None,
|
|
24
|
+
redact: set[str] | None = None,
|
|
25
|
+
) -> str:
|
|
26
|
+
entry = {
|
|
27
|
+
"run_id": uuid.uuid4().hex[:12],
|
|
28
|
+
"ts": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
|
|
29
|
+
"connection": connection,
|
|
30
|
+
"operation": operation,
|
|
31
|
+
"mode": mode,
|
|
32
|
+
"params": _redact(params or {}, redact or set()),
|
|
33
|
+
"status": status,
|
|
34
|
+
"rows_affected": rows_affected,
|
|
35
|
+
"duration_ms": round(duration_ms, 1),
|
|
36
|
+
"actor": actor,
|
|
37
|
+
}
|
|
38
|
+
path: Path = history_path(profile)
|
|
39
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
40
|
+
with path.open("a", encoding="utf-8") as f:
|
|
41
|
+
f.write(json.dumps(entry, default=str) + "\n")
|
|
42
|
+
return entry["run_id"]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _redact(params: dict, secret_names: set[str]) -> dict:
|
|
46
|
+
return {k: ("***" if k in secret_names else v) for k, v in params.items()}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def read(profile: str | None, *, limit: int = 50) -> list[dict]:
|
|
50
|
+
"""Return the last ``limit`` parseable entries, oldest→newest.
|
|
51
|
+
|
|
52
|
+
We parse *every* line and only truncate at the end: a half-written tail
|
|
53
|
+
line (crash mid-append) must not evict a valid older entry from the
|
|
54
|
+
window the way ``lines[-limit:]`` would have.
|
|
55
|
+
"""
|
|
56
|
+
path = history_path(profile)
|
|
57
|
+
if not path.exists():
|
|
58
|
+
return []
|
|
59
|
+
entries: list[dict] = []
|
|
60
|
+
for line in path.read_text(encoding="utf-8").splitlines():
|
|
61
|
+
if not line.strip():
|
|
62
|
+
continue
|
|
63
|
+
try:
|
|
64
|
+
entries.append(json.loads(line))
|
|
65
|
+
except json.JSONDecodeError:
|
|
66
|
+
continue
|
|
67
|
+
return entries[-limit:] if limit > 0 else entries
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def last_for(profile: str | None, connection: str) -> dict | None:
|
|
71
|
+
for entry in reversed(read(profile, limit=200)):
|
|
72
|
+
if entry.get("connection") == connection and entry.get("operation"):
|
|
73
|
+
return entry
|
|
74
|
+
return None
|