Skip to content

Commit 01e50f5

Browse files
committed
Configure with environment variables
1 parent 4a53f4e commit 01e50f5

File tree

5 files changed

+71
-123
lines changed

5 files changed

+71
-123
lines changed

admin_site/os2borgerpc_admin/settings.py

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,18 @@
1515
# Our customized user profile.
1616
AUTH_PROFILE_MODULE = "account.UserProfile"
1717

18-
config = configparser.ConfigParser()
19-
config["settings"] = {}
20-
21-
# We load settings from a file. The fallback values in this
22-
# `settings.py` is overwritten by the values defined in the file
23-
# the env var `BPC_USER_CONFIG_PATH` points to.
24-
25-
# The `BPC_USER_CONFIG_PATH` file is for settings that should generally
26-
# be unique to an instance deployment.
27-
28-
path = os.getenv("BPC_USER_CONFIG_PATH", None)
29-
if path:
30-
try:
31-
with open(path) as fp:
32-
config.read_file(fp)
33-
logger.info("Loaded settings file BPC_USER_CONFIG_PATH from %s" % (path))
34-
except OSError as e:
35-
logger.error(
36-
"Loading settings file BPC_USER_CONFIG_PATH from %s failed with %s."
37-
% (path, e)
38-
)
39-
40-
# use settings section as default
41-
settings = config["settings"]
42-
43-
44-
DEBUG = settings.getboolean("DEBUG", False)
18+
DEBUG = os.getenv("DEBUG", 'false').lower() == 'true'
4519

4620
ADMINS = (
4721
[
48-
(settings.get("ADMIN_NAME"), settings["ADMIN_EMAIL"]),
22+
(os.environ.get("ADMIN_USERNAME"), os.environ["ADMIN_EMAIL"]),
4923
]
50-
if settings.get("ADMIN_EMAIL")
24+
if os.environ.get("ADMIN_EMAIL")
5125
else None
5226
)
5327

5428
MANAGERS = ADMINS
5529

56-
5730
# Template settings
5831
TEMPLATES = [
5932
{
@@ -87,7 +60,7 @@
8760
"USER": os.environ['DB_USER'],
8861
"PASSWORD": os.environ['DB_PASSWORD'],
8962
"HOST": os.environ['DB_HOST'],
90-
"PORT": os.environ['DB_PORT'],
63+
"PORT": os.getenv("DB_PORT", ""),
9164
"OPTIONS": {
9265
"connect_timeout": 2, # Minimum in 2
9366
},
@@ -96,16 +69,13 @@
9669

9770
# Hosts/domain names that are valid for this site; required if DEBUG is False
9871
# See https://docs.djangoproject.com/en/3.1/ref/settings/#allowed-hosts
99-
if settings.get("ALLOWED_HOSTS"):
100-
ALLOWED_HOSTS = settings.get("ALLOWED_HOSTS").split(",")
101-
else:
102-
ALLOWED_HOSTS = []
72+
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "").split(",")
10373

10474
# Django > 4.0 introduced changes related to CSRF. Note that the protocol has to be specified too.
10575
# https://docs.djangoproject.com/en/4.2/releases/4.0/#csrf
10676
# https://docs.djangoproject.com/en/4.2/ref/settings/#csrf-trusted-origins
107-
if settings.get("CSRF_TRUSTED_ORIGINS"):
108-
CSRF_TRUSTED_ORIGINS = settings.get("CSRF_TRUSTED_ORIGINS").split(",")
77+
if os.getenv("CSRF_TRUSTED_ORIGINS", ""):
78+
CSRF_TRUSTED_ORIGINS = os.getenv("CSRF_TRUSTED_ORIGINS", "").split(",")
10979
else:
11080
CSRF_TRUSTED_ORIGINS = []
11181

@@ -114,11 +84,11 @@
11484
# although not all choices may be available on all operating systems.
11585
# In a Windows environment this must be set to your system time zone.
11686
# Timezone/Language
117-
TIME_ZONE = settings["TIME_ZONE"]
87+
TIME_ZONE = os.environ["TIME_ZONE"]
11888

11989
# Language code for this installation. All choices can be found here:
12090
# http://www.i18nguy.com/unicode/language-identifiers.html
121-
LANGUAGE_CODE = settings["LANGUAGE_CODE"]
91+
LANGUAGE_CODE = os.environ["LANGUAGE_CODE"]
12292

12393
LOCALE_PATHS = [os.path.join(install_dir, "locale")]
12494

@@ -172,21 +142,21 @@
172142

173143

174144
# Storage setup
175-
if settings.get("GS_BUCKET_NAME"):
145+
if os.environ.get("GS_BUCKET_NAME"):
176146
# The Google Cloud Storage bucket name. For `django-storages[google]`
177147
# https://django-storages.readthedocs.io/en/latest/backends/gcloud.html
178148
# If it is set, we save all files to Google Cloud.
179149
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
180-
GS_BUCKET_NAME = settings.get("GS_BUCKET_NAME")
150+
GS_BUCKET_NAME = os.environ.get("GS_BUCKET_NAME")
181151
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
182-
settings.get("GS_CREDENTIALS_FILE")
152+
os.environ.get("GS_CREDENTIALS_FILE")
183153
)
184154
GS_QUERYSTRING_AUTH = False
185155
GS_FILE_OVERWRITE = False
186-
GS_CUSTOM_ENDPOINT = settings.get("GS_CUSTOM_ENDPOINT", None)
156+
GS_CUSTOM_ENDPOINT = os.environ.get("GS_CUSTOM_ENDPOINT", None)
187157

