seed grafana admin credentials
Homelab Main / validate (push) Successful in 16s Details
Homelab Main / running-update (push) Successful in 10s Details
Homelab Main / debian-ops (push) Successful in 1m23s Details

This commit is contained in:
juvenal.diaz 2026-09-28 15:45:12 -06:00
parent bb1fb2f16d
commit 7f500b5e18
5 changed files with 284 additions and 1 deletions

40
apps/monitoring/README.md Normal file
View File

@ -0,0 +1,40 @@
# Monitoring secrets
`grafana-admin.secret.yaml` is generated by Jeannie and encrypted with SOPS/age.
Run on the Debian homelab server:
```bash
./jeannie grafana-admin-secret ensure
```
The generated Kubernetes Secret is named `monitoring/grafana-admin-credentials`
and contains:
- `admin-user`
- `admin-password`
The kube-prometheus-stack Helm release is configured to use this existing Secret
for Grafana admin credentials.
Do not commit plaintext credentials. Commit only the SOPS-encrypted
`grafana-admin.secret.yaml` file after it is generated.
To mirror the same login into Bitwarden or Vaultwarden for browser autofill,
unlock the Bitwarden CLI and run:
```bash
export BW_SESSION="$(bw unlock --raw)"
./jeannie grafana-admin-secret bw-sync
```
If you use Vaultwarden, configure the CLI first:
```bash
bw config server https://vault.example.com
```
The vault item defaults to:
- name: `Homelab Grafana`
- URI: `https://grafana.lab2025.duckdns.org/`

View File

