id: vault-backup-verify-drill
namespace: company.team
description: |
Verify that a HashiCorp Vault backup is both fresh and restorable instead of
trusting that a snapshot job reported success. A keyed webhook runs the drill
on demand and a disabled weekly schedule holds the drill day slot. The flow
pulls raft snapshot metadata out of the official Vault container, falls back
to a bundled manifest when no leader answers, turns that manifest into a
freshness verdict with the snapshot age and id, then restores the snapshot
into a scratch namespace to prove the backup can actually come back. A stale
backup opens a GitHub issue and pings Slack, a failed restore drill escalates
again, and the verdict, snapshot age, snapshot id, and verification flag are
published as outputs.
triggers:
- id: on_demand_verify
type: io.kestra.plugin.core.trigger.Webhook
description: POST here to verify a Vault backup on demand, for example after a
restore rehearsal or ahead of an upgrade window.
key: "{{ secret('VAULT_DRILL_WEBHOOK_KEY') }}"
- id: weekly_drill_day
type: io.kestra.plugin.core.trigger.Schedule
description: Drill day net, every Wednesday at 06:00 UTC; shipped disabled until
an operator switches it on.
cron: "0 6 * * 3"
disabled: true
inputs:
- id: max_snapshot_age_days
type: INT
defaults: 7
description: Age budget in days for the newest raft snapshot, anything older is
judged stale and escalates.
- id: vault_addr
type: STRING
defaults: https://vault.example.test:8200
description: Vault server address handed to both containers as VAULT_ADDR for
the snapshot capture and the restore drill.
tasks:
# --- 1. snapshot metadata and freshness verdict ---
- id: drill_workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: One shared directory so the captured snapshot, the manifest parser,
and the restore drill all read the same backup.snap file.
tasks:
- id: capture_snapshot
type: io.kestra.plugin.scripts.shell.Commands
description: Request a raft snapshot from the configured server with the exit
code absorbed, then fall back to a bundled snapshot-manifest.json
heredoc so the verdict still has metadata when no leader answers.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: hashicorp/vault:1.17.2
env:
VAULT_ADDR: "{{ inputs.vault_addr }}"
VAULT_TOKEN: "{{ secret('VAULT_TOKEN') }}"
outputFiles:
- snapshot-manifest.json
commands:
- |
set -u
cat > snapshot-manifest.json <<'JSON'
{
"snapshot_id": "raft-2026-09-30-060000",
"taken_at": "2026-09-30T06:00:00Z",
"size_bytes": 1048576
}
JSON
echo "requesting a raft snapshot from ${VAULT_ADDR}"
vault operator raft snapshot save backup.snap || true
if [ -s backup.snap ]; then
size_bytes=$(wc -c < backup.snap | tr -d ' ')
taken_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)
snapshot_id="raft-$(date -u +%Y%m%d%H%M%S)"
printf '{"snapshot_id": "%s", "taken_at": "%s", "size_bytes": %s}\n' "${snapshot_id}" "${taken_at}" "${size_bytes}" > snapshot-manifest.json
echo "captured snapshot ${snapshot_id} of ${size_bytes} bytes at ${taken_at}"
else
echo "no Vault leader reachable from this worker, keeping the bundled manifest"
fi
echo "manifest bytes $(wc -c < snapshot-manifest.json)"
- id: evaluate_freshness
type: io.kestra.plugin.scripts.python.Script
description: Turn snapshot-manifest.json into a staleness verdict with the
whole-day snapshot age and the snapshot id, emitting them through the
outputs protocol for the branch below.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: python:3.12-slim
env:
MAX_SNAPSHOT_AGE_DAYS: "{{ inputs.max_snapshot_age_days }}"
script: |
import datetime
import json
import os
def parse_timestamp(value):
text = str(value or "").strip()
if not text:
return None
if text.endswith("Z"):
text = text[:-1] + "+00:00"
try:
stamp = datetime.datetime.fromisoformat(text)
except ValueError:
return None
if stamp.tzinfo is None:
stamp = stamp.replace(tzinfo=datetime.timezone.utc)
return stamp
manifest_path = "snapshot-manifest.json"
manifest = {}
if os.path.exists(manifest_path) and os.path.getsize(manifest_path) > 0:
try:
with open(manifest_path, "r") as handle:
manifest = json.load(handle)
except ValueError:
manifest = {}
if not isinstance(manifest, dict):
manifest = {}
try:
max_age = int(os.environ.get("MAX_SNAPSHOT_AGE_DAYS", "7"))
except ValueError:
max_age = 7
if max_age < 1:
max_age = 1
snapshot_id = str(manifest.get("snapshot_id") or "unknown").strip() or "unknown"
taken = parse_timestamp(manifest.get("taken_at"))
now = datetime.datetime.now(datetime.timezone.utc)
if taken is None:
age_days = -1
stale = True
else:
elapsed = (now - taken).total_seconds() / 86400.0
age_days = int(elapsed) if elapsed > 0 else 0
stale = age_days > max_age
verdict = "STALE" if stale else "FRESH"
payload = {
"verdict": verdict,
"snapshot_age_days": age_days,
"snapshot_id": snapshot_id,
"stale": stale,
}
print("snapshot=%s age_days=%d budget=%d verdict=%s" % (snapshot_id, age_days, max_age, verdict))
print("::" + json.dumps({"outputs": payload}) + "::")
- id: restore_drill
type: io.kestra.plugin.scripts.shell.Commands
description: Restore the captured snapshot into a scratch namespace with the
exit code absorbed, writing the command output to restore-check.txt so
the verdict below reads evidence rather than an exit code.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: hashicorp/vault:1.17.2
env:
VAULT_ADDR: "{{ inputs.vault_addr }}"
VAULT_NAMESPACE: drill-scratch
VAULT_TOKEN: "{{ secret('VAULT_TOKEN') }}"
outputFiles:
- restore-check.txt
commands:
- |
set -u
mkdir -p scratch
echo "restore drill against ${VAULT_ADDR} in namespace ${VAULT_NAMESPACE}"
vault operator raft snapshot restore -check backup.snap scratch/restored.snap > restore-check.txt 2>&1 || true
if [ ! -s restore-check.txt ]; then
echo "the restore command produced no output for ${VAULT_ADDR}" > restore-check.txt
fi
if [ -s scratch/restored.snap ]; then
echo "restore_check=passed" >> restore-check.txt
else
echo "restore_check=failed" >> restore-check.txt
fi
echo "restore check bytes $(wc -c < restore-check.txt)"
- id: parse_restore
type: io.kestra.plugin.scripts.python.Script
description: Read restore-check.txt and emit verified true only when the drill
marker reports a passed restore, carrying a short reason into the
escalation below.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: python:3.12-slim
env:
VAULT_ADDR: "{{ inputs.vault_addr }}"
script: |
import json
import os
path = "restore-check.txt"
text = ""
if os.path.exists(path) and os.path.getsize(path) > 0:
with open(path, "r", errors="replace") as handle:
text = handle.read()
vault_addr = os.environ.get("VAULT_ADDR", "vault")
verified = False
if not text:
reason = "the drill produced no restore-check.txt"
else:
markers = [line.strip() for line in text.splitlines()
if line.strip().startswith("restore_check=")]
marker = markers[-1] if markers else ""
if marker == "restore_check=passed":
verified = True
reason = "raft snapshot restored cleanly at " + vault_addr
elif marker.startswith("restore_check=failed"):
verified = False
reason = "restore target scratch/restored.snap came up empty at " + vault_addr
else:
verified = False
reason = "restore-check.txt carried no restore_check marker"
print("verified=" + str(verified).lower() + " reason=" + reason)
print("::" + json.dumps({"outputs": {"verified": verified, "reason": reason}}) + "::")
# --- 2. verification gate ---
- id: check_freshness
type: io.kestra.plugin.core.flow.If
description: Branch on the freshness verdict, a snapshot outside the age budget
escalates while a fresh one is only logged.
condition: "{{ outputs.evaluate_freshness.vars.stale == true }}"
then:
- id: open_stale_issue
type: io.kestra.plugin.github.issues.Create
description: Open a labeled issue on the runbooks repository so a stale backup
becomes tracked work instead of a silent gap.
jwtToken: "{{ secret('GITHUB_TOKEN') }}"
repository: your-org/vault-runbooks
title: "Stale Vault backup at {{ inputs.vault_addr }}"
body: |
## Vault backup freshness drill
The newest raft snapshot for **{{ inputs.vault_addr }}** is **{{ outputs.evaluate_freshness.vars.snapshot_age_days }} day(s)** old against a budget of **{{ inputs.max_snapshot_age_days }} day(s)**.
Snapshot id `{{ outputs.evaluate_freshness.vars.snapshot_id }}`, verdict **{{ outputs.evaluate_freshness.vars.verdict }}**.
Execution {{ execution.id }}
Capture a fresh snapshot, then re-run this flow and confirm the verdict flips to FRESH before closing this issue.
labels:
- vault
- backup
- id: alert_stale_backup
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell ops the backup is older than the age budget and that a
tracking issue is waiting.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Vault backup STALE at {{ inputs.vault_addr }} - snapshot {{ outputs.evaluate_freshness.vars.snapshot_id }} is {{ outputs.evaluate_freshness.vars.snapshot_age_days }} day(s) old against a {{ inputs.max_snapshot_age_days }} day budget. Issue opened in your-org/vault-runbooks. Execution {{ execution.id }}."
}
else:
- id: log_fresh_backup
type: io.kestra.plugin.core.log.Log
description: Record the fresh result so the execution history doubles as backup
evidence.
message: "Vault backup fresh at {{ inputs.vault_addr }} - snapshot {{
outputs.evaluate_freshness.vars.snapshot_id }} is {{
outputs.evaluate_freshness.vars.snapshot_age_days }} day(s) old."
# --- 3. restore drill ---
- id: check_restore
type: io.kestra.plugin.core.flow.If
description: Escalate when the drill could not restore the snapshot, because a
backup that cannot be restored is a backup that does not exist.
condition: "{{ outputs.parse_restore.vars.verified == false }}"
then:
- id: escalate_unrestorable
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert ops that the newest snapshot failed the restore drill, with
the reason and the execution id.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Vault restore drill FAILED at {{ inputs.vault_addr }} - snapshot {{ outputs.evaluate_freshness.vars.snapshot_id }} ({{ outputs.evaluate_freshness.vars.snapshot_age_days }} day(s) old, verdict {{ outputs.evaluate_freshness.vars.verdict }}) could not be restored. {{ outputs.parse_restore.vars.reason }}. Execution {{ execution.id }}."
}
else:
- id: log_restorable
type: io.kestra.plugin.core.log.Log
description: Record the verified result so the execution history doubles as
drill evidence.
message: "Vault backup verified at {{ inputs.vault_addr }} - snapshot {{
outputs.evaluate_freshness.vars.snapshot_id }} is {{
outputs.evaluate_freshness.vars.snapshot_age_days }} day(s) old and
restored cleanly."
errors:
- id: notify_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Escalate any failure to the ops channel with the execution url.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "FAILED: {{ flow.id }} {{ execution.id }}"}
outputs:
- id: verdict
type: STRING
description: Freshness verdict, FRESH when the newest snapshot fits the age
budget and STALE otherwise.
value: "{{ outputs.evaluate_freshness.vars.verdict }}"
- id: snapshot_age_days
type: INT
description: Whole days since the newest raft snapshot, where -1 means the
manifest carried no parseable taken_at value.
value: "{{ outputs.evaluate_freshness.vars.snapshot_age_days }}"
- id: snapshot_id
type: STRING
description: Identifier of the snapshot that was inspected, for example
raft-20261005060000.
value: "{{ outputs.evaluate_freshness.vars.snapshot_id }}"
- id: verified
type: BOOL
description: True when the restore drill rebuilt the snapshot into the scratch
namespace, false when the backup could not be restored.
value: "{{ outputs.parse_restore.vars.verified }}"