Finished Moodle OIDC Login Base

This commit is contained in:
Kevin Veen-Birkenbach 2025-06-18 18:25:23 +02:00
parent f5bacf17b3
commit 19a489b3c5
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
11 changed files with 164 additions and 46 deletions

View File

@ -1,6 +1,36 @@
# Administration # Administration
# Radical Erase of Setup ## Moodle Docker Directory Path
Moodle lives in: ``cd /opt/docker/moodle``
## Upgrade
```bash
docker exec --user daemon moodle php /opt/bitnami/moodle/admin/cli/upgrade.php --non-interactive
```
## Delete Cache
To clean the cache execute:
```bash
docker exec --user daemon moodle php /opt/bitnami/moodle/admin/cli/purge_caches.php
docker exec --user root moodle rm -rf \
/bitnami/moodledata/cache/* \
/bitnami/moodledata/localcache/* \
/bitnami/moodledata/temp/* \
/bitnami/moodledata/sessions/*
docker restart moodle
```
## CLI
A detailled Guid how to use the CLI in moodle you will find [here](https://docs.moodle.org/500/de/Administration_%C3%BCber_Kommandozeile).
## General Administration Tasks
### Radical Erase of Setup
To manually erase the full moodle setup inkluding all data execute: To manually erase the full moodle setup inkluding all data execute:
**CLI:** **CLI:**
@ -24,5 +54,5 @@ DROP DATABASE IF EXISTS moodle;
to delete all data in the database related to this role. to delete all data in the database related to this role.
# Virgin Setup ### Virgin Setup
After the installation you can rerun this role to create a fresh setup of Moodle. After the installation you can rerun this role to create a fresh setup of Moodle.

View File

@ -19,4 +19,5 @@ galaxy_info:
documentation: "https://s.veen.world/cymais" documentation: "https://s.veen.world/cymais"
logo: logo:
class: "fa-solid fa-graduation-cap" class: "fa-solid fa-graduation-cap"
dependencies: [] run_after:
- "docker-keycloak"

View File

@ -12,6 +12,26 @@
- include_tasks: "{{ playbook_dir }}/roles/docker-compose/tasks/create-files.yml" - include_tasks: "{{ playbook_dir }}/roles/docker-compose/tasks/create-files.yml"
- name: Wait until the Moodle container is healthy
shell: docker inspect --format '{% raw %}{{.State.Health.Status}}{% endraw %}' {{ container_name }}
register: health_check
until: health_check.stdout.strip() == "healthy"
retries: 120
delay: 5
- name: "Include ownership settings tasks for moodle"
include_tasks: ownership.yml
- name: "Configure OIDC login for Moodle if enabled" - name: "Configure OIDC login for Moodle if enabled"
include_tasks: oidc.yml include_tasks: oidc.yml
when: applications | is_feature_enabled('oidc',application_id) when: applications | is_feature_enabled('oidc',application_id)
- name: Run Moodle system check
command: >
docker exec --user {{ bitnami_user }} {{ container_name }}
php /opt/bitnami/moodle/admin/cli/checks.php
register: moodle_checks
changed_when: false
failed_when: >
moodle_checks.rc != 0 or
"OK: All" not in moodle_checks.stdout

View File

@ -1,24 +1,57 @@
--- ---
- name: "Upgrade Moodle to apply OIDC plugin"
command: "docker exec {{ container_name }} php admin/cli/upgrade.php --non-interactive" - name: Check if OIDC plugin is present in container
command: >
docker exec --user root {{ container_name }} test -d {{ bitnami_oidc_plugin_dir }}
register: oidc_plugin_check
ignore_errors: true
changed_when: false
- name: Fail if plugin not present to avoid broken auth
fail:
msg: "OIDC plugin not present skipping configuration"
when: oidc_plugin_check.rc != 0
#- name: "Upgrade Moodle to apply OIDC plugin"
# command: "docker exec --user {{ bitnami_user }} {{ container_name }} php /opt/bitnami/moodle/admin/cli/upgrade.php --non-interactive"
#
#- name: Clear Moodle cache
# command: >
# docker exec --user {{ bitnami_user }} {{ container_name }} php /opt/bitnami/moodle/admin/cli/purge_caches.php
- name: "Set Moodle OIDC configuration via CLI" - name: "Set Moodle OIDC configuration via CLI"
loop: loop:
- { name: "issuerurl", value: "{{ oidc.client.issuer_url }}" } - { name: "idptype", value: 3 }
- { name: "clientauthmethod", value: 1 }
- { name: "clientid", value: "{{ oidc.client.id }}" } - { name: "clientid", value: "{{ oidc.client.id }}" }
- { name: "clientsecret", value: "{{ oidc.client.secret }}" } - { name: "clientsecret", value: "{{ oidc.client.secret }}" }
- { name: "authmethod", value: "oidc" } - { name: "opname", value: "{{oidc.button_text}}" }
- { name: "loginflow", value: "authorization_code" } - { name: "oidcscope", value: "openid profile email" }
- { name: "idpname", value: "Keycloak" } - { name: "authendpoint", value: "{{ oidc.client.authorize_url }}" }
- { name: "scopes", value: "openid profile email" }
- { name: "authenticationendpoint", value: "{{ oidc.client.authorize_url }}" }
- { name: "tokenendpoint", value: "{{ oidc.client.token_url }}" } - { name: "tokenendpoint", value: "{{ oidc.client.token_url }}" }
- { name: "userinfoendpoint", value: "{{ oidc.client.user_info_url }}" } - { name: "bindingusernameclaim", value: "{{ oidc.attributes.username }}" }
- { name: "single_sign_off", value: 1 } # Logs the user out from the IDP
- { name: "logouturi", value: "{{ oidc.client.logout_url }}" }
- { name: "icon", value: "moodle:t/lock" }
- { name: "field_map_firstname", value: "{{ oidc.attributes.given_name }}" }
- { name: "field_map_lastname", value: "{{ oidc.attributes.family_name }}" }
#- { name: "showloginform", value: 0 } # Deactivate if OIDC is active
- { name: "alternateloginurl", value: "{{ web_protocol }}://{{ domains | get_domain(application_id) }}/auth/oidc/" }
loop_control: loop_control:
label: "{{ item.name }}" label: "{{ item.name }}"
command: > command: >
docker exec {{ container_name }} php admin/cli/cfg.php --component=auth_oidc docker exec --user {{ bitnami_user }} {{ container_name }} php /opt/bitnami/moodle/admin/cli/cfg.php --component=auth_oidc
--name={{ item.name }} --set="{{ item.value }}" --name={{ item.name }} --set="{{ item.value }}"
- name: "Enable OIDC login" - name: "Enable OIDC login"
command: "docker exec {{ container_name }} php admin/cli/cfg.php --name=auth --set=oidc" command: "docker exec --user {{ bitnami_user }} {{ container_name }} php /opt/bitnami/moodle/admin/cli/cfg.php --name=auth --set=oidc"
- name: Set auth = 'oidc' for all users except guest
shell: >
docker exec {{ database_instance }} mariadb -u {{ database_username }} -p{{ database_password }}
-e "UPDATE moodle.mdl_user SET auth = 'oidc' WHERE username != 'guest';"
args:
executable: /bin/bash
#- name: Prevent Account Creation
# command: docker exec --user {{ bitnami_user }} {{ container_name }} php /opt/bitnami/moodle/admin/cli/cfg.php --name=authpreventaccountcreation --set=1

View File

@ -0,0 +1,19 @@
# This file sets the correct ownership rights for Moodle volumes
- name: Set ownership and permissions on Moodle directories
vars:
moodle_dirs:
- "{{ bitnami_code_dir }}"
- "{{ bitnami_data_dir }}"
block:
- name: Ensure ownership is correct
command: "docker exec --user root {{ container_name }} chown -R {{ bitnami_user_group }} {{ item }}"
loop: "{{ moodle_dirs }}"
- name: Set directory permissions (770)
command: "docker exec --user root {{ container_name }} find {{ item }} -type d -exec chmod 770 {} \\;"
loop: "{{ moodle_dirs }}"
- name: Set file permissions (660)
command: "docker exec --user root {{ container_name }} find {{ item }} -type f -exec chmod 660 {} \\;"
loop: "{{ moodle_dirs }}"

View File

@ -1,14 +1,16 @@
FROM bitnami/moodle:{{ applications[application_id].version }} FROM bitnami/moodle:{{ applications[application_id].version }}
{% if applications | is_feature_enabled('oidc', application_id) %} {% if applications | is_feature_enabled('oidc', application_id) %}
# Install git (required to clone the OIDC plugin) RUN install_packages unzip curl jq \
USER root && VERSION=$(curl -s https://api.github.com/repos/microsoft/moodle-auth_oidc/tags \
RUN install_packages git unzip | jq -r '.[].name' \
| grep v{{ applications[application_id].version }} \
# Clone the Microsoft OIDC plugin into Moodle's auth directory | sort -Vr \
RUN git clone https://github.com/microsoft/moodle-auth_oidc.git \ | head -n1) \
/opt/bitnami/moodle/auth/oidc && \ && echo "Using version $VERSION" \
chown -R www-data:www-data /opt/bitnami/moodle/auth/oidc && curl -L -o /tmp/oidc.zip https://github.com/microsoft/moodle-auth_oidc/archive/refs/tags/${VERSION}.zip \
&& unzip /tmp/oidc.zip -d /tmp \
USER 1001 && mv /tmp/moodle-auth_oidc-* {{ bitnami_oidc_plugin_dir }} \
&& chown -R {{ bitnami_user_group }} {{ bitnami_oidc_plugin_dir }} \
&& rm -rf /tmp/oidc.zip
{% endif %} {% endif %}

View File

@ -11,8 +11,8 @@ services:
- 127.0.0.1:{{ports.localhost.http[application_id]}}:8080 - 127.0.0.1:{{ports.localhost.http[application_id]}}:8080
{% include 'roles/docker-compose/templates/services/base.yml.j2' %} {% include 'roles/docker-compose/templates/services/base.yml.j2' %}
volumes: volumes:
- 'moodle:/bitnami/moodle' - 'code:{{ bitnami_code_link }}'
- 'data:/bitnami/moodledata' - 'data:{{ bitnami_data_dir }}'
healthcheck: healthcheck:
test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/localhost/8080 && echo -e 'GET / HTTP/1.1\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n' >&3 && cat <&3 | grep -q 'HTTP/1.1'"] test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/localhost/8080 && echo -e 'GET / HTTP/1.1\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n' >&3 && cat <&3 | grep -q 'HTTP/1.1'"]
interval: 30s interval: 30s
@ -22,7 +22,7 @@ services:
{% include 'templates/docker/container/networks.yml.j2' %} {% include 'templates/docker/container/networks.yml.j2' %}
{% include 'templates/docker/compose/volumes.yml.j2' %} {% include 'templates/docker/compose/volumes.yml.j2' %}
moodle: code:
data: data:
{% include 'templates/docker/compose/networks.yml.j2' %} {% include 'templates/docker/compose/networks.yml.j2' %}

View File

@ -1,10 +1,7 @@
MOODLE_DATABASE_HOST={{database_host}}
MOODLE_DATABASE_PORT_NUMBER={{database_port}} # General
MOODLE_DATABASE_USER={{database_username}}
MOODLE_DATABASE_NAME={{database_name}}
MOODLE_DATABASE_PASSWORD={{database_password}}
ALLOW_EMPTY_PASSWORD=no
MOODLE_SITE_NAME="{{applications[application_id].site_titel}}" MOODLE_SITE_NAME="{{applications[application_id].site_titel}}"
MOODLE_HOST="{{ domains | get_domain(application_id) }}"
MOODLE_SSLPROXY=yes MOODLE_SSLPROXY=yes
MOODLE_REVERSE_PROXY=yes MOODLE_REVERSE_PROXY=yes
MOODLE_USERNAME={{applications[application_id].users.administrator.username}} MOODLE_USERNAME={{applications[application_id].users.administrator.username}}
@ -12,8 +9,15 @@ MOODLE_PASSWORD={{applications[application_id].credentials.user_password}}
MOODLE_EMAIL={{applications[application_id].users.administrator.email}} MOODLE_EMAIL={{applications[application_id].users.administrator.email}}
BITNAMI_DEBUG={% if enable_debug | bool %}true{% else %}false{% endif %} BITNAMI_DEBUG={% if enable_debug | bool %}true{% else %}false{% endif %}
# Database
MOODLE_DATABASE_HOST={{database_host}}
MOODLE_DATABASE_PORT_NUMBER={{database_port}}
MOODLE_DATABASE_USER={{database_username}}
MOODLE_DATABASE_NAME={{database_name}}
MOODLE_DATABASE_PASSWORD={{database_password}}
MOODLE_HOST={{ system_email.host }} # SMTP
MOODLE_SMTP_HOST={{ system_email.host }}
MOODLE_SMTP_PORT_NUMBER={{ system_email.port }} MOODLE_SMTP_PORT_NUMBER={{ system_email.port }}
MOODLE_SMTP_USER={{ users['no-reply'].email }} MOODLE_SMTP_USER={{ users['no-reply'].email }}
MOODLE_SMTP_PASSWORD={{ users['no-reply'].mailu_token }} MOODLE_SMTP_PASSWORD={{ users['no-reply'].mailu_token }}

View File

@ -3,20 +3,23 @@ users:
administrator: administrator:
username: "{{users.administrator.username}}" username: "{{users.administrator.username}}"
email: "{{users.administrator.email}}" email: "{{users.administrator.email}}"
version: "latest" version: "4.5" # Latest LTS - Necessary for OIDC
features: features:
matomo: true matomo: true
css: false css: false
portfolio_iframe: false portfolio_iframe: false
central_database: true central_database: true
oidc: false oidc: true
csp: csp:
flags: flags:
script-src-elem: script-src-elem:
unsafe-inline: true unsafe-inline: true
unsafe-eval: true unsafe-eval: true
script-src:
unsafe-eval: true
style-src: style-src:
unsafe-inline: true unsafe-inline: true
unsafe-eval: true
whitelist: whitelist:
font-src: font-src:
- "data:" - "data:"

View File

@ -2,3 +2,9 @@
application_id: "moodle" application_id: "moodle"
database_type: "mariadb" database_type: "mariadb"
container_name: "{{ application_id }}" container_name: "{{ application_id }}"
bitnami_code_link: "/bitnami/moodle"
bitnami_code_dir: "/opt{{bitnami_code_link}}"
bitnami_data_dir: "/bitnami/moodledata"
bitnami_oidc_plugin_dir: "{{ bitnami_code_dir }}/auth/oidc"
bitnami_user: "daemon"
bitnami_user_group: "{{ bitnami_user }}:{{ bitnami_user }}"