secondkey 0.2.0__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.
- secondkey/__init__.py +3 -0
- secondkey/__main__.py +3 -0
- secondkey/audit.py +98 -0
- secondkey/classify.py +616 -0
- secondkey/cli.py +651 -0
- secondkey/default_policy.yaml +85 -0
- secondkey/gate.py +192 -0
- secondkey/hooks/__init__.py +0 -0
- secondkey/hooks/claude_code.py +67 -0
- secondkey/hooks/cursor.py +77 -0
- secondkey/notify.py +36 -0
- secondkey/policy.py +165 -0
- secondkey/proxy.py +244 -0
- secondkey/snapshots/__init__.py +155 -0
- secondkey/snapshots/aws.py +109 -0
- secondkey/snapshots/base.py +48 -0
- secondkey/snapshots/docker.py +70 -0
- secondkey/snapshots/files.py +108 -0
- secondkey/snapshots/git.py +216 -0
- secondkey/snapshots/k8s.py +97 -0
- secondkey/snapshots/mysql.py +104 -0
- secondkey/snapshots/postgres.py +388 -0
- secondkey/snapshots/terraform.py +74 -0
- secondkey/sql.py +142 -0
- secondkey/store.py +211 -0
- secondkey/util.py +108 -0
- secondkey-0.2.0.dist-info/METADATA +208 -0
- secondkey-0.2.0.dist-info/RECORD +32 -0
- secondkey-0.2.0.dist-info/WHEEL +5 -0
- secondkey-0.2.0.dist-info/entry_points.txt +2 -0
- secondkey-0.2.0.dist-info/licenses/LICENSE +202 -0
- secondkey-0.2.0.dist-info/top_level.txt +1 -0
secondkey/__init__.py
ADDED
secondkey/__main__.py
ADDED
secondkey/audit.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Append-only, hash-chained audit log (JSON lines).
|
|
2
|
+
|
|
3
|
+
Each entry stores the hash of the previous entry, so editing or deleting a
|
|
4
|
+
line breaks the chain and `secondkey log --verify` reports where.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import hashlib
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any, Iterator
|
|
14
|
+
|
|
15
|
+
from .util import home, now
|
|
16
|
+
|
|
17
|
+
try:
|
|
18
|
+
import fcntl # POSIX only
|
|
19
|
+
except ImportError: # pragma: no cover
|
|
20
|
+
fcntl = None # type: ignore
|
|
21
|
+
|
|
22
|
+
GENESIS = "0" * 64
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def log_path() -> Path:
|
|
26
|
+
return home() / "audit.jsonl"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _digest(entry: dict[str, Any]) -> str:
|
|
30
|
+
body = {k: v for k, v in entry.items() if k != "hash"}
|
|
31
|
+
return hashlib.sha256(json.dumps(body, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _last_hash(fh) -> str:
|
|
35
|
+
fh.seek(0, os.SEEK_END)
|
|
36
|
+
size = fh.tell()
|
|
37
|
+
if size == 0:
|
|
38
|
+
return GENESIS
|
|
39
|
+
block, data = 4096, b""
|
|
40
|
+
pos = size
|
|
41
|
+
while pos > 0:
|
|
42
|
+
step = min(block, pos)
|
|
43
|
+
pos -= step
|
|
44
|
+
fh.seek(pos)
|
|
45
|
+
data = fh.read(step) + data
|
|
46
|
+
lines = data.rstrip(b"\n").split(b"\n")
|
|
47
|
+
if len(lines) > 1 or pos == 0:
|
|
48
|
+
return json.loads(lines[-1])["hash"]
|
|
49
|
+
return GENESIS # pragma: no cover
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def record(event: str, **fields: Any) -> dict[str, Any]:
|
|
53
|
+
path = log_path()
|
|
54
|
+
with open(path, "a+b") as fh:
|
|
55
|
+
if fcntl:
|
|
56
|
+
fcntl.flock(fh, fcntl.LOCK_EX)
|
|
57
|
+
try:
|
|
58
|
+
entry = {"ts": round(now(), 3), "event": event, **fields, "prev": _last_hash(fh)}
|
|
59
|
+
entry["hash"] = _digest(entry)
|
|
60
|
+
fh.seek(0, os.SEEK_END)
|
|
61
|
+
fh.write((json.dumps(entry, sort_keys=True) + "\n").encode())
|
|
62
|
+
fh.flush()
|
|
63
|
+
finally:
|
|
64
|
+
if fcntl:
|
|
65
|
+
fcntl.flock(fh, fcntl.LOCK_UN)
|
|
66
|
+
return entry
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def entries(path: Path | None = None) -> Iterator[dict[str, Any]]:
|
|
70
|
+
p = path or log_path()
|
|
71
|
+
if not p.exists():
|
|
72
|
+
return
|
|
73
|
+
with open(p, encoding="utf-8") as fh:
|
|
74
|
+
for line in fh:
|
|
75
|
+
if line.strip():
|
|
76
|
+
yield json.loads(line)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def verify(path: Path | None = None) -> tuple[bool, int, str]:
|
|
80
|
+
"""Return (ok, entries_checked, message)."""
|
|
81
|
+
prev, n = GENESIS, 0
|
|
82
|
+
p = path or log_path()
|
|
83
|
+
if not p.exists():
|
|
84
|
+
return True, 0, "no audit log yet"
|
|
85
|
+
with open(p, encoding="utf-8") as fh:
|
|
86
|
+
for lineno, line in enumerate(fh, 1):
|
|
87
|
+
if not line.strip():
|
|
88
|
+
continue
|
|
89
|
+
try:
|
|
90
|
+
e = json.loads(line)
|
|
91
|
+
except json.JSONDecodeError:
|
|
92
|
+
return False, n, f"line {lineno}: not valid JSON"
|
|
93
|
+
if e.get("prev") != prev:
|
|
94
|
+
return False, n, f"line {lineno}: chain broken (an entry before it was changed or removed)"
|
|
95
|
+
if _digest(e) != e.get("hash"):
|
|
96
|
+
return False, n, f"line {lineno}: contents were modified"
|
|
97
|
+
prev, n = e["hash"], n + 1
|
|
98
|
+
return True, n, f"{n} entries, chain intact"
|