test(harness): ensure portable socket path length and root-invariant audit failure handling
Core Architecture & Invariant CI / validate (pull_request) Successful in 1m11s
Core Architecture & Invariant CI / validate (pull_request) Successful in 1m11s
This commit is contained in:
1 parent
c034afd0f8
commit
76df30747d
3 files changed
+22
-22
No files matched your search
@@ -8,7 +8,6 @@ test_<subject>_<accepts|rejects|enforces>_<condition>
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
from pathlib import Path
|
||||
|
||||
@@ -285,17 +284,12 @@ def test_audit_logger_automatically_redacts_sensitive_parameters(
|
||||
def test_audit_logger_fails_closed_when_audit_log_unwritable(
|
||||
tmp_path: Path, sample_principal: PrincipalAudit
|
||||
):
|
||||
read_only_dir = tmp_path / "readonly_audit"
|
||||
read_only_dir.mkdir(parents=True, exist_ok=True)
|
||||
unwritable_file = read_only_dir / "audit.jsonl"
|
||||
unwritable_file.touch(mode=0o400)
|
||||
os.chmod(unwritable_file, 0o400)
|
||||
# Using a directory as the audit path causes os.open with O_WRONLY to fail with
|
||||
# EISDIR / OSError across all platforms and environments (including root containers).
|
||||
unwritable_target = tmp_path / "unwritable_audit_dir"
|
||||
unwritable_target.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# In addition, make directory read-only
|
||||
os.chmod(read_only_dir, 0o500)
|
||||
|
||||
try:
|
||||
logger = AuditLogger(unwritable_file)
|
||||
logger = AuditLogger(unwritable_target)
|
||||
with pytest.raises(AuditPersistenceError) as exc_info:
|
||||
logger.log_dispatched(
|
||||
operation_id="op_105",
|
||||
@@ -308,9 +302,6 @@ def test_audit_logger_fails_closed_when_audit_log_unwritable(
|
||||
parameters={"recreate": True},
|
||||
)
|
||||
assert "Failed to persist audit event" in str(exc_info.value)
|
||||
finally:
|
||||
os.chmod(read_only_dir, 0o700)
|
||||
os.chmod(unwritable_file, 0o600)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -473,9 +473,11 @@ def test_appctl_wrapper_fast_paths_completion_when_socket_exists(tmp_path: Path,
|
||||
import socket as std_socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
bash_bin = shutil.which("bash") or "bash"
|
||||
sock_path = tmp_path / "appctl.sock"
|
||||
sock_dir = tempfile.TemporaryDirectory(prefix="appctl_sock_", dir="/tmp")
|
||||
sock_path = Path(sock_dir.name) / "appctl.sock"
|
||||
srv = std_socket.socket(std_socket.AF_UNIX, std_socket.SOCK_STREAM)
|
||||
srv.bind(str(sock_path))
|
||||
try:
|
||||
@@ -512,6 +514,7 @@ def test_appctl_wrapper_fast_paths_completion_when_socket_exists(tmp_path: Path,
|
||||
assert "core" in res_words.stdout
|
||||
finally:
|
||||
srv.close()
|
||||
sock_dir.cleanup()
|
||||
|
||||
|
||||
def test_appctl_wrapper_fails_closed_when_socket_is_missing(tmp_path: Path):
|
||||
|
||||
@@ -22,6 +22,7 @@ import concurrent.futures
|
||||
import hashlib
|
||||
import json
|
||||
import socket
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
@@ -29,7 +30,7 @@ from unittest.mock import patch
|
||||
|
||||
import gitops_dispatcher
|
||||
import pytest
|
||||
from appctl_audit import AuditLogger, TargetLockManager
|
||||
from appctl_audit import AuditLogger, AuditPersistenceError, TargetLockManager
|
||||
from appctl_auth import Role, TokenDescriptor, TokenStore
|
||||
from appctl_contracts import ErrorCode, OperationRequest, OperationType
|
||||
from appctl_daemon import (
|
||||
@@ -64,7 +65,8 @@ def daemon_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
- Valid 0600 audit log file
|
||||
"""
|
||||
monkeypatch.setenv("HOMELAB_DOMAIN", "example.test")
|
||||
socket_path = tmp_path / "appctl.sock"
|
||||
sock_dir = tempfile.TemporaryDirectory(prefix="appctl_sock_", dir="/tmp")
|
||||
socket_path = Path(sock_dir.name) / "appctl.sock"
|
||||
http_port = get_free_port()
|
||||
http_host = "127.0.0.1"
|
||||
|
||||
@@ -191,6 +193,7 @@ def daemon_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
yield ctx
|
||||
finally:
|
||||
daemon.stop()
|
||||
sock_dir.cleanup()
|
||||
|
||||
|
||||
def test_health_endpoint(daemon_env):
|
||||
@@ -519,10 +522,16 @@ def test_audit_failure_blocks_mutation(daemon_env):
|
||||
Unwritable audit.jsonl aborts mutating command with 500 ERR_EXECUTION_FAILED
|
||||
and zero Docker invocations.
|
||||
"""
|
||||
daemon_env.audit_file.chmod(0o400)
|
||||
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
||||
|
||||
with patch("appctl_router.run_bounded_subprocess") as mock_subproc:
|
||||
with (
|
||||
patch.object(
|
||||
daemon_env.daemon.audit_logger,
|
||||
"emit_event",
|
||||
side_effect=AuditPersistenceError("Simulated disk full or permission error"),
|
||||
),
|
||||
patch("appctl_router.run_bounded_subprocess") as mock_subproc,
|
||||
):
|
||||
resp = send_daemon_request(
|
||||
endpoint=daemon_env.socket_path,
|
||||
operation_request=deploy_req,
|
||||
@@ -534,9 +543,6 @@ def test_audit_failure_blocks_mutation(daemon_env):
|
||||
assert resp.error.code.http_status == 500
|
||||
assert mock_subproc.call_count == 0
|
||||
|
||||
# Restore write permission for teardown
|
||||
daemon_env.audit_file.chmod(0o600)
|
||||
|
||||
|
||||
def test_daemon_restart_preserves_state(daemon_env):
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user