Moving my homelab's secrets out of GitLab using OpenBao and KMS on GCP

In One-click homelab every piece of the homelab became a tag away from being rebuilt. The pipelines that do the rebuilding need secrets. They all lived where GitLab tells you to put them: masked, protected CI/CD variables.

That's fine, right up until you ask who can decrypt them.

Fair warning: this post was written wearing the hat below.

A man in a tinfoil hat: Are you ready?

The problem

GitLab encrypts CI variables at rest, but the key lives on GitLab's own application servers. That protects against a stolen database disk, not against a breach of GitLab itself: the attacker gets the ciphertext and the key together.

So the threat model is deliberately narrow: a platform-wide breach of GitLab, or of the cloud the secrets end up in. Nobody is targeting my homelab specifically (I hope!), so the heavyweight answers (keys split between several people, encrypted VMs, dedicated hardware) would guard against an adversary I don't have (again, I hope). And if one shows up, the design is built from the same pieces enterprises use (KMS, IAP, workload identity), so breaking in would cost far more than my homelab's secrets are worth.

The design

The vault is OpenBao on a single free-tier e2-micro in its own GCP project.

Secrets are laid out per component, named after the tag prefixes the IaC repo already uses (exit-node/1.0.12, sre-agent/2.3.0): iac/<component>/*, plus a small iac/shared/ for the rare secret two components need. A single JSON file lists the components, and both sides of the system are generated from it.

Cost

About $0.15 a month, almost all of it KMS:

Monthly
e2-micro VM in us-central1$0 (Free Tier)
Boot and data disk, 10 GB pd-standard each$0 (in the 30 GB free)
Public IPnone: access is IAP only
Two KMS keys (unseal, OpenTofu state)~$0.12
KMS calls, snapshot bucket, logs, IAPa few cents at most

The same setup on a Spot VM in Paris with a public IP would be around $9.

How a pipeline gets its secrets

GITLAB GOOGLE CLOUD · strafonet-vault CI job TF_VAR_* Workload Identity ci-<component> IAP tunnel OpenBao tag + own runner tofu plan/apply checks the JWT service account port 8200 only no public IP role <component> iac/<component>/* 1. JWT 2. tunnel 3. ID token aud=openbao 4. 10-min token + values 5. revoke token, kill tunnel, wipe temp dir

Step by step:

  1. GitLab mints a JWT for the job. The Workload Identity Federation provider only accepts it if it comes from the IaC project, from a protected tag, on one of my own runners. The tag prefix becomes an attribute, and only jobs whose tag starts with exit-node/ may impersonate ci-exit-node.
  2. The job opens the IAP tunnel as that service account, which is allowed to tunnel to port 8200 and nothing else.
  3. It asks Google for an ID token for ci-<component>, with audience openbao.
  4. OpenBao checks the token against Google's keys, matches the email to the role, and hands back a token valid for 10 minutes, whose policy reads iac/<component>/* and nothing more.
  5. The values become TF_VAR_* variables for the plan or apply, the token is revoked, the tunnel is killed, and the temp directory is deleted. That happens on every exit path, including failures.

Wiring it into the IaC repo

The IaC repo gets one include, .gitlab/ci/openbao.yml, with a hidden .openbao job. It asks GitLab for the JWT and sources a small shell script before the job runs.

.openbao:
  id_tokens:
    OPENBAO_GITLAB_JWT:
      aud: https://iam.googleapis.com/projects/<project-number>/locations/global/workloadIdentityPools/gitlab/providers/gitlab-com
  before_script:
    - . "$CI_PROJECT_DIR/.gitlab/ci/openbao.sh" && openbao_load

A component opts in by adding it next to the OpenTofu templates it already uses, and by saying which secrets go into which variables:

opentofu-plan-production-sre-agent:
  extends: [.opentofu:plan, .openbao]
  variables:
    OPENBAO_COMPONENT: "sre-agent"
    OPENBAO_ENV: |
      TF_VAR_sre_agent_telegram_bot_token=sre-agent/telegram#bot_token
      TF_VAR_sre_agent_gatus_token=shared/gatus/sre-agent#token

Each line is TF_VAR_name=path#field, read from iac/<path>. The OpenTofu code doesn't change at all: it still gets the same TF_VAR_* variables, they just come from somewhere else.

The script's entry point reads like the steps above:

openbao_load() {
  # GitLab's CI_DEBUG_TRACE would print every value
  case $- in *x*) openbao_xtrace=1 && set +x ;; *) openbao_xtrace= ;; esac
  [ -n "${OPENBAO_COMPONENT:-}" ] || openbao_fail "OPENBAO_COMPONENT is not set"
  openbao_tmp=$(mktemp -d)
  trap openbao_disconnect EXIT
  openbao_install     # pinned gcloud, checked by hash
  openbao_connect     # IAP tunnel, Google ID token, OpenBao login
  openbao_export      # one TF_VAR_* per OPENBAO_ENV line
  openbao_disconnect  # revoke the token, kill the tunnel, delete the temp dir
  trap - EXIT
  echo "openbao: loaded $(printf '%s\n' "$OPENBAO_ENV" | grep -c =) variables for $OPENBAO_COMPONENT"
  [ -z "$openbao_xtrace" ] || set -x
  unset openbao_xtrace
}

There's no custom image: each job downloads a pinned gcloud, checks its hash, and only uses it for the tunnel.

Three ways back

A vault you can lose is worse than no vault, so there are three independent ways back:

Both export keys are post-quantum, which age supports since version 1.3 (age-keygen -pq). After the joint statement from ONCD, NSA and CISA on new attacks against today's cryptography, that felt like the sensible default.

Possible improvements

One upgrade is planned, and one I left out on purpose because it guards against a stronger attacker than the one I'm planning for:

Closing

Security isn't about finding the one provider you can trust. It's about layers, so that a single mistake or a single leak isn't enough: defense in depth, homelab edition.

Total cost: one coffee a year. Total drama: hopefully zero.