Initial ABSDEV deployment framework development preview
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
"""Strict migration approval validation.
|
||||
|
||||
This module validates approval records only.
|
||||
It does not authenticate operators or execute migrations.
|
||||
"""
|
||||
|
||||
import copy
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from core.compatibility import canonical_hash
|
||||
|
||||
|
||||
class ApprovalError(RuntimeError):
|
||||
"""Invalid, expired or mismatched migration approval."""
|
||||
|
||||
|
||||
CONTEXT_FIELDS = {
|
||||
"site",
|
||||
"release",
|
||||
"revision",
|
||||
"provenance",
|
||||
"compatibility",
|
||||
"database_identity",
|
||||
"backup_sha256",
|
||||
"rehearsal_fingerprint",
|
||||
"pending_migrations",
|
||||
}
|
||||
|
||||
APPROVAL_FIELDS = {
|
||||
"format_version",
|
||||
"approval_id",
|
||||
"approver",
|
||||
"context",
|
||||
"issued_at",
|
||||
"expires_at",
|
||||
"confirmation",
|
||||
"checksum",
|
||||
}
|
||||
|
||||
|
||||
def require(condition, message):
|
||||
|
||||
if not condition:
|
||||
raise ApprovalError(message)
|
||||
|
||||
|
||||
def matches(value, pattern):
|
||||
|
||||
return (
|
||||
isinstance(value, str)
|
||||
and re.fullmatch(pattern, value) is not None
|
||||
)
|
||||
|
||||
|
||||
def timestamp(value):
|
||||
|
||||
require(
|
||||
isinstance(value, datetime)
|
||||
and value.utcoffset() is not None,
|
||||
"Timezone-aware timestamp required",
|
||||
)
|
||||
|
||||
return value.astimezone(timezone.utc)
|
||||
|
||||
|
||||
def parse_timestamp(value):
|
||||
|
||||
require(
|
||||
isinstance(value, str),
|
||||
"Invalid approval timestamp",
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
parsed = datetime.fromisoformat(value)
|
||||
|
||||
return timestamp(parsed)
|
||||
|
||||
except (ValueError, OverflowError):
|
||||
|
||||
raise ApprovalError(
|
||||
"Invalid approval timestamp"
|
||||
) from None
|
||||
|
||||
|
||||
def validate_context(context):
|
||||
|
||||
require(
|
||||
isinstance(context, dict)
|
||||
and set(context) == CONTEXT_FIELDS,
|
||||
"Invalid migration approval context",
|
||||
)
|
||||
|
||||
require(
|
||||
matches(
|
||||
context["site"],
|
||||
r"[a-z0-9][a-z0-9-]{0,62}",
|
||||
),
|
||||
"Invalid site identifier",
|
||||
)
|
||||
|
||||
require(
|
||||
matches(
|
||||
context["release"],
|
||||
r"[1-9][0-9]{0,19}",
|
||||
),
|
||||
"Invalid release identifier",
|
||||
)
|
||||
|
||||
require(
|
||||
matches(
|
||||
context["revision"],
|
||||
r"[a-f0-9]{40}",
|
||||
),
|
||||
"Invalid Git revision",
|
||||
)
|
||||
|
||||
for field in (
|
||||
"provenance",
|
||||
"compatibility",
|
||||
"database_identity",
|
||||
"backup_sha256",
|
||||
"rehearsal_fingerprint",
|
||||
):
|
||||
|
||||
require(
|
||||
matches(
|
||||
context[field],
|
||||
r"[a-f0-9]{64}",
|
||||
),
|
||||
f"Invalid approval evidence: {field}",
|
||||
)
|
||||
|
||||
pending = context["pending_migrations"]
|
||||
|
||||
require(
|
||||
isinstance(pending, list)
|
||||
and 1 <= len(pending) <= 10000,
|
||||
"Invalid pending migration inventory",
|
||||
)
|
||||
|
||||
for name in pending:
|
||||
|
||||
require(
|
||||
matches(
|
||||
name,
|
||||
r"[A-Za-z0-9_][A-Za-z0-9_-]{0,254}",
|
||||
),
|
||||
"Invalid pending migration name",
|
||||
)
|
||||
|
||||
require(
|
||||
len(set(pending)) == len(pending),
|
||||
"Duplicate pending migrations",
|
||||
)
|
||||
|
||||
|
||||
def validate_approver(approver):
|
||||
|
||||
require(
|
||||
matches(
|
||||
approver,
|
||||
r"[A-Za-z0-9][A-Za-z0-9_.@-]{0,127}",
|
||||
),
|
||||
"Invalid approver identifier",
|
||||
)
|
||||
|
||||
|
||||
def approval_checksum(approval):
|
||||
|
||||
return canonical_hash({
|
||||
key: value
|
||||
for key, value in approval.items()
|
||||
if key != "checksum"
|
||||
})
|
||||
|
||||
|
||||
def issue_approval(
|
||||
context,
|
||||
*,
|
||||
approver,
|
||||
now,
|
||||
ttl_hours=24,
|
||||
):
|
||||
|
||||
validate_context(context)
|
||||
validate_approver(approver)
|
||||
|
||||
issued = timestamp(now)
|
||||
|
||||
require(
|
||||
type(ttl_hours) is int
|
||||
and 1 <= ttl_hours <= 24,
|
||||
"Approval lifetime must be between 1 and 24 hours",
|
||||
)
|
||||
|
||||
approval_id = uuid.uuid4().hex
|
||||
|
||||
approval = {
|
||||
"format_version": 1,
|
||||
"approval_id": approval_id,
|
||||
"approver": approver,
|
||||
"context": copy.deepcopy(context),
|
||||
"issued_at": issued.isoformat(),
|
||||
"expires_at": (
|
||||
issued + timedelta(hours=ttl_hours)
|
||||
).isoformat(),
|
||||
"confirmation": (
|
||||
"MIGRATE-" + approval_id
|
||||
),
|
||||
}
|
||||
|
||||
approval["checksum"] = approval_checksum(
|
||||
approval
|
||||
)
|
||||
|
||||
return approval
|
||||
|
||||
|
||||
def verify_approval(
|
||||
approval,
|
||||
context,
|
||||
*,
|
||||
confirmation,
|
||||
now,
|
||||
):
|
||||
|
||||
validate_context(context)
|
||||
|
||||
require(
|
||||
isinstance(approval, dict)
|
||||
and set(approval) == APPROVAL_FIELDS,
|
||||
"Invalid approval record",
|
||||
)
|
||||
|
||||
require(
|
||||
type(approval["format_version"]) is int
|
||||
and approval["format_version"] == 1,
|
||||
"Unsupported approval format",
|
||||
)
|
||||
|
||||
require(
|
||||
matches(
|
||||
approval["approval_id"],
|
||||
r"[a-f0-9]{32}",
|
||||
),
|
||||
"Invalid approval identifier",
|
||||
)
|
||||
|
||||
validate_approver(approval["approver"])
|
||||
validate_context(approval["context"])
|
||||
|
||||
require(
|
||||
matches(
|
||||
approval["checksum"],
|
||||
r"[a-f0-9]{64}",
|
||||
)
|
||||
and approval_checksum(approval)
|
||||
== approval["checksum"],
|
||||
"Approval checksum mismatch",
|
||||
)
|
||||
|
||||
require(
|
||||
approval["context"] == context,
|
||||
"Migration approval context changed",
|
||||
)
|
||||
|
||||
expected = (
|
||||
"MIGRATE-" + approval["approval_id"]
|
||||
)
|
||||
|
||||
require(
|
||||
isinstance(confirmation, str)
|
||||
and confirmation == expected
|
||||
and approval["confirmation"] == expected,
|
||||
"Migration confirmation mismatch",
|
||||
)
|
||||
|
||||
issued = parse_timestamp(
|
||||
approval["issued_at"]
|
||||
)
|
||||
|
||||
expires = parse_timestamp(
|
||||
approval["expires_at"]
|
||||
)
|
||||
|
||||
current = timestamp(now)
|
||||
|
||||
require(
|
||||
timedelta(0)
|
||||
< expires - issued
|
||||
<= timedelta(hours=24),
|
||||
"Invalid approval lifetime",
|
||||
)
|
||||
|
||||
require(
|
||||
issued <= current < expires,
|
||||
"Approval expired or not yet valid",
|
||||
)
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user