2020-05-16 20:14:26 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""This file needs to be run from the root of the project to correctly
|
2020-12-05 21:08:42 +00:00
|
|
|
import authentik. This is done by the dockerfile."""
|
2022-01-19 18:15:57 +00:00
|
|
|
from sys import exit as sysexit
|
2022-09-10 11:24:31 +00:00
|
|
|
from time import sleep
|
2022-03-31 16:37:54 +00:00
|
|
|
from urllib.parse import quote_plus
|
2020-05-16 20:56:14 +00:00
|
|
|
|
2023-08-01 17:30:28 +00:00
|
|
|
from psycopg import OperationalError, connect
|
2020-05-16 20:14:26 +00:00
|
|
|
from redis import Redis
|
|
|
|
from redis.exceptions import RedisError
|
2020-05-16 20:56:14 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.lib.config import CONFIG
|
2020-05-16 20:14:26 +00:00
|
|
|
|
2022-09-10 11:24:31 +00:00
|
|
|
CONFIG.log("info", "Starting authentik bootstrap")
|
2022-01-26 19:45:45 +00:00
|
|
|
|
2022-01-19 08:42:46 +00:00
|
|
|
# Sanity check, ensure SECRET_KEY is set before we even check for database connectivity
|
2023-07-19 21:13:22 +00:00
|
|
|
if CONFIG.get("secret_key") is None or len(CONFIG.get("secret_key")) == 0:
|
2022-09-10 11:24:31 +00:00
|
|
|
CONFIG.log("info", "----------------------------------------------------------------------")
|
|
|
|
CONFIG.log("info", "Secret key missing, check https://goauthentik.io/docs/installation/.")
|
|
|
|
CONFIG.log("info", "----------------------------------------------------------------------")
|
2022-01-19 18:15:57 +00:00
|
|
|
sysexit(1)
|
2022-01-19 08:42:46 +00:00
|
|
|
|
|
|
|
|
2020-05-16 20:14:26 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
conn = connect(
|
2023-07-19 21:13:22 +00:00
|
|
|
dbname=CONFIG.get("postgresql.name"),
|
|
|
|
user=CONFIG.get("postgresql.user"),
|
|
|
|
password=CONFIG.get("postgresql.password"),
|
|
|
|
host=CONFIG.get("postgresql.host"),
|
2023-07-31 17:34:59 +00:00
|
|
|
port=CONFIG.get_int("postgresql.port"),
|
2023-07-19 21:13:22 +00:00
|
|
|
sslmode=CONFIG.get("postgresql.sslmode"),
|
|
|
|
sslrootcert=CONFIG.get("postgresql.sslrootcert"),
|
|
|
|
sslcert=CONFIG.get("postgresql.sslcert"),
|
|
|
|
sslkey=CONFIG.get("postgresql.sslkey"),
|
2020-05-16 20:14:26 +00:00
|
|
|
)
|
|
|
|
conn.cursor()
|
|
|
|
break
|
2021-05-05 18:15:01 +00:00
|
|
|
except OperationalError as exc:
|
2020-05-16 20:14:26 +00:00
|
|
|
sleep(1)
|
2022-09-10 11:24:31 +00:00
|
|
|
CONFIG.log("info", f"PostgreSQL connection failed, retrying... ({exc})")
|
2022-11-12 15:50:01 +00:00
|
|
|
CONFIG.log("info", "PostgreSQL connection successful")
|
2020-05-16 20:14:26 +00:00
|
|
|
|
2021-07-15 09:48:52 +00:00
|
|
|
REDIS_PROTOCOL_PREFIX = "redis://"
|
2023-07-19 21:13:22 +00:00
|
|
|
if CONFIG.get_bool("redis.tls", False):
|
2021-07-15 09:48:52 +00:00
|
|
|
REDIS_PROTOCOL_PREFIX = "rediss://"
|
2021-11-25 12:44:42 +00:00
|
|
|
REDIS_URL = (
|
|
|
|
f"{REDIS_PROTOCOL_PREFIX}:"
|
2023-07-19 21:13:22 +00:00
|
|
|
f"{quote_plus(CONFIG.get('redis.password'))}@{quote_plus(CONFIG.get('redis.host'))}:"
|
2023-07-31 17:34:59 +00:00
|
|
|
f"{CONFIG.get_int('redis.port')}/{CONFIG.get('redis.db')}"
|
2021-11-25 12:44:42 +00:00
|
|
|
)
|
2020-05-16 20:14:26 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2021-11-25 12:44:42 +00:00
|
|
|
redis = Redis.from_url(REDIS_URL)
|
2020-05-16 20:14:26 +00:00
|
|
|
redis.ping()
|
|
|
|
break
|
2021-05-05 18:15:01 +00:00
|
|
|
except RedisError as exc:
|
2020-05-16 20:14:26 +00:00
|
|
|
sleep(1)
|
2022-09-10 11:24:31 +00:00
|
|
|
CONFIG.log("info", f"Redis Connection failed, retrying... ({exc})", redis_url=REDIS_URL)
|
2022-11-12 15:50:01 +00:00
|
|
|
CONFIG.log("info", "Redis Connection successful")
|
2022-01-26 19:45:45 +00:00
|
|
|
|
2022-09-10 11:24:31 +00:00
|
|
|
CONFIG.log("info", "Finished authentik bootstrap")
|