Vault IaC: bootstrap AppRole + config vars + import state (steps 2-4 of #1515) #1519

Closed
opened 2026-08-07 17:48:38 +00:00 by ginjiruu · 3 comments
Owner

Vault IaC: bootstrap AppRole, config vars, and import state (steps 2–4 of #1515)

Claim me. One-time manual Vault-admin work to get vault-config reconciling on mini.

Prerequisites

  • Access to the Vault root token (or a token with the vault-policy-admin policy + ability to write auth/approle/*, sys/policy/*).
  • VAULT_ADDR=https://vault.animeteamspeak.moe and VAULT_TOKEN=<root> exported for every vault command below.
  • kubectl pointed at mini (KUBECONFIG=~/.kube/mini).
  • The old state at ../iac/vault/terraform.tfstate still present.

Order: (2) bootstrap AppRole → (3) create vault-config-vars secret → (4) import state. Do not proceed past a gate until its verification passes.


Stage 2 — Bootstrap the runner AppRole (ADR 0004)

The runner AppRole + its policy are excluded from the IaC: create a dedicated admin-breadth policy here — do not reuse the IaC-managed vault-policy-admin (a tofu-managed policy the runner depends on is the self-lockout risk ADR 0004 exists to prevent).

vault auth enable approle

Create a policy file, e.g. /tmp/vault-runner.hcl:

path "*" {
  capabilities = ["sudo", "read", "create", "update", "delete", "list", "patch"]
}
vault policy write vault-runner /tmp/vault-runner.hcl
vault write auth/approle/role/vault-runner \
  token_policies="vault-runner" \
  token_ttl=24h \
  token_max_ttl=168h

ROLE_ID=$(vault read -field=role_id auth/approle/role/vault-runner/role-id)
SECRET_ID=$(vault write -f -field=secret_id auth/approle/role/vault-runner/secret-id)

Gate 2: vault read auth/approle/role/vault-runner shows the role; vault login -method=approle role_id="$ROLE_ID" secret_id="$SECRET_ID" succeeds and lists vault-runner in token policies. Keep ROLE_ID / SECRET_ID in the current shell — they feed Stage 3.

Do not add these to infra/vault/. This is the one manual Vault-admin step; everything after is code.


Stage 3 — Create the vault-config-vars secret on mini

The vault-config Terraform object reads varsFrom a Secret named vault-config-vars in flux-system. Create it directly with kubectl — not via ESO (see note at the bottom).

kubectl create secret generic vault-config-vars -n flux-system \
  --from-literal=approle_role_id="$ROLE_ID" \
  --from-literal=approle_secret_id="$SECRET_ID" \
  --from-literal=oidc_client_secret='<the animeteamspeak OIDC client secret>'

The oidc_client_secret is the OAuth2 client secret for the vault application in Kanidm (auth.animeteamspeak.moe). If you don't have it: scripts/newOAuth.sh created the app and prints the secret; or re-fetch from the Kanidm OAuth2 app config.

Gate 3: kubectl -n flux-system get secret vault-config-vars -o jsonpath='{.data}' shows all three keys present (base64). Nothing sensitive echoed.

Why kubectl, not ESO: not circular today (ESO authenticates via the independent vault-token), but it would require first writing the AppRole creds into Vault KV — a wider blast radius than a one-shot secret. Circularity only becomes real when ESO itself moves to AppRole auth in #1518; the bootstrap creds for that must come from out-of-band anyway. Direct secret = simplest, matches ADR 0004.


Stage 4 — Import existing state so tofu owns it, not recreates it

The following already exist live in Vault and are declared in infra/vault/; tofu must import them or the first apply will try to create them and conflict:

Resource Import ID
vault_jwt_auth_backend.animeteamspeak oidc
vault_jwt_auth_backend_role.animeteamspeak_role oidc/animeteamspeak_default
vault_identity_group.super_admins_group e2e95fdd-f5bf-6a6b-7ecf-59d76def6399
vault_identity_group_alias.super_admins_group_alias f768dc8e-898b-3eca-c7d4-bf7cdd983a8f
vault_policy.vault_policy_admin vault-policy-admin
vault_policy.manager manager
vault_policy.reader reader
vault_mount.secret secret

The 4 OIDC/identity resources are already in ../iac/vault/terraform.tfstate — restore that state into the cluster secret, which carries their IDs forward. The controller's default backend (verified in controllers/tf_controller_backend.go) writes to secret tfstate-${workspace}-${secretSuffix} in the Terraform object's namespace — here tfstate-default-vault-config in flux-system (NOT default):

WORKSPACE=default
NAME=vault-config

gzip -c ../iac/vault/terraform.tfstate > /tmp/tfstate.gz
kubectl create secret generic tfstate-${WORKSPACE}-${NAME} -n flux-system \
  --from-file=tfstate=/tmp/tfstate.gz \
  --dry-run=client -o=yaml \
  | yq e '.metadata.annotations["encoding"]="gzip"' - \
  > /tmp/tfstate-${WORKSPACE}-${NAME}.yaml
kubectl apply -f /tmp/tfstate-${WORKSPACE}-${NAME}.yaml

Do the remaining imports BEFORE re-enabling the controller. The restored state carries only 4 of the 8 resources; the policies + KV mount exist live in Vault but not in state. Import them locally against the same backend so the state secret ends up complete — otherwise the first plan wants to create them and vault_mount.secret's create fails (path is already in use).

Use the tofu (OpenTofu) binary, not terraform. The kubernetes backend's encoding: gzip state annotation is honoured by tofu but NOT by the Hashicorp terraform binary — terraform misreads the gzip state as empty and clobbers the secret. Import from a scratch copy of infra/vault/ with a kubernetes backend block added (never add it to the committed provider.tf — the controller injects its own backend each reconcile):

mkdir -p /tmp/vault-import && cp infra/vault/*.tf /tmp/vault-import/
cp -r infra/vault/policies /tmp/vault-import/
# append to /tmp/vault-import/provider.tf's terraform {} block:
#   backend "kubernetes" {
#     secret_suffix     = "vault-config"
#     namespace         = "flux-system"
#     in_cluster_config = false
#     config_path       = "~/.kube/mini"
#   }
# For the one-time import, use a token via env (root token) and drop the
# auth_login block, since the AppRole isn't bootstrapped yet:
cd /tmp/vault-import
tofu init -reconfigure
export VAULT_ADDR="https://vault.animeteamspeak.moe"
export VAULT_TOKEN="<root token>"
tofu import vault_policy.vault_policy_admin vault-policy-admin
tofu import vault_policy.manager manager
tofu import vault_policy.reader reader
tofu import vault_mount.secret secret
tofu state list   # expect all 8 resources

The 4 policy/mount imports complete the state; tofu state list should show all 8 resources. The runner-exec alternative (below) also uses tofu (the runner ships OpenTofu). Prefer the local import path over runner-exec — it must finish before the controller is re-enabled, and runner-exec fights the controller's own reconcile loop for the state lock.

# if going the runner-exec route, find the runner pod for vault-config.
# the runner runs OpenTofu, so the binary is `tofu` (try `terraform` if absent).
RUNNER=$(kubectl get pods -n flux-system -l app.kubernetes.io/name=terraform-runner \
  -o jsonpath='{.items[0].metadata.name}')
kubectl exec -n flux-system "$RUNNER" -- tofu import vault_policy.vault_policy_admin vault-policy-admin
kubectl exec -n flux-system "$RUNNER" -- tofu import vault_policy.manager manager
kubectl exec -n flux-system "$RUNNER" -- tofu import vault_policy.reader reader
kubectl exec -n flux-system "$RUNNER" -- tofu import vault_mount.secret secret

If the runner-exec route is awkward, the alternative is to pull the state back out, run local imports against a copy of infra/vault/ with a kubernetes backend pointing at the same secret, and restore. Prefer whichever you can verify end-to-end; document the choice on this issue.

Gate 4: after imports, kubectl get terraform vault-config -n flux-system shows the plan with no "create" of already-existing resources — only oidc_client_secret drift (the old state stored a value; current one is whatever you supplied) and any intended policy/mount alignment. Read the plan from spec.status before applying anything.


After Stage 4 — first apply (not part of this issue)

Watch the plan, set spec.approvePlan to the generated plan name to apply. Record anything that drifts unexpectedly as a comment here.

Out of scope

  • Branch Planner (#1516), forgejo/firefly migration (#1517), drift cleanup (#1518).
# Vault IaC: bootstrap AppRole, config vars, and import state (steps 2–4 of #1515) Claim me. One-time manual Vault-admin work to get `vault-config` reconciling on mini. **Prerequisites** - Access to the Vault root token (or a token with the `vault-policy-admin` policy + ability to write `auth/approle/*`, `sys/policy/*`). - `VAULT_ADDR=https://vault.animeteamspeak.moe` and `VAULT_TOKEN=<root>` exported for every `vault` command below. - `kubectl` pointed at **mini** (`KUBECONFIG=~/.kube/mini`). - The old state at `../iac/vault/terraform.tfstate` still present. **Order:** (2) bootstrap AppRole → (3) create `vault-config-vars` secret → (4) import state. Do not proceed past a gate until its verification passes. --- ## Stage 2 — Bootstrap the runner AppRole (ADR 0004) The runner AppRole + its policy are **excluded** from the IaC: create a *dedicated* admin-breadth policy here — do **not** reuse the IaC-managed `vault-policy-admin` (a tofu-managed policy the runner depends on is the self-lockout risk ADR 0004 exists to prevent). ```bash vault auth enable approle ``` Create a policy file, e.g. `/tmp/vault-runner.hcl`: ```hcl path "*" { capabilities = ["sudo", "read", "create", "update", "delete", "list", "patch"] } ``` ```bash vault policy write vault-runner /tmp/vault-runner.hcl vault write auth/approle/role/vault-runner \ token_policies="vault-runner" \ token_ttl=24h \ token_max_ttl=168h ROLE_ID=$(vault read -field=role_id auth/approle/role/vault-runner/role-id) SECRET_ID=$(vault write -f -field=secret_id auth/approle/role/vault-runner/secret-id) ``` > **Gate 2:** `vault read auth/approle/role/vault-runner` shows the role; `vault login -method=approle role_id="$ROLE_ID" secret_id="$SECRET_ID"` succeeds and lists `vault-runner` in token policies. Keep `ROLE_ID` / `SECRET_ID` in the current shell — they feed Stage 3. **Do not** add these to `infra/vault/`. This is the one manual Vault-admin step; everything after is code. --- ## Stage 3 — Create the `vault-config-vars` secret on mini The `vault-config` Terraform object reads `varsFrom` a Secret named `vault-config-vars` in `flux-system`. Create it directly with `kubectl` — **not** via ESO (see note at the bottom). ```bash kubectl create secret generic vault-config-vars -n flux-system \ --from-literal=approle_role_id="$ROLE_ID" \ --from-literal=approle_secret_id="$SECRET_ID" \ --from-literal=oidc_client_secret='<the animeteamspeak OIDC client secret>' ``` The `oidc_client_secret` is the OAuth2 client secret for the `vault` application in Kanidm (`auth.animeteamspeak.moe`). If you don't have it: `scripts/newOAuth.sh` created the app and prints the secret; or re-fetch from the Kanidm OAuth2 app config. > **Gate 3:** `kubectl -n flux-system get secret vault-config-vars -o jsonpath='{.data}'` shows all three keys present (base64). Nothing sensitive echoed. **Why kubectl, not ESO:** not circular today (ESO authenticates via the independent `vault-token`), but it would require first writing the AppRole creds into Vault KV — a wider blast radius than a one-shot secret. Circularity only becomes real when ESO itself moves to AppRole auth in #1518; the bootstrap creds for that must come from out-of-band anyway. Direct secret = simplest, matches ADR 0004. --- ## Stage 4 — Import existing state so tofu owns it, not recreates it The following already exist **live in Vault** and are declared in `infra/vault/`; tofu must import them or the first apply will try to create them and conflict: | Resource | Import ID | |---|---| | `vault_jwt_auth_backend.animeteamspeak` | `oidc` | | `vault_jwt_auth_backend_role.animeteamspeak_role` | `oidc/animeteamspeak_default` | | `vault_identity_group.super_admins_group` | `e2e95fdd-f5bf-6a6b-7ecf-59d76def6399` | | `vault_identity_group_alias.super_admins_group_alias` | `f768dc8e-898b-3eca-c7d4-bf7cdd983a8f` | | `vault_policy.vault_policy_admin` | `vault-policy-admin` | | `vault_policy.manager` | `manager` | | `vault_policy.reader` | `reader` | | `vault_mount.secret` | `secret` | The 4 OIDC/identity resources are already in `../iac/vault/terraform.tfstate` — **restore that state into the cluster secret**, which carries their IDs forward. The controller's default backend (verified in `controllers/tf_controller_backend.go`) writes to secret `tfstate-${workspace}-${secretSuffix}` in **the Terraform object's namespace** — here `tfstate-default-vault-config` in **`flux-system`** (NOT `default`): ```bash WORKSPACE=default NAME=vault-config gzip -c ../iac/vault/terraform.tfstate > /tmp/tfstate.gz kubectl create secret generic tfstate-${WORKSPACE}-${NAME} -n flux-system \ --from-file=tfstate=/tmp/tfstate.gz \ --dry-run=client -o=yaml \ | yq e '.metadata.annotations["encoding"]="gzip"' - \ > /tmp/tfstate-${WORKSPACE}-${NAME}.yaml kubectl apply -f /tmp/tfstate-${WORKSPACE}-${NAME}.yaml ``` **Do the remaining imports BEFORE re-enabling the controller.** The restored state carries only 4 of the 8 resources; the policies + KV mount exist live in Vault but not in state. Import them locally against the same backend so the state secret ends up complete — otherwise the first plan wants to *create* them and `vault_mount.secret`'s create fails (`path is already in use`). **Use the `tofu` (OpenTofu) binary, not `terraform`.** The kubernetes backend's `encoding: gzip` state annotation is honoured by `tofu` but NOT by the Hashicorp `terraform` binary — `terraform` misreads the gzip state as empty and clobbers the secret. Import from a scratch copy of `infra/vault/` with a kubernetes backend block added (never add it to the committed `provider.tf` — the controller injects its own backend each reconcile): ```bash mkdir -p /tmp/vault-import && cp infra/vault/*.tf /tmp/vault-import/ cp -r infra/vault/policies /tmp/vault-import/ # append to /tmp/vault-import/provider.tf's terraform {} block: # backend "kubernetes" { # secret_suffix = "vault-config" # namespace = "flux-system" # in_cluster_config = false # config_path = "~/.kube/mini" # } # For the one-time import, use a token via env (root token) and drop the # auth_login block, since the AppRole isn't bootstrapped yet: cd /tmp/vault-import tofu init -reconfigure export VAULT_ADDR="https://vault.animeteamspeak.moe" export VAULT_TOKEN="<root token>" tofu import vault_policy.vault_policy_admin vault-policy-admin tofu import vault_policy.manager manager tofu import vault_policy.reader reader tofu import vault_mount.secret secret tofu state list # expect all 8 resources ``` The 4 policy/mount imports complete the state; `tofu state list` should show all 8 resources. The runner-exec alternative (below) also uses `tofu` (the runner ships OpenTofu). **Prefer the local import path over runner-exec — it must finish before the controller is re-enabled, and runner-exec fights the controller's own reconcile loop for the state lock.** ```bash # if going the runner-exec route, find the runner pod for vault-config. # the runner runs OpenTofu, so the binary is `tofu` (try `terraform` if absent). RUNNER=$(kubectl get pods -n flux-system -l app.kubernetes.io/name=terraform-runner \ -o jsonpath='{.items[0].metadata.name}') kubectl exec -n flux-system "$RUNNER" -- tofu import vault_policy.vault_policy_admin vault-policy-admin kubectl exec -n flux-system "$RUNNER" -- tofu import vault_policy.manager manager kubectl exec -n flux-system "$RUNNER" -- tofu import vault_policy.reader reader kubectl exec -n flux-system "$RUNNER" -- tofu import vault_mount.secret secret ``` If the runner-exec route is awkward, the alternative is to pull the state back out, run local imports against a copy of `infra/vault/` with a kubernetes backend pointing at the same secret, and restore. Prefer whichever you can verify end-to-end; document the choice on this issue. > **Gate 4:** after imports, `kubectl get terraform vault-config -n flux-system` shows the plan with **no "create" of already-existing resources** — only `oidc_client_secret` drift (the old state stored a value; current one is whatever you supplied) and any intended policy/mount alignment. Read the plan from `spec.status` before applying anything. --- ## After Stage 4 — first apply (not part of this issue) Watch the plan, set `spec.approvePlan` to the generated plan name to apply. Record anything that drifts unexpectedly as a comment here. ## Out of scope - Branch Planner (#1516), forgejo/firefly migration (#1517), drift cleanup (#1518).
ginjiruu added this to the Vault IaC milestone 2026-08-07 17:48:42 +00:00
Author
Owner

Correction applied (Stage 4): the tfstate secret goes in flux-system, not default — the controller's default kubernetes backend writes to tfstate-${workspace}-${secretSuffix} in the Terraform object's namespace (verified in controllers/tf_controller_backend.go). Restore command now has -n flux-system.

Also: do the 4 policy/mount imports locally against the same backend BEFORE re-enabling the tofu-controller deployment. The restored ../iac/vault state only carries the 4 OIDC/identity resources; policies + KV mount exist live but not in state. If the controller reconciles first, the plan wants to create them and vault_mount.secret's create fails with "path is already in use". Prefer the local terraform init -reconfigure -backend-config=... + terraform import path over runner-exec (avoids state-lock contention with the controller).

Correction applied (Stage 4): the tfstate secret goes in **flux-system**, not `default` — the controller's default kubernetes backend writes to `tfstate-${workspace}-${secretSuffix}` in the Terraform object's namespace (verified in `controllers/tf_controller_backend.go`). Restore command now has `-n flux-system`. Also: do the 4 policy/mount imports locally against the same backend BEFORE re-enabling the tofu-controller deployment. The restored `../iac/vault` state only carries the 4 OIDC/identity resources; policies + KV mount exist live but not in state. If the controller reconciles first, the plan wants to *create* them and `vault_mount.secret`'s create fails with "path is already in use". Prefer the local `terraform init -reconfigure -backend-config=...` + `terraform import` path over runner-exec (avoids state-lock contention with the controller).
Author
Owner

Stage 4 imports completed using tofu (OpenTofu 1.12.1), with the gzip-annotated state secret restored into flux-system (this is the key fix: terraform misreads the encoding: gzip state as empty and clobbers the secret; tofu honours it).

All 8 resources are now in tfstate-default-vault-config (verified live):

  • restored from ../iac/vault: the 4 OIDC/identity resources
  • imported via tofu import (env token, scratch copy of infra/vault/ with a kubernetes backend block): vault_policy.vault_policy_admin, vault_policy.manager, vault_policy.reader, vault_mount.secret

Remaining for the claimer: Stage 2 (AppRole bootstrap) and Stage 3 (vault-config-vars secret) are still outstanding — the controller reconcile is currently erroring on secrets "vault-config-vars" not found. Once Stage 2+3 are done, re-enable the tofu-controller deployment and watch the plan.

**Stage 4 imports completed** using `tofu` (OpenTofu 1.12.1), with the gzip-annotated state secret restored into `flux-system` (this is the key fix: `terraform` misreads the `encoding: gzip` state as empty and clobbers the secret; `tofu` honours it). All 8 resources are now in `tfstate-default-vault-config` (verified live): - restored from `../iac/vault`: the 4 OIDC/identity resources - imported via `tofu import` (env token, scratch copy of `infra/vault/` with a kubernetes backend block): `vault_policy.vault_policy_admin`, `vault_policy.manager`, `vault_policy.reader`, `vault_mount.secret` Remaining for the claimer: Stage 2 (AppRole bootstrap) and Stage 3 (`vault-config-vars` secret) are still outstanding — the controller reconcile is currently erroring on `secrets "vault-config-vars" not found`. Once Stage 2+3 are done, re-enable the tofu-controller deployment and watch the plan.
Author
Owner

All stages complete. Stages 2-3 confirmed: vault-config-vars secret holds approle_role_id/approle_secret_id/oidc_client_secret. Stage 4: all 8 resources in tfstate-default-vault-config (4 OIDC/identity restored from ../iac/vault + 4 policy/mount imported via tofu). First apply succeeded (plan-master-7f2dc887b1) with the approval loop verified — the IaC now lives in lab/iac (see #1515 comment).

All stages complete. Stages 2-3 confirmed: `vault-config-vars` secret holds approle_role_id/approle_secret_id/oidc_client_secret. Stage 4: all 8 resources in `tfstate-default-vault-config` (4 OIDC/identity restored from `../iac/vault` + 4 policy/mount imported via `tofu`). First apply succeeded (`plan-master-7f2dc887b1`) with the approval loop verified — the IaC now lives in `lab/iac` (see #1515 comment).
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lab/clusters#1519
No description provided.