So far in this series the secrets engines I’ve used have all been ones that ship inside Vault: KV for static secrets, the database engine for short-lived Postgres credentials, and Transit for auto-unseal. That covers most of what a Kubernetes platform needs, and if it covers yours then the rest of this post is optional reading! For anything the built-in engines don’t cover, Vault has an extension point: custom secrets engines, which run as external plugins. The Vault community has created engines for GitHub App tokens, Cloudflare, Artifactory, Slack and a long tail of other things that can hand out credentials with a lease attached.
This post covers what a custom secrets engine is, then a general method for deploying one into a Vault cluster running on Kubernetes, and finally a worked example using vault-plugin-secrets-github with the Vault Secrets Operator. The method should work (or can be adapted) for any external plugin; the GitHub engine just happens to be the one I wanted on my own cluster.
What a custom secrets engine is#
The engines we’ve used so far are compiled into the Vault binary. An external plugin is a separate binary that Vault launches as a child process and talks to over RPC, rather than executing it inside its own address space. From the client’s point of view nothing changes: you enable it at a path, write config to it and read credentials from it, the same as KV or the database engine.
Configuring a custom secrets engine needs three things:
plugin_directoryhas to be set in the server config- the binary has to exist inside that directory
- the plugin catalog needs an entry pairing a name with the SHA256 of the file
Vault refuses to run a binary whose hash doesn’t match the catalog (which stops an attacker swapping the file on disk, for example).
The catalog configuration is stored in Vault’s Raft storage, so it replicates across the cluster. The plugin binary sits on a filesystem and doesn’t. That means that my three node cluster has one catalog registration but needs three copies of the plugin file, one on every pod, and getting them there is most of the work in this post.
flowchart LR
helm["Helm chart
(synced by ArgoCD)"]
release["Plugin release
(GitHub)"]
register["vault plugin register"]
enable["vault secrets enable"]
subgraph pod["Each Vault pod (vault-0, vault-1, vault-2)"]
init["Init container
download + sha256sum -c"]
dir[("emptyDir
/usr/local/libexec/vault")]
vault["Vault server"]
plugin["Plugin child process"]
end
subgraph raft["Raft storage (replicated)"]
catalog[("Plugin catalog
name, version, SHA256")]
end
helm -->|"1. renders init container"| init
release -->|binary| init
init -->|writes binary| dir
register -->|"2. records hash"| catalog
enable -->|"3. mount"| vault
vault -->|looks up name + hash| catalog
vault -->|verifies hash| dir
vault -->|starts| plugin
vault <-.->|RPC| plugin
Deploying a custom Vault plugin on Kubernetes#
Getting the binary into every Vault pod#
Since every workload in my cluster is deployed by ArgoCD, I want as much as possible configured in YAML. The Vault helm chart has a server.extraInitContainers section, which lets you add a list of raw container specs that are rendered into the StatefulSet’s pod template. Using this, an init container can download the plugin release binary and drop it into a volume that the Vault container then mounts.
For the plugin volume we use an emptyDir, so it starts empty and the init container can download the plugin every time a pod starts. The values below are for the GitHub plugin from the worked example; for another plugin, only the variables at the top of the script should need to change.
server:
volumes:
- name: plugins
emptyDir: {}
volumeMounts:
- name: plugins
mountPath: /usr/local/libexec/vault
readOnly: true
extraInitContainers:
- name: fetch-plugin
image: curlimages/curl:8.11.0
securityContext:
runAsUser: 100 # match the vault user so the files are owned correctly
runAsGroup: 100
allowPrivilegeEscalation: false
command: [sh, -c]
args:
- |
set -euo pipefail
# Plugin-specific values, change these together for an upgrade
plugin=vault-plugin-secrets-github
ver=v2.4.0
asset=${plugin}-linux-amd64
digest=cd7b9ac7f765aeeddad2ad0483b37f8cc05eb37188fa23949987f3f7c02f83b2
base=https://github.com/martinbaillie/${plugin}/releases/download/${ver}
curl -fsSL --proto '=https' --tlsv1.2 --retry 3 --retry-all-errors \
-o /plugins/${asset} "${base}/${asset}"
echo "${digest} /plugins/${asset}" | sha256sum -c -
mv /plugins/${asset} /plugins/${plugin}
chmod 0755 /plugins/${plugin}
volumeMounts:
- name: plugins
mountPath: /pluginsThe script includes a digest comparison, which runs before Vault starts. If it fails, the pod stays in Init and won’t run the Vault instance. This is a much better place to fail than having Vault start but error when somebody tries to mount the plugin.
The other way to do this is to bake the plugin file into a custom Vault container image instead, and for a production platform that’s probably the better answer: the artifact then lives in your registry, pinned and scanned like everything else, and pods stop needing egress to GitHub on every restart.
# Something like this...
FROM hashicorp/vault:2.0.3
COPY --chmod=0755 vault-plugin-secrets-foo /usr/local/libexec/vault/vault-plugin-secrets-fooI went with the init container approach because it keeps me on the official image. That way a Vault upgrade is just a tag change rather than a rebuild of my own image. A 32MB download per pod is cheap in my home lab, and it keeps more of the configuration in ArgoCD.
Setting plugin_directory#
Next, Vault needs to know where to look for the plugins. Depending on your deployment mode (e.g. single node, HA with Raft, HA with another backend) you will need to configure the value in different places. Most likely you already have config in the right place and just need to add one line.
| Mode | Value |
|---|---|
| Single replica, file backend | server.standalone.config |
| HA with Raft | server.ha.raft.config |
| HA with Consul or another external backend | server.ha.config |
For a Raft backed cluster like mine the config looks like this:
server:
ha:
raft:
config: |
ui = true
plugin_directory = "/usr/local/libexec/vault"
listener "tcp" {
# unchanged
}Rolling the pods#
Syncing with ArgoCD (or running helm upgrade) won’t change anything on its own, because the chart sets server.updateStrategyType to OnDelete. The ConfigMap and pod template update while the running pods carry on with the old configuration, so each pod has to be deleted, starting with the followers and then the leader.
# Find the current leader
vault operator raft list-peers
# This assumes vault-0 is the leader
# Delete the followers first, one at a time
kubectl delete pod vault-1 -n vault
kubectl wait --for=condition=Ready pod/vault-1 -n vault --timeout=300s
kubectl delete pod vault-2 -n vault
kubectl wait --for=condition=Ready pod/vault-2 -n vault --timeout=300s
# Gracefully transfer leadership
vault operator step-down
vault operator raft list-peers
# Delete the old leader
kubectl delete pod vault-0 -n vault
kubectl wait --for=condition=Ready pod/vault-0 -n vault --timeout=300sThe chart’s readiness probe runs vault status which fails while a pod is sealed, so Ready means unsealed and serving. My cluster auto-unseals using the Transit secrets engine; if you’re using Shamir keys you’d need to unseal each pod by hand.
You can confirm the binary is in place and Vault is unsealed using the following:
kubectl exec -n vault vault-0 -- sh -c 'ls -l /usr/local/libexec/vault && vault status'
-rwxr-xr-x 1 vault vault 32137376 Nov 20 17:36 vault-plugin-secrets-github
...
Sealed false
HA Mode standbyRegistering and mounting#
The third step is registering the plugin, which gives Vault the file name, its SHA256, an optional version (useful for upgrades), the plugin type (secret) and the name that mounts will refer to. Registration is just one call against the active node (with a token that has sudo permissions on sys/plugins/catalog/*):
vault plugin register \
-sha256=cd7b9ac7f765aeeddad2ad0483b37f8cc05eb37188fa23949987f3f7c02f83b2 \
-command=vault-plugin-secrets-github \
-version=v2.4.0 \
secret githubBe careful with names: a plugin registered under a built-in name such as kv or pki takes precedence over the built-in for any new mount.
The engine then mounts like any other, with -plugin-version selecting the catalog entry:
vault secrets enable -path=github -plugin-version=v2.4.0 github
vault secrets list -detailed | grep githubUpgrading#
Because the catalog is versioned, an upgrade is pretty simple: bump the version and digest in values.yaml, roll the pods so they download the new binary, register the new version, then tune the mount to use it:
# 1. Bump ver and digest in values.yaml, commit, and let ArgoCD sync
# (or run: helm upgrade vault hashicorp/vault -n vault -f values.yaml)
# 2. Roll the pods (as above)
# 3. register the new version and move the mount
vault plugin register -sha256=<new-digest> -command=vault-plugin-secrets-github \
-version=v2.5.0 secret github
vault secrets tune -plugin-version=v2.5.0 githubImportant things to remember#
The emptyDir is per pod. Restarting a pod without the init container, or removing the init container while a catalog entry still exists, produces “plugin not found in the plugin catalog” at mount time even with the registration intact. The catalog entry is not a promise that the binary is there.
A Raft snapshot doesn’t contain the plugin. Restoring onto fresh volumes brings back the catalog and not the binaries, so the init container has to run (or the file has to be copied in by hand) before any mount is touched.
The container runs as uid 100. An init container running as root leaves the files owned by root, which works for execution and is awkward for anything that writes to that directory later. runAsUser: 100 keeps ownership consistent.
Mixed architectures need two digests. linux-arm64 is a different binary with a different hash, and one values file can’t know which node a pod will land on. On a mixed cluster, resolve uname -m inside the script and carry both digests, or the arm64 pods crash loop on “unsupported plugin file format”.
ConfigMaps and Secrets don’t work as a plugin volume. They cap at 1MiB, mount through a ..data symlink layer and aren’t executable, which for a 32MB binary is three separate reasons not to try.
Worked example: the GitHub secrets engine#
vault-plugin-secrets-github mints short-lived GitHub App installation tokens, scoped to specific repositories and permissions, so a workload never needs a long-lived personal access token. With the deployment above done, what’s left is some GitHub-specific configuration and wiring the engine into VSO.
Configuring GitHub#
The engine issues tokens on behalf of a GitHub App, so the first job is creating one.
Create and install the GitHub App#
The plugin README walks through creating the App step by step, so I’ll only cover the parts that need a decision. Create it under your organisation (https://github.com/organizations/<your-org>/settings/apps/new, which needs org owner or App manager rights) rather than your personal account, give it a name that’s unique across GitHub such as vault-secrets-<your-org>, and untick Active under Webhook, since the plugin doesn’t use the homepage, callback or webhook URLs.
The permissions you select are the superset, and each token request can narrow them further. A sensible starting point under Repository permissions:
- Metadata: Read-only (GitHub makes this mandatory)
- Contents: Read and write (clone and push)
- Pull requests: Read and write
- Packages: Read (if you pull from GHCR)
- Anything else your pipelines need (Actions, Checks, Issues, Workflows, and so on)
Choose Only on this account for where the App can be installed, then create it. You need three values from the App afterwards:
- App ID: the number at the top of the App’s General settings page, which is different from the Client ID. This becomes
app_id. - Private key: under Private keys, click Generate a private key. A
.pemfile downloads; keep it safe until it’s in Vault. - Installation ID: click Install App in the sidebar, install it on your org with either all repositories or a selection (tokens can only reach repos in this selection), and take the ID from the URL you land on,
https://github.com/organizations/<org>/settings/installations/<id>(orhttps://github.com/settings/installations/<id>for a personal account).
Configure the Vault engine#
First we write the App ID and private key to Vault config:
vault write github/config app_id=123456 prv_key=@/tmp/app-key.pem
vault read github/configThe private key goes into Vault’s storage and is never returned on read, so the file on disk can be deleted as soon as the write succeeds.
Requesting a token is a read on github/token with the installation ID (or org name), or optionally narrowed to specific repositories and permissions:
# Everything the installation is allowed to use
vault read github/token installation_id=456
# Narrowed to one repository and one permission
vault write github/token installation_id=456 \
repositories=myapp \
permissions=contents=readThat path accepts reads, creates and updates alike, a deliberate concession in the plugin so that both vault read and a Kubernetes custom resource can drive it. Every response also carries hashed_token, a SHA256 of the token that matches the field GitHub writes into its audit log, which makes joining a Vault lease to a GitHub audit event a search rather than a guess.
exclude_repository_metadata=true on the config. Without it every token response has the repository metadata for the whole installation, which is a lot of JSON for a lease.Giving an application access#
Granting an application access to GitHub tokens works the same way as it did for the KV and database secrets: a Vault policy for the path, and a Kubernetes auth role that binds a service account to it.
vault policy write github-token - <<EOF
path "github/token" {
capabilities = ["read", "create", "update"]
}
EOF
vault write auth/kubernetes/role/app-github \
bound_service_account_names=myapp \
bound_service_account_namespaces=infrastructure \
audience=vault \
token_policies=github-token \
alias_name_source=serviceaccount_uidUsing it with the Vault Secrets Operator#
VSO consumes the token the same way it did the database credentials. A VaultDynamicSecret reads github/token, writes it into a Kubernetes Secret, and restarts workloads based on the rolloutRestartTargets configuration. The vaultAuthRef points at a VaultAuth resource for the app-github role, set up the same way as in the dynamic database credentials post.
apiVersion: secrets.hashicorp.com/v1beta1
kind: VaultDynamicSecret
metadata:
name: app-github-token
namespace: infrastructure
spec:
# Mount the secrets backend
mount: github
# Path to the token endpoint
path: token
# Constraints, sent as the request body
params:
installation_id: "456"
repositories: myapp
permissions: contents=read
# Where to store the token, VSO creates the secret
destination:
create: true
name: app-github-token
# Re-read at 67% of the lease
renewalPercent: 67
# Revoke the token if the resource is deleted
revoke: true
# Restart consumers when the token changes
rolloutRestartTargets:
- kind: Deployment
name: myapp
vaultAuthRef: infrastructure/app-github-vault-authOne thing to note here is that the lease isn’t renewable. The plugin sets the TTL to the token’s expires_at, and GitHub installation tokens only live for an hour, so VSO logs a “Lease is not renewable” warning and does a fresh read at renewalPercent instead. A new token lands roughly every forty minutes, with a rollout restart each time. refreshAfter stays unset, because VSO only uses it for engines that return no lease duration.
revoke: true revokes the token at GitHub when the resource is deleted, because the plugin revokes against the installation token endpoint. The VSO role’s policy needs update on sys/leases/revoke, otherwise the revoke fails and the token simply expires.Conclusion#
Most of the effort in running a custom secrets engine on Kubernetes goes into the platform side: making sure every Vault pod has the binary, with the right hash, before anything tries to mount it. Once that’s done, the GitHub engine behaves like any built-in one, and VSO has no idea it’s talking to a plugin.
For my home lab the init container is the right trade-off, because it keeps me on the official image and keeps the configuration in ArgoCD. On a production platform I’d bake the plugin into a custom image instead, so the binary is pinned and scanned in your own registry and pods don’t depend on reaching GitHub every time they restart.
The decision that needs the most thought is what the tokens are allowed to do. The Vault policy is three capabilities on one path, so the real scoping happens in the GitHub App permissions and in the narrowing on each VaultDynamicSecret. That’s the same shape as the database engine, where the creation statement did the scoping and Vault enforced it. If your pipelines are still using long-lived personal access tokens, swapping them for hour-long installation tokens is reason enough to go through the setup.
The hour-long lifetime does have a cost, though. A job that runs for longer than its token, such as a long build or a migration that pushes back to GitHub at the end, will find its credential has expired partway through. The rollout restart every forty minutes doesn’t help either, because restarting a workload in the middle of a long task just throws the work away. That’s a problem I want to come back to in a future post.

