Thor contract, part 1: [gandalf] title tag and a read-only API key for Thor
Lint / JS (eslint) (push) Successful in 14s
Lint / Python (flake8) (push) Successful in 1m47s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m48s
Lint / JS (eslint) (pull_request) Successful in 12s
Lint / Python (flake8) (pull_request) Successful in 1m0s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 1m21s
Test / Python Tests (pytest) (pull_request) Successful in 1m43s
Test / Python Tests (pytest) (push) Successful in 1m6s

Titles move to the shared standard [source][auto][gandalf][network]
Summary [scope][production][issue], built in one place
(ticket_text.title). Checked by running Tinker's TicketHash.php: every
Gandalf title shape hashes the same as before, so open tickets keep
matching.

Thor can read /api/status, /api/events, /api/attention and
/api/suppressions with a machine key (Authorization: Bearer, only its
SHA-256 in config, read-only, optional source CIDRs; a wrong key is a
401 and never falls back to identity headers). /api/suppressions
?include_ended=1&hours=N returns every suppression in force in the
window with start, planned end, actual end and who ended it: new
ended_at/ended_by columns, added automatically.

Refs #66 (items 1 and 2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-25 19:51:46 -05:00
1 parent bf5d36ed1e
commit f924ce42c3
9 files changed
+391 -25

No files matched your search

+82 -3
View File
@@ -397,21 +397,100 @@ def create_suppression(
return cur.lastrowid
def deactivate_suppression(sup_id: int) -> None:
_suppression_schema_ready = False
def ensure_suppression_schema() -> None:
"""Add ended_at/ended_by (gandalf#66), so a removed suppression keeps its end time.
Thor decides from history whether a window was planned, so it needs every suppression's
start and end, not only the active ones. Runs once per process; no manual migration.
"""
global _suppression_schema_ready
if _suppression_schema_ready:
return
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute('SHOW COLUMNS FROM suppression_rules')
have = {r['Field'] for r in cur.fetchall()}
if 'ended_at' not in have:
cur.execute('ALTER TABLE suppression_rules ADD COLUMN ended_at TIMESTAMP NULL')
# Expired ones already ended at their expiry; ones removed by hand before this
# column existed keep ended_at NULL: their end time was never recorded
cur.execute('UPDATE suppression_rules SET ended_at=expires_at '
'WHERE active=FALSE AND expires_at IS NOT NULL AND expires_at <= NOW()')
if 'ended_by' not in have:
cur.execute("ALTER TABLE suppression_rules ADD COLUMN ended_by VARCHAR(255) NOT NULL DEFAULT ''")
_suppression_schema_ready = True
def deactivate_suppression(sup_id: int, ended_by: str = '') -> None:
ensure_suppression_schema()
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
'UPDATE suppression_rules SET active=FALSE WHERE id=%s', (sup_id,)
'UPDATE suppression_rules SET active=FALSE, ended_at=NOW(), ended_by=%s '
'WHERE id=%s AND active=TRUE',
(ended_by or '', sup_id),
)
def get_suppression_windows(since: datetime) -> list:
"""Every suppression in force at any point since `since` (naive UTC), oldest first.
Each: id, target_type, target_name, target_detail, reason, started_at, started_by,
expires_at (planned end, None = until removed), ended_at, ended_by, state
('active', 'ended' or 'expired'), end_known. Times are ISO 8601 UTC with a Z.
"""
ensure_suppression_schema()
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"""SELECT * FROM suppression_rules
WHERE (active=TRUE AND (expires_at IS NULL OR expires_at > NOW()))
OR COALESCE(ended_at, expires_at) >= %s
OR (ended_at IS NULL AND expires_at IS NULL AND created_at >= %s)
ORDER BY created_at""",
(since, since),
)
rows = cur.fetchall()
now = datetime.now(timezone.utc).replace(tzinfo=None)
out = []
for r in rows:
expires, ended = r.get('expires_at'), r.get('ended_at')
live = bool(r.get('active')) and (expires is None or expires > now)
if live:
state, end = 'active', None
elif ended is not None:
# Removed by hand, or by Gandalf's own restart flow, before or at expiry
state, end = ('expired' if expires is not None and ended >= expires else 'ended'), ended
elif expires is not None and expires <= now:
state, end = 'expired', expires
else:
state, end = 'ended', None # removed before end times were recorded
out.append({
'id': r['id'], 'target_type': r['target_type'], 'target_name': r.get('target_name') or '',
'target_detail': r.get('target_detail') or '', 'reason': r.get('reason') or '',
'started_at': _iso_z(r.get('created_at')), 'started_by': r.get('suppressed_by') or '',
'expires_at': _iso_z(expires), 'ended_at': _iso_z(end),
'ended_by': r.get('ended_by') or ('expired' if state == 'expired' else ''),
'state': state, 'end_known': live or end is not None,
})
return out
def _iso_z(value) -> Optional[str]:
return value.isoformat() + 'Z' if value is not None and hasattr(value, 'isoformat') else None
def cleanup_expired_suppressions() -> int:
"""Mark expired time-limited suppressions as inactive. Returns count deactivated."""
ensure_suppression_schema()
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"""UPDATE suppression_rules
SET active=FALSE
SET active=FALSE, ended_at=expires_at, ended_by='expired'
WHERE active=TRUE AND expires_at IS NOT NULL AND expires_at <= NOW()"""
)
n = cur.rowcount