|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Bootstrap the Bitwarden operator's machine token through Omni, never Git. |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +CLUSTER="unraid-lab" |
| 6 | +BWS_ACCESS_TOKEN="${BWS_ACCESS_TOKEN:-$(security find-generic-password -w -a "$USER" -s BWS_ACCESS_TOKEN)}" |
| 7 | + |
| 8 | +temporary="$(mktemp -d)" |
| 9 | +trap 'rm -rf "$temporary"' EXIT |
| 10 | + |
| 11 | +cat >"$temporary/manifests.yaml" <<YAML |
| 12 | +apiVersion: v1 |
| 13 | +kind: Namespace |
| 14 | +metadata: |
| 15 | + name: monitoring |
| 16 | +--- |
| 17 | +apiVersion: v1 |
| 18 | +kind: Secret |
| 19 | +metadata: |
| 20 | + name: bw-auth-token |
| 21 | + namespace: monitoring |
| 22 | +type: Opaque |
| 23 | +stringData: |
| 24 | + token: "${BWS_ACCESS_TOKEN}" |
| 25 | +YAML |
| 26 | + |
| 27 | +python3 - "$temporary/manifests.yaml" "$temporary/configpatch.yaml" "$CLUSTER" <<'PY' |
| 28 | +import sys |
| 29 | +from pathlib import Path |
| 30 | +
|
| 31 | +import yaml |
| 32 | +
|
| 33 | +
|
| 34 | +class Literal(str): |
| 35 | + pass |
| 36 | +
|
| 37 | +
|
| 38 | +def represent_literal(dumper, data): |
| 39 | + return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|") |
| 40 | +
|
| 41 | +
|
| 42 | +yaml.SafeDumper.add_representer(Literal, represent_literal) |
| 43 | +
|
| 44 | +manifests_path = Path(sys.argv[1]) |
| 45 | +configpatch_path = Path(sys.argv[2]) |
| 46 | +cluster = sys.argv[3] |
| 47 | +manifests = manifests_path.read_text() |
| 48 | +list(yaml.safe_load_all(manifests)) |
| 49 | +
|
| 50 | +patch = { |
| 51 | + "cluster": { |
| 52 | + "inlineManifests": [ |
| 53 | + {"name": "bitwarden-auth-token", "contents": Literal(manifests)} |
| 54 | + ] |
| 55 | + } |
| 56 | +} |
| 57 | +configpatch = { |
| 58 | + "metadata": { |
| 59 | + "namespace": "default", |
| 60 | + "type": "ConfigPatches.omni.sidero.dev", |
| 61 | + "id": f"510-cluster-{cluster}-bitwarden-auth-token", |
| 62 | + "labels": {"omni.sidero.dev/cluster": cluster}, |
| 63 | + }, |
| 64 | + "spec": {"data": Literal(yaml.safe_dump(patch, sort_keys=False))}, |
| 65 | +} |
| 66 | +configpatch_path.write_text(yaml.safe_dump(configpatch, sort_keys=False)) |
| 67 | +print("manifests: valid") |
| 68 | +PY |
| 69 | + |
| 70 | +if [[ "${1:-}" == "--apply" ]]; then |
| 71 | + omnictl apply -f "$temporary/configpatch.yaml" |
| 72 | + printf 'Bitwarden auth-token ConfigPatch applied to Omni.\n' |
| 73 | +else |
| 74 | + printf 'Bitwarden auth-token ConfigPatch rendered and validated; re-run with --apply to push to Omni.\n' |
| 75 | +fi |
0 commit comments