blob: 1fc07e726596ff9d5e54de043bb1f7f5082dce0a [file] [log] [blame]
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +01001# Matrix server (synapse).
2# This needs a secret provisioned, create with:
3# ns=matrix
4#
5# SIGNING_KEY="$(kubectl run -n $ns -i --quiet --restart=Never --rm synapse-generate-config --image=matrixdotorg/synapse:v1.19.2 --env SYNAPSE_SERVER_NAME=dummy --env SYNAPSE_REPORT_STATS=no -o yaml --command -- sh -c '/start.py generate >/dev/null && cat /data/*.signing.key')"
6# kubectl -n $ns create secret generic synapse --from-literal=postgres_password=$(pwgen 24 1) --from-literal=macaroon_secret_key=$(pwgen 32 1) --from-literal=registration_shared_secret=$(pwgen 32 1) --from-literal=homeserver_signing_key="$SIGNING_KEY" --from-literal=redis_password=$(pwgen 32 1) --from-literal=worker_replication_secret=$(pwgen 32 1)
7# kubectl -n $ns create secret generic oauth2-cas-proxy --from-literal=oauth2_secret=...
8#
9# When migrating from matrix.libsonnet, instance signing key, redis passwsord
10# and worker replication secret need to be added to existing synapse secret:
11#
12# echo "homeserver_signing_key: $(kubectl -n $ns exec deploy/synapse -- sh -c 'cat /data/*.signing.key' | base64 -w0)"
13# echo "redis_password: $(pwgen 32 1 | tr -d '\n' | base64 -w0)"
14# echo "worker_replication_secret: $(pwgen 32 1 | tr -d '\n' | base64 -w0)"
15# kubectl -n $ns edit secret synapse
16# # ...add homeserver_signing_key, redis_password and worker_replication_secret keys
17#
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010018# Additionally some resources need to be explicitly removed due to
19# label/annotations changes:
20# kubectl -n $ns delete deployment riot-web oauth2-cas-proxy wellknown synapse
21#
22# Some service configuration customization fields have been renamed:
23# .riotConfig → .riot.config
24# .synapseConfig → .synapse.config
25#
Piotr Dobrowolskifb119aa2021-01-31 19:07:32 +010026# When migrating from CAS to OpenID Connect authentication scheme following need
27# to be ensured:
28# * https://{homeserver}/_synapse/oidc/callback is added to allowed callback URLs list
29# * openid scope is enabled for configured client
30#
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +020031# In order to deploy matrix-media-repo as a replacement for synapse built-in
32# media workers the following steps need to be carried out:
33#
34# 1. Generate password and bootstrap extra postgres user
35# pwgen 32 1 > secrets/plain/media-repo-$ns-postgres
36# echo "create database mediarepo; create user mediarepo with password '$(cat secrets/plain/media-repo-$ns-postgres)'; grant all privileges on database mediarepo to mediarepo;" | kubectl -n $ns exec -it deploy/waw3-postgres psql
37# secretstore sync secrets
38#
39# 2. Fetch Ceph RGW credentials
40# kubectl get secrets -n ceph-waw3 rook-ceph-object-user-waw-hdd-redundant-3-object-$ns -o json | jq '.data|map_values(@base64d)' > secrets/plain/media-repo-$ns-ceph.json
41# secretstore sync secrets
42#
43# 3. Create an apropriate bucket using s3cmd
44# s3cmd --access_key="$(jq -r '.AccessKey' secrets/plain/media-repo-$ns-ceph.json)" --secret_key="$(jq -r '.SecretKey' secrets/plain/media-repo-$ns-ceph.json)" --host=object.ceph-waw3.hswaw.net --host-bucket=object.ceph-waw3.hswaw.net mb s3://media-repo-$ns
45#
46# 4. Add relevant configuration overrides in cfg.mediaRepo key for your
47# deployment configuration file:
48#
49# mediaRepo+: {
50# enable: true,
51# route: false,
52# s3+: {
53# endpoint: std.strReplace((import "secrets/plain/media-repo-$ns-ceph.json").Endpoint, "http://", ""),
54# accessKey: (import "secrets/plain/media-repo-$ns-ceph.json").AccessKey,
55# secretKey: (import "secrets/plain/media-repo-$ns-ceph.json").SecretKey,
56# bucketName: "media-repo-$ns",
57# region: "eu",
58# },
59# db+: {
60# password: std.strReplace(importstr "secrets/plain/media-repo-$ns-postgres", "\n", ""),
61# },
62# },
63#
64# 5. Additionally, when migrating from already deployed synapse media worker the
65# following command needs to be run in order to import existing media files:
66# kubectl -n $ns exec deploy/media-repo -- import_synapse -baseUrl http://synapse-media:8008 -dbHost waw3-postgres -dbPassword "$(kubectl -n $ns get secret synapse -o json | jq -r '.data.postgres_password | @base64d')" -config /config/config.yaml -serverName 'SERVER_NAME'
67#
68# 6. After migrating data over from native synapse media worker storage traffic
69# can be rerouted to matrix-media-repo by switching cfg.mediaRepo.route flag
70# to true
71#
72# 7. Run import step #5 again to make sure no media were left missing in old
73# media worker deployment - import operation is indempotent and can be ran
74# against a synapse media worker that's not handling user traffic anymore.
75#
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +010076# Sequencing appservices is fun. The appservice needs to run first (for
77# instance, via a bootstrap job), and on startup it will spit out a
78# registration file. This registration file then needs to be fed to synapse -
79# this is done via specialy named secrets (appservice-X-registration, for X key
80# in the appservices object).
81#
82# For appservice-irc instances, you can use this oneliner magic to get the
83# registration YAML from logs.
84# kubectl -n matrix create secret generic appservice-irc-freenode-registration --from-file=registration.yaml=<(kubectl -n matrix logs job/appservice-irc-freenode-bootstrap | tail -n +4 | sed -r 's/(.*aliases:.*)/ group_id: "+freenode:hackerspace.pl"\n\1/')
85#
86# For appservice-telegram instances, you can use this oneliner magic:
87# kubectl -n matrix create secret generic appservice-telegram-prod-registration --from-file=registration.yaml=<(kubectl -n matrix logs job/appservice-telegram-prod-bootstrap | grep -A 100 SNIPSNIP | grep -v SNIPSNIP)
88
89local kube = import "../../../kube/kube.libsonnet";
90local postgres = import "../../../kube/postgres.libsonnet";
91local redis = import "../../../kube/redis.libsonnet";
92
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010093local riot = import "./riot.libsonnet";
94local cas = import "./cas.libsonnet";
95local wellKnown = import "./wellknown.libsonnet";
96local synapse = import "./synapse.libsonnet";
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +020097local mediaRepo = import "./media-repo.libsonnet";
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010098
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +010099{
100 local app = self,
101 local cfg = app.cfg,
102 cfg:: {
103 namespace: error "cfg.namespace must be set",
104 # webDomain is the domain name at which element will run
105 webDomain: error "cfg.webDomain must be set",
106 # serverName is the server part of the MXID this homeserver will cover
107 serverName: error "cfg.serverName must be set",
108 storageClassName: "waw-hdd-redundant-3",
109
110 images: {
Piotr Dobrowolskif56db192021-09-16 21:51:10 +0200111 synapse: "matrixdotorg/synapse:v1.42.0",
Piotr Dobrowolskicf3d8482021-09-16 14:30:02 +0200112 riot: "vectorim/element-web:v1.8.5",
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100113 casProxy: "registry.k0.hswaw.net/q3k/oauth2-cas-proxy:0.1.4",
Piotr Dobrowolski0f26c4a2021-09-14 19:58:39 +0200114 appserviceIRC: "matrixdotorg/matrix-appservice-irc:release-v0.29.0",
Serge Bazanski8eae4542021-05-19 15:42:13 +0000115 appserviceTelegram: "dock.mau.dev/tulir/mautrix-telegram@sha256:c6e25cb57e1b67027069e8dc2627338df35d156315c004a6f2b34b6aeaa79f77",
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100116 wellKnown: "registry.k0.hswaw.net/q3k/wellknown:1611960794-adbf560851a46ad0e58b42f0daad7ef19535687c",
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +0200117 mediaRepo: "turt2live/matrix-media-repo:v1.2.8",
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100118 },
119
120 # OpenID Connect provider configuration.
121 # Currently only client_secret can be provided as a secretKeyRef.
122 #
123 # https://${cfg.webDomain}/_synapse/oidc/callback needs to be set as
124 # allowed OAuth2/OpenID Connect callback URL
125 #
126 # See: https://github.com/matrix-org/synapse/blob/v1.25.0/docs/openid.md
127 oidc: {
128 enable: false,
129 config: {
130 issuer: error "oidc.config.issuer must be set",
131 client_id: error "oidc.config.client_id must be set",
132 client_secret: error "oidc.config.client_secret must be set",
133
134 # Set this to true when migrating from existing CAS deployment
135 allow_existing_users: false,
136 user_mapping_provider: {
137 config: {
138 localpart_template: '{{ user.sub }}',
139 display_name_template: '{{ user.sub }}',
140 },
141 },
142
143 # Extra configuration required when migrating from
144 # oauth2-cas-proxy bound to https://sso.hackerspace.pl
145 # user_profile_method: "userinfo_endpoint",
146 # client_auth_method: "client_secret_post",
147 },
148 },
149
150 # Central Authentication Scheme, a single-sign-on system. Note: this flow is now called 'SSO' in Matrix, we keep this name for legacy reasons.
151 # Refer to https://matrix.org/docs/spec/client_server/r0.6.1#sso-client-login
152 cas: {
153 # whether to enable the CAS proxy (ie. connect to hswaw sso via OAuth)
154 enable: false,
155 # generate client ID and secret in with your OAuth2 provider, refer to https://www.oauth.com/oauth2-servers/client-registration/client-id-secret/
156 oauth2: {
157 clientID: error "cas.oauth2.clientID must be set",
158 clientSecret: error "cas.oauth2.clientSecret must be set",
159 scope: error "cas.oauth2.scope must be set",
160 authorizeURL: error "cas.oauth2.authorizeURL must be set",
161 tokenURL: error "cas.oauth2.tokenURL must be set",
162 userinfoURL: error "cas.oauth2.userinfoURL must be set",
163 },
164 },
165
166 # Serve /.well-known/matrix configuration endpoints required when using
167 # cfg.webDomain directly as mxid.
168 wellKnown: false,
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +0200169
170 # matrix-media-repo S3-based media storage container
171 mediaRepo: {
172 enable: false,
173
174 # Route /_matrix/media/ endpoints to matrix-media-repo. Set this
175 # to true after migrating media files to matrix-media-repo.
176 route: false,
177
178 s3: {
179 endpoint: error "mediaRepo.s3.endpoint needs to be set",
180 accessKey: error "mediaRepo.s3.accessKey needs to be set",
181 secretKey: error "mediaRepo.s3.secretKey needs to be set",
182 bucketName: error "mediaRepo.s3.bucketName needs to be set",
183 region: error "mediaRepo.s3.region needs to be set",
184 },
185
186 db: {
187 username: "mediarepo",
188 password: error "mediaRepo.db.password needs to be set",
189 database: "mediarepo",
190 host: "waw3-postgres",
191 },
192 },
193
194 # List of administrative users MXIDs (used in matrix-media-repo only)
195 admins: [],
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100196 },
197
Piotr Dobrowolskifb119aa2021-01-31 19:07:32 +0100198 # DEPRECATED: this needs to be removed in favor of namespace.Contain() in
199 # modules that depend on this (appservices/instance defintions)
200 metadata(component):: {
201 namespace: cfg.namespace,
202 labels: {
203 "app.kubernetes.io/name": "matrix",
204 "app.kubernetes.io/managed-by": "kubecfg",
205 "app.kubernetes.io/component": component,
206 },
207 },
208
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100209 namespace: kube.Namespace(cfg.namespace),
210
211 postgres3: postgres {
212 cfg+: {
213 namespace: cfg.namespace,
214 appName: "synapse",
215 database: "synapse",
216 username: "synapse",
217 prefix: "waw3-",
218 password: { secretKeyRef: { name: "synapse", key: "postgres_password" } },
219 storageClassName: cfg.storageClassName,
220 storageSize: "100Gi",
221 initdbArgs: "--encoding='UTF8' --lc-collate='C' --lc-ctype='C'",
222 },
223 },
224
225 redis: redis {
226 cfg+: {
227 namespace: cfg.namespace,
228 appName: "synapse",
229 storageClassName: cfg.storageClassName,
230 password: { secretKeyRef: { name: "synapse", key: "redis_password" } },
231 },
232 },
233
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100234 riot: riot {
235 ns: app.namespace,
236 cfg+: {
237 webDomain: cfg.webDomain,
238 serverName: cfg.serverName,
239 image: cfg.images.riot,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100240 },
241 },
242
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100243 cas: if cfg.cas.enable && cfg.oidc.enable then error "cfg.cas.enable and cfg.oidc.enable options are exclusive"
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100244 else if cfg.cas.enable then cas {
245 ns: app.namespace,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100246 cfg+: {
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100247 image: cfg.images.casProxy,
248 webDomain: cfg.webDomain,
249 oauth2: cfg.cas.oauth2,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100250 },
251 },
252
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100253 wellKnown: if cfg.wellKnown then wellKnown {
254 ns: app.namespace,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100255 cfg+: {
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100256 image: cfg.images.wellKnown,
257 webDomain: cfg.webDomain,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100258 },
259 } else {},
260
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +0200261 mediaRepo: if cfg.mediaRepo.enable then mediaRepo {
262 ns: app.namespace,
263 cfg+: {
264 image: cfg.images.mediaRepo,
265
266 homeservers: [
267 {name: cfg.serverName, csApi: "https://" + cfg.webDomain}
268 ],
269 admins: cfg.admins,
270
271 s3: cfg.mediaRepo.s3,
272 db: cfg.mediaRepo.db,
273 },
274 } else {},
275
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100276 synapse: synapse {
277 ns: app.namespace,
278 postgres: app.postgres3,
279 redis: app.redis,
280 appservices: app.appservices,
281 cfg+: app.cfg {
282 image: app.cfg.images.synapse,
283
284 macaroonSecretKey: { secretKeyRef: { name: "synapse", key: "macaroon_secret_key" } },
285 registrationSharedSecret: { secretKeyRef: { name: "synapse", key: "registration_shared_secret" } },
286 workerReplicationSecret: { secretKeyRef: { name: "synapse", key: "worker_replication_secret" } },
287 },
288 },
289
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100290 // Any appservice you add here will require an appservice-X-registration
291 // secret containing a registration.yaml file. Adding something to this
292 // dictionary will cause Synapse to not start until that secret is
293 // available - so change things carefully!
294 // If bootstrapping a new appservice, just keep it out of this dictionary
295 // until it spits you a registration YAML and you feed that to a secret.
296 appservices: {},
297
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100298 ingress: app.namespace.Contain(kube.Ingress("matrix")) {
299 metadata+: {
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100300 annotations+: {
301 "kubernetes.io/tls-acme": "true",
302 "certmanager.k8s.io/cluster-issuer": "letsencrypt-prod",
303 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
304 "nginx.ingress.kubernetes.io/use-regex": "true",
305 },
306 },
307 spec+: {
308 tls: [
309 {
310 hosts: [cfg.webDomain],
311 secretName: "synapse-tls",
312 },
313 ],
314 rules: [
315 {
316 host: cfg.webDomain,
317 http: {
318 paths: [
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100319 { path: path, backend: app.synapse.genericWorker.svc.name_port }
320 for path in app.synapse.genericWorker.paths
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100321 ] + [
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100322 { path: "/", backend: app.riot.svc.name_port },
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +0200323 { path: "/_matrix/media/", backend: if cfg.mediaRepo.route then app.mediaRepo.svc.name_port else app.synapse.mediaWorker.svc.name_port },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100324 { path: "/_matrix/", backend: app.synapse.main.svc.name_port },
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100325
326 # Used by OpenID Connect login flow
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100327 { path: "/_synapse/", backend: app.synapse.main.svc.name_port },
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100328 ] + (if cfg.cas.enable then [
329 { path: "/_cas", backend: app.cas.svc.name_port },
330 ] else []) + (if cfg.wellKnown then [
331 { path: "/.well-known/matrix", backend: app.wellKnown.svc.name_port },
332 ] else [])
333 },
334 }
335 ],
336 },
337 },
338}