root: re-use custom log helper from config and cleanup duplicate functions
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
aabb8af486
commit
28835fbca7
|
@ -62,7 +62,7 @@ class ConfigLoader:
|
||||||
self.update_from_file(env_file)
|
self.update_from_file(env_file)
|
||||||
self.update_from_env()
|
self.update_from_env()
|
||||||
|
|
||||||
def _log(self, level: str, message: str, **kwargs):
|
def log(self, level: str, message: str, **kwargs):
|
||||||
"""Custom Log method, we want to ensure ConfigLoader always logs JSON even when
|
"""Custom Log method, we want to ensure ConfigLoader always logs JSON even when
|
||||||
'structlog' or 'logging' hasn't been configured yet."""
|
'structlog' or 'logging' hasn't been configured yet."""
|
||||||
output = {
|
output = {
|
||||||
|
@ -95,7 +95,7 @@ class ConfigLoader:
|
||||||
with open(url.path, "r", encoding="utf8") as _file:
|
with open(url.path, "r", encoding="utf8") as _file:
|
||||||
value = _file.read()
|
value = _file.read()
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
self._log("error", f"Failed to read config value from {url.path}: {exc}")
|
self.log("error", f"Failed to read config value from {url.path}: {exc}")
|
||||||
value = url.query
|
value = url.query
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -105,12 +105,12 @@ class ConfigLoader:
|
||||||
with open(path, encoding="utf8") as file:
|
with open(path, encoding="utf8") as file:
|
||||||
try:
|
try:
|
||||||
self.update(self.__config, yaml.safe_load(file))
|
self.update(self.__config, yaml.safe_load(file))
|
||||||
self._log("debug", "Loaded config", file=path)
|
self.log("debug", "Loaded config", file=path)
|
||||||
self.loaded_file.append(path)
|
self.loaded_file.append(path)
|
||||||
except yaml.YAMLError as exc:
|
except yaml.YAMLError as exc:
|
||||||
raise ImproperlyConfigured from exc
|
raise ImproperlyConfigured from exc
|
||||||
except PermissionError as exc:
|
except PermissionError as exc:
|
||||||
self._log(
|
self.log(
|
||||||
"warning",
|
"warning",
|
||||||
"Permission denied while reading file",
|
"Permission denied while reading file",
|
||||||
path=path,
|
path=path,
|
||||||
|
@ -144,7 +144,7 @@ class ConfigLoader:
|
||||||
current_obj[dot_parts[-1]] = value
|
current_obj[dot_parts[-1]] = value
|
||||||
idx += 1
|
idx += 1
|
||||||
if idx > 0:
|
if idx > 0:
|
||||||
self._log("debug", "Loaded environment variables", count=idx)
|
self.log("debug", "Loaded environment variables", count=idx)
|
||||||
self.update(self.__config, outer)
|
self.update(self.__config, outer)
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
|
|
@ -3,10 +3,7 @@
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
from hashlib import sha512
|
from hashlib import sha512
|
||||||
from json import dumps
|
|
||||||
from time import time
|
|
||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
|
|
||||||
import structlog
|
import structlog
|
||||||
|
@ -20,20 +17,6 @@ from authentik.lib.sentry import sentry_init
|
||||||
from authentik.lib.utils.reflection import get_env
|
from authentik.lib.utils.reflection import get_env
|
||||||
from authentik.stages.password import BACKEND_APP_PASSWORD, BACKEND_INBUILT, BACKEND_LDAP
|
from authentik.stages.password import BACKEND_APP_PASSWORD, BACKEND_INBUILT, BACKEND_LDAP
|
||||||
|
|
||||||
|
|
||||||
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__,
|
|
||||||
"timestamp": time(),
|
|
||||||
}
|
|
||||||
data.update(**kwargs)
|
|
||||||
print(dumps(data), file=sys.stderr)
|
|
||||||
|
|
||||||
|
|
||||||
LOGGER = structlog.get_logger()
|
LOGGER = structlog.get_logger()
|
||||||
|
|
||||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||||
|
@ -484,4 +467,4 @@ if DEBUG:
|
||||||
|
|
||||||
INSTALLED_APPS.append("authentik.core")
|
INSTALLED_APPS.append("authentik.core")
|
||||||
|
|
||||||
j_print("Booting authentik", version=__version__)
|
CONFIG.log("info", "Booting authentik", version=__version__)
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
"""This file needs to be run from the root of the project to correctly
|
"""This file needs to be run from the root of the project to correctly
|
||||||
import authentik. This is done by the dockerfile."""
|
import authentik. This is done by the dockerfile."""
|
||||||
from json import dumps
|
|
||||||
from sys import exit as sysexit
|
from sys import exit as sysexit
|
||||||
from sys import stderr
|
from time import sleep
|
||||||
from time import sleep, time
|
|
||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
|
|
||||||
from psycopg2 import OperationalError, connect
|
from psycopg2 import OperationalError, connect
|
||||||
|
@ -13,27 +11,13 @@ from redis.exceptions import RedisError
|
||||||
|
|
||||||
from authentik.lib.config import CONFIG
|
from authentik.lib.config import CONFIG
|
||||||
|
|
||||||
|
CONFIG.log("info", "Starting authentik bootstrap")
|
||||||
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__,
|
|
||||||
"timestamp": time(),
|
|
||||||
}
|
|
||||||
data.update(**kwargs)
|
|
||||||
print(dumps(data), file=stderr)
|
|
||||||
|
|
||||||
|
|
||||||
j_print("Starting authentik bootstrap")
|
|
||||||
|
|
||||||
# Sanity check, ensure SECRET_KEY is set before we even check for database connectivity
|
# 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:
|
if CONFIG.y("secret_key") is None or len(CONFIG.y("secret_key")) == 0:
|
||||||
j_print("----------------------------------------------------------------------")
|
CONFIG.log("info", "----------------------------------------------------------------------")
|
||||||
j_print("Secret key missing, check https://goauthentik.io/docs/installation/.")
|
CONFIG.log("info", "Secret key missing, check https://goauthentik.io/docs/installation/.")
|
||||||
j_print("----------------------------------------------------------------------")
|
CONFIG.log("info", "----------------------------------------------------------------------")
|
||||||
sysexit(1)
|
sysexit(1)
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,8 +34,8 @@ while True:
|
||||||
break
|
break
|
||||||
except OperationalError as exc:
|
except OperationalError as exc:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
j_print(f"PostgreSQL connection failed, retrying... ({exc})")
|
CONFIG.log("info", f"PostgreSQL connection failed, retrying... ({exc})")
|
||||||
j_print("PostgreSQL connection successful")
|
CONFIG.log("info", "PostgreSQL connection successful")
|
||||||
|
|
||||||
REDIS_PROTOCOL_PREFIX = "redis://"
|
REDIS_PROTOCOL_PREFIX = "redis://"
|
||||||
if CONFIG.y_bool("redis.tls", False):
|
if CONFIG.y_bool("redis.tls", False):
|
||||||
|
@ -68,7 +52,7 @@ while True:
|
||||||
break
|
break
|
||||||
except RedisError as exc:
|
except RedisError as exc:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
j_print(f"Redis Connection failed, retrying... ({exc})", redis_url=REDIS_URL)
|
CONFIG.log("info", f"Redis Connection failed, retrying... ({exc})", redis_url=REDIS_URL)
|
||||||
j_print("Redis Connection successful")
|
CONFIG.log("info", "Redis Connection successful")
|
||||||
|
|
||||||
j_print("Finished authentik bootstrap")
|
CONFIG.log("info", "Finished authentik bootstrap")
|
||||||
|
|
Reference in a new issue