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

+72 -1
View File
@@ -135,6 +135,77 @@ class TestPagesAndNav:
assert client.get('/api/links', headers=AUTH).status_code == 200
import hashlib # noqa: E402
THOR_KEY = 'test-thor-key-not-real'
KEYS = [{'name': 'thor', 'sha256': hashlib.sha256(THOR_KEY.encode()).hexdigest(), 'scopes': ['read']}]
BEARER = {'Authorization': f'Bearer {THOR_KEY}'}
@pytest.fixture
def key_client(client):
with patch.object(gandalf, '_cfg', dict(CONFIG, api_keys=KEYS)):
yield client
class TestMachineKey:
"""gandalf#66: Thor reads status, events, attention and suppressions with a read-only key."""
def test_reads_with_the_key(self, key_client):
for url in ('/api/status', '/api/events', '/api/attention', '/api/suppressions'):
assert key_client.get(url, headers=BEARER).status_code == 200, url
def test_wrong_key_is_refused_even_with_identity_headers(self, key_client):
resp = key_client.get('/api/status', headers=dict(AUTH, Authorization='Bearer nope'))
assert resp.status_code == 401 and resp.get_json()['error'] == 'Invalid API key'
def test_key_opens_only_the_marked_routes(self, key_client):
assert key_client.get('/api/devices', headers=BEARER).status_code == 401
assert key_client.get('/', headers=BEARER).status_code == 401
def test_key_cannot_write(self, key_client):
resp = key_client.post('/api/suppressions', headers=BEARER, json={'target_type': 'all', 'reason': 'x'})
assert resp.status_code in (401, 403, 405)
assert not gandalf.db.create_suppression.called
def test_no_key_configured_means_no_key_works(self, client):
assert client.get('/api/status', headers=BEARER).status_code == 401
def test_key_without_read_scope(self, client):
keys = [dict(KEYS[0], scopes=[])]
with patch.object(gandalf, '_cfg', dict(CONFIG, api_keys=keys)):
assert client.get('/api/status', headers=BEARER).status_code == 401
def test_source_restriction(self, client):
keys = [dict(KEYS[0], sources=['10.10.10.0/24'])]
with patch.object(gandalf, '_cfg', dict(CONFIG, api_keys=keys)):
inside = client.get('/api/status', headers=BEARER, environ_base={'REMOTE_ADDR': '10.10.10.70'})
outside = client.get('/api/status', headers=BEARER, environ_base={'REMOTE_ADDR': '192.168.1.5'})
assert inside.status_code == 200 and outside.status_code == 401
def test_users_still_sign_in_as_before(self, key_client):
assert key_client.get('/api/status', headers=AUTH).status_code == 200
assert key_client.get('/api/status').status_code == 401
def test_ended_suppressions_for_thor(self, key_client):
gandalf.db.get_suppression_windows.return_value = [{'id': 2, 'state': 'expired'}]
data = key_client.get('/api/suppressions?include_ended=1&hours=48', headers=BEARER).get_json()
assert data['suppressions'] == [{'id': 2, 'state': 'expired'}] and data['since'].endswith('Z')
since = gandalf.db.get_suppression_windows.call_args.args[0]
assert 47.9 < (datetime.utcnow() - since).total_seconds() / 3600 < 48.1
def test_ended_suppressions_bad_hours(self, key_client):
for h in ('x', '0', '800'):
assert key_client.get(f'/api/suppressions?include_ended=1&hours={h}', headers=BEARER).status_code == 400
def test_stalled_cluster_warning_reaches_thor(self, key_client):
stalled = {'event_type': 'cluster_hosts_stalled', 'severity': 'warning', 'target_name': 'proxmox-cluster',
'target_detail': '', 'description': '3 hosts stopped answering ... OOM kills on compute-storage-01'}
gandalf.db.get_active_events.return_value = [stalled]
events = key_client.get('/api/events', headers=BEARER).get_json()['active']
assert events[0]['event_type'] == 'cluster_hosts_stalled' and 'OOM' in events[0]['description']
class TestDeviceApi:
def test_requires_auth(self, client):
assert client.get('/api/devices').status_code == 401
@@ -974,7 +1045,7 @@ class TestModemRestart:
def test_failed_restart_undoes_the_silence(self, client):
resp, km, _ = self._post(client, restart=gandalf.modem_api.ModemUnreachable('timeout'))
assert resp.status_code == 502
gandalf.db.deactivate_suppression.assert_called_once_with(42)
gandalf.db.deactivate_suppression.assert_called_once_with(42, ended_by='jared')
assert km.call_args_list[-1].args == (self.CFG['uptime_kuma'], False)
def test_one_at_a_time(self, client):