@ -1987,6 +1987,11 @@ resource "helm_release" "prometheus_stack" {
}
grafana = {
nodeSelector = local.prometheus_stack_node_selector
admin = {
existingSecret = "grafana-admin-credentials"
userKey = "admin-user"
passwordKey = "admin-password"
}
sidecar = {
resources = {
requests = {

View File

@ -100,6 +100,8 @@ Operator View And Reports
Observability And GitOps
grafana-dashboards {list|apply} List or apply repo-managed Grafana dashboards.
grafana-admin-secret {ensure|apply|file|bw-sync}
Generate/apply Grafana admin credentials; optionally mirror to Bitwarden.
gitops-status Print focused Argo CD health and sync status.
platform Deploy platform stack only.
edge Deploy/check the OCI edge stack only.
@ -304,6 +306,9 @@ case "${1:-}" in
grafana-dashboards)
grafana_dashboards "$@"
;;
grafana-admin-secret)
grafana_admin_secret "${2:-ensure}"
;;
workers)
workers_manage "$@"
;;

View File

@ -503,6 +503,9 @@ run_tofu_stack() {
ensure_edge_tailscale_routes
ensure_edge_haproxy_stats_password
fi
if [[ "${stack}" == "bootstrap/platform" ]]; then
ensure_grafana_admin_secret
fi
if truthy "${auto_approve}"; then
apply_args+=("-auto-approve")

View File

@ -189,4 +189,234 @@ EOF
security_attack_path() {
require_debian_server "security-attack-path"
"${REPO_ROOT}/scripts/security-attack-path" "${@:2}"
}
}
grafana_admin_secret_file() {
printf '%s\n' "${HOMELAB_GRAFANA_ADMIN_SECRET_FILE:-${REPO_ROOT}/apps/monitoring/grafana-admin.secret.yaml}"
}
generate_grafana_admin_password() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -base64 36 | tr -d '\n'
elif command -v python3 >/dev/null 2>&1; then
python3 - <<'PY'
import secrets
import string
alphabet = string.ascii_letters + string.digits + "-_"
print("".join(secrets.choice(alphabet) for _ in range(48)))
PY
else
echo "openssl or python3 is required to generate the Grafana admin password." >&2
return 1
fi
}
ensure_grafana_admin_secret_file() {
local secret_file
local secret_dir
local tmp_file
local password
secret_file="$(grafana_admin_secret_file)"
if [[ -s "${secret_file}" ]]; then
return 0
fi
ensure_sops_age_tools
secret_dir="$(dirname "${secret_file}")"
mkdir -p "${secret_dir}"
tmp_file="$(mktemp)"
password="$(generate_grafana_admin_password)"
chmod 600 "${tmp_file}"
{
printf 'apiVersion: v1\n'
printf 'kind: Secret\n'
printf 'metadata:\n'
printf ' name: grafana-admin-credentials\n'
printf ' namespace: monitoring\n'
printf ' labels:\n'
printf ' app.kubernetes.io/name: grafana\n'
printf ' app.kubernetes.io/managed-by: jeannie\n'
printf 'type: Opaque\n'
printf 'stringData:\n'
printf ' admin-user: admin\n'
printf ' admin-password: "%s"\n' "${password}"
} >"${tmp_file}"
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops --encrypt --input-type yaml --output-type yaml "${tmp_file}" >"${secret_file}"
rm -f "${tmp_file}"
chmod 600 "${secret_file}"
echo "Created SOPS-encrypted Grafana admin secret at ${secret_file}."
echo "Commit this encrypted file after validating the deployment; it contains no plaintext password."
}
apply_grafana_admin_secret() {
local secret_file
secret_file="$(grafana_admin_secret_file)"
ensure_grafana_admin_secret_file
if ! command -v kubectl >/dev/null 2>&1; then
echo "kubectl is required to apply the Grafana admin secret." >&2
return 1
fi
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops -d "${secret_file}" |
kubectl --kubeconfig "${KUBECONFIG_PATH}" apply -f -
}
grafana_admin_secret_value() {
local key="$1"
local secret_file
secret_file="$(grafana_admin_secret_file)"
SOPS_AGE_KEY_FILE="$(sops_age_key_file)" sops -d "${secret_file}" |
awk -F': ' -v key="${key}" '
$1 ~ "^[[:space:]]*" key "$" {
value = $2
sub(/^[[:space:]]+/, "", value)
sub(/^"/, "", value)
sub(/"$/, "", value)
print value
exit
}
'
}
sync_grafana_admin_secret_to_bitwarden() {
local secret_file
local username
local password
local item_name="${HOMELAB_GRAFANA_BW_ITEM_NAME:-Homelab Grafana}"
local item_uri="${HOMELAB_GRAFANA_URL:-https://grafana.${LAB_DOMAIN:-lab2025.duckdns.org}/}"
local status_json
local item_list_json
local existing_id
local item_json
local encoded_item
secret_file="$(grafana_admin_secret_file)"
ensure_grafana_admin_secret_file
if ! command -v bw >/dev/null 2>&1; then
echo "Bitwarden CLI 'bw' is required for bw-sync." >&2
echo "Install it, run 'bw config server <vault-url>' if using Vaultwarden, then login/unlock." >&2
return 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "python3 is required for bw-sync." >&2
return 1
fi
status_json="$(bw status)"
if ! STATUS_JSON="${status_json}" python3 - <<'PY'
import json
import os
import sys
status = json.loads(os.environ["STATUS_JSON"])
sys.exit(0 if status.get("status") == "unlocked" else 1)
PY
then
echo "Bitwarden CLI is not unlocked. Run 'bw login' or 'export BW_SESSION=$(bw unlock --raw)' first." >&2
return 1
fi
username="$(grafana_admin_secret_value "admin-user")"
password="$(grafana_admin_secret_value "admin-password")"
if [[ -z "${username}" || -z "${password}" ]]; then
echo "Could not read Grafana admin credentials from ${secret_file}." >&2
return 1
fi
item_list_json="$(bw list items --search "${item_name}")"
existing_id="$(ITEMS_JSON="${item_list_json}" ITEM_NAME="${item_name}" python3 - <<'PY'
import json
import os
items = json.loads(os.environ["ITEMS_JSON"] or "[]")
name = os.environ["ITEM_NAME"]
for item in items:
if item.get("name") == name:
print(item.get("id", ""))
break
PY
)"
if [[ -n "${existing_id}" ]]; then
item_json="$(bw get item "${existing_id}")"
else
item_json='{}'
fi
item_json="$(
ITEM_JSON="${item_json}" \
ITEM_NAME="${item_name}" \
ITEM_URI="${item_uri}" \
GRAFANA_USERNAME="${username}" \
GRAFANA_PASSWORD="${password}" \
python3 - <<'PY'
import json
import os
try:
item = json.loads(os.environ["ITEM_JSON"] or "{}")
except json.JSONDecodeError:
item = {}
item["type"] = 1
item["object"] = "item"
item["name"] = os.environ["ITEM_NAME"]
item["notes"] = "Managed by Jeannie from the SOPS-encrypted Grafana admin secret."
item.setdefault("login", {})
item["login"]["username"] = os.environ["GRAFANA_USERNAME"]
item["login"]["password"] = os.environ["GRAFANA_PASSWORD"]
item["login"]["uris"] = [{"match": 1, "uri": os.environ["ITEM_URI"]}]
print(json.dumps(item, separators=(",", ":")))
PY
)"
encoded_item="$(printf '%s' "${item_json}" | bw encode)"
if [[ -n "${existing_id}" ]]; then
bw edit item "${existing_id}" "${encoded_item}" >/dev/null
echo "Updated Bitwarden/Vaultwarden login item '${item_name}'."
else
bw create item "${encoded_item}" >/dev/null
echo "Created Bitwarden/Vaultwarden login item '${item_name}'."
fi
}
ensure_grafana_admin_secret() {
require_debian_server "grafana-admin-secret"
apply_grafana_admin_secret >/dev/null
echo "Grafana admin credentials secret is present in Kubernetes."
}
grafana_admin_secret() {
local action="${1:-ensure}"
require_debian_server "grafana-admin-secret"
case "${action}" in
ensure | apply)
apply_grafana_admin_secret
echo "Grafana admin credentials secret is ready."
;;
file)
ensure_grafana_admin_secret_file
grafana_admin_secret_file
;;
bw-sync)
sync_grafana_admin_secret_to_bitwarden
;;
*)
echo "Usage: ./jeannie grafana-admin-secret {ensure|apply|file|bw-sync}" >&2
exit 1
;;
esac
}