From 002e4ee7490adbc84113493734e6de40f1323bda Mon Sep 17 00:00:00 2001 From: Gnuxie Date: Fri, 30 Jul 2021 15:03:36 +0100 Subject: [PATCH] Create docker-compose for testing purposes --- docker-compose.yaml | 30 +++++++++++ docker/Dockerfile.synapse.latest | 12 +++++ docker/Dockerfile.synapse.registration | 6 +++ docker/README.md | 10 ++++ docker/synapse-data/homeserver.yaml | 70 ++++++++++++++++++++++++++ mjolnir_testing.sh | 23 +++++++++ src/index.ts | 12 +++++ 7 files changed, 163 insertions(+) create mode 100644 docker-compose.yaml create mode 100644 docker/Dockerfile.synapse.latest create mode 100644 docker/Dockerfile.synapse.registration create mode 100644 docker/README.md create mode 100644 docker/synapse-data/homeserver.yaml create mode 100755 mjolnir_testing.sh diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..91aff32 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,30 @@ +# THIS IS TO BE USED FOR DEVELOPMENT AND TESTING PURPOSES ONLY +version: '3.8' +services: + synapse_release: + build: + context: . + dockerfile: docker/Dockerfile.synapse.latest + ports: + - "9999:9999" + volumes: + - ./docker/synapse-data:/data + synapse_registration: + build: + context: . + dockerfile: docker/Dockerfile.synapse.registration + volumes: + - ./docker/synapse-data:/data + depends_on: [synapse_release] + mjolnir: + environment: + NODE_ENV: development + build: + context: . + dockerfile: Dockerfile + ports: + - "8005/tcp" + volumes: + - ./config:/data/config + - ./lib:/mjolnir/lib + depends_on: [synapse_registration] diff --git a/docker/Dockerfile.synapse.latest b/docker/Dockerfile.synapse.latest new file mode 100644 index 0000000..ac13101 --- /dev/null +++ b/docker/Dockerfile.synapse.latest @@ -0,0 +1,12 @@ +# A custom Dockerfile to rebuild synapse from the official release + plugins + +FROM matrixdotorg/synapse:latest + +RUN pip show matrix-synapse + +COPY synapse_antispam /synapse_antispam +RUN /usr/local/bin/python -m pip install /synapse_antispam + +VOLUME ["/data"] + +EXPOSE 8008/tcp 8009/tcp 8448/tcp diff --git a/docker/Dockerfile.synapse.registration b/docker/Dockerfile.synapse.registration new file mode 100644 index 0000000..0b2bb43 --- /dev/null +++ b/docker/Dockerfile.synapse.registration @@ -0,0 +1,6 @@ +# Only exists to register a user, temporary solution + +FROM matrixdotorg/synapse:latest +VOLUME ["/data"] +CMD ["-c", "/data/homeserver.yaml", "-u", "mjolnir", "-p", "mjolnir", "--admin", "http://synapse_release:9999"] +ENTRYPOINT ["register_new_matrix_user"] diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..90d2527 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,10 @@ +## Testing mjolnir with docker + +At the moment a test environment for mjolnir can be setup by running +`./mjolnir_testing.sh` from the parent directory. This script will use +the `docker-compose.yaml` in the parent directory. + +This sets up synapse container with a user for mjolnir to use. +The container for mjolnir, creates and joins the moderation room +which has to be specified with an alias in the config under `managementRoom`. +Eventually this setup should be moved to a testing module. diff --git a/docker/synapse-data/homeserver.yaml b/docker/synapse-data/homeserver.yaml new file mode 100644 index 0000000..8359e57 --- /dev/null +++ b/docker/synapse-data/homeserver.yaml @@ -0,0 +1,70 @@ +server_name: localhost:9999 +pid_file: /data/homeserver.pid +public_baseurl: http://localhost:9999 +listeners: + - port: 9999 + tls: false + type: http + x_forwarded: true + + resources: + - names: [client, federation] + compress: false +federation_ip_range_blacklist: + - '127.0.0.0/8' + - '10.0.0.0/8' + - '172.16.0.0/12' + - '192.168.0.0/16' + - '100.64.0.0/10' + - '169.254.0.0/16' + - '::1/128' + - 'fe80::/64' + - 'fc00::/7' +# can not figure out how to exclude the db from the /data volume +database: + name: sqlite3 + args: + database: /data/homeserver.db +media_store_path: "/data/media_store" +enable_registration: true + +report_stats: false +registration_shared_secret: "REGISTRATION_SHARED_SECRET" +macaroon_secret_key: "MACROON_SECRET_KEY" +#form_secret: "" +signing_key_path: "/data/localhost:9999.signing.key" +trusted_key_servers: + - server_name: "matrix.org" +suppress_key_server_warning: true + + +rc_message: + per_second: 10000 + burst_count: 10000 + +rc_registration: + per_second: 10000 + burst_count: 10000 + +rc_login: + address: + per_second: 10000 + burst_count: 10000 + account: + per_second: 10000 + burst_count: 10000 + failed_attempts: + per_second: 10000 + burst_count: 10000 + +rc_admin_redaction: + per_second: 10000 + burst_count: 10000 + +rc_joins: + local: + per_second: 10000 + burst_count: 10000 + remote: + per_second: 10000 + burst_count: 10000 diff --git a/mjolnir_testing.sh b/mjolnir_testing.sh new file mode 100755 index 0000000..1b07ebb --- /dev/null +++ b/mjolnir_testing.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# This script only exists so that the persitent state in volumes +# is removed. Eventually this should be moved to a module +# managing the integration tests. +cleanup () { + set +e + rm -f docker/synapse-data/homeserver.db + set -e +} + +case "$1" in + up) + cleanup + exec docker-compose up + ;; + down) + exec docker-compose down + ;; + *) + echo "Usage: $SCRIPTNAME {up|down}" >&2 + exit 3 + ;; +esac diff --git a/src/index.ts b/src/index.ts index 8b5c745..fdd70d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,11 @@ if (config.health.healthz.enabled) { Healthz.listen(); } +async function createManagementRoom(client: MatrixClient, alias: string) { + let roomId = await client.createRoom(); + await client.createRoomAlias(alias, roomId); +} + (async function () { const storage = new SimpleFsStorageProvider(path.join(config.dataPath, "bot.json")); @@ -56,6 +61,13 @@ if (config.health.healthz.enabled) { } config.RUNTIME.client = client; + // probably better to change to a config value + if (process.env.NODE_ENV === "development") { + // expects the config value to be an alias + // don't know what to do about that for now + // probably should validate it so someone doesn't have a huge headache + await createManagementRoom(client, config.managementRoom); + } client.on("room.invite", async (roomId: string, inviteEvent: any) => { const membershipEvent = new MembershipEvent(inviteEvent);