188158
# Make this unique, and don't share it with anybody.
189-
SECRET_KEY = settings["SECRET_KEY"]
159+
SECRET_KEY = os.environ["SECRET_KEY"]
190160

191161
MIDDLEWARE = (
192162
"django.middleware.security.SecurityMiddleware",
@@ -203,14 +173,14 @@
203173

204174
# Email settings
205175

206-
DEFAULT_FROM_EMAIL = settings.get("DEFAULT_FROM_EMAIL")
207-
ADMIN_EMAIL = settings.get("ADMIN_EMAIL")
208-
EMAIL_HOST = settings.get("EMAIL_HOST")
209-
EMAIL_PORT = settings.get("EMAIL_PORT")
210-
SERVER_EMAIL = settings.get("SERVER_EMAIL")
211-
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
212-
EMAIL_HOST_USER = settings.get("EMAIL_USER")
213-
EMAIL_HOST_PASSWORD = settings.get("EMAIL_PASSWORD")
176+
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
177+
ADMIN_EMAIL = os.environ.get("ADMIN_EMAIL")
178+
EMAIL_HOST = os.environ.get("EMAIL_HOST")
179+
EMAIL_PORT = os.environ.get("EMAIL_PORT")
180+
SERVER_EMAIL = os.environ.get("SERVER_EMAIL")
181+
EMAIL_BACKEND = "os.environ.core.mail.backends.smtp.EmailBackend"
182+
EMAIL_HOST_USER = os.environ.get("EMAIL_USER")
183+
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_PASSWORD")
214184

215185
ROOT_URLCONF = "os2borgerpc_admin.urls"
216186

@@ -300,11 +270,11 @@
300270
},
301271
"root": {
302272
"handlers": ["console", "mail_admins"],
303-
"level": settings.get("LOG_LEVEL", fallback="ERROR"),
273+
"level": os.getenv("LOG_LEVEL", "ERROR"),
304274
},
305275
}
306276

307-
INITIALIZE_DATABASE = settings.getboolean("INITIALIZE_DATABASE", False)
277+
INITIALIZE_DATABASE = os.getenv("INITIALIZE_DATABASE", 'false').lower() == 'true'
308278

309279
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
310280

@@ -313,12 +283,12 @@
313283
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
314284

