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
|
|
|
|
|
|
|
from psycopg2 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
|
|
|
|
if CONFIG.y("secret_key") is None or len(CONFIG.y("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(
|
|
|
|
dbname=CONFIG.y("postgresql.name"),
|
|
|
|
user=CONFIG.y("postgresql.user"),
|
|
|
|
password=CONFIG.y("postgresql.password"),
|
|
|
|
host=CONFIG.y("postgresql.host"),
|
2021-06-09 08:59:48 +00:00
|
|
|
port=int(CONFIG.y("postgresql.port")),
|
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://"
|
|
|
|
if CONFIG.y_bool("redis.tls", False):
|
|
|
|
REDIS_PROTOCOL_PREFIX = "rediss://"
|
2021-11-25 12:44:42 +00:00
|
|
|
REDIS_URL = (
|
|
|
|
f"{REDIS_PROTOCOL_PREFIX}:"
|
2022-03-31 16:37:54 +00:00
|
|
|
f"{quote_plus(CONFIG.y('redis.password'))}@{quote_plus(CONFIG.y('redis.host'))}:"
|
2022-11-15 13:31:29 +00:00
|
|
|
f"{int(CONFIG.y('redis.port'))}/{CONFIG.y('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")
|