Compare commits

...

2 Commits

Author SHA1 Message Date
juvenal.diaz c25a8a84e0 feat: add Pimox out-of-band helpers
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details
2026-10-22 20:22:24 -06:00
juvenal.diaz 2629a26068 feat: add service-specific secret rotation 2026-10-22 18:34:51 -06:00
6 changed files with 174 additions and 0 deletions

View File

@ -62,7 +62,9 @@ var allowedCommands = map[string]bool{
"preflight": true, "doctor-preapply": true, "inventory-check": true,
"state-backup": true, "fix-debian-docker-root": true,
"state": true,
"pimox": true,
"secrets-init": true, "secrets-check": true, "tailnet-policy-check": true,
"secrets": true,
"ai-index": true, "ai-check": true, "ask": true, "ai-evals": true,
"ai-scheduler": true, "ai-memory": true, "model-observe": true,
"impact": true, "review-last-change": true,

View File

@ -129,3 +129,15 @@ func TestStateCommandIsRemotelyEnabled(t *testing.T) {
t.Fatal("state must be exposed through the remote API")
}
}
func TestSecretsCommandIsRemotelyEnabled(t *testing.T) {
if !allowedCommands["secrets"] {
t.Fatal("secrets must be exposed through the remote API")
}
}
func TestPimoxCommandIsRemotelyEnabled(t *testing.T) {
if !allowedCommands["pimox"] {
t.Fatal("pimox must be exposed through the remote API")
}
}

12
jeannie
View File

@ -141,6 +141,7 @@ Recovery And State
drill-pihole-restore Run the Pi-hole restore drill.
state-backup Back up local OpenTofu state files.
state status [STACK] [--json] Inspect OpenTofu locks and drift without changing state.
pimox {status [VMID]|console VMID} Inspect Pimox VMs or open a serial emergency console.
Focused Doctors
doctor-edge [--all|--details|--verbose|--json]
@ -160,6 +161,7 @@ Access, Secrets, And Policy
kubeconfig-readonly Create/check read-only kubeconfig material.
secrets-init Initialize SOPS/age secret tooling.
secrets-check Validate repo-managed secret prerequisites.
secrets rotate {status|plan|SERVICE} Report rotation age or rotate a supported service.
tailnet-policy-check Validate Tailscale ACL policy as code.
fix-debian-docker-root Repair Debian Docker root placement.
@ -515,6 +517,9 @@ case "${1:-}" in
fi
state_status "${@:3}"
;;
pimox)
pimox_oob "${@:2}"
;;
fix-debian-docker-root)
fix_debian_docker_root
;;
@ -524,6 +529,13 @@ case "${1:-}" in
secrets-check)
secrets_check
;;
secrets)
if [[ "${2:-}" != "rotate" ]]; then
echo "Usage: ./jeannie secrets rotate {status|plan|SERVICE [--yes]}" >&2
exit 2
fi
secrets_rotate "${@:3}"
;;
tailnet-policy-check)
tailnet_policy_check
;;

View File

