Merge pull request 'Provisioning v2 (take 2): data-only manifest + full-file env template + generic provisioner — replaces #15' (#16) from secrets-provisioning-v3 into main
ci/woodpecker/push/deploy Pipeline failed
ci/woodpecker/push/deploy Pipeline failed
Reviewed-on: #16 https://ai.bryanmail.net/c/9f4c84c7-7aa1-4b24-a27e-98be6a5e737e
This commit was merged in pull request #16.
This commit is contained in:
+39
-82
@@ -48,6 +48,17 @@ when:
|
|||||||
# already unconditionally rsynced at the top of the deploy step regardless
|
# already unconditionally rsynced at the top of the deploy step regardless
|
||||||
# of which stacks changed, so it's safe to exclude it from the stack list
|
# of which stacks changed, so it's safe to exclude it from the stack list
|
||||||
# everywhere folders are detected below.
|
# everywhere folders are detected below.
|
||||||
|
#
|
||||||
|
# 2026-09-03/07 INCIDENT + REDESIGN: hand-maintained grep -v + printf
|
||||||
|
# line-surgery cases repeatedly drifted (missing '=', duplicated keys,
|
||||||
|
# mismatched env var names rendering EMPTY secrets) and broke live
|
||||||
|
# services. Root cause: no authoritative key list and shell heredocs
|
||||||
|
# hostile to hand-editing. Stacks migrate one at a time to a data-driven
|
||||||
|
# model: secrets/secrets-map.yaml (data only) + per-stack .env.template
|
||||||
|
# (authoritative FULL file) + deploy/provision-stack.py (whole-file
|
||||||
|
# render, hard failure naming any missing value). Migrated stacks call
|
||||||
|
# the script; unmigrated stacks keep legacy case entries until their own
|
||||||
|
# PR. See the "Secrets & Deployment Architecture — Global Direction" note.
|
||||||
# ─────────────────────────────────────────────────────────────────────────────
|
# ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
@@ -163,6 +174,11 @@ steps:
|
|||||||
from_secret: entertainment_sparky_encryption_key
|
from_secret: entertainment_sparky_encryption_key
|
||||||
ENTERTAINMENT_BETTER_AUTH_SECRET:
|
ENTERTAINMENT_BETTER_AUTH_SECRET:
|
||||||
from_secret: entertainment_better_auth_secret
|
from_secret: entertainment_better_auth_secret
|
||||||
|
# ── ai stack (manifest-driven — secrets/secrets-map.yaml +
|
||||||
|
# ai/ai.env.template + deploy/provision-stack.py). Env var names
|
||||||
|
# below match the template placeholders EXACTLY; this block is the
|
||||||
|
# only per-secret touchpoint left in this file for migrated stacks
|
||||||
|
# (Woodpecker v3 requires explicit from_secret declarations). ──
|
||||||
AI_AWS_ACCESS_KEY_ID:
|
AI_AWS_ACCESS_KEY_ID:
|
||||||
from_secret: ai_aws_access_key_id
|
from_secret: ai_aws_access_key_id
|
||||||
AI_AWS_SECRET_ACCESS_KEY:
|
AI_AWS_SECRET_ACCESS_KEY:
|
||||||
@@ -171,38 +187,18 @@ steps:
|
|||||||
from_secret: ai_litellm_master_key
|
from_secret: ai_litellm_master_key
|
||||||
AI_LITELLM_SALT_KEY:
|
AI_LITELLM_SALT_KEY:
|
||||||
from_secret: ai_litellm_salt_key
|
from_secret: ai_litellm_salt_key
|
||||||
AI_LITELLM_DB_PASSWORD:
|
|
||||||
from_secret: ai_litellm_db_password
|
|
||||||
AI_LITELLM_DATABASE_MIGRATIONS:
|
|
||||||
from_secret: litellm_database_migrations
|
|
||||||
AI_LITELLM_MODIFY_PARAMS:
|
|
||||||
from_secret: litellm_modify_params
|
|
||||||
AI_OPEN_WEBUI_SECRET_KEY:
|
|
||||||
from_secret: ai_webui_secret_key
|
|
||||||
AI_OPEN_WEBUI_DATABASE_URL:
|
|
||||||
from_secret: ai_open_webui_database_url
|
|
||||||
AI_OPEN_WEB_UI_OAUTH_CLIENT_SECRET:
|
|
||||||
from_secret: ai_oauth_client_secret
|
|
||||||
AI_OPEN_WEB_UI_ENABLE_OAUTH_SIGNUP:
|
|
||||||
from_secret: ai_open_web_ui_enable_oauth_signup
|
|
||||||
AI_OPEN_WEB_UI_OAUTH_MERGE_ACCOUNTS_BY_EMAIL:
|
|
||||||
from_secret: ai_open_web_ui_oauth_merge_accounts_by_email
|
|
||||||
AI_OPEN_WEB_UI_OAUTH_PROVIDER_NAME:
|
|
||||||
from_secret: ai_open_web_ui_oauth_provider_name
|
|
||||||
AI_OPEN_WEB_UI_OPENID_PROVIDER_URL:
|
|
||||||
from_secret: ai_open_web_ui_openid_provider_url
|
|
||||||
AI_OPEN_WEB_UI_OAUTH_CLIENT_ID:
|
|
||||||
from_secret: ai_open_web_ui_oauth_client_id
|
|
||||||
AI_OPEN_WEB_UI_OAUTH_SCOPES:
|
|
||||||
from_secret: ai_open_web_ui_oauth_scopes
|
|
||||||
AI_OPEN_WEB_UI_OPENID_REDIRECT_URI:
|
|
||||||
from_secret: ai_open_web_ui_openid_redirect_uri
|
|
||||||
AI_LITELLM_DATABASE_URL:
|
AI_LITELLM_DATABASE_URL:
|
||||||
from_secret: ai_litellm_database_url
|
from_secret: ai_litellm_database_url
|
||||||
AI_LITELLM_POSTGRES_PASSWORD:
|
AI_LITELLM_POSTGRES_PASSWORD:
|
||||||
from_secret: ai_litellm_postgres_password
|
from_secret: ai_litellm_postgres_password
|
||||||
AI_AWS_REGION_NAME:
|
AI_OPEN_WEBUI_SECRET_KEY:
|
||||||
from_secret: ai_aws_region_name
|
from_secret: ai_webui_secret_key
|
||||||
|
AI_OPEN_WEBUI_DATABASE_URL:
|
||||||
|
from_secret: ai_open_webui_database_url
|
||||||
|
AI_OPEN_WEBUI_OAUTH_CLIENT_SECRET:
|
||||||
|
from_secret: ai_oauth_client_secret
|
||||||
|
AI_MCPO_API_KEY:
|
||||||
|
from_secret: ai_mcpo_api_key
|
||||||
FLOWAGENT_AZURE_CLIENT_ID:
|
FLOWAGENT_AZURE_CLIENT_ID:
|
||||||
from_secret: flowagent_azure_client_id
|
from_secret: flowagent_azure_client_id
|
||||||
FLOWAGENT_AZURE_TENANT_ID:
|
FLOWAGENT_AZURE_TENANT_ID:
|
||||||
@@ -210,7 +206,7 @@ steps:
|
|||||||
FLOWAGENT_AZURE_CLIENT_SECRET:
|
FLOWAGENT_AZURE_CLIENT_SECRET:
|
||||||
from_secret: flowagent_azure_client_secret
|
from_secret: flowagent_azure_client_secret
|
||||||
commands:
|
commands:
|
||||||
- apk add --no-cache openssh-client
|
- apk add --no-cache openssh-client python3 py3-yaml
|
||||||
- mkdir -p ~/.ssh
|
- mkdir -p ~/.ssh
|
||||||
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
|
- echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa
|
||||||
- chmod 600 ~/.ssh/id_rsa
|
- chmod 600 ~/.ssh/id_rsa
|
||||||
@@ -257,9 +253,9 @@ steps:
|
|||||||
# dependency on a running Swarm's Docker secret store. Native Docker
|
# dependency on a running Swarm's Docker secret store. Native Docker
|
||||||
# secrets (Pattern C) can't satisfy that: they only exist inside an
|
# secrets (Pattern C) can't satisfy that: they only exist inside an
|
||||||
# already-running Swarm, which is exactly the circular dependency
|
# already-running Swarm, which is exactly the circular dependency
|
||||||
# this stack can't have. Mirrors the ai) case's grep -v + printf
|
# this stack can't have. Mirrors the retired ai) case's grep -v +
|
||||||
# rewrite-in-place approach, never sed (values may contain slash,
|
# printf rewrite-in-place approach, never sed (values may contain
|
||||||
# dollar sign, ampersand).
|
# slash, dollar sign, ampersand).
|
||||||
#
|
#
|
||||||
# TEST PHASE: target is git.env.pipelinetest, NOT the real git.env.
|
# TEST PHASE: target is git.env.pipelinetest, NOT the real git.env.
|
||||||
# The real file is never opened for writing by this step. First run
|
# The real file is never opened for writing by this step. First run
|
||||||
@@ -270,6 +266,8 @@ steps:
|
|||||||
# documented GITEA_MCP_ACCESS_TOKEN key. Cutover to the real file —
|
# documented GITEA_MCP_ACCESS_TOKEN key. Cutover to the real file —
|
||||||
# and pointing git.yaml/stack-deploy at it — is a deliberate,
|
# and pointing git.yaml/stack-deploy at it — is a deliberate,
|
||||||
# separate follow-up after manually diffing this render.
|
# separate follow-up after manually diffing this render.
|
||||||
|
# (Candidate for the secrets-map.yaml/provision-stack.py migration
|
||||||
|
# in its own PR; kept legacy for now.)
|
||||||
ssh -o StrictHostKeyChecking=no root@$${SWARM_MANAGER_IP} "FILE=/volume1/docker/compose-files/git.env.pipelinetest
|
ssh -o StrictHostKeyChecking=no root@$${SWARM_MANAGER_IP} "FILE=/volume1/docker/compose-files/git.env.pipelinetest
|
||||||
TMP=\$FILE.tmp.\$\$
|
TMP=\$FILE.tmp.\$\$
|
||||||
[ -f \$FILE ] || cp /volume1/docker/compose-files/git.env \$FILE
|
[ -f \$FILE ] || cp /volume1/docker/compose-files/git.env \$FILE
|
||||||
@@ -317,56 +315,15 @@ steps:
|
|||||||
create_or_update_secret 'vaultwarden_admin_token' '$${VAULTWARDEN_ADMIN_TOKEN}'
|
create_or_update_secret 'vaultwarden_admin_token' '$${VAULTWARDEN_ADMIN_TOKEN}'
|
||||||
create_or_update_secret 'vaultwarden_database_url_v2' '$${VAULTWARDEN_DATABASE_URL}'";;
|
create_or_update_secret 'vaultwarden_database_url_v2' '$${VAULTWARDEN_DATABASE_URL}'";;
|
||||||
ai)
|
ai)
|
||||||
# NOTE: ai stack uses Pattern B (host .env, not native Docker secrets) —
|
# MIGRATED (2026-09-07) to manifest-driven provisioning after the
|
||||||
# LiteLLM/boto3 and Open WebUI don't support the _FILE convention for these
|
# line-surgery approach repeatedly drifted (dropped keys 2026-09-03;
|
||||||
# vars. Instead of Docker secrets, we regenerate ONLY the migrated lines in
|
# duplicate AI_LITELLM_POSTGRES_PASSWORD + stray legacy keys +
|
||||||
# the remote ai/ai.env in place via grep -v + printf (never sed, since values
|
# WEB_UI/WEBUI env-name mismatch rendering an EMPTY OAuth client
|
||||||
# may contain slash, dollar sign, ampersand). All other lines — including
|
# secret, found 2026-09-07). All logic lives in
|
||||||
# MCPO_API_KEY, OAUTH_CLIENT_ID, WEBUI_URL, and other non-secret config — are
|
# deploy/provision-stack.py; the authoritative key list lives in
|
||||||
# left completely untouched. MCPO_API_KEY migration is deferred to a
|
# ai/ai.env.template; the mapping lives in secrets/secrets-map.yaml.
|
||||||
# follow-up; this step never reads or writes it.
|
# This case is intentionally one line.
|
||||||
#
|
python3 deploy/provision-stack.py ai;;
|
||||||
# IMPORTANT: ai.yaml's litellm service references ${AI_AWS_ACCESS_KEY_ID} /
|
|
||||||
# ${AI_AWS_SECRET_ACCESS_KEY} (AI_-prefixed) and renders them into the
|
|
||||||
# container as plain AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY (see commit
|
|
||||||
# 37ed671a, "Change AWS keys to use Woodpecker Secrets"). So ai.env must be
|
|
||||||
# written with the AI_-prefixed key names, NOT the plain ones — writing
|
|
||||||
# plain AWS_ACCESS_KEY_ID here would leave ${AI_AWS_ACCESS_KEY_ID}
|
|
||||||
# unresolved at compose-render time (empty), silently breaking Bedrock auth.
|
|
||||||
# All other migrated vars in ai.yaml use plain (unprefixed) names, so only
|
|
||||||
# these two lines need the AI_ prefix.
|
|
||||||
#
|
|
||||||
# FLOWAGENT NOTE (added alongside the ai.yaml mcpo image/secrets cutover):
|
|
||||||
# the 3 flowagent_azure_* values are provisioned as native Docker secrets
|
|
||||||
# below (Pattern C, matches every other _FILE-convention stack), NOT written
|
|
||||||
# into ai/ai.env — mcpo's flowagent entry reads them via
|
|
||||||
# /run/secrets/flowagent_azure_* (see mcp-config/flowagent/entrypoint.sh),
|
|
||||||
# not via env var, so they don't belong in this stack's Pattern B .env block.
|
|
||||||
ssh -o StrictHostKeyChecking=no root@$${SWARM_MANAGER_IP} "FILE=/volume1/docker/compose-files/ai/ai.env
|
|
||||||
TMP=\$FILE.tmp.\$\$
|
|
||||||
grep -vE '^(AI_AWS_ACCESS_KEY_ID|AI_AWS_SECRET_ACCESS_KEY|AI_LITELLM_MASTER_KEY|AI_LITELLM_SALT_KEY|AI_LITELLM_POSTGRES_PASSWORD|AI_LITELLM_DATABASE_URL|AI_OPEN_WEBUI_SECRET_KEY|AI_OPEN_WEBUI_DATABASE_URL|AI_OPEN_WEB_UI_OAUTH_CLIENT_SECRET)=' \$FILE > \$TMP 2>/dev/null || touch \$TMP
|
|
||||||
{ cat \$TMP
|
|
||||||
printf 'AI_AWS_ACCESS_KEY_ID=%s\n' '$${AI_AWS_ACCESS_KEY_ID}'
|
|
||||||
printf 'AI_AWS_SECRET_ACCESS_KEY=%s\n' '$${AI_AWS_SECRET_ACCESS_KEY}'
|
|
||||||
printf 'AI_AWS_REGION_NAME=%s\n' '$${AI_AWS_REGION_NAME}'
|
|
||||||
printf 'AI_LITELLM_MASTER_KEY=%s\n' '$${AI_LITELLM_MASTER_KEY}'
|
|
||||||
printf 'AI_LITELLM_SALT_KEY=%s\n' '$${AI_LITELLM_SALT_KEY}'
|
|
||||||
printf 'AI_LITELLM_POSTGRES_PASSWORD=%s\n' '$${AI_LITELLM_DB_PASSWORD}'
|
|
||||||
printf 'AI_LITELLM_DATABASE_URL=%s\n' '$${AI_LITELLM_DATABASE_URL}'
|
|
||||||
printf 'AI_LITELLM_POSTGRES_PASSWORD=%s\n' '$${AI_LITELLM_POSTGRES_PASSWORD}'
|
|
||||||
printf 'AI_LITELLM_MODIFY_PARAMS=%s\n' '$${AI_LITELLM_MODIFY_PARAMS}'
|
|
||||||
printf 'AI_LITELLM_DATABASE_MIGRATIONS=%s\n' '$${AI_LITELLM_DATABASE_MIGRATIONS}'
|
|
||||||
printf 'AI_OPEN_WEBUI_SECRET_KEY=%s\n' '$${AI_OPEN_WEBUI_SECRET_KEY}'
|
|
||||||
printf 'AI_OPEN_WEBUI_DATABASE_URL=%s\n' '$${AI_OPEN_WEBUI_DATABASE_URL}'
|
|
||||||
printf 'AI_WEBUI_SECRET_KEY=%s\n' '$${AI_OPEN_WEBUI_SECRET_KEY}'
|
|
||||||
printf 'AI_OPEN_WEBUI_OAUTH_CLIENT_SECRET=%s\n' '$${AI_OPEN_WEBUI_OAUTH_CLIENT_SECRET}'
|
|
||||||
} > \$FILE
|
|
||||||
rm -f \$TMP
|
|
||||||
echo ' [OK] ai/ai.env secrets updated'
|
|
||||||
source /tmp/cs.sh
|
|
||||||
create_or_update_secret 'flowagent_azure_client_id' '$${FLOWAGENT_AZURE_CLIENT_ID}'
|
|
||||||
create_or_update_secret 'flowagent_azure_tenant_id' '$${FLOWAGENT_AZURE_TENANT_ID}'
|
|
||||||
create_or_update_secret 'flowagent_azure_client_secret' '$${FLOWAGENT_AZURE_CLIENT_SECRET}'";;
|
|
||||||
entertainment)
|
entertainment)
|
||||||
ssh -o StrictHostKeyChecking=no root@$${SWARM_MANAGER_IP} "source /tmp/cs.sh
|
ssh -o StrictHostKeyChecking=no root@$${SWARM_MANAGER_IP} "source /tmp/cs.sh
|
||||||
create_or_update_secret 'entertainment_discord_token' '$${ENTERTAINMENT_DISCORD_TOKEN}'
|
create_or_update_secret 'entertainment_discord_token' '$${ENTERTAINMENT_DISCORD_TOKEN}'
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
# ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
# ai.env.template — AUTHORITATIVE template for ai/ai.env (rendered by
|
||||||
|
# deploy/provision-stack.py per secrets/secrets-map.yaml).
|
||||||
|
#
|
||||||
|
# - This file IS the complete key list for ai.env. The whole file is
|
||||||
|
# rendered on every provisioning run — no line surgery, so a key can
|
||||||
|
# never silently go missing again (root cause of the 2026-09-03 outage).
|
||||||
|
# - Key names match EXACTLY what ai/ai.yaml references (AI_<SERVICE>_*
|
||||||
|
# naming adopted on main 2026-09-06).
|
||||||
|
# - Non-secret config lives here as LITERAL values (visible, reviewable).
|
||||||
|
# - Secret values are dollar-brace placeholders resolved from the CI env
|
||||||
|
# (Woodpecker from_secret vars) at provisioning time. provision-stack.py
|
||||||
|
# FAILS HARD if any placeholder is missing/empty.
|
||||||
|
# - The rendered ai/ai.env exists only on the host (gitignored).
|
||||||
|
# - Rendered by provision-stack.py, NOT Woodpecker's yaml preprocessor —
|
||||||
|
# single-dollar placeholders are safe here (deploy.yml's double-dollar
|
||||||
|
# rule does NOT apply to this file).
|
||||||
|
#
|
||||||
|
# Consumed by ai/ai.yaml. DOMAIN_NAME comes from deploy/global.env, not here.
|
||||||
|
# ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
# ── LiteLLM (non-secret config) ──────────────────────────────────────────────
|
||||||
|
AI_AWS_REGION_NAME=us-east-2
|
||||||
|
AI_LITELLM_MODIFY_PARAMS=False
|
||||||
|
AI_LITELLM_DATABASE_MIGRATIONS=True
|
||||||
|
|
||||||
|
# ── LiteLLM (secrets) ────────────────────────────────────────────────────────
|
||||||
|
AI_AWS_ACCESS_KEY_ID=${AI_AWS_ACCESS_KEY_ID}
|
||||||
|
AI_AWS_SECRET_ACCESS_KEY=${AI_AWS_SECRET_ACCESS_KEY}
|
||||||
|
AI_LITELLM_MASTER_KEY=${AI_LITELLM_MASTER_KEY}
|
||||||
|
AI_LITELLM_SALT_KEY=${AI_LITELLM_SALT_KEY}
|
||||||
|
# Full connection URL is itself a secret (ai_litellm_database_url) — the
|
||||||
|
# URL structure never appears in git.
|
||||||
|
AI_LITELLM_DATABASE_URL=${AI_LITELLM_DATABASE_URL}
|
||||||
|
AI_LITELLM_POSTGRES_PASSWORD=${AI_LITELLM_POSTGRES_PASSWORD}
|
||||||
|
|
||||||
|
# ── Open WebUI (non-secret config) ───────────────────────────────────────────
|
||||||
|
AI_OPEN_WEBUI_URL=https://ai.bryanmail.net
|
||||||
|
AI_OPEN_WEBUI_ENABLE_OAUTH_SIGNUP=true
|
||||||
|
AI_OPEN_WEBUI_OAUTH_MERGE_ACCOUNTS_BY_EMAIL=true
|
||||||
|
AI_OPEN_WEBUI_OAUTH_PROVIDER_NAME=Authentik
|
||||||
|
AI_OPEN_WEBUI_OPENID_PROVIDER_URL=https://auth.bryanmail.net/application/o/open-web-ui/.well-known/openid-configuration
|
||||||
|
# OAuth client ID is a public identifier by OAuth2 design (it is sent to the
|
||||||
|
# browser); the client SECRET below is the protected credential.
|
||||||
|
AI_OPEN_WEBUI_OAUTH_CLIENT_ID=hVmhi1dS3TnG2cUw5QwLOx5FDLSWtnQUdZyeB5zK
|
||||||
|
AI_OPEN_WEBUI_OAUTH_SCOPES=openid email profile
|
||||||
|
AI_OPEN_WEBUI_OPENID_REDIRECT_URI=https://ai.bryanmail.net/oauth/oidc/callback
|
||||||
|
|
||||||
|
# ── Open WebUI (secrets) ─────────────────────────────────────────────────────
|
||||||
|
AI_OPEN_WEBUI_SECRET_KEY=${AI_OPEN_WEBUI_SECRET_KEY}
|
||||||
|
AI_OPEN_WEBUI_DATABASE_URL=${AI_OPEN_WEBUI_DATABASE_URL}
|
||||||
|
AI_OPEN_WEBUI_OAUTH_CLIENT_SECRET=${AI_OPEN_WEBUI_OAUTH_CLIENT_SECRET}
|
||||||
|
|
||||||
|
# ── mcpo / mcpo-critical (secrets) ───────────────────────────────────────────
|
||||||
|
MCPO_API_KEY=${AI_MCPO_API_KEY}
|
||||||
@@ -0,0 +1,198 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""provision-stack.py — manifest-driven env-file rendering + Docker secret
|
||||||
|
provisioning for one stack.
|
||||||
|
|
||||||
|
Usage (from the CI workspace root, inside the provision-secrets step):
|
||||||
|
python3 deploy/provision-stack.py <stack>
|
||||||
|
|
||||||
|
Reads secrets/secrets-map.yaml (data only — no code, no values) and, for the
|
||||||
|
named stack:
|
||||||
|
|
||||||
|
1. env_template -> renders the COMPLETE env file. Placeholders of the form
|
||||||
|
dollar-brace VARNAME are resolved from this process's environment (the
|
||||||
|
Woodpecker from_secret-backed vars). The whole file is rendered every
|
||||||
|
run; nothing is line-edited in place, so keys can never silently go
|
||||||
|
missing (root cause of the 2026-09-03 ai.env incident).
|
||||||
|
2. env_dest -> ships the rendered file to the Swarm manager over ssh stdin
|
||||||
|
(write-to-temp + atomic mv, mode 600). The rendered file never touches
|
||||||
|
the CI workspace disk under the repo (no chance of being committed) and
|
||||||
|
never appears on a command line.
|
||||||
|
3. docker_secrets -> for each swarm-secret-name -> ENV_VAR mapping, creates
|
||||||
|
or rotates the Docker secret. Values are passed via ssh stdin only.
|
||||||
|
Rotation uses the same sha256-checksum-label convention as
|
||||||
|
deploy/create-secrets.sh (unchanged secrets are skipped silently).
|
||||||
|
|
||||||
|
Safety properties:
|
||||||
|
- FAILS HARD (non-zero) if any referenced env var is missing or empty, and
|
||||||
|
lists the missing NAMES. A partial/broken render can never ship.
|
||||||
|
- FAILS HARD if any unresolved placeholder remains after rendering.
|
||||||
|
- NEVER prints a secret value — names and counts only.
|
||||||
|
- Requires SWARM_MANAGER_IP in the environment and a usable ssh identity
|
||||||
|
(both already set up by the provision-secrets step).
|
||||||
|
|
||||||
|
Stacks not present in the manifest exit 0 with a notice, so this script is
|
||||||
|
safe to call unconditionally; legacy case-entries in deploy.yml keep handling
|
||||||
|
unmigrated stacks.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
MANIFEST_PATH = os.path.join(REPO_ROOT, "secrets", "secrets-map.yaml")
|
||||||
|
REMOTE_BASE = "/volume1/docker/compose-files"
|
||||||
|
PLACEHOLDER_RE = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
|
||||||
|
|
||||||
|
SSH_OPTS = ["-o", "StrictHostKeyChecking=no"]
|
||||||
|
|
||||||
|
|
||||||
|
def die(msg: str) -> None:
|
||||||
|
print(f"ERROR: {msg}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def load_manifest() -> dict:
|
||||||
|
try:
|
||||||
|
import yaml # py3-yaml, installed by the provision-secrets step
|
||||||
|
except ImportError:
|
||||||
|
die("PyYAML not available — provision-secrets step must apk add py3-yaml")
|
||||||
|
if not os.path.isfile(MANIFEST_PATH):
|
||||||
|
die(f"manifest not found: {MANIFEST_PATH}")
|
||||||
|
with open(MANIFEST_PATH, "r", encoding="utf-8") as fh:
|
||||||
|
data = yaml.safe_load(fh) or {}
|
||||||
|
stacks = data.get("stacks")
|
||||||
|
if not isinstance(stacks, dict):
|
||||||
|
die("manifest has no 'stacks:' mapping")
|
||||||
|
return stacks
|
||||||
|
|
||||||
|
|
||||||
|
def ssh_target() -> str:
|
||||||
|
ip = os.environ.get("SWARM_MANAGER_IP", "").strip()
|
||||||
|
if not ip:
|
||||||
|
die("SWARM_MANAGER_IP is empty — check Woodpecker repo secrets")
|
||||||
|
return f"root@{ip}"
|
||||||
|
|
||||||
|
|
||||||
|
def ssh_run(target: str, remote_cmd: str, stdin_data: bytes | None = None,
|
||||||
|
check: bool = True) -> subprocess.CompletedProcess:
|
||||||
|
proc = subprocess.run(
|
||||||
|
["ssh", *SSH_OPTS, target, remote_cmd],
|
||||||
|
input=stdin_data, capture_output=True,
|
||||||
|
)
|
||||||
|
if check and proc.returncode != 0:
|
||||||
|
# stderr may be verbose but must never contain our secret values —
|
||||||
|
# we only ever send values via stdin, never embed them in remote_cmd.
|
||||||
|
die(f"remote command failed (rc={proc.returncode}): {remote_cmd}\n"
|
||||||
|
f"{proc.stderr.decode(errors='replace').strip()}")
|
||||||
|
return proc
|
||||||
|
|
||||||
|
|
||||||
|
def render_template(template_path: str) -> str:
|
||||||
|
if not os.path.isfile(template_path):
|
||||||
|
die(f"env_template not found: {template_path}")
|
||||||
|
with open(template_path, "r", encoding="utf-8") as fh:
|
||||||
|
raw = fh.read()
|
||||||
|
|
||||||
|
referenced = sorted(set(PLACEHOLDER_RE.findall(raw)))
|
||||||
|
missing = [v for v in referenced
|
||||||
|
if not os.environ.get(v, "").strip()]
|
||||||
|
if missing:
|
||||||
|
die("template references vars that are MISSING or EMPTY in the CI "
|
||||||
|
"environment (add them via from_secret in "
|
||||||
|
".woodpecker/deploy.yml provision-secrets, and as Woodpecker "
|
||||||
|
"secrets):\n " + "\n ".join(missing))
|
||||||
|
|
||||||
|
rendered = PLACEHOLDER_RE.sub(lambda m: os.environ[m.group(1)], raw)
|
||||||
|
|
||||||
|
# Belt-and-braces: nothing placeholder-shaped may survive the render.
|
||||||
|
leftover = sorted(set(PLACEHOLDER_RE.findall(rendered)))
|
||||||
|
if leftover:
|
||||||
|
die("unresolved placeholders remain after rendering: "
|
||||||
|
+ ", ".join(leftover))
|
||||||
|
|
||||||
|
print(f" [render] {template_path}: {len(referenced)} secret placeholder(s) "
|
||||||
|
f"resolved: {', '.join(referenced)}")
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def ship_env_file(target: str, rendered: str, dest_rel: str) -> None:
|
||||||
|
dest = f"{REMOTE_BASE}/{dest_rel}"
|
||||||
|
tmp = f"{dest}.provision-tmp"
|
||||||
|
# Value travels over ssh stdin; never on a command line; atomic mv.
|
||||||
|
ssh_run(target,
|
||||||
|
f"umask 077 && cat > {tmp} && chmod 600 {tmp} && mv {tmp} {dest}",
|
||||||
|
stdin_data=rendered.encode())
|
||||||
|
keys = [ln.split("=", 1)[0] for ln in rendered.splitlines()
|
||||||
|
if "=" in ln and not ln.lstrip().startswith("#") and ln.strip()]
|
||||||
|
print(f" [env] shipped {dest} ({len(keys)} keys): {', '.join(keys)}")
|
||||||
|
|
||||||
|
|
||||||
|
def provision_docker_secret(target: str, name: str, env_var: str) -> None:
|
||||||
|
value = os.environ.get(env_var, "")
|
||||||
|
if not value.strip():
|
||||||
|
die(f"docker secret '{name}': env var {env_var} is missing/empty")
|
||||||
|
|
||||||
|
new_hash = hashlib.sha256(value.encode()).hexdigest()
|
||||||
|
probe = ssh_run(
|
||||||
|
target,
|
||||||
|
f"docker secret inspect {name} "
|
||||||
|
"--format '{{index .Spec.Labels \"checksum\"}}' 2>/dev/null || true",
|
||||||
|
check=True)
|
||||||
|
old_hash = probe.stdout.decode().strip()
|
||||||
|
|
||||||
|
if old_hash == new_hash:
|
||||||
|
print(f" [skip] docker secret {name} (unchanged)")
|
||||||
|
return
|
||||||
|
|
||||||
|
if old_hash:
|
||||||
|
rm = ssh_run(target, f"docker secret rm {name}", check=False)
|
||||||
|
if rm.returncode != 0:
|
||||||
|
die(f"docker secret {name}: value changed but removal failed — "
|
||||||
|
"it is probably referenced by a running service. Provision "
|
||||||
|
"under a versioned name (see vaultwarden_database_url_v2 "
|
||||||
|
"precedent) or scale the service down first.")
|
||||||
|
action = "update"
|
||||||
|
else:
|
||||||
|
action = "create"
|
||||||
|
|
||||||
|
ssh_run(target,
|
||||||
|
f"docker secret create --label checksum={new_hash} "
|
||||||
|
f"--label managed-by=woodpecker {name} -",
|
||||||
|
stdin_data=value.encode())
|
||||||
|
print(f" [{action}] docker secret {name} (value via stdin)")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
die("usage: provision-stack.py <stack>")
|
||||||
|
stack = sys.argv[1]
|
||||||
|
|
||||||
|
stacks = load_manifest()
|
||||||
|
cfg = stacks.get(stack)
|
||||||
|
if cfg is None:
|
||||||
|
print(f" [info] stack '{stack}' not in secrets-map.yaml — "
|
||||||
|
"legacy provisioning (deploy.yml case-entry) applies. Nothing to do.")
|
||||||
|
return
|
||||||
|
|
||||||
|
target = ssh_target()
|
||||||
|
print(f"==> provision-stack: {stack}")
|
||||||
|
|
||||||
|
template_rel = cfg.get("env_template")
|
||||||
|
dest_rel = cfg.get("env_dest")
|
||||||
|
if template_rel and not dest_rel:
|
||||||
|
die("env_template set but env_dest missing in manifest")
|
||||||
|
if template_rel:
|
||||||
|
rendered = render_template(os.path.join(REPO_ROOT, template_rel))
|
||||||
|
ship_env_file(target, rendered, dest_rel)
|
||||||
|
|
||||||
|
for name, env_var in (cfg.get("docker_secrets") or {}).items():
|
||||||
|
provision_docker_secret(target, name, env_var)
|
||||||
|
|
||||||
|
print(f"==> provision-stack: {stack} done")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
# secrets-map.yaml — DATA-ONLY manifest for deploy/provision-stack.py
|
||||||
|
#
|
||||||
|
# RULES:
|
||||||
|
# - This file contains NO code, NO shell, NO secret values — only names.
|
||||||
|
# - Each stack entry declares:
|
||||||
|
# env_template: repo path of the FULL env-file template (tracked).
|
||||||
|
# The template is authoritative: the COMPLETE env file is
|
||||||
|
# rendered from it on every provisioning run. Nothing is
|
||||||
|
# line-edited in place, so keys can never silently go
|
||||||
|
# missing.
|
||||||
|
# env_dest: host path (relative to /volume1/docker/compose-files/)
|
||||||
|
# the rendered env file is shipped to. Rendered file
|
||||||
|
# exists ONLY on the host — never committed to git.
|
||||||
|
# docker_secrets: map of docker-swarm-secret-name -> CI ENV VAR NAME
|
||||||
|
# (Pattern C). The env var must be declared via
|
||||||
|
# from_secret: in .woodpecker/deploy.yml's
|
||||||
|
# provision-secrets step (Woodpecker v3 requires explicit
|
||||||
|
# per-secret declaration; there is no expose-all).
|
||||||
|
#
|
||||||
|
# ADDING A NEW SECRET (3 small steps, no shell edits):
|
||||||
|
# 1. Add the secret value in Woodpecker UI (repo Settings -> Secrets).
|
||||||
|
# 2. Declare it in .woodpecker/deploy.yml provision-secrets environment:
|
||||||
|
# block (from_secret) — mechanical two-line addition.
|
||||||
|
# 3. Reference it here (docker_secrets:) and/or in the stack's
|
||||||
|
# .env.template as a dollar-brace placeholder.
|
||||||
|
#
|
||||||
|
# Stacks not listed here fall through to deploy.yml's legacy case-entries
|
||||||
|
# untouched. Migration is deliberately one stack per PR.
|
||||||
|
# ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
stacks:
|
||||||
|
ai:
|
||||||
|
env_template: ai/ai.env.template
|
||||||
|
env_dest: ai/ai.env
|
||||||
|
docker_secrets:
|
||||||
|
flowagent_azure_client_id: FLOWAGENT_AZURE_CLIENT_ID
|
||||||
|
flowagent_azure_tenant_id: FLOWAGENT_AZURE_TENANT_ID
|
||||||
|
flowagent_azure_client_secret: FLOWAGENT_AZURE_CLIENT_SECRET
|
||||||
Reference in New Issue
Block a user