From 5f5c3eb3b9d1d0c66a3f6a22b09a0f3b90fad194 Mon Sep 17 00:00:00 2001 From: Canina Engineering Date: Sun, 25 Oct 2026 06:52:05 +0000 Subject: [PATCH] fix(infra): refresh Docker upstream DNS and provision revalidation runtime --- nginx.conf | 16 ++- scripts/compose.stage.yml | 8 +- scripts/provision-revalidation-env.py | 56 ++++++++++ scripts/rollout-stage.sh | 8 ++ scripts/tests/test_revalidation_runtime.py | 114 +++++++++++++++++++++ 5 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 scripts/provision-revalidation-env.py create mode 100644 scripts/tests/test_revalidation_runtime.py diff --git a/nginx.conf b/nginx.conf index c8d98c34..89e36cca 100644 --- a/nginx.conf +++ b/nginx.conf @@ -1,5 +1,9 @@ server { listen 8080; + # Docker service IPs change on recreation; resolve at request time, not startup. + resolver 127.0.0.11 valid=5s ipv6=off; + resolver_timeout 2s; + set $backend_upstream __BACKEND_HOST__:3000; server_name canina.ir stage.canina.ir localhost; # Security Headers @@ -31,7 +35,7 @@ server { # Uploads Static Files location /uploads { - proxy_pass http://__BACKEND_HOST__:3000; + proxy_pass http://$backend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; @@ -42,7 +46,7 @@ server { # Backend API location /api { - proxy_pass http://__BACKEND_HOST__:3000; + proxy_pass http://$backend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; @@ -54,6 +58,10 @@ server { server { listen 8080; + # Docker service IPs change on recreation; resolve at request time, not startup. + resolver 127.0.0.11 valid=5s ipv6=off; + resolver_timeout 2s; + set $backend_upstream __BACKEND_HOST__:3000; server_name admin.canina.ir stageadmin.canina.ir; root /app/admin_static; @@ -73,7 +81,7 @@ server { # Uploads Static Files for Admin location /uploads { - proxy_pass http://__BACKEND_HOST__:3000; + proxy_pass http://$backend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; @@ -84,7 +92,7 @@ server { # Backend API for Admin location /api { - proxy_pass http://__BACKEND_HOST__:3000; + proxy_pass http://$backend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; diff --git a/scripts/compose.stage.yml b/scripts/compose.stage.yml index 5dc24761..45e852ad 100644 --- a/scripts/compose.stage.yml +++ b/scripts/compose.stage.yml @@ -31,7 +31,7 @@ services: - path: .support-chat.env required: false - path: .revalidation.env - required: false + required: true dns: - 217.218.127.127 - 217.218.155.155 @@ -42,6 +42,8 @@ services: - REDIS_PORT=6379 - PORT=3000 - FRONTEND_URL=https://stage.canina.ir + # Direct Next port avoids nginx /api forwarding back into this backend. + - FRONTEND_INTERNAL_URL=http://frontend_stage:3000 # Code-only stage startup: schema/seed changes require separate authorization. command: ["node", "dist/main.js"] volumes: @@ -69,9 +71,11 @@ services: VITE_API_URL: "https://stageapi.canina.ir" container_name: canino_frontend_stage restart: always + environment: + - INTERNAL_API_URL=http://backend_stage:3000/api env_file: - path: .revalidation.env - required: false + required: true dns: - 217.218.127.127 - 217.218.155.155 diff --git a/scripts/provision-revalidation-env.py b/scripts/provision-revalidation-env.py new file mode 100644 index 00000000..e3cfec37 --- /dev/null +++ b/scripts/provision-revalidation-env.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +"""Provision one shared backend/Next runtime secret; never emit its value. + +Run under the environment's serialized CI deployment lock, before Compose config. +Existing operational files are read-only inputs, not rewritten or chmodded. +""" +import argparse +import os +from pathlib import Path +import re +import secrets +import stat + + +def provision(source: Path, output: Path) -> None: + for path in (source, output): + if any(parent.is_symlink() for parent in (path, *path.parents)): + raise ValueError('Unsafe runtime path') + if not source.exists(): + fd = os.open(source, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, 'w') as stream: + stream.write('REVALIDATION_SECRET=' + secrets.token_urlsafe(48) + '\n') + fd = os.open(source, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) + with os.fdopen(fd) as stream: + info = os.fstat(stream.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1 or info.st_size > 4096: + raise ValueError('Unsafe runtime file') + text = stream.read(4097) + lines = [line.strip() for line in text.splitlines() + if line.strip() and not line.lstrip().startswith('#')] + if any(not line.startswith('REVALIDATION_SECRET=') for line in lines): + raise ValueError('Unexpected runtime configuration') + values = [line.partition('=')[2].strip().strip('\"\'') for line in lines] + if len(values) != 1 or not re.fullmatch(r'[A-Za-z0-9_+/=-]{32,256}', values[0]): + raise ValueError('Invalid revalidation runtime configuration') + fd = os.open(output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, 'w') as stream: + stream.write('REVALIDATION_SECRET=' + values[0] + '\n') + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--environment', choices=('stage', 'prod'), required=True) + parser.add_argument('--source', type=Path, required=True) + parser.add_argument('--output', type=Path, required=True) + args = parser.parse_args() + if os.environ.get('GITHUB_ACTIONS', os.environ.get('GITEA_ACTIONS')) != 'true': + parser.error('Provisioning requires serialized CI deployment') + try: + provision(args.source, args.output) + except (ValueError, OSError): + parser.exit(1, 'ERROR: revalidation provisioning failed; no secret values logged\n') + + +if __name__ == '__main__': + main() diff --git a/scripts/rollout-stage.sh b/scripts/rollout-stage.sh index cd487220..c5ae3e8c 100644 --- a/scripts/rollout-stage.sh +++ b/scripts/rollout-stage.sh @@ -1,6 +1,14 @@ #!/bin/bash # Sourced by the CI/CD deploy entrypoint, never a standalone deploy command. # Invoked only by the authenticated Stage CI entrypoint under its rollout lock. +# Call after runtime-copy exclusions, before Compose config; do not copy the +# revalidation file separately (the helper refuses to overwrite release files). +stage_revalidation_provision() { + python3 scripts/provision-revalidation-env.py --environment stage \ + --source "$STAGE_DIR/.revalidation.env" \ + --output "$TARGET_DIR/.revalidation.env" +} + stage_runtime_provision() { [ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" = true ] [ "${STAGE_PROVISION_ENABLED:-}" = true ] diff --git a/scripts/tests/test_revalidation_runtime.py b/scripts/tests/test_revalidation_runtime.py new file mode 100644 index 00000000..08307f80 --- /dev/null +++ b/scripts/tests/test_revalidation_runtime.py @@ -0,0 +1,114 @@ +import importlib.util +import os +from pathlib import Path +import stat +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[2] +HELPER = ROOT / 'scripts/provision-revalidation-env.py' + +class RevalidationRuntimeTests(unittest.TestCase): + def load(self): + self.assertTrue(HELPER.is_file(), 'secure revalidation provisioning helper missing') + spec = importlib.util.spec_from_file_location('revalidation_env', HELPER) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + def test_preserves_existing_secret_and_writes_private_release_file(self): + module = self.load() + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + source, output = root/'runtime.env', root/'release.env' + original = '# existing runtime configuration\nREVALIDATION_SECRET=' + 'a'*48 + '\n' + source.write_text(original) + module.provision(source, output) + self.assertEqual(source.read_text(), original) + self.assertEqual(output.read_text(), 'REVALIDATION_SECRET='+'a'*48+'\n') + self.assertEqual(stat.S_IMODE(output.stat().st_mode), 0o600) + + def test_missing_runtime_secret_is_generated_once_and_reused(self): + module = self.load() + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + source = root/'runtime.env' + module.provision(source, root/'first.env') + module.provision(source, root/'second.env') + self.assertEqual((root/'first.env').read_bytes(), (root/'second.env').read_bytes()) + self.assertEqual(stat.S_IMODE(source.stat().st_mode), 0o600) + self.assertGreaterEqual(len(source.read_text().split('=', 1)[1].strip()), 43) + + def test_unsafe_or_invalid_input_is_rejected_without_touching_output(self): + module = self.load() + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + target = root/'target' + target.write_text('REVALIDATION_SECRET='+'b'*48+'\n') + source = root/'source' + source.symlink_to(target) + with self.assertRaises(ValueError): + module.provision(source, root/'output') + source.unlink() + os.link(target, source) + with self.assertRaises(ValueError): + module.provision(source, root/'output') + source.unlink() + for content in ('', 'REVALIDATION_SECRET=short\n', 'REVALIDATION_SECRET='+'a'*48+'\nREVALIDATION_SECRET='+'b'*48+'\n', 'REVALIDATION_SECRET='+'a'*48+'\nUNEXPECTED=value\n'): + source.write_text(content) + with self.assertRaises(ValueError): + module.provision(source, root/'output') + self.assertFalse((root/'output').exists()) + + def test_cli_is_silent_and_uses_same_contract_for_each_environment(self): + import sys + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + for environment in ('stage', 'prod'): + result = subprocess.run([sys.executable, str(HELPER), '--environment', environment, + '--source', str(root/(environment+'.env')), '--output', str(root/(environment+'-release.env'))], + env={**os.environ, 'GITHUB_ACTIONS': 'true'}, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout + result.stderr, '') + self.assertEqual((root/(environment+'.env')).read_bytes(), (root/(environment+'-release.env')).read_bytes()) + self.assertNotEqual((root/'stage.env').read_bytes(), (root/'prod.env').read_bytes()) + + def test_symlink_parent_cannot_create_a_persistent_secret(self): + module = self.load() + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + real = root/'real' + real.mkdir() + link = root/'link' + link.symlink_to(real, target_is_directory=True) + with self.assertRaises(ValueError): + module.provision(link/'source', root/'output') + self.assertFalse((real/'source').exists()) + +class StageEnvironmentTests(unittest.TestCase): + def test_stage_uses_direct_next_revalidation_and_shared_required_secret(self): + config = (ROOT/'scripts/compose.stage.yml').read_text() + self.assertIn('- FRONTEND_INTERNAL_URL=http://frontend_stage:3000', config) + frontend = config.split(' frontend_stage:', 1)[1] + self.assertIn('- INTERNAL_API_URL=http://backend_stage:3000/api', frontend) + self.assertEqual(config.count('path: .revalidation.env\n required: true'), 2) + rollout = (ROOT/'scripts/rollout-stage.sh').read_text() + self.assertIn('stage_revalidation_provision() {', rollout) + self.assertIn('--source "$STAGE_DIR/.revalidation.env"', rollout) + self.assertIn('--output "$TARGET_DIR/.revalidation.env"', rollout) + +class RoutingTests(unittest.TestCase): + def test_all_backend_routes_use_runtime_docker_dns_without_uri_rewrite(self): + config = (ROOT/'nginx.conf').read_text() + servers = config.split('server {')[1:] + self.assertEqual(len(servers), 2) + for server in servers: + self.assertIn('resolver 127.0.0.11 valid=5s ipv6=off;', server) + self.assertIn('resolver_timeout 2s;', server) + self.assertIn('set $backend_upstream __BACKEND_HOST__:3000;', server) + self.assertEqual(server.count('proxy_pass http://$backend_upstream;'), 2) + self.assertNotIn('proxy_pass http://__BACKEND_HOST__', config) + +if __name__ == '__main__': + unittest.main()