Initialize observability stack with local monitoring and server134 deployment
This commit is contained in:
Executable
+79
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
root_dir="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
check_script="${root_dir}/scripts/check-ports.sh"
|
||||
temp_dir="$(mktemp -d)"
|
||||
listener_pid=""
|
||||
|
||||
cleanup() {
|
||||
if [[ -n "${listener_pid}" ]]; then
|
||||
kill "${listener_pid}" >/dev/null 2>&1 || true
|
||||
wait "${listener_pid}" 2>/dev/null || true
|
||||
fi
|
||||
rm -f "${temp_dir}/docker" "${temp_dir}/python3" "${temp_dir}/listener.log"
|
||||
rmdir "${temp_dir}"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
cat >"${temp_dir}/python3" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
exec py "$@"
|
||||
EOF
|
||||
chmod +x "${temp_dir}/python3"
|
||||
|
||||
cat >"${temp_dir}/docker" <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
if [[ "${DOCKER_COMPOSE_OWNS_PORT:-false}" == "true" && "$*" == "compose port test-service 9999" ]]; then
|
||||
printf '127.0.0.1:%s\n' "${TEST_HOST_PORT}"
|
||||
exit 0
|
||||
fi
|
||||
if [[ "${DOCKER_EXTERNAL_OWNS_PORT:-false}" == "true" && "${1:-}" == "ps" ]]; then
|
||||
echo "deadbeef external-monitor 127.0.0.1:${TEST_HOST_PORT}->9999/tcp"
|
||||
exit 0
|
||||
fi
|
||||
exit 1
|
||||
EOF
|
||||
chmod +x "${temp_dir}/docker"
|
||||
|
||||
free_port="$(py -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()')"
|
||||
spec="test-service 9999 127.0.0.1 ${free_port}"
|
||||
|
||||
PATH="${temp_dir}:${PATH}" PORT_CHECK_SPEC="${spec}" "${check_script}" >/dev/null
|
||||
echo "PASS: an unused port is accepted"
|
||||
|
||||
py -c 'import socket,sys,time; s=socket.socket(); s.bind(("127.0.0.1", int(sys.argv[1]))); s.listen(); print("ready", flush=True); time.sleep(30)' "${free_port}" >"${temp_dir}/listener.log" &
|
||||
listener_pid=$!
|
||||
for _ in $(seq 1 50); do
|
||||
[[ -s "${temp_dir}/listener.log" ]] && break
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
set +e
|
||||
conflict_output="$(PATH="${temp_dir}:${PATH}" PORT_CHECK_SPEC="${spec}" "${check_script}" 2>&1)"
|
||||
conflict_code=$?
|
||||
set -e
|
||||
if [[ ${conflict_code} -eq 0 || "${conflict_output}" != *"${free_port}"* ]]; then
|
||||
echo "FAIL: an externally occupied port was not rejected with a useful message" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "PASS: an externally occupied port is rejected"
|
||||
|
||||
PATH="${temp_dir}:${PATH}" \
|
||||
PORT_CHECK_SPEC="${spec}" \
|
||||
DOCKER_COMPOSE_OWNS_PORT=true \
|
||||
TEST_HOST_PORT="${free_port}" \
|
||||
"${check_script}" >/dev/null
|
||||
echo "PASS: a port owned by the existing Compose service is accepted"
|
||||
|
||||
docker_port="$(py -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()')"
|
||||
docker_spec="test-service 9999 127.0.0.1 ${docker_port}"
|
||||
set +e
|
||||
docker_conflict_output="$(PATH="${temp_dir}:${PATH}" PORT_CHECK_SPEC="${docker_spec}" DOCKER_EXTERNAL_OWNS_PORT=true TEST_HOST_PORT="${docker_port}" "${check_script}" 2>&1)"
|
||||
docker_conflict_code=$?
|
||||
set -e
|
||||
if [[ ${docker_conflict_code} -eq 0 || "${docker_conflict_output}" != *"external-monitor"* ]]; then
|
||||
echo "FAIL: a port published by another Docker container was not rejected" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "PASS: a port published by another Docker container is rejected"
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Reject real listener conflicts and unrelated Docker mappings before startup."""
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location("local", Path(__file__).parents[1] / "scripts/local.py")
|
||||
local = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(local)
|
||||
|
||||
|
||||
class PortChecks(unittest.TestCase):
|
||||
def test_free_loopback_port(self):
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
port = sock.getsockname()[1]
|
||||
local.check_port("grafana", "127.0.0.1", port, 3000, [])
|
||||
|
||||
def test_real_listener_rejected(self):
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
sock.listen()
|
||||
with self.assertRaisesRegex(RuntimeError, "PORT CONFLICT"):
|
||||
local.check_port("grafana", "127.0.0.1", sock.getsockname()[1], 3000, [])
|
||||
|
||||
def container(self, project="observability-local", service="grafana", ip="127.0.0.1"):
|
||||
return {"Name": "/test", "Config": {"Labels": {
|
||||
"com.docker.compose.project": project,
|
||||
"com.docker.compose.service": service}},
|
||||
"NetworkSettings": {"Ports": {"3000/tcp": [{"HostIp": ip, "HostPort": "3001"}]}}}
|
||||
|
||||
def test_own_mapping_allows_update(self):
|
||||
local.check_port("grafana", "127.0.0.1", 3001, 3000, [self.container()])
|
||||
|
||||
def test_other_project_rejected(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "PORT CONFLICT"):
|
||||
local.check_port("grafana", "127.0.0.1", 3001, 3000, [self.container(project="other")])
|
||||
|
||||
def test_wildcard_mapping_rejected(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "PORT CONFLICT"):
|
||||
local.check_port("grafana", "127.0.0.1", 3001, 3000, [self.container(project="other", ip="0.0.0.0")])
|
||||
|
||||
def test_wrong_service_rejected(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "PORT CONFLICT"):
|
||||
local.check_port("tempo", "127.0.0.1", 3001, 3000, [self.container()])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,42 @@
|
||||
"""Credential validation must fail before deployment and must not echo secrets."""
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location("local", Path(__file__).parents[1] / "scripts/local.py")
|
||||
local = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(local)
|
||||
|
||||
|
||||
class MySQLConfigChecks(unittest.TestCase):
|
||||
def validate(self, content):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "my.cnf"
|
||||
path.write_text(content, encoding="utf-8")
|
||||
local.validate_mysql_config(path)
|
||||
|
||||
def test_missing_file_has_actionable_error(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
with self.assertRaisesRegex(RuntimeError, "mysql-exporter.cnf"):
|
||||
local.validate_mysql_config(Path(directory) / "mysql-exporter.cnf")
|
||||
|
||||
def test_placeholder_is_rejected(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "password"):
|
||||
self.validate("[client]\nhost=host.docker.internal\nport=3306\nuser=mysqld_exporter\npassword=REPLACE_WITH_MYSQL_MONITOR_PASSWORD\n")
|
||||
|
||||
def test_valid_config_accepts_percent_without_interpolation(self):
|
||||
self.validate("[client]\nhost=host.docker.internal\nport=3306\nuser=mysqld_exporter\npassword=test-only-50%\n")
|
||||
|
||||
def test_invalid_file_does_not_echo_its_contents(self):
|
||||
with self.assertRaises(RuntimeError) as caught:
|
||||
self.validate("sensitive-canary-invalid-config")
|
||||
self.assertNotIn("sensitive-canary", str(caught.exception))
|
||||
|
||||
def test_invalid_port_is_rejected(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "port"):
|
||||
self.validate("[client]\nhost=host.docker.internal\nport=70000\nuser=mysqld_exporter\npassword=test-only\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user