#!/usr/bin/env python3 """MOYI test deployment helper. Python 3 standard library only.""" import argparse import hashlib import http.cookiejar import ipaddress import json import os from pathlib import Path import re import secrets import socket import stat import subprocess import sys import urllib.error import urllib.parse import urllib.request HERE = Path(__file__).resolve().parent ROOT = HERE.parent.parent def fail(message): raise ValueError(message) def execute(command, capture=False, data=None): # Never let docker compose or a diagnostic command consume the caller's # shell stdin. This is especially important when deploy.py is invoked from # an SSH heredoc; only an explicit data payload may be sent to a process. options = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL} if command[:2] == ["docker", "compose"] and "--env-file" in command: environment = os.environ.copy() environment["MOYI_RUNTIME_ENV_FILE"] = str(Path(command[command.index("--env-file") + 1]).resolve()) options["env"] = environment result = subprocess.run(command, cwd=HERE, capture_output=capture, text=True, encoding="utf-8", errors="replace", **options) if result.returncode: # Compose error output can contain expanded credentials. Do not echo it. fail("Command failed: " + " ".join(command[:3]) + ". Check local configuration/permissions; do not share secrets.") return result.stdout if capture else "" def initialize(): destination = HERE / ".env.cloud" if destination.exists(): fail(".env.cloud already exists; refusing to overwrite credentials.") content = (HERE / ".env.cloud.example").read_text(encoding="utf-8-sig") for marker in ("GENERATE_AUTH_PEPPER", "GENERATE_CREDENTIAL_KEY", "GENERATE_ADMIN_PASSWORD"): content = content.replace(marker, secrets.token_hex(32)) manifest = ROOT / "release-manifest.json" if manifest.exists(): release = json.loads(manifest.read_text(encoding="utf-8")) content = content.replace("MOYI_IMAGE_TAG=dev", "MOYI_IMAGE_TAG=" + release["release"]) content = content.replace("MOYI_API_DOCKERFILE=Dockerfile\n", "MOYI_API_DOCKERFILE=Dockerfile.runtime\n") content = content.replace("MOYI_FRONTEND_DOCKERFILE=Dockerfile\n", "MOYI_FRONTEND_DOCKERFILE=Dockerfile.runtime\n") descriptor = os.open(destination, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) with os.fdopen(descriptor, "w", encoding="utf-8", newline="\n") as output: output.write(content) (HERE / "certs").mkdir(mode=0o700, exist_ok=True) print("Created .env.cloud (0600). Secrets generated but NOT printed. Edit database/Tair settings locally.") def compose_command(env_file, mode): command = ["docker", "compose", "--env-file", str(env_file), "-f", "docker-compose.cloud.yml"] if mode == "https": command += ["-f", "docker-compose.https.yml"] return command def load_config(env_file, mode): if not env_file.is_file(): fail("Environment file missing. Run: python3 deploy.py init") if os.name != "nt" and stat.S_IMODE(env_file.stat().st_mode) & 0o077: fail("Environment file must be private. Run chmod 600 on it.") # A later empty assignment silently overrides an earlier valid secret in # Compose. Reject duplicates before resolving or starting any container. seen = set() for line in env_file.read_text(encoding="utf-8-sig").splitlines(): match = re.match(r"^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=", line) if match: key = match.group(1) if key in seen: fail("Duplicate environment setting: " + key + ". Keep exactly one assignment; values are not printed.") seen.add(key) config = execute(compose_command(env_file, mode) + ["config", "--format", "json"], capture=True) resolved = json.loads(config) # Compose serializes literal '$' as '$$' for re-parseable config output. for service in resolved.get("services", {}).values(): for key, value in service.get("environment", {}).items(): if isinstance(value, str): service["environment"][key] = value.replace("$$", "$") return resolved def validate_config(config, mode): services = config["services"] api = services["moyi-api"]["environment"] required = ("SPRING_DATASOURCE_URL", "SPRING_DATASOURCE_USERNAME", "SPRING_DATASOURCE_PASSWORD", "MOYI_REDIS_HOST", "MOYI_REDIS_PASSWORD", "AUTH_PASSWORD_PEPPER", "CREDENTIAL_ENCRYPTION_KEY", "ADMIN_PASSWORD") for key in required: value = str(api.get(key, "")) if not value or any(marker in value.upper() for marker in ("CHANGE_ME", "GENERATE_", "REPLACE-WITH")): fail("Missing/placeholder setting: " + key) if not str(api["ADMIN_PASSWORD"]).strip(): fail("ADMIN_PASSWORD must not be blank.") for key, minimum in (("AUTH_PASSWORD_PEPPER", 32), ("CREDENTIAL_ENCRYPTION_KEY", 32)): if len(str(api[key])) < minimum: fail(key + " is too short (minimum " + str(minimum) + " characters).") if api["AUTH_PASSWORD_PEPPER"] == api["CREDENTIAL_ENCRYPTION_KEY"]: fail("Auth pepper and credential encryption key must differ.") url = str(api["SPRING_DATASOURCE_URL"]) if not url.startswith("jdbc:postgresql://"): fail("This release requires RDS PostgreSQL (confirmed target: 15), not MySQL.") database = urllib.parse.urlsplit(url[5:]) if not database.hostname or database.username or database.password or database.path in ("", "/", "/postgres", "/template0", "/template1"): fail("Use a dedicated PostgreSQL database and separate username/password fields.") if not re.fullmatch(r"[A-Za-z0-9:_-]{2,80}:", str(api.get("MOYI_REDIS_KEY_PREFIX", ""))): fail("Set a dedicated Redis key prefix ending in ':'.") if str(api.get("MOYI_REDIS_SSL", "")).lower() not in ("true", "false"): fail("MOYI_REDIS_SSL must be true or false.") if services["moyi-api"].get("ports"): fail("Backend ports must not be published.") ports = services["frontend"].get("ports", []) if mode == "tunnel" and any(p.get("host_ip") != "127.0.0.1" for p in ports): fail("Tunnel mode must bind only to 127.0.0.1.") if mode == "https": domain = services["frontend"]["environment"].get("MOYI_DOMAIN", "") if not re.fullmatch(r"(?=.{1,253}$)[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?", domain) or "." not in domain: fail("MOYI_DOMAIN must be a hostname, without protocol/port/path.") if str(api.get("AUTH_SECURE_COOKIE")).lower() != "true": fail("HTTPS requires secure cookies.") elif str(api.get("AUTH_SECURE_COOKIE")).lower() != "false": fail("Local SSH-tunnel HTTP requires secure cookies disabled.") subnet = ipaddress.ip_network(config["networks"]["default"]["ipam"]["config"][0]["subnet"]) return api, database, subnet def verify_release(): manifest_path = ROOT / "release-manifest.json" if not manifest_path.exists(): print("Source checkout: no precompiled release manifest.") return manifest = json.loads(manifest_path.read_text(encoding="utf-8")) for name, expected in manifest["sha256"].items(): path = (ROOT / name).resolve() if not path.is_relative_to(ROOT.resolve()) or not path.is_file(): fail("Release file missing or outside package: " + name) if hashlib.sha256(path.read_bytes()).hexdigest() != expected: fail("Release checksum mismatch: " + name) print("Release checksums OK: " + manifest["release"]) def check_certificate(domain): certificate = HERE / "certs/fullchain.pem" key = HERE / "certs/privkey.pem" if not certificate.is_file() or not key.is_file(): fail("HTTPS requires certs/fullchain.pem and certs/privkey.pem.") if os.name != "nt" and stat.S_IMODE(key.stat().st_mode) & 0o077: fail("TLS private key must have mode 0600.") # -checkhost output alone is not portable as an exit-status assertion. details = execute(["openssl", "x509", "-in", str(certificate), "-noout", "-checkhost", domain], capture=True) if "does match certificate" not in details: fail("TLS certificate does not cover MOYI_DOMAIN.") execute(["openssl", "x509", "-in", str(certificate), "-noout", "-checkend", "86400"], capture=True) public_cert = execute(["openssl", "x509", "-in", str(certificate), "-noout", "-pubkey"], capture=True) public_key = execute(["openssl", "pkey", "-in", str(key), "-pubout", "-passin", "pass:"], capture=True) if public_cert.strip() != public_key.strip(): fail("TLS certificate and private key do not match.") renewal = subprocess.run(["openssl", "x509", "-in", str(certificate), "-noout", "-checkend", "2592000"], capture_output=True) print("TLS certificate/key OK." if renewal.returncode == 0 else "WARNING: TLS certificate expires within 30 days; renew it.") def check_network(api, database, subnet): targets = (("RDS", database.hostname, database.port or 5432), ("Tair", api["MOYI_REDIS_HOST"], int(api.get("MOYI_REDIS_PORT", 6379)))) for label, host, port in targets: try: addresses = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) if any(ipaddress.ip_address(item[4][0]) in subnet for item in addresses): fail(label + " address overlaps MOYI_DOCKER_SUBNET. Select a non-overlapping Docker subnet.") with socket.create_connection((host, port), timeout=5): pass except OSError: fail(label + " TCP check failed. Check private-network interconnection and whitelist.") print(label + " TCP reachable (authentication/TLS checked after application startup).") def preflight(env_file, mode, network=False): verify_release() config = load_config(env_file, mode) api, database, subnet = validate_config(config, mode) if mode == "https": check_certificate(config["services"]["frontend"]["environment"]["MOYI_DOMAIN"]) if network: check_network(api, database, subnet) print("Configuration OK. No credentials printed. Mode: " + mode) return config def smoke(config, mode, login=False): ports = config["services"]["frontend"]["ports"] port = next(str(p["published"]) for p in ports if p.get("host_ip") == "127.0.0.1") base = "http://127.0.0.1:" + port if mode == "tunnel" else "https://" + config["services"]["frontend"]["environment"]["MOYI_DOMAIN"] opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())) def request(path, expected=200, payload=None): body = json.dumps(payload).encode() if payload is not None else None req = urllib.request.Request(base + path, data=body, headers={"Content-Type": "application/json"}) try: response = opener.open(req, timeout=15) except urllib.error.HTTPError as error: response = error if response.code != expected: fail("Smoke check failed: " + path + " HTTP " + str(response.code)) return response.read() if b'