You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
405 lines
16 KiB
Python
405 lines
16 KiB
Python
"""Load the source-controlled platform policy shared with the admin panel."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import hashlib
|
|
import hmac
|
|
import re
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
POLICY_MANIFEST_PATH = (
|
|
Path(__file__).resolve().parents[2]
|
|
/ "config"
|
|
/ "platform_default_network_policy.json"
|
|
)
|
|
MAX_MANIFEST_BYTES = 65536
|
|
MAX_CATALOG_RULES = 100
|
|
MAX_PLATFORM_FILTER_RULES = 70
|
|
MAX_PLATFORM_POLICY_SNAPSHOT_BYTES = 32768
|
|
PLATFORM_POLICY_SNAPSHOT_SCHEMA_VERSION = 1
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PlatformNetworkRule:
|
|
rule_id: str
|
|
label: str
|
|
category: str
|
|
protocols: tuple[str, ...]
|
|
port_from: int
|
|
port_to: int
|
|
directions: tuple[str, ...]
|
|
reason: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UnrestrictedPort:
|
|
protocol: str
|
|
port: int
|
|
reason: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PlatformDefaultNetworkPolicy:
|
|
schema_version: int
|
|
policy_version: int
|
|
scope: str
|
|
applies_to: str
|
|
migrate_existing: bool
|
|
rules: tuple[PlatformNetworkRule, ...]
|
|
explicitly_unrestricted: tuple[UnrestrictedPort, ...]
|
|
|
|
|
|
def _exact_keys(value: dict[str, Any], expected: set[str], context: str) -> None:
|
|
if set(value) != expected:
|
|
raise RuntimeError(f"{context} fields are invalid")
|
|
|
|
|
|
def _text(
|
|
value: Any, context: str, *, max_bytes: int,
|
|
identifier: bool = False,
|
|
) -> str:
|
|
if (not isinstance(value, str) or not value or value != value.strip()
|
|
or len(value.encode("utf-8")) > max_bytes
|
|
or any(ord(char) < 32 or ord(char) == 127 for char in value)):
|
|
raise RuntimeError(f"{context} is invalid")
|
|
if identifier and not re.fullmatch(r"[a-z0-9][a-z0-9_-]{0,63}", value):
|
|
raise RuntimeError(f"{context} is invalid")
|
|
return value
|
|
|
|
|
|
def _string_tuple(
|
|
value: Any, allowed: set[str], context: str,
|
|
) -> tuple[str, ...]:
|
|
if (not isinstance(value, list) or not value
|
|
or any(not isinstance(item, str) or item not in allowed for item in value)
|
|
or len(set(value)) != len(value)):
|
|
raise RuntimeError(f"{context} is invalid")
|
|
return tuple(value)
|
|
|
|
|
|
def load_platform_default_network_policy(
|
|
path: Path = POLICY_MANIFEST_PATH,
|
|
) -> PlatformDefaultNetworkPolicy:
|
|
"""Return a strictly validated immutable policy or fail engine startup."""
|
|
try:
|
|
encoded = path.read_bytes()
|
|
if not encoded or len(encoded) > MAX_MANIFEST_BYTES:
|
|
raise RuntimeError("platform default network policy size is invalid")
|
|
raw = json.loads(encoded.decode("utf-8"))
|
|
except (OSError, UnicodeError, json.JSONDecodeError) as exc:
|
|
raise RuntimeError("platform default network policy cannot be loaded") from exc
|
|
if not isinstance(raw, dict):
|
|
raise RuntimeError("platform default network policy must be an object")
|
|
_exact_keys(raw, {
|
|
"schema_version", "policy_version", "scope", "applies_to",
|
|
"migrate_existing", "rules", "explicitly_unrestricted",
|
|
}, "platform default network policy")
|
|
if raw["schema_version"] != 1 or raw["policy_version"] != 1:
|
|
raise RuntimeError("platform default network policy version is unsupported")
|
|
if (raw["scope"] != "public_ipv4_interface"
|
|
or raw["applies_to"] != "new_public_cloud_image_instances_only"
|
|
or raw["migrate_existing"] is not False):
|
|
raise RuntimeError("platform default network policy scope is invalid")
|
|
if (not isinstance(raw["rules"], list) or not raw["rules"]
|
|
or len(raw["rules"]) > MAX_CATALOG_RULES):
|
|
raise RuntimeError("platform default network policy rules are invalid")
|
|
|
|
rules: list[PlatformNetworkRule] = []
|
|
rule_ids: set[str] = set()
|
|
expanded: set[tuple[str, int, int, str]] = set()
|
|
categories = {"permanent": 0, "default": 0}
|
|
for index, item in enumerate(raw["rules"]):
|
|
if not isinstance(item, dict):
|
|
raise RuntimeError(f"platform rule {index} is invalid")
|
|
_exact_keys(item, {
|
|
"id", "label", "category", "protocols", "port_from",
|
|
"port_to", "directions", "reason",
|
|
}, f"platform rule {index}")
|
|
rule_id = _text(
|
|
item["id"], f"platform rule {index} id",
|
|
max_bytes=64, identifier=True)
|
|
if rule_id in rule_ids:
|
|
raise RuntimeError("platform rule ids must be unique")
|
|
rule_ids.add(rule_id)
|
|
category = item["category"]
|
|
if category not in {"permanent", "default"}:
|
|
raise RuntimeError(f"platform rule {index} category is invalid")
|
|
categories[category] += 1
|
|
protocols = _string_tuple(
|
|
item["protocols"], {"tcp", "udp"},
|
|
f"platform rule {index} protocols",
|
|
)
|
|
directions = _string_tuple(
|
|
item["directions"], {"ingress", "egress"},
|
|
f"platform rule {index} directions",
|
|
)
|
|
port_from = item["port_from"]
|
|
port_to = item["port_to"]
|
|
if (isinstance(port_from, bool) or not isinstance(port_from, int)
|
|
or isinstance(port_to, bool) or not isinstance(port_to, int)
|
|
or not 1 <= port_from <= port_to <= 65535):
|
|
raise RuntimeError(f"platform rule {index} port range is invalid")
|
|
for protocol in protocols:
|
|
for direction in directions:
|
|
identity = (protocol, port_from, port_to, direction)
|
|
if identity in expanded:
|
|
raise RuntimeError("platform rules must not contain duplicates")
|
|
expanded.add(identity)
|
|
if len(expanded) > MAX_PLATFORM_FILTER_RULES:
|
|
raise RuntimeError("platform rules exceed the filter priority budget")
|
|
rules.append(PlatformNetworkRule(
|
|
rule_id=rule_id,
|
|
label=_text(
|
|
item["label"], f"platform rule {index} label",
|
|
max_bytes=120),
|
|
category=category,
|
|
protocols=protocols,
|
|
port_from=port_from,
|
|
port_to=port_to,
|
|
directions=directions,
|
|
reason=_text(
|
|
item["reason"], f"platform rule {index} reason",
|
|
max_bytes=500),
|
|
))
|
|
if any(count < 1 for count in categories.values()):
|
|
raise RuntimeError("platform default network policy categories are invalid")
|
|
|
|
unrestricted_raw = raw["explicitly_unrestricted"]
|
|
if (not isinstance(unrestricted_raw, list)
|
|
or len(unrestricted_raw) > MAX_CATALOG_RULES):
|
|
raise RuntimeError("explicitly unrestricted ports are invalid")
|
|
unrestricted: list[UnrestrictedPort] = []
|
|
unrestricted_ids: set[tuple[str, int]] = set()
|
|
for index, item in enumerate(unrestricted_raw):
|
|
if not isinstance(item, dict):
|
|
raise RuntimeError(f"unrestricted port {index} is invalid")
|
|
_exact_keys(item, {"protocol", "port", "reason"},
|
|
f"unrestricted port {index}")
|
|
protocol = item["protocol"]
|
|
port = item["port"]
|
|
if (protocol not in {"tcp", "udp"}
|
|
or isinstance(port, bool) or not isinstance(port, int)
|
|
or not 1 <= port <= 65535
|
|
or (protocol, port) in unrestricted_ids):
|
|
raise RuntimeError(f"unrestricted port {index} is invalid")
|
|
if any(
|
|
protocol == rule_protocol and port_from <= port <= port_to
|
|
for rule_protocol, port_from, port_to, _direction in expanded
|
|
):
|
|
raise RuntimeError("a port cannot be both restricted and unrestricted")
|
|
unrestricted_ids.add((protocol, port))
|
|
unrestricted.append(UnrestrictedPort(
|
|
protocol=protocol,
|
|
port=port,
|
|
reason=_text(
|
|
item["reason"], f"unrestricted port {index} reason",
|
|
max_bytes=500),
|
|
))
|
|
|
|
return PlatformDefaultNetworkPolicy(
|
|
schema_version=raw["schema_version"],
|
|
policy_version=raw["policy_version"],
|
|
scope=raw["scope"],
|
|
applies_to=raw["applies_to"],
|
|
migrate_existing=raw["migrate_existing"],
|
|
rules=tuple(rules),
|
|
explicitly_unrestricted=tuple(unrestricted),
|
|
)
|
|
|
|
|
|
PLATFORM_DEFAULT_NETWORK_POLICY = load_platform_default_network_policy()
|
|
|
|
|
|
def _rule_document(rule: PlatformNetworkRule | dict[str, Any]) -> dict[str, Any]:
|
|
if isinstance(rule, PlatformNetworkRule):
|
|
return {
|
|
"id": rule.rule_id,
|
|
"label": rule.label,
|
|
"category": rule.category,
|
|
"protocols": list(rule.protocols),
|
|
"port_from": rule.port_from,
|
|
"port_to": rule.port_to,
|
|
"directions": list(rule.directions),
|
|
"reason": rule.reason,
|
|
}
|
|
return {
|
|
"id": rule["id"],
|
|
"label": rule["label"],
|
|
"category": rule["category"],
|
|
"protocols": list(rule["protocols"]),
|
|
"port_from": rule["port_from"],
|
|
"port_to": rule["port_to"],
|
|
"directions": list(rule["directions"]),
|
|
"reason": rule["reason"],
|
|
}
|
|
|
|
|
|
def _snapshot_document(
|
|
catalog_revision: int, rules: list[dict[str, Any]],
|
|
) -> dict[str, Any]:
|
|
return {
|
|
"schema_version": PLATFORM_POLICY_SNAPSHOT_SCHEMA_VERSION,
|
|
"base_policy_version": PLATFORM_DEFAULT_NETWORK_POLICY.policy_version,
|
|
"catalog_revision": catalog_revision,
|
|
"rules": rules,
|
|
}
|
|
|
|
|
|
def platform_policy_snapshot_digest(document: dict[str, Any]) -> str:
|
|
encoded = json.dumps(
|
|
document, ensure_ascii=False, separators=(",", ":"),
|
|
).encode("utf-8")
|
|
return hashlib.sha256(encoded).hexdigest()
|
|
|
|
|
|
def default_platform_policy_snapshot() -> dict[str, Any]:
|
|
"""Return an explicit immutable baseline for legacy/no-test creates."""
|
|
document = _snapshot_document(
|
|
0, [_rule_document(rule) for rule in PLATFORM_DEFAULT_NETWORK_POLICY.rules],
|
|
)
|
|
return {**document, "digest": platform_policy_snapshot_digest(document)}
|
|
|
|
|
|
def normalize_platform_policy_snapshot(value: Any) -> dict[str, Any]:
|
|
"""Validate one complete PHP-sealed static+test policy snapshot."""
|
|
expected_keys = {
|
|
"schema_version", "base_policy_version", "catalog_revision",
|
|
"rules", "digest",
|
|
}
|
|
if not isinstance(value, dict) or set(value) != expected_keys:
|
|
raise ValueError("platform policy snapshot fields are invalid")
|
|
revision = value.get("catalog_revision")
|
|
digest = value.get("digest")
|
|
if (value.get("schema_version") != PLATFORM_POLICY_SNAPSHOT_SCHEMA_VERSION
|
|
or value.get("base_policy_version")
|
|
!= PLATFORM_DEFAULT_NETWORK_POLICY.policy_version
|
|
or isinstance(revision, bool) or not isinstance(revision, int)
|
|
or not 0 <= revision <= 2**63 - 1
|
|
or not isinstance(digest, str)
|
|
or re.fullmatch(r"[0-9a-f]{64}", digest) is None
|
|
or not isinstance(value.get("rules"), list)):
|
|
raise ValueError("platform policy snapshot is invalid")
|
|
|
|
base = [
|
|
_rule_document(rule) for rule in PLATFORM_DEFAULT_NETWORK_POLICY.rules
|
|
]
|
|
raw_rules = value["rules"]
|
|
if len(raw_rules) < len(base) or raw_rules[:len(base)] != base:
|
|
raise ValueError("platform policy baseline does not match this engine")
|
|
|
|
expanded: list[tuple[str, int, int, str]] = [
|
|
(protocol, rule.port_from, rule.port_to, direction)
|
|
for rule in PLATFORM_DEFAULT_NETWORK_POLICY.rules
|
|
for direction in rule.directions
|
|
for protocol in rule.protocols
|
|
]
|
|
unrestricted = {
|
|
(item.protocol, item.port)
|
|
for item in PLATFORM_DEFAULT_NETWORK_POLICY.explicitly_unrestricted
|
|
}
|
|
dynamic: list[dict[str, Any]] = []
|
|
seen_ids: set[str] = set()
|
|
rule_keys = {
|
|
"id", "label", "category", "protocols", "port_from", "port_to",
|
|
"directions", "reason",
|
|
}
|
|
for index, raw in enumerate(raw_rules[len(base):]):
|
|
if (not isinstance(raw, dict) or set(raw) != rule_keys
|
|
or raw.get("category") != "test"):
|
|
raise ValueError(f"platform test rule {index} is invalid")
|
|
rule_id = raw.get("id")
|
|
if (not isinstance(rule_id, str)
|
|
or re.fullmatch(r"test-[0-9a-f]{32}", rule_id) is None
|
|
or rule_id in seen_ids):
|
|
raise ValueError(f"platform test rule {index} id is invalid")
|
|
seen_ids.add(rule_id)
|
|
try:
|
|
protocols = _string_tuple(
|
|
raw.get("protocols"), {"tcp", "udp"},
|
|
f"platform test rule {index} protocols",
|
|
)
|
|
except RuntimeError as exc:
|
|
raise ValueError(
|
|
f"platform test rule {index} protocols are invalid",
|
|
) from exc
|
|
protocols = tuple(
|
|
item for item in ("tcp", "udp") if item in protocols
|
|
)
|
|
try:
|
|
directions = _string_tuple(
|
|
raw.get("directions"), {"ingress", "egress"},
|
|
f"platform test rule {index} directions",
|
|
)
|
|
except RuntimeError as exc:
|
|
raise ValueError(
|
|
f"platform test rule {index} directions are invalid",
|
|
) from exc
|
|
directions = tuple(
|
|
item for item in ("ingress", "egress") if item in directions
|
|
)
|
|
port_from = raw.get("port_from")
|
|
port_to = raw.get("port_to")
|
|
if (isinstance(port_from, bool) or not isinstance(port_from, int)
|
|
or isinstance(port_to, bool) or not isinstance(port_to, int)
|
|
or not 1 <= port_from <= port_to <= 65535):
|
|
raise ValueError(f"platform test rule {index} ports are invalid")
|
|
for protocol in protocols:
|
|
if any(
|
|
blocked_protocol == protocol
|
|
and port_from <= blocked_port <= port_to
|
|
for blocked_protocol, blocked_port in unrestricted
|
|
):
|
|
raise ValueError("explicitly unrestricted port cannot be tested")
|
|
for direction in directions:
|
|
if any(
|
|
old_protocol == protocol and old_direction == direction
|
|
and port_from <= old_to and port_to >= old_from
|
|
for old_protocol, old_from, old_to, old_direction in expanded
|
|
):
|
|
raise ValueError("platform test rules overlap")
|
|
expanded.append((protocol, port_from, port_to, direction))
|
|
if len(expanded) > MAX_PLATFORM_FILTER_RULES:
|
|
raise ValueError("platform policy exceeds the filter priority budget")
|
|
try:
|
|
label = _text(
|
|
raw.get("label"), f"platform test rule {index} label",
|
|
max_bytes=120,
|
|
)
|
|
reason = _text(
|
|
raw.get("reason"), f"platform test rule {index} reason",
|
|
max_bytes=500,
|
|
)
|
|
except RuntimeError as exc:
|
|
raise ValueError(
|
|
f"platform test rule {index} text is invalid",
|
|
) from exc
|
|
dynamic.append(_rule_document({
|
|
"id": rule_id,
|
|
"label": label,
|
|
"category": "test",
|
|
"protocols": protocols,
|
|
"port_from": port_from,
|
|
"port_to": port_to,
|
|
"directions": directions,
|
|
"reason": reason,
|
|
}))
|
|
dynamic.sort(key=lambda item: item["id"])
|
|
rules = base + dynamic
|
|
if raw_rules != rules:
|
|
raise ValueError("platform policy rules are not canonical")
|
|
document = _snapshot_document(revision, rules)
|
|
expected_digest = platform_policy_snapshot_digest(document)
|
|
normalized = {**document, "digest": expected_digest}
|
|
encoded = json.dumps(
|
|
normalized, ensure_ascii=False, separators=(",", ":"),
|
|
).encode("utf-8")
|
|
if len(encoded) > MAX_PLATFORM_POLICY_SNAPSHOT_BYTES:
|
|
raise ValueError("platform policy snapshot is too large")
|
|
if not hmac.compare_digest(expected_digest, digest):
|
|
raise ValueError("platform policy snapshot digest is invalid")
|
|
return normalized
|