2020-05-16 21:14:26 +01: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."""
|
2020-09-20 19:31:02 +01:00
|
|
|
from json import dumps
|
|
|
|
from sys import stderr
|
2020-11-16 00:41:33 +00:00
|
|
|
from time import sleep, time
|
2020-05-16 21:56:14 +01:00
|
|
|
|
|
|
|
from psycopg2 import OperationalError, connect
|
2020-05-16 21:14:26 +01:00
|
|
|
from redis import Redis
|
|
|
|
from redis.exceptions import RedisError
|
2020-05-16 21:56:14 +01:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.lib.config import CONFIG
|
2020-05-16 21:14:26 +01:00
|
|
|
|
2020-09-20 19:31:02 +01:00
|
|
|
|
|
|
|
def j_print(event: str, log_level: str = "info", **kwargs):
|
|
|
|
"""Print event in the same format as structlog with JSON.
|
|
|
|
Used before structlog is configured."""
|
|
|
|
data = {
|
|
|
|
"event": event,
|
|
|
|
"level": log_level,
|
|
|
|
"logger": __name__,
|
2020-11-16 00:41:33 +00:00
|
|
|
"timestamp": time(),
|
2020-09-20 19:31:02 +01:00
|
|
|
}
|
|
|
|
data.update(**kwargs)
|
|
|
|
print(dumps(data), file=stderr)
|
|
|
|
|
2020-05-16 21:14:26 +01: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:
|
|
|
|
j_print("Secret key missing, check https://goauthentik.io/docs/installation/.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
2020-05-16 21:14:26 +01: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 09:59:48 +01:00
|
|
|
port=int(CONFIG.y("postgresql.port")),
|
2020-05-16 21:14:26 +01:00
|
|
|
)
|
|
|
|
conn.cursor()
|
|
|
|
break
|
2021-05-05 19:15:01 +01:00
|
|
|
except OperationalError as exc:
|
2020-05-16 21:14:26 +01:00
|
|
|
sleep(1)
|
2021-05-05 19:15:01 +01:00
|
|
|
j_print(f"PostgreSQL Connection failed, retrying... ({exc})")
|
2020-05-16 21:14:26 +01:00
|
|
|
|
2021-07-15 10:48:52 +01: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}:"
|
|
|
|
f"{CONFIG.y('redis.password')}@{CONFIG.y('redis.host')}:"
|
|
|
|
f"{int(CONFIG.y('redis.port'))}/{CONFIG.y('redis.ws_db')}"
|
|
|
|
)
|
2020-05-16 21:14:26 +01:00
|
|
|
while True:
|
|
|
|
try:
|
2021-11-25 12:44:42 +00:00
|
|
|
redis = Redis.from_url(REDIS_URL)
|
2020-05-16 21:14:26 +01:00
|
|
|
redis.ping()
|
|
|
|
break
|
2021-05-05 19:15:01 +01:00
|
|
|
except RedisError as exc:
|
2020-05-16 21:14:26 +01:00
|
|
|
sleep(1)
|
2021-11-25 12:44:42 +00:00
|
|
|
j_print(f"Redis Connection failed, retrying... ({exc})", redis_url=REDIS_URL)
|