security(discovery): enforce Zone isolation and retire unsafe legacy writers

Bind Agent observations, review, enrichment and telemetry to current Zone
and inventory authority. Preserve immutable MAC ownership and historical
snapshots with stale-result rejection and transactional security audits.

Retire unbound direct/range/scheduled discovery and one/bulk legacy summary
writes in both modes. Require reconciled exact inventory identity or
explicit Zone-scoped IP selection for CSV metadata imports.

Independent Blocker #1 application-security re-audit: PASS on tree
cf863aadc9. Evidence: 141 focused PostgreSQL/HTTP
tests passed; Chromium and PowerShell checks passed. Two documented baseline
source assertions remain unchanged. Production deployment is not approved.
This commit is contained in:
lussierj committed 2026-10-17 00:45:04 +00:00
1 parent e898f283ab
commit 8b0d32cd6b
26 files changed
+3798 -4092

No files matched your search

+22
View File
@@ -10,6 +10,8 @@ from psycopg.types.json import Jsonb
from app.agent_commands.exceptions import AgentCommandConflict, AgentCommandNotFound
from app.agent_commands.policy import READ_ONLY_COMMAND_TYPES, rejection_execution_outcome, rejection_reason
from app.config import settings
from app.discovery.repository import DiscoveryRepository
from fastapi import HTTPException
ConnectionFactory = Callable[[], psycopg.Connection]
@@ -120,9 +122,29 @@ class AgentCommandRepository:
connection = self._connection_factory()
try:
with connection.cursor(row_factory=dict_row) as cur:
DiscoveryRepository.lock(cur)
self._retire_unsupported(cur, agent_id)
discovery = DiscoveryRepository(self._connection_factory)
cur.execute(f"""SELECT q.*, ({NO_DELIVERY_EVIDENCE_SQL}) AS no_delivery_evidence
FROM minerops_agent_command_queue q WHERE agent_id=%s
AND command_status='pending' AND command_type=ANY(%s)
ORDER BY created_at,id FOR UPDATE SKIP LOCKED LIMIT 1000""",
(agent_id,list(READ_ONLY_COMMAND_TYPES)))
pending=list(cur.fetchall())
for command in pending:
try:
discovery.validate_command(cur,agent_id,command)
except HTTPException as exc:
cur.execute("""UPDATE minerops_agent_command_queue SET command_status='cancelled',
completed_at=COALESCE(completed_at,now()), error_message=concat_ws(chr(10),NULLIF(error_message,''),%s::text)
WHERE id=%s""",(exc.detail,command['id']))
DiscoveryRepository.audit(cur,'discovery.command_rejected',None,
{'command_id':command['id'],'agent_id':agent_id,'reason':exc.detail,
'execution_outcome':rejection_execution_outcome({**command,'previous_status':'pending'})})
cur.execute(CLAIM_COMMANDS_SQL, (agent_id, list(READ_ONLY_COMMAND_TYPES), limit))
rows = [dict(row) for row in cur.fetchall()]
for row in rows:
discovery.validate_command(cur,agent_id,row)
connection.commit()
return rows
except Exception: