Files
aoc/backend/mapping_loader.py
Tomas Kracmar 9271b4e461
Some checks failed
CI / lint-and-test (push) Has been cancelled
feat: implement Phase 2 stabilization
- Cache Graph API tokens with expiry-aware reuse in graph/auth.py
- Add tenacity-based retry/backoff wrapper (utils/http.py) and apply to all Graph/source API calls
- Add Pydantic request/response models (models/api.py) and FastAPI query constraints
- Add unit tests for event_model, auth and integration tests for API endpoints
- Configure ruff linter/formatter in pyproject.toml
- Add GitHub Actions CI pipeline (.github/workflows/ci.yml)
- Add requirements-dev.txt with pytest, mongomock, httpx, ruff
- Clean up typing imports and fix ruff linting across codebase
2026-04-14 12:02:28 +02:00

59 lines
1.9 KiB
Python

from functools import lru_cache
from pathlib import Path
from typing import Any
import yaml
DEFAULT_MAPPING: dict[str, Any] = {
"category_labels": {
"ApplicationManagement": "Application",
"UserManagement": "User",
"GroupManagement": "Group",
"RoleManagement": "Role",
"Device": "Device",
"Policy": "Policy",
"ResourceManagement": "Resource",
},
"summary_templates": {
"default": "{operation} on {target} by {actor}",
"Device": "{operation} on device {target} by {actor}",
"ApplicationManagement": "{operation} for app {target} by {actor}",
},
"display": {
"default": {
"actor_field": "actor_display",
"actor_label": "User",
},
"ApplicationManagement": {
"actor_field": "actor_upn",
"actor_label": "User",
},
"Device": {
"actor_field": "target_display",
"actor_label": "Device",
},
},
}
@lru_cache(maxsize=1)
def get_mapping() -> dict[str, Any]:
"""
Load mapping from mappings.yml if present; otherwise fall back to defaults.
Users can edit mappings.yml to change labels and summary templates.
"""
path = Path(__file__).parent / "mappings.yml"
if path.exists():
try:
with path.open("r") as f:
data = yaml.safe_load(f) or {}
return {
"category_labels": data.get("category_labels") or DEFAULT_MAPPING["category_labels"],
"summary_templates": data.get("summary_templates") or DEFAULT_MAPPING["summary_templates"],
"display": data.get("display") or DEFAULT_MAPPING["display"],
}
except Exception:
# If mapping fails to load, use defaults to keep the app running.
return DEFAULT_MAPPING
return DEFAULT_MAPPING