""" tests/unit/test_appctl_auth.py — Unit Tests for Authentication & 3-Role RBAC (P4 Slice 3) Behavior-driven test naming convention: test___ """ from __future__ import annotations import hashlib import json import os from pathlib import Path import pytest from appctl_auth import ( AuthenticationError, AuthorizationError, Principal, Role, TokenStore, authenticate_request, enforce_rbac, load_admitted_workloads, ) from appctl_contracts import ErrorCode, OperationType @pytest.fixture def secret_tokens() -> dict[str, str]: return { "admin_secret": "sec_admin_9876543210abcdef", "operator_secret": "sec_operator_1234567890abcdef", } @pytest.fixture def temp_tokens_file(tmp_path: Path, secret_tokens: dict[str, str]) -> Path: tokens_path = tmp_path / "tokens.json" admin_hash = hashlib.sha256( secret_tokens["admin_secret"].encode("utf-8") ).hexdigest() op_hash = hashlib.sha256( secret_tokens["operator_secret"].encode("utf-8") ).hexdigest() data = [ { "token_id": "tok_admin_01", "identity": "operator", "role": "homelab-admin", "sha256": admin_hash, }, { "token_id": "tok_gitops_01", "identity": "gitops-dispatcher", "role": "homelab-operator", "sha256": op_hash, }, ] tokens_path.write_text(json.dumps(data), encoding="utf-8") os.chmod(tokens_path, 0o400) return tokens_path @pytest.fixture def admitted_workloads() -> set[str]: return {"snappymail", "mail", "webmail", "jellyfin", "media", "docs"} # --------------------------------------------------------------------------- # TokenStore & Strict Permissions (0400) # --------------------------------------------------------------------------- def test_token_store_accepts_valid_0400_store(temp_tokens_file: Path): store = TokenStore(temp_tokens_file) tokens = store.load_tokens() assert len(tokens) == 2 assert tokens[0].role == Role.ADMIN assert tokens[1].role == Role.OPERATOR def test_token_store_rejects_insecure_file_permissions(tmp_path: Path): bad_tokens_file = tmp_path / "insecure_tokens.json" bad_tokens_file.write_text("[]", encoding="utf-8") # Insecure modes: 0644, 0600, 0777 for insecure_mode in [0o644, 0o600, 0o777, 0o444]: os.chmod(bad_tokens_file, insecure_mode) store = TokenStore(bad_tokens_file) with pytest.raises(AuthenticationError) as exc_info: store.load_tokens() assert "mode 0400 required" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_AUTH_REQUIRED def test_token_store_rejects_missing_file(tmp_path: Path): missing_file = tmp_path / "nonexistent.json" store = TokenStore(missing_file) with pytest.raises(AuthenticationError) as exc_info: store.load_tokens() assert "not found" in str(exc_info.value) # --------------------------------------------------------------------------- # Bearer Token Authentication # --------------------------------------------------------------------------- def test_token_store_authenticates_valid_bearer_secrets( temp_tokens_file: Path, secret_tokens: dict[str, str] ): store = TokenStore(temp_tokens_file) p_admin = store.authenticate_bearer(secret_tokens["admin_secret"]) assert p_admin.identity == "operator" assert p_admin.role == Role.ADMIN assert p_admin.token_id == "tok_admin_01" assert p_admin.auth_method == "bearer_token" p_op = store.authenticate_bearer(secret_tokens["operator_secret"]) assert p_op.identity == "gitops-dispatcher" assert p_op.role == Role.OPERATOR assert p_op.token_id == "tok_gitops_01" def test_token_store_authenticates_uppercase_hex_descriptor(tmp_path: Path): """Ensure tokens with uppercase hex characters in tokens.json authenticate cleanly (CR-DI-10).""" raw_secret = "my_custom_secret_token" raw_hash_upper = hashlib.sha256(raw_secret.encode("utf-8")).hexdigest().upper() tokens_file = tmp_path / "upper_tokens.json" tokens_file.write_text( json.dumps( [ { "token_id": "tok_upper_01", "identity": "upper-user", "role": "homelab-operator", "sha256": raw_hash_upper, } ] ), encoding="utf-8", ) os.chmod(tokens_file, 0o400) store = TokenStore(tokens_file) principal = store.authenticate_bearer(raw_secret) assert principal.identity == "upper-user" assert principal.role == Role.OPERATOR def test_token_store_rejects_invalid_bearer_secret(temp_tokens_file: Path): store = TokenStore(temp_tokens_file) with pytest.raises(AuthenticationError) as exc_info: store.authenticate_bearer("wrong_secret_token_123") assert "Invalid or unknown Bearer token" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_AUTH_REQUIRED def test_token_store_rejects_empty_bearer_secret(temp_tokens_file: Path): store = TokenStore(temp_tokens_file) with pytest.raises(AuthenticationError) as exc_info: store.authenticate_bearer("") assert "Empty Bearer token provided" in str(exc_info.value) # --------------------------------------------------------------------------- # Transport Authentication Rules (HTTP vs. Unix Domain Socket) # --------------------------------------------------------------------------- def test_authenticate_request_over_http_requires_valid_bearer( temp_tokens_file: Path, secret_tokens: dict[str, str] ): store = TokenStore(temp_tokens_file) # Valid Bearer token auth_header = f"Bearer {secret_tokens['admin_secret']}" p = authenticate_request("http", auth_header, token_store=store) assert p.role == Role.ADMIN # Missing Authorization header -> 401 with pytest.raises(AuthenticationError) as exc_info: authenticate_request("http", None, token_store=store) assert "Bearer token required over network transport" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_AUTH_REQUIRED # Malformed Authorization header -> 401 with pytest.raises(AuthenticationError): authenticate_request("http", "Basic dXNlcjpwYXNz", token_store=store) def test_authenticate_request_over_uds_falls_back_to_viewer( temp_tokens_file: Path, secret_tokens: dict[str, str] ): store = TokenStore(temp_tokens_file) # Missing Authorization header over UDS with peer_uid=1000 p = authenticate_request("unix_socket", None, peer_uid=1000, token_store=store) assert p.identity == "unix_uid_1000" assert p.role == Role.VIEWER assert p.token_id is None assert p.auth_method == "unix_peercred" # Explicit Bearer token over UDS confers full privileges auth_header = f"Bearer {secret_tokens['admin_secret']}" p_admin = authenticate_request( "unix_socket", auth_header, peer_uid=1000, token_store=store ) assert p_admin.role == Role.ADMIN assert p_admin.auth_method == "bearer_token" def test_authenticate_request_over_uds_rejects_missing_peer_uid(): with pytest.raises(AuthenticationError) as exc_info: authenticate_request("unix_socket", None, peer_uid=None) assert "Missing peer credentials" in str(exc_info.value) # --------------------------------------------------------------------------- # Deployments Registry Scope Loader # --------------------------------------------------------------------------- def test_load_admitted_workloads_parses_names_and_aliases(tmp_path: Path): deploy_file = tmp_path / "deployments.yaml" deploy_file.write_text( """ schemaVersion: "1.0" repositories: snappymail: path: "/home/operator/Sites/snappymail" aliases: - "mail" - "webmail" jellyfin: path: "/home/operator/Sites/homelab-jellyfin" aliases: - "media" """, encoding="utf-8", ) admitted = load_admitted_workloads(deploy_file) assert "snappymail" in admitted assert "mail" in admitted assert "webmail" in admitted assert "jellyfin" in admitted assert "media" in admitted assert "unknown_app" not in admitted def test_load_admitted_workloads_handles_aliases_edge_cases_and_json(tmp_path: Path): deploy_file = tmp_path / "deployments.json" deploy_file.write_text( json.dumps( { "repositories": { "app1": { "path": "/home/operator/Sites/app1", "aliases": [123, None, " ", "valid_alias"], } } } ), encoding="utf-8", ) admitted = load_admitted_workloads(deploy_file) assert "app1" in admitted assert "valid_alias" in admitted assert 123 not in admitted assert None not in admitted assert "" not in admitted def test_load_admitted_workloads_fails_closed_on_missing_file( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ): admitted = load_admitted_workloads(tmp_path / "missing.yaml") assert admitted == set() # Test HOMELAB_DEPLOYMENTS_FILE fallback deploy_file = tmp_path / "env_deploy.yaml" deploy_file.write_text( "repositories:\n env_app:\n path: /sites/env_app\n", encoding="utf-8" ) monkeypatch.setenv("HOMELAB_DEPLOYMENTS_FILE", str(deploy_file)) admitted_env = load_admitted_workloads(None) assert "env_app" in admitted_env # --------------------------------------------------------------------------- # RBAC Policy Matrix Enforcement # --------------------------------------------------------------------------- def test_rbac_admin_permitted_across_all_operations_and_targets( admitted_workloads: set[str], ): admin = Principal( identity="admin_user", role=Role.ADMIN, auth_method="bearer_token" ) # Mutations across Core and Sites enforce_rbac(admin, OperationType.WORKLOAD_DEPLOY, "core", admitted_workloads) enforce_rbac(admin, OperationType.WORKLOAD_DEPLOY, "traefik", admitted_workloads) enforce_rbac( admin, OperationType.WORKLOAD_DEPLOY, "unregistered_site", admitted_workloads ) enforce_rbac(admin, OperationType.WORKLOAD_CONFIG, "core", admitted_workloads) enforce_rbac(admin, OperationType.WORKLOAD_LOGS, "traefik", admitted_workloads) enforce_rbac(admin, OperationType.PLATFORM_SYNC_HOMEPAGE, None, admitted_workloads) enforce_rbac(admin, OperationType.WORKLOAD_LIST, None, admitted_workloads) def test_rbac_operator_permitted_on_admitted_workloads( admitted_workloads: set[str], ): operator = Principal( identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token" ) # Permitted on admitted workload enforce_rbac( operator, OperationType.WORKLOAD_DEPLOY, "snappymail", admitted_workloads ) enforce_rbac(operator, OperationType.WORKLOAD_UPDATE, "mail", admitted_workloads) enforce_rbac( operator, OperationType.WORKLOAD_CONFIG, "jellyfin", admitted_workloads ) enforce_rbac(operator, OperationType.WORKLOAD_LOGS, "docs", admitted_workloads) enforce_rbac( operator, OperationType.PLATFORM_SYNC_HOMEPAGE, None, admitted_workloads ) enforce_rbac(operator, OperationType.WORKLOAD_LIST, None, admitted_workloads) def test_rbac_operator_denied_on_core_targets( admitted_workloads: set[str], ): operator = Principal( identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token" ) # Targeting "core" stack with pytest.raises(AuthorizationError) as exc_info: enforce_rbac( operator, OperationType.WORKLOAD_DEPLOY, "core", admitted_workloads ) assert "not permitted to target core service 'core'" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED # Targeting core services (traefik, stalwart, etc.) for core_svc in ["traefik", "authelia", "stalwart", "socket-proxy"]: with pytest.raises(AuthorizationError) as exc_info: enforce_rbac( operator, OperationType.WORKLOAD_RESTART, core_svc, admitted_workloads ) assert f"not permitted to target core service '{core_svc}'" in str( exc_info.value ) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_rbac_operator_denied_on_case_variant_core_targets(admitted_workloads: set[str]): """Ensure case variants ('Core', 'TRAEFIK', 'Authelia') are rejected for operator role (CR-DI-06).""" operator = Principal(identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token") for variant in ["Core", "CORE", "Traefik", "TRAEFIK", "Authelia"]: with pytest.raises(AuthorizationError) as exc_info: enforce_rbac(operator, OperationType.WORKLOAD_CONFIG, variant, admitted_workloads) assert "not permitted to target core service" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_rbac_operator_denied_without_target_on_mutating_or_sensitive_ops( admitted_workloads: set[str], ): operator = Principal( identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token" ) for op in [ OperationType.WORKLOAD_DEPLOY, OperationType.WORKLOAD_UPDATE, OperationType.WORKLOAD_CONFIG, OperationType.WORKLOAD_LOGS, ]: with pytest.raises(AuthorizationError) as exc_info: enforce_rbac(operator, op, None, admitted_workloads) assert f"Operation '{op.value}' requires an explicit workload target" in str( exc_info.value ) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED with pytest.raises(AuthorizationError) as exc_info: enforce_rbac(operator, op, "", admitted_workloads) assert f"Operation '{op.value}' requires an explicit workload target" in str( exc_info.value ) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_rbac_operator_with_default_admitted_workloads( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ): deployments_file = tmp_path / "deployments.yaml" deployments_file.write_text( "repositories:\n customapp:\n path: ~/Sites/customapp\n", encoding="utf-8" ) monkeypatch.setenv("HOMELAB_DEPLOYMENTS_FILE", str(deployments_file)) operator = Principal( identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token" ) enforce_rbac( operator, OperationType.WORKLOAD_DEPLOY, "customapp", admitted_workloads=None ) with pytest.raises(AuthorizationError) as exc_info: enforce_rbac( operator, OperationType.WORKLOAD_DEPLOY, "unknownapp", admitted_workloads=None, ) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_rbac_operator_denied_on_unregistered_workload( admitted_workloads: set[str], ): operator = Principal( identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token" ) with pytest.raises(AuthorizationError) as exc_info: enforce_rbac( operator, OperationType.WORKLOAD_DEPLOY, "unregistered_app", admitted_workloads, ) assert "not registered in deployments.yaml" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_rbac_viewer_permitted_only_on_non_sensitive_reads(): viewer = Principal( identity="unix_uid_1000", role=Role.VIEWER, auth_method="unix_peercred" ) # Permitted reads enforce_rbac(viewer, OperationType.WORKLOAD_LIST, None) enforce_rbac(viewer, OperationType.WORKLOAD_GET, "snappymail") enforce_rbac(viewer, OperationType.WORKLOAD_RESOLVE, "snappymail") enforce_rbac(viewer, OperationType.PLATFORM_SSL, "snappymail") enforce_rbac(viewer, OperationType.PLATFORM_HEALTH, None) def test_rbac_viewer_denied_on_mutations(): viewer = Principal( identity="unix_uid_1000", role=Role.VIEWER, auth_method="unix_peercred" ) mutations = [ OperationType.WORKLOAD_DEPLOY, OperationType.WORKLOAD_STOP, OperationType.WORKLOAD_RESTART, OperationType.WORKLOAD_UPDATE, OperationType.WORKLOAD_PULL, OperationType.PLATFORM_SYNC_HOMEPAGE, ] for mut in mutations: with pytest.raises(AuthorizationError) as exc_info: enforce_rbac(viewer, mut, "snappymail") assert "not permitted to execute mutating operation" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_rbac_viewer_denied_on_sensitive_reads(): viewer = Principal( identity="unix_uid_1000", role=Role.VIEWER, auth_method="unix_peercred" ) for sens_op in [OperationType.WORKLOAD_CONFIG, OperationType.WORKLOAD_LOGS]: with pytest.raises(AuthorizationError) as exc_info: enforce_rbac(viewer, sens_op, "snappymail") assert "not permitted to access sensitive operation" in str(exc_info.value) assert exc_info.value.code == ErrorCode.ERR_PERMISSION_DENIED def test_default_deployments_file_resolves_to_workloads_dir( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, run_isolated_script_expr ): """DEFAULT_DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed when absent.""" import appctl_auth resolved = run_isolated_script_expr( "import appctl_auth; print(appctl_auth.DEFAULT_DEPLOYMENTS_FILE)" ) expected = tmp_path / "Workloads" / "deployments.yaml" assert Path(resolved) == expected # Absent ~/Workloads/deployments.yaml must fail closed (no CORE_DIR fallback) monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False) monkeypatch.delenv("WORKLOADS_DIR", raising=False) monkeypatch.delenv("SITES_DIR", raising=False) monkeypatch.setattr(appctl_auth, "DEFAULT_DEPLOYMENTS_FILE", expected) assert appctl_auth.load_admitted_workloads() == set() # WORKLOADS_DIR takes precedence when HOMELAB_DEPLOYMENTS_FILE is unset custom_workloads = tmp_path / "CustomWorkloads" custom_workloads.mkdir() (custom_workloads / "deployments.yaml").write_text( "repositories:\n custom_app:\n path: ~/Workloads/custom_app\n", encoding="utf-8", ) monkeypatch.setenv("WORKLOADS_DIR", str(custom_workloads)) assert appctl_auth.load_admitted_workloads() == {"custom_app"}