fix(infra): refresh Docker upstream DNS and provision revalidation runtime
This commit is contained in:
1 parent
3805057923
commit
5f5c3eb3b9
5 files changed
+196
-6
No files matched your search
+12
-4
@@ -1,5 +1,9 @@
|
|||||||
server {
|
server {
|
||||||
listen 8080;
|
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;
|
server_name canina.ir stage.canina.ir localhost;
|
||||||
|
|
||||||
# Security Headers
|
# Security Headers
|
||||||
@@ -31,7 +35,7 @@ server {
|
|||||||
|
|
||||||
# Uploads Static Files
|
# Uploads Static Files
|
||||||
location /uploads {
|
location /uploads {
|
||||||
proxy_pass http://__BACKEND_HOST__:3000;
|
proxy_pass http://$backend_upstream;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection 'upgrade';
|
proxy_set_header Connection 'upgrade';
|
||||||
@@ -42,7 +46,7 @@ server {
|
|||||||
|
|
||||||
# Backend API
|
# Backend API
|
||||||
location /api {
|
location /api {
|
||||||
proxy_pass http://__BACKEND_HOST__:3000;
|
proxy_pass http://$backend_upstream;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection 'upgrade';
|
proxy_set_header Connection 'upgrade';
|
||||||
@@ -54,6 +58,10 @@ server {
|
|||||||
|
|
||||||
server {
|
server {
|
||||||
listen 8080;
|
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;
|
server_name admin.canina.ir stageadmin.canina.ir;
|
||||||
|
|
||||||
root /app/admin_static;
|
root /app/admin_static;
|
||||||
@@ -73,7 +81,7 @@ server {
|
|||||||
|
|
||||||
# Uploads Static Files for Admin
|
# Uploads Static Files for Admin
|
||||||
location /uploads {
|
location /uploads {
|
||||||
proxy_pass http://__BACKEND_HOST__:3000;
|
proxy_pass http://$backend_upstream;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection 'upgrade';
|
proxy_set_header Connection 'upgrade';
|
||||||
@@ -84,7 +92,7 @@ server {
|
|||||||
|
|
||||||
# Backend API for Admin
|
# Backend API for Admin
|
||||||
location /api {
|
location /api {
|
||||||
proxy_pass http://__BACKEND_HOST__:3000;
|
proxy_pass http://$backend_upstream;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection 'upgrade';
|
proxy_set_header Connection 'upgrade';
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ services:
|
|||||||
- path: .support-chat.env
|
- path: .support-chat.env
|
||||||
required: false
|
required: false
|
||||||
- path: .revalidation.env
|
- path: .revalidation.env
|
||||||
required: false
|
required: true
|
||||||
dns:
|
dns:
|
||||||
- 217.218.127.127
|
- 217.218.127.127
|
||||||
- 217.218.155.155
|
- 217.218.155.155
|
||||||
@@ -42,6 +42,8 @@ services:
|
|||||||
- REDIS_PORT=6379
|
- REDIS_PORT=6379
|
||||||
- PORT=3000
|
- PORT=3000
|
||||||
- FRONTEND_URL=https://stage.canina.ir
|
- 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.
|
# Code-only stage startup: schema/seed changes require separate authorization.
|
||||||
command: ["node", "dist/main.js"]
|
command: ["node", "dist/main.js"]
|
||||||
volumes:
|
volumes:
|
||||||
@@ -69,9 +71,11 @@ services:
|
|||||||
VITE_API_URL: "https://stageapi.canina.ir"
|
VITE_API_URL: "https://stageapi.canina.ir"
|
||||||
container_name: canino_frontend_stage
|
container_name: canino_frontend_stage
|
||||||
restart: always
|
restart: always
|
||||||
|
environment:
|
||||||
|
- INTERNAL_API_URL=http://backend_stage:3000/api
|
||||||
env_file:
|
env_file:
|
||||||
- path: .revalidation.env
|
- path: .revalidation.env
|
||||||
required: false
|
required: true
|
||||||
dns:
|
dns:
|
||||||
- 217.218.127.127
|
- 217.218.127.127
|
||||||
- 217.218.155.155
|
- 217.218.155.155
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -1,6 +1,14 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Sourced by the CI/CD deploy entrypoint, never a standalone deploy command.
|
# Sourced by the CI/CD deploy entrypoint, never a standalone deploy command.
|
||||||
# Invoked only by the authenticated Stage CI entrypoint under its rollout lock.
|
# 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() {
|
stage_runtime_provision() {
|
||||||
[ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" = true ]
|
[ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" = true ]
|
||||||
[ "${STAGE_PROVISION_ENABLED:-}" = true ]
|
[ "${STAGE_PROVISION_ENABLED:-}" = true ]
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in new issue
Block a user