187 lines
4.2 KiB
Python
187 lines
4.2 KiB
Python
"""Static ABSDEV site isolation and registry validation.
|
|
|
|
This module does not inspect or modify live infrastructure.
|
|
"""
|
|
|
|
from core.configuration import validate_config
|
|
from core.compatibility import canonical_hash
|
|
|
|
|
|
class SitePolicyError(RuntimeError):
|
|
"""Unsafe site configuration or resource conflict."""
|
|
|
|
|
|
SITE_BASE = "/opt/laravel-sites"
|
|
|
|
RESERVED_USERS = {
|
|
"root",
|
|
"postgres",
|
|
"mysql",
|
|
"admin",
|
|
"administrator",
|
|
"mariadb.sys",
|
|
"mysql.sys",
|
|
"mysql.session",
|
|
"mysql.infoschema",
|
|
}
|
|
|
|
RESERVED_DATABASES = {
|
|
"mysql",
|
|
"postgres",
|
|
"template0",
|
|
"template1",
|
|
"information_schema",
|
|
"performance_schema",
|
|
"sys",
|
|
}
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise SitePolicyError(message)
|
|
|
|
|
|
def validated(config):
|
|
try:
|
|
return validate_config(config)
|
|
except Exception:
|
|
raise SitePolicyError(
|
|
"Invalid site configuration"
|
|
) from None
|
|
|
|
|
|
def validate_site(config):
|
|
config = validated(config)
|
|
|
|
site = config["site"]
|
|
docker = config["docker"]
|
|
database = config["database"]
|
|
|
|
identifier = site["identifier"]
|
|
|
|
require(
|
|
site["root"] == SITE_BASE + "/" + identifier,
|
|
"Site root must match the trusted site directory",
|
|
)
|
|
|
|
require(
|
|
docker["project"] == "site-" + identifier,
|
|
"Docker project must match site identity",
|
|
)
|
|
|
|
require(
|
|
1024 <= site["origin_port"] <= 65535,
|
|
"Privileged or invalid origin port",
|
|
)
|
|
|
|
require(
|
|
database["username"].lower() not in RESERVED_USERS,
|
|
"Reserved database username",
|
|
)
|
|
|
|
require(
|
|
database["name"].lower() not in RESERVED_DATABASES,
|
|
"Reserved database name",
|
|
)
|
|
|
|
services = [
|
|
docker["php_service"],
|
|
docker["web_service"],
|
|
docker["queue_service"],
|
|
docker["scheduler_service"],
|
|
]
|
|
|
|
require(
|
|
len(set(services)) == len(services),
|
|
"Application service names must be distinct",
|
|
)
|
|
|
|
require(
|
|
docker["backend_network"] != docker["proxy_network"],
|
|
"Backend and proxy networks must be distinct",
|
|
)
|
|
|
|
result = {
|
|
"format_version": 1,
|
|
"status": "VALID",
|
|
"site": identifier,
|
|
"configuration_fingerprint": canonical_hash(config),
|
|
"static_policy_verified": True,
|
|
"runtime_verified": False,
|
|
"execution_permitted": False,
|
|
}
|
|
|
|
result["fingerprint"] = canonical_hash(result)
|
|
|
|
return result
|
|
|
|
|
|
def validate_registry(configurations):
|
|
require(
|
|
isinstance(configurations, list)
|
|
and 1 <= len(configurations) <= 10000,
|
|
"Invalid site registry",
|
|
)
|
|
|
|
resources = {
|
|
"identifier": set(),
|
|
"domain": set(),
|
|
"root": set(),
|
|
"origin_port": set(),
|
|
"docker_project": set(),
|
|
"database": set(),
|
|
"database_username": set(),
|
|
}
|
|
|
|
reports = []
|
|
|
|
for item in configurations:
|
|
config = validated(item)
|
|
|
|
report = validate_site(config)
|
|
|
|
site = config["site"]
|
|
docker = config["docker"]
|
|
database = config["database"]
|
|
|
|
# Conservatively reject duplicate database names
|
|
# and users across the registry, even when the
|
|
# database containers differ.
|
|
|
|
keys = {
|
|
"identifier": site["identifier"],
|
|
"domain": site["domain"].lower(),
|
|
"root": site["root"],
|
|
"origin_port": site["origin_port"],
|
|
"docker_project": docker["project"].lower(),
|
|
"database": database["name"].lower(),
|
|
"database_username": database["username"].lower(),
|
|
}
|
|
|
|
for category, value in keys.items():
|
|
require(
|
|
value not in resources[category],
|
|
f"Duplicate site resource: {category}",
|
|
)
|
|
|
|
resources[category].add(value)
|
|
|
|
reports.append(report)
|
|
|
|
result = {
|
|
"format_version": 1,
|
|
"status": "VALID",
|
|
"site_count": len(reports),
|
|
"sites": sorted(
|
|
reports,
|
|
key=lambda item: item["site"],
|
|
),
|
|
"registry_conflicts_checked": True,
|
|
"runtime_verified": False,
|
|
"execution_permitted": False,
|
|
}
|
|
|
|
result["fingerprint"] = canonical_hash(result)
|
|
|
|
return result
|