forked from mtusi2026/mtusi2026lab7
51 lines
1.2 KiB
Bash
Executable File
51 lines
1.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
. "$script_dir/openbao-common.sh"
|
|
|
|
require_command "$BAO_BIN"
|
|
require_command python3
|
|
require_file "$BAO_CACERT"
|
|
require_private_file "$openbao_operator_file"
|
|
|
|
mkdir -p "$openbao_runtime_dir"
|
|
keys_file=$(mktemp "$openbao_runtime_dir/.unseal.XXXXXX")
|
|
status_file=$(mktemp "$openbao_runtime_dir/.status.XXXXXX")
|
|
chmod 0600 "$keys_file" "$status_file"
|
|
|
|
cleanup() {
|
|
rm -f "$keys_file" "$status_file"
|
|
}
|
|
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
openbao_status "$status_file"
|
|
|
|
if [ "$(json_field "$status_file" sealed)" = "false" ]; then
|
|
echo "OpenBao is already unsealed"
|
|
exit 0
|
|
fi
|
|
|
|
python3 - "$openbao_operator_file" >"$keys_file" <<'PY'
|
|
import json
|
|
import sys
|
|
|
|
with open(sys.argv[1], encoding="utf-8") as source:
|
|
material = json.load(source)
|
|
|
|
threshold = material["unseal_threshold"]
|
|
for key in material["unseal_keys_b64"][:threshold]:
|
|
print(key)
|
|
PY
|
|
|
|
while IFS= read -r key; do
|
|
"$BAO_BIN" operator unseal "$key" </dev/null >/dev/null
|
|
done <"$keys_file"
|
|
|
|
openbao_status "$status_file"
|
|
[ "$(json_field "$status_file" sealed)" = "false" ] || fail "OpenBao remains sealed"
|
|
|
|
echo "OpenBao unsealed"
|