fix(deploy): generalize connector secret overrides

This commit is contained in:
2026-08-04 15:21:49 +02:00
parent b5071f1494
commit eb01979072
9 changed files with 384 additions and 91 deletions
+95 -46
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
# Render optional secret overrides with disposable sources and enforce their isolation contract.
# Render selected secret contracts through the real Compose preflight wrapper.
set -euo pipefail
root="$(cd "$(dirname "$0")/.." && pwd -P)"
fixture_root="$(mktemp -d "${TMPDIR:-/tmp}/thoth-compose-secret-policy.XXXXXX")"
fixture_root="$(mktemp -d "${TMPDIR%/}/thoth-compose-secret-policy.XXXXXX")"
trap 'rm -rf "$fixture_root"' EXIT HUP INT TERM
write_secret() {
@@ -14,19 +14,16 @@ write_secret() {
render() {
local name="$1"; shift
docker compose --env-file "$fixture_root/.env" -f "$root/compose.yaml" "$@" config --format json \
>"$fixture_root/$name.json"
"$root/scripts/compose-with-preflight.sh" --env-file "$fixture_root/operator.env" \
-f "$root/compose.yaml" "$@" config --format json >"$fixture_root/$name.json"
}
assert_render_contract() {
local name="$1" expected_targets="$2"
node - "$fixture_root/$name.json" "$name" "$expected_targets" \
"$fixture_root/ssh-private-key" "$fixture_root/ssh-known-hosts" \
"$fixture_root/https-credentials" "$fixture_root/https-ca.pem" \
"$fixture_root/dwh-password" "$fixture_root/vector-password" <<'NODE'
local name="$1" expected_bind_targets="$2" expected_secret_targets="$3"
node - "$fixture_root/$name.json" "$name" "$expected_bind_targets" "$expected_secret_targets" <<'NODE'
const fs = require("fs");
const [configPath, name, expectedTargets, ...sourcePaths] = process.argv.slice(2);
const [configPath, name, expectedBindTargets, expectedSecretTargets] = process.argv.slice(2);
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
const core = config.services?.core;
if (!core) throw new Error(`${name}: missing core service`);
@@ -34,13 +31,13 @@ if (name === "base" && (core.volumes || []).some((mount) => mount.source === "/d
throw new Error("base: /dev/null must never be used as a secret mount source");
}
const mountTargets = (core.volumes || [])
const bindTargets = (core.volumes || [])
.filter((mount) => mount.target?.startsWith("/run/secrets/"))
.map((mount) => mount.target)
.sort();
const expectedMountTargets = expectedTargets ? expectedTargets.split(",").filter(Boolean).sort() : [];
if (mountTargets.join(",") !== expectedMountTargets.join(",")) {
throw new Error(`${name}: unexpected /run/secrets bind targets: ${mountTargets.join(",")}`);
const expectedBinds = expectedBindTargets ? expectedBindTargets.split(",").filter(Boolean).sort() : [];
if (bindTargets.join(",") !== expectedBinds.join(",")) {
throw new Error(`${name}: unexpected /run/secrets bind targets: ${bindTargets.join(",")}`);
}
for (const mount of (core.volumes || []).filter((item) => item.target?.startsWith("/run/secrets/"))) {
if (mount.type !== "bind" || !mount.read_only) {
@@ -49,11 +46,9 @@ for (const mount of (core.volumes || []).filter((item) => item.target?.startsWit
}
const secretTargets = (core.secrets || []).map((secret) => secret.target).sort();
if (name === "connector" && secretTargets.join(",") !== "psd-clinical-dwh-password,psd-clinical-vector-password") {
throw new Error(`${name}: connector secret targets do not match declared THT_WS_*_FILE paths`);
}
if (name !== "connector" && secretTargets.length !== 0) {
throw new Error(`${name}: unexpected Docker secrets`);
const expectedSecrets = expectedSecretTargets ? expectedSecretTargets.split(",").filter(Boolean).sort() : [];
if (secretTargets.join(",") !== expectedSecrets.join(",")) {
throw new Error(`${name}: connector targets do not match generated THT_WS_*_FILE bindings`);
}
if (name === "ssh") {
@@ -62,41 +57,52 @@ if (name === "ssh") {
if (!command.includes(option)) throw new Error(`ssh: missing strict SSH option ${option}`);
}
}
if (name === "https") {
if (core.environment?.GIT_CONFIG_VALUE_1 !== "/run/secrets/workspace-registry-git-ca") {
throw new Error("https: HTTPS CA verification is not configured");
}
if (name === "https" && core.environment?.GIT_CONFIG_VALUE_1 !== "/run/secrets/workspace-registry-git-ca") {
throw new Error("https: HTTPS CA verification is not configured");
}
const rendered = JSON.stringify(config);
for (const secret of ["fixture-ssh-private-key", "fixture-ssh-known-hosts", "fixture-https-credentials", "fixture-https-ca", "fixture-dwh-password", "fixture-vector-password"]) {
for (const secret of ["fixture-ssh-private-key", "fixture-ssh-known-hosts", "fixture-https-credentials", "fixture-https-ca", "fixture-dwh-password", "fixture-vector-api-key"]) {
if (rendered.includes(secret)) throw new Error(`${name}: rendered Compose leaked fixture secret value`);
}
for (const sourcePath of sourcePaths) {
if (!sourcePath.startsWith("/")) throw new Error(`${name}: fixture source must be absolute`);
}
NODE
}
assert_required_source() {
local variable="$1" override="$2"
assert_missing_source_rejected() {
local variable="$1"
local generated="$fixture_root/missing-$variable.yaml"
local missing_env="$fixture_root/missing-$variable.env"
grep -v "^$variable=" "$fixture_root/.env" >"$missing_env"
if env -u "$variable" docker compose --env-file "$missing_env" -f "$root/compose.yaml" -f "$override" config --quiet \
grep -v "^$variable=" "$fixture_root/operator.env" >"$missing_env"
if env -u "$variable" "$root/scripts/generate-connector-secrets-override.sh" \
--bindings-env "$fixture_root/workspace-bindings.env" --operator-env "$missing_env" --output "$generated" \
>"$fixture_root/missing-$variable.out" 2>"$fixture_root/missing-$variable.err"; then
echo "selected override accepted missing $variable" >&2
echo "connector generator accepted missing $variable" >&2
exit 1
fi
grep -Fq "$variable" "$fixture_root/missing-$variable.err"
}
assert_unsafe_source_rejected() {
local value="$1" label="$2"
local unsafe_env="$fixture_root/$label.env"
sed "s|^THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_SOURCE=.*|THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_SOURCE=$value|" \
"$fixture_root/operator.env" >"$unsafe_env"
if env -u THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_SOURCE \
"$root/scripts/compose-with-preflight.sh" --env-file "$unsafe_env" -f "$root/compose.yaml" config --quiet \
>"$fixture_root/$label.out" 2>"$fixture_root/$label.err"; then
echo "Compose preflight accepted $label source path" >&2
exit 1
fi
grep -Fq 'unsafe source path' "$fixture_root/$label.err"
}
write_secret "$fixture_root/pi-auth.json" 'fixture-pi-auth'
write_secret "$fixture_root/ssh-private-key" 'fixture-ssh-private-key'
write_secret "$fixture_root/ssh-known-hosts" 'fixture-ssh-known-hosts'
write_secret "$fixture_root/https-credentials" 'fixture-https-credentials'
write_secret "$fixture_root/https-ca.pem" 'fixture-https-ca'
write_secret "$fixture_root/dwh-password" 'fixture-dwh-password'
write_secret "$fixture_root/vector-password" 'fixture-vector-password'
write_secret "$fixture_root/vector-api-key" 'fixture-vector-api-key'
printf '%s\n' \
'THT_WORKSPACE_GIT_REMOTE=https://git.example.invalid/platform/thoth-workspaces.git' \
@@ -105,23 +111,66 @@ printf '%s\n' \
"THT_WORKSPACE_GIT_KNOWN_HOSTS_FILE=$fixture_root/ssh-known-hosts" \
"THT_WORKSPACE_GIT_CREDENTIALS_FILE=$fixture_root/https-credentials" \
"THT_WORKSPACE_GIT_CA_FILE=$fixture_root/https-ca.pem" \
"THT_WS_PSD_CLINICAL_DWH_PASSWORD_SOURCE=$fixture_root/dwh-password" \
"THT_WS_PSD_CLINICAL_VECTOR_PASSWORD_SOURCE=$fixture_root/vector-password" >"$fixture_root/.env"
"THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_SOURCE=$fixture_root/dwh-password" \
"THT_WS_NORTH_STAR_RESEARCH_VECTOR_API_KEY_SOURCE=$fixture_root/vector-api-key" >"$fixture_root/operator.env"
printf '%s\n' \
'THT_WS_NORTH_STAR_RESEARCH_DWH_TRANSPORT=postgres_direct' \
'THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_FILE=/run/secrets/north-star-research-dwh-password' \
'THT_WS_NORTH_STAR_RESEARCH_VECTOR_TRANSPORT=rest_api' \
'THT_WS_NORTH_STAR_RESEARCH_VECTOR_API_KEY_FILE=/run/secrets/north-star-research-vector-api-key' \
>"$fixture_root/workspace-bindings.env"
connector_override="$fixture_root/compose.connector-secrets.local.yaml"
"$root/scripts/generate-connector-secrets-override.sh" \
--bindings-env "$fixture_root/workspace-bindings.env" --operator-env "$fixture_root/operator.env" \
--output "$connector_override"
render base
assert_render_contract base ''
assert_render_contract base '' ''
render ssh -f "$root/deploy/compose.git-ssh.yaml"
assert_render_contract ssh '/run/secrets/workspace-registry-git-known-hosts,/run/secrets/workspace-registry-git-ssh-key'
assert_render_contract ssh '/run/secrets/workspace-registry-git-known-hosts,/run/secrets/workspace-registry-git-ssh-key' ''
render https -f "$root/deploy/compose.git-https.yaml"
assert_render_contract https '/run/secrets/workspace-registry-git-ca,/run/secrets/workspace-registry-git-credentials'
render connector -f "$root/deploy/compose.connector-secrets.yaml"
assert_render_contract connector ''
assert_render_contract https '/run/secrets/workspace-registry-git-ca,/run/secrets/workspace-registry-git-credentials' ''
render connector -f "$connector_override"
assert_render_contract connector '' 'north-star-research-dwh-password,north-star-research-vector-api-key'
assert_required_source THT_WORKSPACE_GIT_SSH_KEY_FILE "$root/deploy/compose.git-ssh.yaml"
assert_required_source THT_WORKSPACE_GIT_KNOWN_HOSTS_FILE "$root/deploy/compose.git-ssh.yaml"
assert_required_source THT_WORKSPACE_GIT_CREDENTIALS_FILE "$root/deploy/compose.git-https.yaml"
assert_required_source THT_WORKSPACE_GIT_CA_FILE "$root/deploy/compose.git-https.yaml"
assert_required_source THT_WS_PSD_CLINICAL_DWH_PASSWORD_SOURCE "$root/deploy/compose.connector-secrets.yaml"
assert_required_source THT_WS_PSD_CLINICAL_VECTOR_PASSWORD_SOURCE "$root/deploy/compose.connector-secrets.yaml"
assert_missing_source_rejected THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_SOURCE
assert_missing_source_rejected THT_WS_NORTH_STAR_RESEARCH_VECTOR_API_KEY_SOURCE
assert_unsafe_source_rejected 'relative/secret' relative-source
assert_unsafe_source_rejected '/private/secrets/../secret' non-normalized-source
if THT_WS_NORTH_STAR_RESEARCH_DWH_PASSWORD_SOURCE='relative/host-override' \
"$root/scripts/compose-with-preflight.sh" --env-file "$fixture_root/operator.env" -f "$root/compose.yaml" config --quiet \
>"$fixture_root/host-override.out" 2>"$fixture_root/host-override.err"; then
echo "Compose preflight accepted a relative exported source path" >&2
exit 1
fi
grep -Fq 'unsafe source path' "$fixture_root/host-override.err"
if THT_WS_HOST_ONLY_PASSWORD_SOURCE='relative/host-only' \
"$root/scripts/compose-with-preflight.sh" --env-file "$fixture_root/operator.env" -f "$root/compose.yaml" config --quiet \
>"$fixture_root/host-only.out" 2>"$fixture_root/host-only.err"; then
echo "Compose preflight accepted a relative host-only source path" >&2
exit 1
fi
grep -Fq 'unsafe source path' "$fixture_root/host-only.err"
if "$root/scripts/compose-with-preflight.sh" --env-file "$fixture_root/operator.env" \
-f "$root/compose.yaml" -f "$root/deploy/compose.git-ssh.yaml" -f "$root/deploy/compose.git-https.yaml" config --quiet \
>"$fixture_root/combined.out" 2>"$fixture_root/combined.err"; then
echo "Compose preflight accepted combined SSH and HTTPS overrides" >&2
exit 1
fi
grep -Fq 'mutually exclusive' "$fixture_root/combined.err"
cp "$root/deploy/compose.git-ssh.yaml" "$fixture_root/transport-a.yaml"
cp "$root/deploy/compose.git-https.yaml" "$fixture_root/transport-b.yaml"
if "$root/scripts/compose-with-preflight.sh" --env-file "$fixture_root/operator.env" \
-f "$root/compose.yaml" -f "$fixture_root/transport-a.yaml" -f "$fixture_root/transport-b.yaml" config --quiet \
>"$fixture_root/renamed-combined.out" 2>"$fixture_root/renamed-combined.err"; then
echo "Compose preflight accepted renamed combined Git overrides" >&2
exit 1
fi
grep -Fq 'mutually exclusive' "$fixture_root/renamed-combined.err"
echo "Compose secret policy passed."