forked from mtusi2026/mtusi2026lab7
88 lines
2.3 KiB
Bash
Executable File
88 lines
2.3 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
. "$script_dir/openbao-common.sh"
|
|
|
|
if [ "$#" -ne 2 ]; then
|
|
echo "usage: $0 <api|worker> <runtime-output-directory>" >&2
|
|
exit 2
|
|
fi
|
|
|
|
service=$1
|
|
output_dir=$2
|
|
|
|
case "$service" in
|
|
api|worker) ;;
|
|
*) fail "service must be api or worker" ;;
|
|
esac
|
|
|
|
require_runtime_path "$output_dir"
|
|
require_command "$BAO_BIN"
|
|
require_command python3
|
|
require_unsealed
|
|
|
|
if [ -z "${BAO_TOKEN:-}" ]; then
|
|
load_operator_token
|
|
fi
|
|
|
|
role="messenger-$service"
|
|
parent_dir=$(dirname "$output_dir")
|
|
staging_dir="$parent_dir/.${service}.$$"
|
|
response_file="$staging_dir/response.json"
|
|
|
|
cleanup() {
|
|
rm -rf "$staging_dir"
|
|
}
|
|
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
mkdir -p "$parent_dir" "$staging_dir"
|
|
chmod 0700 "$parent_dir" "$staging_dir"
|
|
|
|
"$BAO_BIN" read -field=role_id "auth/approle/role/$role/role-id" >"$staging_dir/role-id"
|
|
"$BAO_BIN" write -f -format=json "auth/approle/role/$role/secret-id" >"$response_file"
|
|
|
|
python3 - "$response_file" "$staging_dir" <<'PY'
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
with open(sys.argv[1], encoding="utf-8") as source:
|
|
data = json.load(source)["data"]
|
|
|
|
for name, field in (("secret-id", "secret_id"), ("secret-id-accessor", "secret_id_accessor")):
|
|
path = os.path.join(sys.argv[2], name)
|
|
with open(path, "w", encoding="utf-8") as destination:
|
|
destination.write(data[field] + "\n")
|
|
os.chmod(path, 0o600)
|
|
PY
|
|
|
|
rm -f "$response_file"
|
|
chmod 0600 "$staging_dir/role-id"
|
|
|
|
old_accessor=
|
|
if [ -f "$output_dir/secret-id-accessor" ]; then
|
|
old_accessor=$(cat "$output_dir/secret-id-accessor")
|
|
fi
|
|
|
|
mkdir -p "$output_dir"
|
|
chmod 0700 "$output_dir"
|
|
for file in role-id secret-id secret-id-accessor; do
|
|
mv "$staging_dir/$file" "$output_dir/$file"
|
|
done
|
|
|
|
if [ -n "$old_accessor" ]; then
|
|
"$BAO_BIN" write "auth/approle/role/$role/secret-id-accessor/destroy" \
|
|
secret_id_accessor="$old_accessor" >/dev/null 2>&1 || true
|
|
fi
|
|
|
|
if [ "$output_dir" = "$openbao_runtime_dir/approle/$service" ]; then
|
|
"$script_dir/sync-docker-workspace.sh"
|
|
docker compose --project-directory "$repo_dir" --env-file "${ENV_FILE:-$repo_dir/lab7.env.example}" \
|
|
run --rm --no-deps -e SERVICE="$service" openbao-identity-install
|
|
fi
|
|
|
|
echo "issued short-lived AppRole credentials for $service in ${output_dir#"$repo_dir/"}"
|