This commit is contained in:
Mateusz Gruszczyński committed 2026-11-28 17:36:53 +01:00
1 parent 5b6d4a7903
commit 4d1087e357
160 files changed
+2407 -1933

No files matched your search

+2 -4
View File
@@ -2,8 +2,8 @@
"""Migrate Nginx Proxy Manager proxy hosts from SQLite into RustProxy."""
from __future__ import annotations
from migrate_npm_core import * # stable migration/model API
from migrate_npm_api import * # stable administration API helpers
from migrate_npm_core import *
from migrate_npm_api import *
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
@@ -69,7 +69,5 @@ def main() -> None:
except (MigrationError, OSError, sqlite3.Error) as error:
parser.exit(1, f"Migration failed: {error}\n")
if __name__ == "__main__":
main()
+1 -6
View File
@@ -15,7 +15,7 @@ class RustProxyClient:
handlers: list[Any] = [urllib.request.HTTPCookieProcessor(jar)]
if parsed.scheme == "https":
if insecure_tls:
context = ssl._create_unverified_context() # Explicit operator opt-in for local/self-signed admin TLS.
context = ssl._create_unverified_context()
else:
context = ssl.create_default_context(cafile=str(ca_file) if ca_file else None)
handlers.append(urllib.request.HTTPSHandler(context=context))
@@ -71,8 +71,6 @@ class RustProxyClient:
raise MigrationError("unexpected RustProxy PUT /api/config response")
return value
def write_private_json(path: Path, value: dict[str, Any]) -> None:
path = path.expanduser()
path.parent.mkdir(parents=True, exist_ok=True)
@@ -103,7 +101,6 @@ def split_ids(values: list[str] | None) -> list[str] | None:
out.append(item)
return out
def print_report(decisions: list[Decision]) -> None:
imported = sum(1 for d in decisions if d.action == "import")
skipped = sum(1 for d in decisions if d.action == "skip")
@@ -120,5 +117,3 @@ def print_report(decisions: list[Decision]) -> None:
print(" reason: " + decision.reason)
for warning in decision.warnings or []:
print(" warning: " + warning)
-18
View File
@@ -31,11 +31,9 @@ import urllib.request
MAX_CONFIG_BYTES = 2_097_152
class MigrationError(RuntimeError):
pass
@dataclass(frozen=True)
class NpmProxyHost:
id: int
@@ -55,7 +53,6 @@ class NpmProxyHost:
block_exploits: bool
meta: dict[str, Any]
@dataclass
class Decision:
npm_id: int
@@ -66,7 +63,6 @@ class Decision:
warnings: list[str] | None = None
reason: str | None = None
def json_value(raw: Any, fallback: Any) -> Any:
if raw is None or raw == "":
return fallback
@@ -77,13 +73,11 @@ def json_value(raw: Any, fallback: Any) -> Any:
except (TypeError, json.JSONDecodeError):
return fallback
def bool_value(raw: Any) -> bool:
if isinstance(raw, str):
return raw.strip().lower() in {"1", "true", "yes", "on"}
return bool(raw)
def normalize_host_pattern(value: str) -> str:
value = value.strip().lower().rstrip(".")
wildcard = value.startswith("*.")
@@ -100,11 +94,9 @@ def normalize_host_pattern(value: str) -> str:
raise MigrationError(f"hostname must be ASCII/punycode: {value!r}")
return ("*." if wildcard else "") + base
def sql_columns(connection: sqlite3.Connection, table: str) -> set[str]:
return {str(row[1]) for row in connection.execute(f"PRAGMA table_info({table})")}
def load_npm_hosts(database: Path) -> list[NpmProxyHost]:
if database.is_symlink() or not database.is_file():
raise MigrationError(f"NPM SQLite database not found or unsafe: {database}")
@@ -172,7 +164,6 @@ def load_npm_hosts(database: Path) -> list[NpmProxyHost]:
))
return result
def backend_url(item: NpmProxyHost) -> str:
if item.forward_scheme not in {"http", "https"}:
raise MigrationError(f"unsupported upstream scheme {item.forward_scheme!r}")
@@ -193,13 +184,11 @@ def backend_url(item: NpmProxyHost) -> str:
rendered = host
return f"{item.forward_scheme}://{rendered}:{item.forward_port}"
def slug(value: str) -> str:
value = value.lower().replace("*.", "wildcard-")
value = re.sub(r"[^a-z0-9_-]+", "-", value).strip("-_")
return value[:40] or "host"
def unique_id(base: str, used: set[str]) -> str:
base = re.sub(r"[^A-Za-z0-9_-]+", "-", base)[:64].strip("-_") or "npm"
candidate = base
@@ -211,7 +200,6 @@ def unique_id(base: str, used: set[str]) -> str:
used.add(candidate)
return candidate
def listener_inventory(config: dict[str, Any]) -> tuple[list[str], list[str]]:
http_ids: list[str] = []
https_ids: list[str] = []
@@ -225,7 +213,6 @@ def listener_inventory(config: dict[str, Any]) -> tuple[list[str], list[str]]:
(https_ids if binding.get("tls") else http_ids).append(listener_id)
return http_ids, https_ids
def selected_tls_ports(config: dict[str, Any], listener_ids: list[str]) -> set[int]:
selected = set(listener_ids)
ports: set[int] = set()
@@ -247,7 +234,6 @@ def selected_tls_ports(config: dict[str, Any], listener_ids: list[str]) -> set[i
ports.add(port)
return ports
def existing_host_owners(config: dict[str, Any]) -> dict[str, str]:
result: dict[str, str] = {}
for vhost in config.get("vhosts") or []:
@@ -263,7 +249,6 @@ def existing_host_owners(config: dict[str, Any]) -> dict[str, str]:
pass
return result
def warnings_for(item: NpmProxyHost, tls_verify: bool) -> list[str]:
warnings: list[str] = []
if item.access_list_id:
@@ -284,7 +269,6 @@ def warnings_for(item: NpmProxyHost, tls_verify: bool) -> list[str]:
warnings.append("NPM meta.nginx_err is set; review the source host before cutover")
return warnings
def build_migration(
npm_hosts: list[NpmProxyHost],
config: dict[str, Any],
@@ -399,5 +383,3 @@ def build_migration(
owners[host] = vid
decisions.append(Decision(item.id, "import", normalized, backend=origin, rustproxy_ids=created_ids, warnings=warnings))
return new_config, decisions