Skip to content

Commit 810bcbb

Browse files
Merge pull request #44 from OS2borgerPC/40-erstat-konfigurationsfil-med-miljøvariabler
40 erstat konfigurationsfil med miljøvariabler
2 parents e568458 + 0de3ec9 commit 810bcbb

File tree

7 files changed

+73
-129
lines changed

7 files changed

+73
-129
lines changed

.github/workflows/docker-image.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ jobs:
4040
file: ./docker/Dockerfile
4141
push: true
4242
tags: ${{ steps.meta.outputs.tags }}
43-
labels: ${{ steps.meta.outputs.labels }}
43+
labels: ${{ steps.meta.outputs.labels }}

admin_site/os2borgerpc_admin/settings.py

+26-57
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Django settings for OS2borgerPC admin project.
22

33
import os
4-
import configparser
54
import logging
65
import django
76

@@ -15,45 +14,18 @@
1514
# Our customized user profile.
1615
AUTH_PROFILE_MODULE = "account.UserProfile"
1716

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)
17+
DEBUG = os.getenv("DEBUG", 'false').lower() == 'true'
4518

4619
ADMINS = (
4720
[
48-
(settings.get("ADMIN_NAME"), settings["ADMIN_EMAIL"]),
21+
(os.environ.get("ADMIN_USERNAME"), os.environ["ADMIN_EMAIL"]),
4922
]
50-
if settings.get("ADMIN_EMAIL")
23+
if os.environ.get("ADMIN_EMAIL")
5124
else None
5225
)
5326

5427
MANAGERS = ADMINS
5528

56-
5729
# Template settings
5830
TEMPLATES = [
5931
{
@@ -87,7 +59,7 @@
8759
"USER": os.environ['DB_USER'],
8860
"PASSWORD": os.environ['DB_PASSWORD'],
8961
"HOST": os.environ['DB_HOST'],
90-
"PORT": os.environ['DB_PORT'],
62+
"PORT": os.getenv("DB_PORT", ""),
9163
"OPTIONS": {
9264
"connect_timeout": 2, # Minimum in 2
9365
},
@@ -96,16 +68,13 @@
9668

9769
# Hosts/domain names that are valid for this site; required if DEBUG is False
9870
# 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 = []
71+
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "").split(",")
10372

10473
# Django > 4.0 introduced changes related to CSRF. Note that the protocol has to be specified too.
10574
# https://docs.djangoproject.com/en/4.2/releases/4.0/#csrf
10675
# 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(",")
76+
if os.getenv("CSRF_TRUSTED_ORIGINS", ""):
77+
CSRF_TRUSTED_ORIGINS = os.getenv("CSRF_TRUSTED_ORIGINS", "").split(",")
10978
else:
11079
CSRF_TRUSTED_ORIGINS = []
11180

@@ -114,11 +83,11 @@
11483
# although not all choices may be available on all operating systems.
11584
# In a Windows environment this must be set to your system time zone.
11685
# Timezone/Language
117-
TIME_ZONE = settings["TIME_ZONE"]
86+
TIME_ZONE = os.environ["TIME_ZONE"]
11887

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

12392
LOCALE_PATHS = [os.path.join(install_dir, "locale")]
12493

@@ -172,21 +141,21 @@
172141

173142

174143
# Storage setup
175-
if settings.get("GS_BUCKET_NAME"):
144+
if os.environ.get("GS_BUCKET_NAME"):
176145
# The Google Cloud Storage bucket name. For `django-storages[google]`
177146
# https://django-storages.readthedocs.io/en/latest/backends/gcloud.html
178147
# If it is set, we save all files to Google Cloud.
179148
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
180-
GS_BUCKET_NAME = settings.get("GS_BUCKET_NAME")
149+
GS_BUCKET_NAME = os.environ.get("GS_BUCKET_NAME")
181150
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
182-
settings.get("GS_CREDENTIALS_FILE")
151+
os.environ.get("GS_CREDENTIALS_FILE")
183152
)
184153
GS_QUERYSTRING_AUTH = False
185154
GS_FILE_OVERWRITE = False
186-
GS_CUSTOM_ENDPOINT = settings.get("GS_CUSTOM_ENDPOINT", None)
155+
GS_CUSTOM_ENDPOINT = os.environ.get("GS_CUSTOM_ENDPOINT", None)
187156

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