@ -3281,6 +3281,11 @@ state_status() {
"${REPO_ROOT}/scripts/state-status" "$@"
}
pimox_oob() {
require_debian_server "pimox"
"${REPO_ROOT}/scripts/pimox-oob" "$@"
}
storage_status() {
require_debian_server "storage"
local kubeconfig="${KUBECONFIG_PATH}"

View File

@ -363,6 +363,96 @@ dex_secret() {
esac
}
secrets_rotate() {
local service="${1:-status}"
local confirm=false
local max_age_days="${LAB_SECRET_ROTATION_MAX_AGE_DAYS:-30}"
local secret_file
local backup_dir
local backup_file
local grafana_secret
local gitea_secret
local office_secret
if [[ "${service}" == "status" ]]; then
printf '%s\n' "== Secret rotation status (due after ${max_age_days} days) =="
while IFS= read -r secret_file; do
[[ -n "${secret_file}" ]] || continue
local modified age_days
modified="$(stat -c %Y "${REPO_ROOT}/${secret_file}" 2>/dev/null || stat -f %m "${REPO_ROOT}/${secret_file}")"
age_days=$(( ( $(date +%s) - modified ) / 86400 ))
printf '%-48s age=%sd %s\n' "${secret_file}" "${age_days}" "$([[ ${age_days} -ge ${max_age_days} ]] && echo DUE || echo current)"
done < <(git -C "${REPO_ROOT}" ls-files '*.secret.yaml' '*.secret.yml' '*.secret.json' '*.enc.yaml' '*.enc.yml' '*.enc.json')
return 0
fi
if [[ "${service}" == "plan" ]]; then
service="${2:-all}"
elif [[ "${2:-}" == "--yes" ]]; then
confirm=true
fi
if [[ "${3:-}" == "--yes" ]]; then
confirm=true
fi
case "${service}" in
dex)
cat <<'EOF'
Dex rotation plan:
- generate new Grafana, Gitea, and Office OIDC client secrets
- update the encrypted apps/dex/dex.secret.yaml file
- redeploy Dex and update each dependent client
- validate the affected services before committing the encrypted file
EOF
[[ "${confirm}" == true ]] || return 0
require_debian_server "secrets rotate dex"
ensure_sops_age_tools
secret_file="$(dex_secret_file)"
[[ -s "${secret_file}" ]] || { echo "Missing Dex secret file: ${secret_file}" >&2; return 1; }
backup_dir="${HOMELAB_STATE_DIR:-${HOME}/.local/share/homelab}/secret-rotation-backups"
mkdir -p "${backup_dir}"
backup_file="${backup_dir}/dex-secret-$(date +%Y%m%d-%H%M%S).yaml"
cp -p "${secret_file}" "${backup_file}"
chmod 600 "${backup_file}"
grafana_secret="$(generate_dex_secret_value)"
gitea_secret="$(generate_dex_secret_value)"
office_secret="$(generate_dex_secret_value)"
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops set "${secret_file}" '["stringData"]["DEX_GRAFANA_CLIENT_SECRET"]' "\"${grafana_secret}\""
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops set "${secret_file}" '["stringData"]["DEX_GITEA_CLIENT_SECRET"]' "\"${gitea_secret}\""
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops set "${secret_file}" '["stringData"]["DEX_OFFICE_CLIENT_SECRET"]' "\"${office_secret}\""
deploy_dex
configure_grafana_dex_secret
configure_gitea_sso_source
if sudo docker inspect "${OFFICE_NEXTCLOUD_CONTAINER_NAME:-homelab-office-nextcloud}" >/dev/null 2>&1; then
deploy_office
fi
echo "Dex client secrets rotated and dependent services updated. Backup: ${backup_file}"
;;
gitea-runner)
cat <<'EOF'
Gitea runner rotation requires a new registration token issued by Gitea.
Use the Gitea runner registration flow, then run:
./jeannie gitea-runner-secret migrate
./jeannie gitea-runner-secret apply
The token is not generated locally because random replacement would invalidate the runner.
EOF
return 0
;;
office)
echo "Office OIDC rotation is coordinated by 'secrets rotate dex'; run that service so Dex and Nextcloud receive the same client secret."
return 0
;;
all)
echo "Use 'secrets rotate status' and rotate one supported service at a time: dex, gitea-runner, or office."
return 0
;;
*)
echo "Usage: ./jeannie secrets rotate {status|plan SERVICE|SERVICE [--yes]}" >&2
return 2
;;
esac
}
gitea_runner_plain_secret_file() {
printf '%s\n' "${HOMELAB_GITEA_RUNNER_PLAIN_SECRET_FILE:-${REPO_ROOT}/apps/gitea-runner/secrets.yaml}"
}

53
scripts/pimox-oob Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env bash
set -euo pipefail
PIMOX_HOST="${LAB_PIMOX_HOST:-${LAB_CLUSTER_PIMOX_HOST:-192.168.100.80}}"
PIMOX_USER="${LAB_PIMOX_USER:-${LAB_CLUSTER_PIMOX_USER:-jv}}"
PIMOX_KEY="${LAB_PIMOX_SSH_KEY_PATH:-${LAB_CLUSTER_PIMOX_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}}"
usage() {
echo "Usage: ./jeannie pimox {status [VMID]|console VMID}" >&2
exit 2
}
valid_vmid() {
[[ "${1:-}" =~ ^[0-9]+$ ]] && ((10#$1 > 0))
}
pimox_ssh() {
ssh -i "${PIMOX_KEY}" -o BatchMode=yes -o ConnectTimeout=10 \
-o StrictHostKeyChecking=accept-new "${PIMOX_USER}@${PIMOX_HOST}" "$@"
}
status_vm() {
local vmid="$1"
if ! valid_vmid "${vmid}"; then
echo "VMID must be numeric: ${vmid}" >&2
return 2
fi
pimox_ssh "sudo qm status ${vmid}; echo '--- config ---'; sudo qm config ${vmid} | grep -E '^(name|cores|sockets|memory|cpu|agent|serial[0-9]*):' || true; echo '--- guest agent ---'; sudo qm agent ${vmid} ping 2>&1 || true"
}
case "${1:-}" in
status)
if [[ -n "${2:-}" ]]; then
status_vm "$2"
else
echo "Pimox host: ${PIMOX_USER}@${PIMOX_HOST}"
pimox_ssh "sudo qm list"
fi
;;
console)
if [[ "${JEANNIE_REMOTE_SERVER:-false}" == true ]]; then
echo "Interactive Pimox console cannot run through the remote API; SSH to Debian and run ./jeannie pimox console VMID." >&2
exit 2
fi
valid_vmid "${2:-}" || usage
exec ssh -tt -i "${PIMOX_KEY}" -o ConnectTimeout=10 \
-o StrictHostKeyChecking=accept-new "${PIMOX_USER}@${PIMOX_HOST}" \
"sudo qm terminal $2"
;;
*)
usage
;;
esac