2023-11-06 11:46:14 +00:00
|
|
|
#!/usr/bin/env -S bash -e
|
2023-03-30 12:04:38 +00:00
|
|
|
MODE_FILE="${TMPDIR}/authentik-mode"
|
|
|
|
|
2021-11-19 23:32:24 +00:00
|
|
|
function log {
|
|
|
|
printf '{"event": "%s", "level": "info", "logger": "bootstrap"}\n' "$@" > /dev/stderr
|
|
|
|
}
|
|
|
|
|
2021-11-25 12:38:31 +00:00
|
|
|
function wait_for_db {
|
|
|
|
python -m lifecycle.wait_for_db
|
2023-03-30 12:04:38 +00:00
|
|
|
python -m lifecycle.migrate
|
2021-11-25 12:38:31 +00:00
|
|
|
log "Bootstrap completed"
|
|
|
|
}
|
2021-05-13 18:11:49 +00:00
|
|
|
|
|
|
|
function check_if_root {
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
2021-11-19 23:32:24 +00:00
|
|
|
log "Not running as root, disabling permission fixes"
|
2023-04-14 11:52:11 +00:00
|
|
|
exec $1
|
2021-05-13 18:11:49 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
SOCKET="/var/run/docker.sock"
|
2021-06-09 13:56:12 +00:00
|
|
|
GROUP="authentik"
|
2021-05-13 18:11:49 +00:00
|
|
|
if [[ -e "$SOCKET" ]]; then
|
|
|
|
# Get group ID of the docker socket, so we can create a matching group and
|
|
|
|
# add ourselves to it
|
|
|
|
DOCKER_GID=$(stat -c '%g' $SOCKET)
|
2021-09-09 13:24:35 +00:00
|
|
|
# Ensure group for the id exists
|
2021-05-13 22:50:20 +00:00
|
|
|
getent group $DOCKER_GID || groupadd -f -g $DOCKER_GID docker
|
2021-05-13 18:11:49 +00:00
|
|
|
usermod -a -G $DOCKER_GID authentik
|
2021-09-09 13:24:35 +00:00
|
|
|
# since the name of the group might not be docker, we need to lookup the group id
|
2021-12-04 19:06:02 +00:00
|
|
|
GROUP_NAME=$(getent group $DOCKER_GID | sed 's/:/\n/g' | head -1)
|
2021-09-09 13:24:35 +00:00
|
|
|
GROUP="authentik:${GROUP_NAME}"
|
2021-05-13 18:11:49 +00:00
|
|
|
fi
|
2022-10-19 18:24:28 +00:00
|
|
|
# Fix permissions of certs and media
|
2022-03-09 20:56:18 +00:00
|
|
|
chown -R authentik:authentik /media /certs
|
2022-10-19 18:24:28 +00:00
|
|
|
chmod ug+rwx /media
|
|
|
|
chmod ug+rx /certs
|
2023-04-14 11:52:11 +00:00
|
|
|
exec chpst -u authentik:$GROUP env HOME=/authentik $1
|
2021-05-13 18:11:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 16:46:49 +00:00
|
|
|
function run_authentik {
|
|
|
|
if [[ -x "$(command -v authentik)" ]]; then
|
|
|
|
exec authentik $@
|
|
|
|
else
|
|
|
|
exec go run -v ./cmd/server/ $@
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-30 12:04:38 +00:00
|
|
|
function set_mode {
|
|
|
|
echo $1 > $MODE_FILE
|
|
|
|
trap cleanup EXIT
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup {
|
|
|
|
rm -f ${MODE_FILE}
|
|
|
|
}
|
2021-10-05 18:45:18 +00:00
|
|
|
|
2023-06-28 14:44:50 +00:00
|
|
|
function prepare_debug {
|
2023-09-11 11:36:54 +00:00
|
|
|
poetry install --no-ansi --no-interaction
|
2023-06-28 14:44:50 +00:00
|
|
|
touch /unittest.xml
|
|
|
|
chown authentik:authentik /unittest.xml
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ "${AUTHENTIK_REMOTE_DEBUG}" == "true" ]]; then
|
|
|
|
prepare_debug
|
|
|
|
fi
|
|
|
|
|
2020-09-09 22:14:59 +00:00
|
|
|
if [[ "$1" == "server" ]]; then
|
2021-11-25 12:38:31 +00:00
|
|
|
wait_for_db
|
2023-03-30 12:04:38 +00:00
|
|
|
set_mode "server"
|
|
|
|
# If we have bootstrap credentials set, run bootstrap tasks outside of main server
|
|
|
|
# sync, so that we can sure the first start actually has working bootstrap
|
|
|
|
# credentials
|
2022-06-09 18:09:51 +00:00
|
|
|
if [[ ! -z "${AUTHENTIK_BOOTSTRAP_PASSWORD}" || ! -z "${AUTHENTIK_BOOTSTRAP_TOKEN}" ]]; then
|
|
|
|
python -m manage bootstrap_tasks
|
|
|
|
fi
|
2023-04-20 16:46:49 +00:00
|
|
|
run_authentik
|
2020-09-09 22:14:59 +00:00
|
|
|
elif [[ "$1" == "worker" ]]; then
|
2022-01-24 20:51:57 +00:00
|
|
|
wait_for_db
|
2023-03-30 12:04:38 +00:00
|
|
|
set_mode "worker"
|
2023-06-28 14:44:50 +00:00
|
|
|
check_if_root "python -m manage worker"
|
2022-08-18 18:13:30 +00:00
|
|
|
elif [[ "$1" == "worker-status" ]]; then
|
|
|
|
wait_for_db
|
|
|
|
celery -A authentik.root.celery flower \
|
|
|
|
--port=9000
|
2020-10-03 18:36:36 +00:00
|
|
|
elif [[ "$1" == "bash" ]]; then
|
|
|
|
/bin/bash
|
2022-08-05 06:39:00 +00:00
|
|
|
elif [[ "$1" == "test-all" ]]; then
|
2023-06-28 14:44:50 +00:00
|
|
|
prepare_debug
|
2021-06-09 13:50:32 +00:00
|
|
|
check_if_root "python -m manage test authentik"
|
2021-10-05 18:45:18 +00:00
|
|
|
elif [[ "$1" == "healthcheck" ]]; then
|
2023-04-20 16:46:49 +00:00
|
|
|
run_authentik healthcheck $(cat $MODE_FILE)
|
2021-11-25 12:38:31 +00:00
|
|
|
elif [[ "$1" == "dump_config" ]]; then
|
2022-07-30 20:41:29 +00:00
|
|
|
exec python -m authentik.lib.config
|
|
|
|
elif [[ "$1" == "debug" ]]; then
|
|
|
|
exec sleep infinity
|
2020-09-09 22:14:59 +00:00
|
|
|
else
|
2022-07-30 20:41:29 +00:00
|
|
|
exec python -m manage "$@"
|
2020-09-09 22:14:59 +00:00
|
|
|
fi
|