191160
MIDDLEWARE = (
192161
"django.middleware.security.SecurityMiddleware",
@@ -203,14 +172,14 @@
203172

204173
# Email settings
205174

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")
175+
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
176+
ADMIN_EMAIL = os.environ.get("ADMIN_EMAIL")
177+
EMAIL_HOST = os.environ.get("EMAIL_HOST")
178+
EMAIL_PORT = os.environ.get("EMAIL_PORT")
179+
SERVER_EMAIL = os.environ.get("SERVER_EMAIL")
180+
EMAIL_BACKEND = "os.environ.core.mail.backends.smtp.EmailBackend"
181+
EMAIL_HOST_USER = os.environ.get("EMAIL_USER")
182+
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_PASSWORD")
214183

215184
ROOT_URLCONF = "os2borgerpc_admin.urls"
216185

@@ -300,11 +269,11 @@
300269
},
301270
"root": {
302271
"handlers": ["console", "mail_admins"],
303-
"level": settings.get("LOG_LEVEL", fallback="ERROR"),
272+
"level": os.getenv("LOG_LEVEL", "ERROR"),
304273
},
305274
}
306275

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

309278
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
310279

@@ -313,12 +282,12 @@
313282
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
314283

315284
# Handler for citizen login.
316-
CITIZEN_LOGIN_API_VALIDATOR = settings.get(
285+
CITIZEN_LOGIN_API_VALIDATOR = os.environ.get(
317286
"CITIZEN_LOGIN_API_VALIDATOR", "system.utils.cicero_validate"
318287
)
319288

320289
# Cicero specific stuff.
321-
CICERO_URL = settings.get("CICERO_URL")
290+
CICERO_URL = os.environ.get("CICERO_URL")
322291

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

compose.yaml

+21-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,32 @@ services:
1616
# Defaults in case values aren't passed in via the environment, so 0 (root) isn't used. Justfile overrides them.
1717
# NOTE: Keep in sync with the UID/GID in docker/Dockerfile
1818
environment:
19-
UID: 75030
20-
GID: 75030
2119
DB_HOST: db
2220
DB_NAME: bpc
2321
DB_USER: bpc
2422
DB_PASSWORD: bpc
2523
DB_PORT: ""
26-
user: "${UID}:${GID}"
24+
ALLOWED_HOSTS: "*"
25+
DEBUG: True
26+
SECRET_KEY: v3rys1kr3t
27+
# Admin contact - fill in your own name and email as desired.
28+
ADMIN_USERNAME: OS2borgerPC Admin
29+
ADMIN_EMAIL: [email protected]
30+
ADMIN_PASSWORD: admin
31+
# Timezone/Language
32+
TIME_ZONE: Europe/Copenhagen
33+
LANGUAGE_CODE: da-dk
34+
35+
INITIALIZE_DATABASE: True
36+
37+
LOG_LEVEL: INFO
38+
39+
# IF USING THE CICERO INTEGRATION
40+
CICERO_URL: CICERO_SERVER_HERE
41+
# This particular line makes it both skip the connection to the Cicero server AND the validation of the username/password
42+
# For that reason, depending on what you're testing, you might want to comment out this line
43+
CITIZEN_LOGIN_API_VALIDATOR: system.utils.always_validate_citizen
44+
2745
build:
2846
context: .
2947
dockerfile: docker/Dockerfile

dev-environment/dev-settings.ini

-27
This file was deleted.

docker/Dockerfile

+24-13
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/docker-entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ then
1212
python ./manage.py migrate
1313
fi
1414

15-
./manage.py create_superuser_if_none_exists --username $ADMIN_USERNAME --email $ADMIN_EMAIL --password $ADMIN_PASSWORD
15+
./manage.py create_superuser_if_none_exists --username "$ADMIN_USERNAME" --email "$ADMIN_EMAIL" --password "$ADMIN_PASSWORD"
1616

1717
exec "$@"

docker/insecure-settings.ini

-27
This file was deleted.

0 commit comments

Comments
 (0)