diff --git a/playbooks/matrix_webhooks.yml b/playbooks/matrix_webhooks.yml new file mode 100644 index 0000000..3294342 --- /dev/null +++ b/playbooks/matrix_webhooks.yml @@ -0,0 +1,27 @@ +--- + +# Infrastructure +# Ansible instructions to deploy the infrastructure +# Copyright (C) 2021 Saibotk +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +- hosts: matrix-webhooks + roles: + - docker + - docker_compose + - docker_cleanup + - traefik + - matrix_webhooks + environment: + PYTHONPATH: /opt/ansible-dependencies/lib/python2.7/site-packages diff --git a/playbooks/site.yml b/playbooks/site.yml index b84759d..eaa3242 100644 --- a/playbooks/site.yml +++ b/playbooks/site.yml @@ -28,6 +28,7 @@ - import_playbook: mastodon.yml - import_playbook: codimd.yml - import_playbook: matrix.yml +- import_playbook: matrix-webhooks.yml - import_playbook: static_websites.yml - import_playbook: teamspeak.yml - import_playbook: owncast.yml diff --git a/roles/matrix_webhooks/README.md b/roles/matrix_webhooks/README.md new file mode 100644 index 0000000..6822137 --- /dev/null +++ b/roles/matrix_webhooks/README.md @@ -0,0 +1,37 @@ +Matrix-Webhooks +========= + +This will setup a [Matrix-Webhooks-appservice](https://github.com/turt2live/matrix-appservice-webhooks) instance using the official docker container and traefik as a reverse proxy. + +Requirements +------------ + +You will need to have docker, docker-compose and traefik installed or declared as dependencies with their respective roles. + +**This role assumes that you have setup traefik with an endpoint called `websecure`.** + +**You will also need to manually copy the appservice's registration file to your homeserver's directory and enable it in the config for this appservice to work!** + +Role Variables +-------------- + +**Please look at the [defaults/main.yml](defaults/main.yml) for all available variables and their description.** + +**Note: Lines that are commented out via `#` are usually still valid/used variables, but they are not defined by default, so they might enable a feature, when uncommenting/defining them!** + +### Global variables, that are used: + +- `proxy_network`: Defined by the local traefik installation, this is the shared proxy network used by traefik to reach the containers. (optional) +- `proxy_hiddenservice`: Defined by the local traefik installation, this is used to generate the alt-svc header for the alternative Tor domain. (optional) + +Dependencies +------------ + +- docker +- docker-compose +- traefik + +License +------- + +GPL-3.0-only diff --git a/roles/matrix_webhooks/defaults/main.yml b/roles/matrix_webhooks/defaults/main.yml new file mode 100644 index 0000000..5665201 --- /dev/null +++ b/roles/matrix_webhooks/defaults/main.yml @@ -0,0 +1,48 @@ +--- +# Default variables for the matrix-webhooks role + +# Infrastructure +# Ansible instructions to deploy the infrastructure +# Copyright (C) 2021 Saibotk +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# The install location +matrix_webhooks_install_location: /srv/matrix-webhooks + +# The location to store webhook related data in +matrix_webhooks_data_location: "{{ matrix_webhooks_install_location }}/data" + +# Under which domain should the service be reachable via traefik +matrix_webhooks_domain: "webhooks.example.com" + +# The certresolver that is used by traefik for this domain +matrix_webhooks_traefik_certresolver: letsencrypt_http + +# The URL to the Client->Server API of the corresponding homeserver +matrix_webhooks_hs_api_url: "https://matrix.example.com" + +# The domain part/right hand side of the MXID that this bot should run under (example: matrix.org) +matrix_webhooks_hs_servername: "example.com" + +# Container versions +# renovate: depName=docker.io/turt2live/matrix-appservice-webhooks +matrix_webhooks_image_version: "latest" + +# The Provision API key +matrix_webhooks_provisioning_key: "{{ lookup('passwordstore', matrix_webhooks_domain + '/provisioning-api create=true length=42') }}" + +# Webhook user profile (seems to be unused at the moment) +matrix_webhooks_display_name: "Captain Webhook" +matrix_webhooks_avatar: "http://i.imgur.com/IDOBtEJ.png" diff --git a/roles/matrix_webhooks/handlers/main.yml b/roles/matrix_webhooks/handlers/main.yml new file mode 100644 index 0000000..45f1cee --- /dev/null +++ b/roles/matrix_webhooks/handlers/main.yml @@ -0,0 +1,32 @@ +--- +# Handlers file for the matrix-webhooks role + +# Infrastructure +# Ansible instructions to deploy the infrastructure +# Copyright (C) 2021 Saibotk +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +- name: restart matrix webhooks + docker_compose: + services: + - appservice-webhooks + state: present + project_src: "{{ matrix_webhooks_install_location }}" + restarted: true + tags: + - docker + - matrix + - webhooks + become: true diff --git a/roles/matrix_webhooks/meta/main.yml b/roles/matrix_webhooks/meta/main.yml new file mode 100644 index 0000000..c2f1a51 --- /dev/null +++ b/roles/matrix_webhooks/meta/main.yml @@ -0,0 +1,17 @@ +galaxy_info: + author: saibotk + description: Deploys a matrix webhook appservice server via docker using traefik. + license: GPL-3.0-only + min_ansible_version: 2.9 + + platforms: + - name: CentOS + versions: + - 7 + + galaxy_tags: [] + +dependencies: + - docker + - docker_compose + - traefik diff --git a/roles/matrix_webhooks/tasks/main.yml b/roles/matrix_webhooks/tasks/main.yml new file mode 100644 index 0000000..fe6f23b --- /dev/null +++ b/roles/matrix_webhooks/tasks/main.yml @@ -0,0 +1,102 @@ +--- +# Tasks file for the matrix-webhooks role + +# Infrastructure +# Ansible instructions to deploy the infrastructure +# Copyright (C) 2019-2020 Christoph (Sheogorath) Kern +# Copyright (C) 2019-2020 Alexander (w4tsn) Wellbrock +# Copyright (C) 2020-2021 Saibotk +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +- name: Update default SELinux contexts + sefcontext: + target: '{{ item }}(/.*)?' + setype: "container_file_t" + state: present + with_items: + - "{{ matrix_webhooks_data_location }}" + become: true + +- name: Create install directory + file: + path: "{{ item }}" + state: directory + mode: '0700' + owner: 'root' + group: 'root' + with_items: + - "{{ matrix_webhooks_install_location }}" + become: true + +- name: Create data directory + file: # noqa risky-file-permissions # Container manages permissions on its own + path: "{{ item }}" + state: directory + setype: "container_file_t" + with_items: + - "{{ matrix_webhooks_data_location }}" + become: true + +- name: Deploy docker-compose.yml + template: + src: "docker-compose.yml" + dest: "{{ matrix_webhooks_install_location }}/docker-compose.yml" + mode: '0600' + owner: 'root' + group: 'root' + validate: docker-compose -f %s config -q + tags: + - matrix + - webhooks + become: true + +- name: Deploy webhooks config files + template: + src: "{{ item }}" + dest: "{{ matrix_webhooks_data_location }}/{{ item }}" + setype: "container_file_t" + mode: '0640' + owner: 'root' + group: 'root' + with_items: + - "database.json" + - "config.yaml" + tags: + - docker + - matrix + - webhooks + notify: restart matrix webhooks + become: true + +- name: Generate webhooks registration + command: 'docker-compose run appservice-webhooks node index.js -r \ + -f /data/appservice-registration-webhooks.yaml \ + -u "https://{{ matrix_webhooks_domain }}" -c /data/config.yaml' + args: + chdir: "{{ matrix_webhooks_install_location }}" + creates: "{{ matrix_webhooks_install_location }}/appservice-registration-webhooks.yaml" + become: true + +- name: Compose matrix-webhooks + docker_compose: + state: present + project_src: "{{ matrix_webhooks_install_location }}" + pull: true + remove_orphans: true + tags: + - docker + - matrix + - webhooks + become: true diff --git a/roles/matrix_webhooks/templates/config.yaml b/roles/matrix_webhooks/templates/config.yaml new file mode 100644 index 0000000..5594414 --- /dev/null +++ b/roles/matrix_webhooks/templates/config.yaml @@ -0,0 +1,39 @@ +{{ ansible_managed | comment }} + +# Configuration specific to the application service. All fields (unless otherwise marked) are required. +homeserver: + # The domain for the client-server API calls. + url: "{{ matrix_webhooks_hs_api_url }}" + + # The domain part for user IDs on this home server. Usually, but not always, this is the same as the + # home server's URL. + domain: "{{ matrix_webhooks_hs_servername }}" + +# Configuration specific to the bridge. All fields (unless otherwise marked) are required. +webhookBot: + # The localpart to use for the bot. May require re-registering the application service. + localpart: "_webhook" + + # Appearance options for the Matrix bot + appearance: + displayName: "{{ matrix_webhooks_display_name }}" + avatarUrl: "{{ matrix_webhooks_avatar }}" # webhook icon + +# Provisioning API options +provisioning: + # Your secret for the API. Required for all provisioning API requests. + secret: '{{ matrix_webhooks_provisioning_key }}' + +# Configuration related to the web portion of the bridge. Handles the inbound webhooks +web: + hookUrlBase: 'https://{{ matrix_webhooks_domain }}/' + +logging: + file: data/logs/webhook.log + console: true + consoleLevel: info + fileLevel: verbose + writeFiles: true + rotate: + size: 52428800 # bytes, default is 50mb + count: 5 diff --git a/roles/matrix_webhooks/templates/database.json b/roles/matrix_webhooks/templates/database.json new file mode 100644 index 0000000..f1d3573 --- /dev/null +++ b/roles/matrix_webhooks/templates/database.json @@ -0,0 +1,13 @@ +{ + "defaultEnv": { + "ENV": "NODE_ENV" + }, + "development": { + "driver": "sqlite3", + "filename": "data/development.db" + }, + "production": { + "driver": "sqlite3", + "filename": "data/production.db" + } +} diff --git a/roles/matrix_webhooks/templates/docker-compose.yml b/roles/matrix_webhooks/templates/docker-compose.yml new file mode 100644 index 0000000..5b63e71 --- /dev/null +++ b/roles/matrix_webhooks/templates/docker-compose.yml @@ -0,0 +1,60 @@ +--- +# Infrastructure +# Ansible instructions to deploy the infrastructure +# Copyright (C) 2019-2020 Christoph (Sheogorath) Kern +# Copyright (C) 2019-2020 Alexander (w4tsn) Wellbrock +# Copyright (C) 2020 Saibotk +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +version: '2' +services: + appservice-webhooks: + image: docker.io/turt2live/matrix-appservice-webhooks:{{ matrix_webhooks_image_version }} + mem_limit: 128mb + memswap_limit: 256mb + read_only: true + security_opt: + - no-new-privileges + volumes: + - "{{ matrix_webhooks_data_location }}:/data" + labels: + - "traefik.enable=true" + + - "traefik.http.routers.matrix-webhooks.rule=Host(`{{ matrix_webhooks_domain }}`) && PathPrefix(`/`)" + - "traefik.http.routers.matrix-webhooks.entrypoints=websecure" + - "traefik.http.routers.matrix-webhooks.tls.certresolver={{ matrix_webhooks_traefik_certresolver }}" + - "traefik.http.routers.matrix-webhooks.middlewares=matrix-webhooks,compress" + - "traefik.http.services.matrix-webhooks.loadbalancer.server.port=9000" + - "traefik.http.middlewares.matrix-webhooks.headers.sslredirect=true" + - "traefik.http.middlewares.matrix-webhooks.headers.stsSeconds=63072000" + - "traefik.http.middlewares.matrix-webhooks.headers.referrerPolicy=no-referrer" + +{% if proxy_network is defined %} + - "traefik.docker.network={{ proxy_network }}" +{% endif %} +{% if proxy_hiddenservice is defined and proxy_hiddenservice.content is defined %} + - "traefik.http.middlewares.matrix-webhooks.headers.customresponseheaders.alt-svc:h2={{ proxy_hiddenservice['content'] | b64decode | trim }}:443; ma=2592000" +{% endif %} +{% if proxy_network is defined %} + networks: + {{ proxy_network }}: +{% endif %} + restart: always + +{% if proxy_network is defined %} +networks: + {{ proxy_network }}: + external: true +{% endif %}