Files
magistr/scripts/check-production-secrets.sh
2026-07-13 03:28:18 +03:00

74 lines
2.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
K8S_DIR=${K8S_DIR:-"$ROOT_DIR/../k8s"}
fail() {
echo "Ошибка проверки production-секретов: $1" >&2
exit 1
}
require_pattern() {
local pattern="$1"
local file="$2"
local description="$3"
grep -Eq "$pattern" "$file" || fail "$description"
}
forbid_pattern() {
local pattern="$1"
local file="$2"
local description="$3"
if grep -Eq "$pattern" "$file"; then
fail "$description"
fi
}
[[ -d "$K8S_DIR" ]] || fail "не найдена директория Kubernetes: $K8S_DIR"
require_pattern '^app\.jwt\.secret=\$\{JWT_SECRET:\}$' \
"$ROOT_DIR/backend/src/main/resources/application.properties" \
"JWT_SECRET должен быть обязательной переменной без встроенного значения"
require_pattern '^[[:space:]]*private String secret;$' \
"$ROOT_DIR/backend/src/main/java/com/magistr/app/config/auth/JwtProperties.java" \
"JwtProperties не должен содержать встроенный секрет"
forbid_pattern '^[[:space:]]*kind:[[:space:]]*Secret[[:space:]]*$' \
"$K8S_DIR/config.yaml" \
"config.yaml не должен создавать Secret с отслеживаемыми значениями"
for yaml in "$K8S_DIR"/*.yaml; do
forbid_pattern '^[[:space:]]*stringData:[[:space:]]*$' "$yaml" \
"production-манифесты не должны содержать stringData"
done
require_pattern 'secretName:[[:space:]]*tenants-secret' "$K8S_DIR/backend.yaml" \
"tenant-конфигурация должна монтироваться из tenants-secret"
require_pattern 'name:[[:space:]]*app-secret' "$K8S_DIR/backend.yaml" \
"backend должен получать JWT_SECRET через app-secret"
require_pattern 'SPRING_PROFILES_ACTIVE:[[:space:]]*"production"' "$K8S_DIR/config.yaml" \
"Kubernetes должен явно включать production-профиль"
require_pattern 'JWT_REFRESH_COOKIE_SECURE:[[:space:]]*"true"' "$K8S_DIR/config.yaml" \
"production refresh-cookie должен быть Secure"
require_pattern 'name:[[:space:]]*otel-postgres-secret' "$K8S_DIR/otel-collector.yaml" \
"OTel Collector должен получать credentials из Secret"
if awk '
/^[[:space:]]*password:[[:space:]]*/ && $0 !~ /\$\{env:[A-Z0-9_]+\}/ { unsafe=1 }
END { exit unsafe ? 1 : 0 }
' "$K8S_DIR/otel-collector.yaml"; then
:
else
fail "пароли OTel Collector должны задаваться только через env-ссылки"
fi
if grep -REq 'createInsecureClient|trustAll|X509TrustManager' \
"$ROOT_DIR/backend/src/main/java/com/magistr/app/config/tenant"; then
fail "в Kubernetes-клиенте обнаружен небезопасный TLS trust manager"
fi
echo "Проверка production-секретов завершена успешно"