feat(config): validate runtime settings at boot and fail fast on critical errors
This commit is contained in:
1 parent
be92bb2493
commit
7f323ed78b
4 files changed
+134
-2
No files matched your search
@@ -27,6 +27,12 @@ class RuntimeSettings:
|
||||
config_path: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ValidationIssue:
|
||||
level: str # ERROR | WARN
|
||||
message: str
|
||||
|
||||
|
||||
def _parse_scalar(value):
|
||||
value = value.strip()
|
||||
if value.startswith(("'", '"')) and value.endswith(("'", '"')) and len(value) >= 2:
|
||||
@@ -254,3 +260,63 @@ def load_runtime_settings():
|
||||
enforcement_sets=enforcement_sets,
|
||||
config_path=config_path,
|
||||
)
|
||||
|
||||
|
||||
def validate_runtime_settings(settings):
|
||||
issues = []
|
||||
|
||||
if settings.check_interval < 1:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
"ERROR",
|
||||
f"check_interval must be >= 1 (got {settings.check_interval})",
|
||||
)
|
||||
)
|
||||
if settings.heal_verify_delay < 0:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
"ERROR",
|
||||
f"heal_verify_delay must be >= 0 (got {settings.heal_verify_delay})",
|
||||
)
|
||||
)
|
||||
if settings.heal_min_interval < 0:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
"ERROR",
|
||||
f"heal_min_interval must be >= 0 (got {settings.heal_min_interval})",
|
||||
)
|
||||
)
|
||||
if not settings.enforcement_targets:
|
||||
issues.append(
|
||||
ValidationIssue("ERROR", "At least one enforcement target is required.")
|
||||
)
|
||||
|
||||
for target in settings.enforcement_targets:
|
||||
if (
|
||||
not isinstance(target, tuple)
|
||||
or len(target) != 2
|
||||
or not all(isinstance(part, str) and part.strip() for part in target)
|
||||
):
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
"ERROR",
|
||||
f"Malformed enforcement target: {target!r}",
|
||||
)
|
||||
)
|
||||
|
||||
if settings.check_jitter_pct < 0.0 or settings.check_jitter_pct > 0.90:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
"WARN",
|
||||
f"check_jitter_pct should be between 0.0 and 0.90 (got {settings.check_jitter_pct})",
|
||||
)
|
||||
)
|
||||
if not os.path.isabs(settings.state_file):
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
"WARN",
|
||||
f"state_file is not absolute: {settings.state_file}",
|
||||
)
|
||||
)
|
||||
|
||||
return issues
|
||||
Reference in new issue
Block a user