Add setup_fingerprint_logging/src/setup_fingerprint_logging/core.py
This commit is contained in:
commit
d2d95bb475
1 changed files with 54 additions and 0 deletions
|
|
@ -0,0 +1,54 @@
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_input(param_name: str, value: Any) -> None:
|
||||||
|
"""Validate that an input is a non-empty string."""
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise TypeError(f"Parameter '{param_name}' must be of type str, got {type(value).__name__}.")
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError(f"Parameter '{param_name}' cannot be empty or whitespace.")
|
||||||
|
|
||||||
|
|
||||||
|
def generate_setup_fingerprint(
|
||||||
|
policy_hash: str,
|
||||||
|
runner_image: str,
|
||||||
|
kernel: str,
|
||||||
|
python_version: str,
|
||||||
|
gate_version: str
|
||||||
|
) -> str:
|
||||||
|
"""Erzeugt einen deterministischen Fingerprint-Hash basierend auf übergebenen Setup-Parametern.
|
||||||
|
|
||||||
|
Alle Parameter werden validiert und in konsistenter Reihenfolge zu einem JSON-String serialisiert,
|
||||||
|
bevor ein SHA256-Hash erzeugt wird. Dies garantiert, dass identische Eingaben stets denselben
|
||||||
|
Fingerprint ergeben.
|
||||||
|
"""
|
||||||
|
# Input validation
|
||||||
|
_validate_input("policy_hash", policy_hash)
|
||||||
|
_validate_input("runner_image", runner_image)
|
||||||
|
_validate_input("kernel", kernel)
|
||||||
|
_validate_input("python_version", python_version)
|
||||||
|
_validate_input("gate_version", gate_version)
|
||||||
|
|
||||||
|
params = {
|
||||||
|
"policy_hash": policy_hash,
|
||||||
|
"runner_image": runner_image,
|
||||||
|
"kernel": kernel,
|
||||||
|
"python_version": python_version,
|
||||||
|
"gate_version": gate_version,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Deterministic serialization
|
||||||
|
serialized = json.dumps(params, sort_keys=True, separators=(",", ":"))
|
||||||
|
fingerprint = hashlib.sha256(serialized.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
logger.info("Generated setup fingerprint for parameters: %s", list(params.keys()))
|
||||||
|
logger.debug("Serialized parameters for fingerprint: %s", serialized)
|
||||||
|
|
||||||
|
assert isinstance(fingerprint, str) and len(fingerprint) == 64, "Invalid fingerprint generated."
|
||||||
|
|
||||||
|
return fingerprint
|
||||||
Loading…
Reference in a new issue