Sub topic 4
Making Logs Tamper-Evident and Retaining Them
This chapter assumes you understand the ML lifecycle, Git and CI/CD, and basic Python. It also assumes you know what an experiment tracker or model registry does. We will focus on preserving evidence after an event is recorded. A tamper-evident log makes unauthorized changes detectable. It does not prevent every change.
Why ordinary logs are not enough
An audit log records events such as model training, approval, deployment, and rollback. A normal append-only file can still be edited by someone with storage access. A database administrator might update a row, or an operator might delete old records.
A tamper-evident design links each event to the previous event. Each event also stores a cryptographic hash, which is a fixed-length value computed from its contents. Changing one event changes its hash and breaks the link to the next event.
import hashlib
import json
from datetime import datetime, timezone
GENESIS = "0" * 64
def digest(body: dict) -> str:
payload = json.dumps(
body, sort_keys=True, separators=(",", ":")
).encode("utf-8")
return hashlib.sha256(payload).hexdigest()
def append_event(log: list, actor: str, action: str,
model: str, version: str, details: dict) -> dict:
body = {
"ts": datetime.now(timezone.utc).isoformat(),
"actor": actor,
"action": action,
"model": model,
"version": version,
"details": details,
"prev_hash": log[-1]["hash"] if log else GENESIS,
}
event = {**body, "hash": digest(body)}
log.append(event)
return event
def verify(log: list) -> bool:
previous = GENESIS
for event in log:
body = {key: value for key, value in event.items()
if key != "hash"}
if body["prev_hash"] != previous:
return False
if digest(body) != event["hash"]:
return False
previous = event["hash"]
return True
events = []
append_event(events, "alice", "promote", "credit-model",
"2.4.0", {"environment": "production"})
print(verify(events)) # True
events[0]["action"] = "delete"
print(verify(events)) # FalseStrengthening the design
Hash chaining detects modification when verification starts from a trusted first hash. It does not prove that the original log was truthful, and it does not stop an attacker from replacing both the events and the stored starting hash.
Use independent protection for stronger evidence:
- Store periodic chain checkpoints in a separate access-controlled system.
- Restrict write and delete permissions through separate service identities.
- Send copies to immutable or write-once storage.
- Record the retention policy and legal hold status with the log metadata.
- Monitor failed verification and unexpected gaps.
- Use UTC timestamps and synchronized clocks, but treat timestamps as evidence rather than perfect truth.
| Control | Main protection |
|---|---|
| Hash chain | Detects edits within the chain |
| Immutable storage | Limits deletion and replacement |
| Independent checkpoint | Protects the chain anchor |
| Access logs | Shows who accessed the evidence |
Exercise
Exercise
If an attacker changes an event and recalculates every later hash, what additional control helps detect the replacement chain?
Show answer
An independently stored checkpoint or immutable copy can preserve a trusted reference outside the attacker's control.