From d8fe1014f37da99cde22926399566028d95d5b2f Mon Sep 17 00:00:00 2001
From: Julian Rother <julian@cccv.de>
Date: Sat, 4 Dec 2021 16:59:11 +0100
Subject: [PATCH] Fix config loading for Debian package

CONFIG_FILENAME in uwsgi.ini was copy-pasted from uffd without adding
support for it in uffd-nginxauth. However, the way it is used for uffd
does not work anyway, since it is appended to the app's instance_path
(it sets the filename not the path!). For some reason linking the config
file to "config.cfg" in the instance folder, does not work either.

This change introduces the environment variable CONFIG_PATH. If set, it's
value is used as is for reading the config file.
---
 app.py       | 8 +++++---
 debian/links | 1 -
 uwsgi.ini    | 2 +-
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/app.py b/app.py
index 52e1e2c..366e516 100644
--- a/app.py
+++ b/app.py
@@ -10,10 +10,12 @@ def create_app(test_config=None):
 	app = Flask(__name__)
 	app.config['SECRET_KEY'] = secrets.token_hex(128)
 	app.config.from_pyfile('default_config.py')
-	if not test_config:
-		app.config.from_pyfile('config.py', silent=True)
-	else:
+	if test_config:
 		app.config.from_mapping(test_config)
+	elif os.environ.get('CONFIG_PATH'):
+		app.config.from_pyfile(os.environ['CONFIG_PATH'], silent=True)
+	else:
+		app.config.from_pyfile('config.py', silent=True)
 	# OAuth2Session.fetch_token verifies that the passed URIs scheme (the scheme
 	# of request.url) is HTTPS. The way we deploy this app, request.url does not
 	# reflect the actual request url, so we disable this check.
diff --git a/debian/links b/debian/links
index 78050aa..56e2fe9 100644
--- a/debian/links
+++ b/debian/links
@@ -1,3 +1,2 @@
-/etc/uffd-nginxauth/uffd-nginxauth.cfg /usr/share/uffd-nginxauth/instance/config.cfg
 /etc/uffd-nginxauth/uwsgi.ini /etc/uwsgi/apps-available/uffd-nginxauth.ini
 /etc/uwsgi/apps-available/uffd-nginxauth.ini /etc/uwsgi/apps-enabled/uffd-nginxauth.ini
diff --git a/uwsgi.ini b/uwsgi.ini
index 971017a..8dabc69 100644
--- a/uwsgi.ini
+++ b/uwsgi.ini
@@ -11,6 +11,6 @@ vacuum = true
 env = PYTHONIOENCODING=UTF-8
 env = LANG=en_GB.utf8
 env = TZ=Europe/Berlin
-env = CONFIG_FILENAME=/etc/uffd-nginxauth/uffd-nginxauth.cfg
+env = CONFIG_PATH=/etc/uffd-nginxauth/uffd-nginxauth.cfg
 chdir = /usr/share/uffd-nginxauth
 module = app:create_app()
-- 
GitLab