315285
# Handler for citizen login.
316-
CITIZEN_LOGIN_API_VALIDATOR = settings.get(
286+
CITIZEN_LOGIN_API_VALIDATOR = os.environ.get(
317287
"CITIZEN_LOGIN_API_VALIDATOR", "system.utils.cicero_validate"
318288
)
319289

320290
# Cicero specific stuff.
321-
CICERO_URL = settings.get("CICERO_URL")
291+
CICERO_URL = os.environ.get("CICERO_URL")
322292

323293
# All Python Markdown's officially supported extensions can be added here without
324294
# any extra setup.

compose.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ services:
2323
DB_USER: bpc
2424
DB_PASSWORD: bpc
2525
DB_PORT: ""
26+
ALLOWED_HOSTS: "*"
27+
DEBUG: True
28+
SECRET_KEY: v3rys1kr3t
29+
# Admin contact - fill in your own name and email as desired.
30+
ADMIN_USERNAME: OS2borgerPC Admin
31+
ADMIN_EMAIL: [email protected]
32+
ADMIN_PASSWORD: admin
33+
# Timezone/Language
34+
TIME_ZONE: Europe/Copenhagen
35+
LANGUAGE_CODE: da-dk
36+
37+
INITIALIZE_DATABASE: True
38+
39+
LOG_LEVEL: INFO
40+
41+
# IF USING THE CICERO INTEGRATION
42+
CICERO_URL: CICERO_SERVER_HERE
43+
# This particular line makes it both skip the connection to the Cicero server AND the validation of the username/password
44+
# For that reason, depending on what you're testing, you might want to comment out this line
45+
CITIZEN_LOGIN_API_VALIDATOR: system.utils.always_validate_citizen
46+
2647
user: "${UID}:${GID}"
2748
build:
2849
context: .

dev-environment/dev-settings.ini

Lines changed: 0 additions & 27 deletions
This file was deleted.

docker/Dockerfile

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ LABEL org.opencontainers.image.title="OS2borgerPC" \
1919
org.opencontainers.image.source="https://github.com/OS2borgerPC/admin-site"
2020

2121
ENV PYTHONUNBUFFERED=1 \
22-
BPC_USER_CONFIG_PATH=/user-settings.ini \
2322
PYTHONPATH=/code/admin_site/:$PYTHONPATH\
2423
DJANGO_SETTINGS_MODULE=os2borgerpc_admin.settings
2524

@@ -79,19 +78,31 @@ COPY --from=frontend \
7978
/code/nodejs/node_modules/bootstrap-table/dist/locale/bootstrap-table-da-DK.min.js \
8079
/frontend/js/
8180

82-
# Unfortunately, `collectstatic` requires all settings to be set. We include a
83-
# set of insecure setting here for only this purpose. We make sure to delete it
84-
# afterward. If `insecure-settings.ini` is found in any production image,
85-
# consider it a bug. See `insecure-settings.ini` for a detailed explanation.
86-
ENV DB_NAME="dummy" \
87-
DB_USER="dummy" \
88-
DB_PASSWORD="dummy" \
89-
DB_HOST="dummy" \
90-
DB_PORT="dummy"
81+
82+
83+
84+
# WARNING: The below config is not used for anything other than `collectstatic`.
85+
86+
# It is unsafe to add insecure defaults anywhere inside the production-ready
87+
# docker image. They have a tendency to be used knowingly or unknowingly as
88+
# fallback values. Given a misconfiguration, like a typo, this could result in a
89+
# insecure production system. Normally all secrets and unsafe defaults should
90+
# be relegated to `docker compose` or similar.
91+
92+
# Unfortunately, Django requires all settings to run `collectstatic`. We include a
93+
# set of insecure setting here for only this purpose.
94+
# They are only set for the RUN statement, and do not persist in the image.
9195
RUN set -ex \
92-
&& BPC_USER_CONFIG_PATH=/code/docker/insecure-settings.ini python ./manage.py collectstatic --no-input --clear \
93-
&& BPC_USER_CONFIG_PATH=/code/docker/insecure-settings.ini python ./manage.py compilemessages \
94-
&& rm /code/docker/insecure-settings.ini
96+
&& export DB_NAME="insecure" \
97+
DB_USER="insecure" \
98+
DB_PASSWORD="insecure" \
99+
DB_HOST="insecure" \
100+
DB_PORT="insecure" \
101+
SECRET_KEY="insecure" \
102+
TIME_ZONE="Europe/Copenhagen" \
103+
LANGUAGE_CODE="da-dk" \
104+
&& ./manage.py collectstatic --no-input --clear \
105+
&& ./manage.py compilemessages
95106

96107
# Run the server as non-root user on port 9999
97108
USER 1001

docker/insecure-settings.ini

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)