From 63d12da368dfc281e22130f0e1b89bde4e47822f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 2 Feb 2026 14:20:18 +0100 Subject: [PATCH] web-app-discourse: fix admin creation, pin repo version, add self-signed CA support - Pin Discourse repository to configurable version instead of implicit HEAD - Add TLS/self-signed CA env + volume injection to container config - Make force_https depend on resolved TLS state - Replace fragile printf-based admin creation with safe heredoc - Ensure admin user creation works with arbitrary password characters - Expose DISCOURSE_VERSION via role vars for reproducible deploys https://chatgpt.com/share/6980a49e-3000-800f-b6a4-cb6a3e15a3d8 --- roles/web-app-discourse/config/main.yml | 3 +- roles/web-app-discourse/tasks/03_docker.yml | 13 +++---- .../web-app-discourse/templates/config.yml.j2 | 36 ++++++++++++++++--- roles/web-app-discourse/vars/main.yml | 6 ++++ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/roles/web-app-discourse/config/main.yml b/roles/web-app-discourse/config/main.yml index ed37295521..e1d25d8645 100644 --- a/roles/web-app-discourse/config/main.yml +++ b/roles/web-app-discourse/config/main.yml @@ -36,9 +36,10 @@ docker: redis: backup: disabled: true - enabled: true + enabled: true discourse: name: "discourse" + version: "v2026.2.0-latest" image: "local_discourse/<< defaults_applications[web-app-discourse].docker.services.discourse.name >>" # Necessary to define this for the docker 2 loc backup backup: no_stop_required: true diff --git a/roles/web-app-discourse/tasks/03_docker.yml b/roles/web-app-discourse/tasks/03_docker.yml index 8b996c114f..18a9a2bcd7 100644 --- a/roles/web-app-discourse/tasks/03_docker.yml +++ b/roles/web-app-discourse/tasks/03_docker.yml @@ -13,12 +13,13 @@ - name: pull docker repository git: - repo: "{{ DISCOURSE_REPOSITORY_URL }}" - dest: "{{ DISCOURSE_REPOSITORY_DIR }}" - update: yes - depth: 1 - notify: recreate discourse - become: true + repo: "{{ DISCOURSE_REPOSITORY_URL }}" + dest: "{{ DISCOURSE_REPOSITORY_DIR }}" + version: "{{ DISCOURSE_VERSION }}" + update: yes + depth: 1 + notify: recreate discourse + become: true ignore_errors: true - name: set chmod 700 for '{{ DISCOURSE_CONTAINERS_DIR }}' diff --git a/roles/web-app-discourse/templates/config.yml.j2 b/roles/web-app-discourse/templates/config.yml.j2 index 851505e59d..77ff5aedde 100644 --- a/roles/web-app-discourse/templates/config.yml.j2 +++ b/roles/web-app-discourse/templates/config.yml.j2 @@ -94,6 +94,11 @@ env: ## see https://meta.discourse.org/t/-/137387/23 for details #DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456 +{% if DISCOURSE_TLS_SELFSIGNED_ENABLED | bool %} + CA_TRUST_CERT: {{ CA_ROOT.cert_host }} + CA_TRUST_NAME: {{ domains | get_domain(application_id) }} +{% endif %} + ## The Docker container is stateless; all data is stored in /shared volumes: - volume: @@ -103,6 +108,15 @@ volumes: host: /var/discourse/shared/standalone/log/var-log guest: /var/log +{% if DISCOURSE_TLS_SELFSIGNED_ENABLED | bool %} + - volume: + host: {{ CA_ROOT.cert_host }} + guest: {{ CA_ROOT.cert_host }} + - volume: + host: {{ PATH_CA_TRUST }} + guest: {{ PATH_CA_TRUST }} +{% endif %} + ## Plugins go here ## see https://meta.discourse.org/t/19157 for details hooks: @@ -123,7 +137,10 @@ hooks: ## Any custom commands to run after building run: - exec: echo "Beginning of custom commands" - - exec: rails r "SiteSetting.force_https = true" + - exec: rails r "SiteSetting.force_https = {{ DISCOURSE_TLS_ENABLED | bool | string | lower }}" +{% if DISCOURSE_TLS_SELFSIGNED_ENABLED | bool %} + - exec: {{ PATH_CA_TRUST }} +{% endif %} ## If you want to set the 'From' email address for your first registration, uncomment and change: ## After getting the first signup email, re-comment the line. It only needs to run once. #- exec: rails r "SiteSetting.notification_email='info@unconfigured.discourse.org'" @@ -134,10 +151,6 @@ run: - exec: rails r "SiteSetting.username_change_period = 0" # Deactivate changing of username - # Activate Administrator User - #- exec: printf '{{ users.administrator.email }}\n{{users.administrator.password}}\n{{users.administrator.password}}\nY\n' | rake admin:create - #- exec: rails r "User.find_by_email('{{ users.administrator.email }}').update(username: '{{users.administrator.username}}')" - # The following code is just an inspiration, how to connect with the oidc account. as long as this is not set the admini account needs to be manually connected with oidc # docker exec -it {{ DISCOURSE_CONTAINER }} rails runner "user = User.find_by_email('test@infinito.nexus'); UserAuth.create(user_id: user.id, provider: 'oidc', uid: 'eindeutige_oidc_id', info: { name: user.username, email: user.email })" @@ -151,6 +164,19 @@ run: - exec: rails r "SiteSetting.openid_connect_rp_initiated_logout = true" {% endif %} + # Activate Administrator User (always yes, non-interactive) + - exec: | + cat <<'EOF' | rake admin:create + {{ users.administrator.email }} + {{ users.administrator.password }} + {{ users.administrator.password }} + Y + Y + EOF + + - exec: > + rails r "User.find_by_email('{{ users.administrator.email }}')&.update!(username: '{{ users.administrator.username }}')" + {% if DISCOURSE_LDAP_ENABLED | bool %} # Enable LDAP authentication - exec: rails r "SiteSetting.ldap_auth_enabled = true" diff --git a/roles/web-app-discourse/vars/main.yml b/roles/web-app-discourse/vars/main.yml index 68a265dbd7..b4e5fe7399 100644 --- a/roles/web-app-discourse/vars/main.yml +++ b/roles/web-app-discourse/vars/main.yml @@ -14,6 +14,7 @@ DISCOURSE_PLUGINS: "{{ applications | get_app_conf(application_ DISCOURSE_PG_NETWORK: "{{ applications | get_app_conf('svc-db-' ~ DISCOURSE_DATABASE_TYPE, 'docker.network' ) }}" DISCOURSE_REDIS_HOST: "{{ application_id | get_entity_name }}-redis" DISCOURSE_REPOSITORY_URL: "{{ applications | get_app_conf(application_id, 'docker.services.discourse.repository') }}" +DISCOURSE_VERSION: "{{ applications | get_app_conf(application_id, 'docker.services.discourse.version') }}" ## Directories DISCOURSE_REPOSITORY_DIR: "{{ docker_compose.directories.services }}{{ applications | get_app_conf( application_id, 'repository') }}/" @@ -33,3 +34,8 @@ DISCOURSE_LOCAL: "C.UTF-8" DISCOURSE_OIDC_ENABLED: "{{ applications | get_app_conf(application_id, 'docker.services.oidc.enabled') }}" DISCOURSE_OIDC_LOGOUT_REDIRECT_URL: "{{ lookup('tls_resolve', application_id, want='url.base') }}" + +# TLS +DISCOURSE_TLS_ENABLED: "{{ lookup('tls_resolve', application_id, want='enabled') | bool }}" +DISCOURSE_TLS_SELFSIGNED: "{{ lookup('tls_resolve', application_id, want='mode') == 'self_signed' }}" +DISCOURSE_TLS_SELFSIGNED_ENABLED: "{{ ( DISCOURSE_TLS_ENABLED | bool ) and ( DISCOURSE_TLS_SELFSIGNED | bool ) }}" \ No newline at end of file