tuxflotte-installer/scripts/modules/12_enrollment_auth.sh

72 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
readonly SCRIPT_NAME="${0##*/}"
readonly RUNTIME_DIR="/run/tuxflotte/enrollment"
readonly AUTHORIZATION_FILE="${RUNTIME_DIR}/authorization.json"
log() {
printf '[%s] %s\n' "${SCRIPT_NAME}" "$*" >&2
}
fatal() {
printf '[%s] FEHLER: %s\n' "${SCRIPT_NAME}" "$*" >&2
exit 1
}
require_root() {
if [[ "${EUID}" -ne 0 ]]; then
fatal "Das Enrollment-Autorisierungsmodul muss als root ausgeführt werden."
fi
}
prepare_runtime_directory() {
install -d \
--mode=0700 \
--owner=root \
--group=root \
"${RUNTIME_DIR}"
rm -f -- "${AUTHORIZATION_FILE}"
}
store_bootstrap_authorization() {
local activation_code
activation_code="${TUXFLOTTE_ACTIVATION_CODE:-LAB-2026-START}"
jq \
--null-input \
--arg activation_code "${activation_code}" \
'{
schema_version: 1,
authorization_type: "bootstrap_activation_code",
activation_code: $activation_code
}' >"${AUTHORIZATION_FILE}"
chmod 0600 "${AUTHORIZATION_FILE}"
}
validate_authorization() {
jq --exit-status '
.schema_version == 1
and .authorization_type == "bootstrap_activation_code"
and (.activation_code | type == "string")
and (.activation_code | length > 0)
' "${AUTHORIZATION_FILE}" >/dev/null ||
fatal "Enrollment-Autorisierung ist ungültig."
}
main() {
require_root
prepare_runtime_directory
store_bootstrap_authorization
validate_authorization
log "Temporäre Bootstrap-Autorisierung wurde vorbereitet."
}
main "$@"