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,442 @@
|
||||
"""Strict, non-secret deployment configuration validation."""
|
||||
|
||||
import copy
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path, PurePosixPath
|
||||
|
||||
|
||||
class ConfigurationError(ValueError):
|
||||
"""Invalid deployment configuration."""
|
||||
|
||||
|
||||
def require(condition, message):
|
||||
|
||||
if not condition:
|
||||
raise ConfigurationError(message)
|
||||
|
||||
|
||||
def object_fields(value, fields, location):
|
||||
|
||||
require(
|
||||
isinstance(value, dict),
|
||||
f"{location} must be an object",
|
||||
)
|
||||
|
||||
require(
|
||||
set(value) == set(fields),
|
||||
f"{location} contains missing or unknown fields",
|
||||
)
|
||||
|
||||
|
||||
def string_pattern(value, pattern, location):
|
||||
|
||||
require(
|
||||
isinstance(value, str)
|
||||
and re.fullmatch(pattern, value) is not None,
|
||||
f"Invalid {location}",
|
||||
)
|
||||
|
||||
|
||||
def integer(value, minimum, maximum, location):
|
||||
|
||||
require(
|
||||
type(value) is int
|
||||
and minimum <= value <= maximum,
|
||||
f"Invalid {location}",
|
||||
)
|
||||
|
||||
|
||||
def required_true(value, location):
|
||||
|
||||
require(
|
||||
value is True,
|
||||
f"{location} must be enabled",
|
||||
)
|
||||
|
||||
|
||||
def absolute_path(value, location):
|
||||
|
||||
require(
|
||||
isinstance(value, str)
|
||||
and re.fullmatch(
|
||||
r"/[A-Za-z0-9._/-]+", value
|
||||
) is not None,
|
||||
f"Invalid {location}",
|
||||
)
|
||||
|
||||
path = PurePosixPath(value)
|
||||
|
||||
require(
|
||||
path.is_absolute()
|
||||
and str(path) == value
|
||||
and ".." not in path.parts
|
||||
and value != "/",
|
||||
f"Unsafe {location}",
|
||||
)
|
||||
|
||||
|
||||
def domain(value):
|
||||
|
||||
require(
|
||||
isinstance(value, str)
|
||||
and len(value) <= 253,
|
||||
"Invalid site domain",
|
||||
)
|
||||
|
||||
parts = value.split(".")
|
||||
|
||||
require(
|
||||
len(parts) >= 2,
|
||||
"Invalid site domain",
|
||||
)
|
||||
|
||||
for part in parts:
|
||||
|
||||
string_pattern(
|
||||
part,
|
||||
r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?",
|
||||
"domain label",
|
||||
)
|
||||
|
||||
|
||||
def repository_url(value):
|
||||
|
||||
require(
|
||||
isinstance(value, str),
|
||||
"Invalid repository URL",
|
||||
)
|
||||
|
||||
patterns = (
|
||||
r"(?:[A-Za-z0-9_-]+@)?"
|
||||
r"[A-Za-z0-9.-]+:"
|
||||
r"[A-Za-z0-9_./-]+\.git",
|
||||
|
||||
r"https://[A-Za-z0-9.-]+"
|
||||
r"(?::[0-9]+)?"
|
||||
r"/[A-Za-z0-9_./-]+\.git",
|
||||
|
||||
r"ssh://(?:[A-Za-z0-9_-]+@)?"
|
||||
r"[A-Za-z0-9.-]+"
|
||||
r"(?::[0-9]+)?"
|
||||
r"/[A-Za-z0-9_./-]+\.git",
|
||||
)
|
||||
|
||||
require(
|
||||
any(
|
||||
re.fullmatch(pattern, value)
|
||||
for pattern in patterns
|
||||
)
|
||||
and ".." not in value,
|
||||
"Unsafe or unsupported repository URL",
|
||||
)
|
||||
|
||||
|
||||
def validate_config(data):
|
||||
|
||||
object_fields(
|
||||
data,
|
||||
{
|
||||
"schema_version",
|
||||
"site",
|
||||
"repository",
|
||||
"database",
|
||||
"docker",
|
||||
"retention",
|
||||
"migration",
|
||||
"backup",
|
||||
"health",
|
||||
},
|
||||
"Configuration",
|
||||
)
|
||||
|
||||
require(
|
||||
type(data["schema_version"]) is int
|
||||
and data["schema_version"] == 1,
|
||||
"Unsupported configuration schema",
|
||||
)
|
||||
|
||||
identifier = r"[a-z0-9][a-z0-9-]{0,62}"
|
||||
service_name = r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}"
|
||||
database_name = r"[A-Za-z_][A-Za-z0-9_]{0,62}"
|
||||
|
||||
site = data["site"]
|
||||
|
||||
object_fields(
|
||||
site,
|
||||
{"identifier", "domain", "root", "origin_port"},
|
||||
"site",
|
||||
)
|
||||
|
||||
string_pattern(
|
||||
site["identifier"], identifier, "site identifier"
|
||||
)
|
||||
|
||||
domain(site["domain"])
|
||||
absolute_path(site["root"], "site root")
|
||||
|
||||
integer(
|
||||
site["origin_port"], 1024, 65535, "origin port"
|
||||
)
|
||||
|
||||
repository = data["repository"]
|
||||
|
||||
object_fields(
|
||||
repository,
|
||||
{"url", "branch"},
|
||||
"repository",
|
||||
)
|
||||
|
||||
repository_url(repository["url"])
|
||||
|
||||
branch = repository["branch"]
|
||||
|
||||
string_pattern(
|
||||
branch,
|
||||
r"[A-Za-z0-9][A-Za-z0-9_./-]{0,127}",
|
||||
"repository branch",
|
||||
)
|
||||
|
||||
require(
|
||||
".." not in branch
|
||||
and all(
|
||||
part
|
||||
and not part.startswith(".")
|
||||
and not part.endswith(".lock")
|
||||
and not part.endswith(".")
|
||||
for part in branch.split("/")
|
||||
),
|
||||
"Unsafe repository branch",
|
||||
)
|
||||
|
||||
database = data["database"]
|
||||
|
||||
object_fields(
|
||||
database,
|
||||
{
|
||||
"driver",
|
||||
"container",
|
||||
"host",
|
||||
"port",
|
||||
"name",
|
||||
"username",
|
||||
},
|
||||
"database",
|
||||
)
|
||||
|
||||
require(
|
||||
database["driver"] in ("mysql", "pgsql"),
|
||||
"Unsupported database driver",
|
||||
)
|
||||
|
||||
string_pattern(
|
||||
database["container"],
|
||||
service_name,
|
||||
"database container",
|
||||
)
|
||||
|
||||
string_pattern(
|
||||
database["host"],
|
||||
r"[A-Za-z0-9][A-Za-z0-9.-]{0,252}",
|
||||
"database host",
|
||||
)
|
||||
|
||||
string_pattern(
|
||||
database["name"],
|
||||
database_name,
|
||||
"database name",
|
||||
)
|
||||
|
||||
string_pattern(
|
||||
database["username"],
|
||||
database_name,
|
||||
"database username",
|
||||
)
|
||||
|
||||
integer(
|
||||
database["port"], 1, 65535, "database port"
|
||||
)
|
||||
|
||||
docker = data["docker"]
|
||||
|
||||
object_fields(
|
||||
docker,
|
||||
{
|
||||
"project",
|
||||
"application_path",
|
||||
"php_service",
|
||||
"web_service",
|
||||
"queue_service",
|
||||
"scheduler_service",
|
||||
"backend_network",
|
||||
"proxy_network",
|
||||
},
|
||||
"docker",
|
||||
)
|
||||
|
||||
absolute_path(
|
||||
docker["application_path"],
|
||||
"container application path",
|
||||
)
|
||||
|
||||
service_keys = (
|
||||
"php_service",
|
||||
"web_service",
|
||||
"queue_service",
|
||||
"scheduler_service",
|
||||
)
|
||||
|
||||
for key in (
|
||||
"project",
|
||||
*service_keys,
|
||||
"backend_network",
|
||||
"proxy_network",
|
||||
):
|
||||
|
||||
string_pattern(
|
||||
docker[key], service_name, f"docker.{key}"
|
||||
)
|
||||
|
||||
require(
|
||||
len({docker[key] for key in service_keys}) == 4,
|
||||
"Docker service names must be distinct",
|
||||
)
|
||||
|
||||
retention = data["retention"]
|
||||
|
||||
object_fields(
|
||||
retention,
|
||||
{"keep_releases", "automatic_deletion"},
|
||||
"retention",
|
||||
)
|
||||
|
||||
integer(
|
||||
retention["keep_releases"],
|
||||
1, 100,
|
||||
"retention count",
|
||||
)
|
||||
|
||||
require(
|
||||
retention["automatic_deletion"] is False,
|
||||
"Automatic destructive cleanup is prohibited",
|
||||
)
|
||||
|
||||
migration = data["migration"]
|
||||
|
||||
object_fields(
|
||||
migration,
|
||||
{
|
||||
"require_approval",
|
||||
"require_rehearsal",
|
||||
"approval_ttl_hours",
|
||||
},
|
||||
"migration",
|
||||
)
|
||||
|
||||
required_true(
|
||||
migration["require_approval"], "migration approval"
|
||||
)
|
||||
|
||||
required_true(
|
||||
migration["require_rehearsal"], "migration rehearsal"
|
||||
)
|
||||
|
||||
integer(
|
||||
migration["approval_ttl_hours"],
|
||||
1, 72,
|
||||
"migration approval TTL",
|
||||
)
|
||||
|
||||
backup = data["backup"]
|
||||
|
||||
object_fields(
|
||||
backup,
|
||||
{"required", "verify_checksum"},
|
||||
"backup",
|
||||
)
|
||||
|
||||
required_true(backup["required"], "database backup")
|
||||
|
||||
required_true(
|
||||
backup["verify_checksum"], "backup checksum"
|
||||
)
|
||||
|
||||
health = data["health"]
|
||||
|
||||
object_fields(
|
||||
health,
|
||||
{"endpoints", "timeout_seconds"},
|
||||
"health",
|
||||
)
|
||||
|
||||
endpoints = health["endpoints"]
|
||||
|
||||
require(
|
||||
isinstance(endpoints, list)
|
||||
and 1 <= len(endpoints) <= 20,
|
||||
"Invalid health endpoint list",
|
||||
)
|
||||
|
||||
for endpoint in endpoints:
|
||||
|
||||
string_pattern(
|
||||
endpoint,
|
||||
r"/[A-Za-z0-9/_-]*",
|
||||
"health endpoint",
|
||||
)
|
||||
|
||||
require(
|
||||
not endpoint.startswith("//"),
|
||||
"External health endpoints are prohibited",
|
||||
)
|
||||
|
||||
require(
|
||||
len(set(endpoints)) == len(endpoints),
|
||||
"Duplicate health endpoints",
|
||||
)
|
||||
|
||||
integer(
|
||||
health["timeout_seconds"],
|
||||
1, 120,
|
||||
"health timeout",
|
||||
)
|
||||
|
||||
return copy.deepcopy(data)
|
||||
|
||||
|
||||
def unique_json_object(pairs):
|
||||
|
||||
result = {}
|
||||
|
||||
for key, value in pairs:
|
||||
|
||||
require(
|
||||
key not in result,
|
||||
"Duplicate JSON configuration field",
|
||||
)
|
||||
|
||||
result[key] = value
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def load_config(path):
|
||||
|
||||
try:
|
||||
|
||||
with Path(path).open(
|
||||
"r", encoding="utf-8"
|
||||
) as handle:
|
||||
|
||||
data = json.load(
|
||||
handle,
|
||||
object_pairs_hook=unique_json_object,
|
||||
)
|
||||
|
||||
except (OSError, UnicodeError, ValueError) as error:
|
||||
|
||||
raise ConfigurationError(
|
||||
"Unable to read valid configuration"
|
||||
) from error
|
||||
|
||||
return validate_config(data)
|
||||
Reference in New Issue
Block a user