blob: 28550f64f6afdb18380e44a9d0fdf28bfd6e456e [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 Dobrowolski8ec86572021-01-30 13:06:07 +010031# Sequencing appservices is fun. The appservice needs to run first (for
32# instance, via a bootstrap job), and on startup it will spit out a
33# registration file. This registration file then needs to be fed to synapse -
34# this is done via specialy named secrets (appservice-X-registration, for X key
35# in the appservices object).
36#
37# For appservice-irc instances, you can use this oneliner magic to get the
38# registration YAML from logs.
39# 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/')
40#
41# For appservice-telegram instances, you can use this oneliner magic:
42# 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)
43
44local kube = import "../../../kube/kube.libsonnet";
45local postgres = import "../../../kube/postgres.libsonnet";
46local redis = import "../../../kube/redis.libsonnet";
47
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010048local riot = import "./riot.libsonnet";
49local cas = import "./cas.libsonnet";
50local wellKnown = import "./wellknown.libsonnet";
51local synapse = import "./synapse.libsonnet";
52
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +010053{
54 local app = self,
55 local cfg = app.cfg,
56 cfg:: {
57 namespace: error "cfg.namespace must be set",
58 # webDomain is the domain name at which element will run
59 webDomain: error "cfg.webDomain must be set",
60 # serverName is the server part of the MXID this homeserver will cover
61 serverName: error "cfg.serverName must be set",
62 storageClassName: "waw-hdd-redundant-3",
63
64 images: {
65 synapse: "matrixdotorg/synapse:v1.25.0",
66 riot: "vectorim/riot-web:v1.7.18",
67 casProxy: "registry.k0.hswaw.net/q3k/oauth2-cas-proxy:0.1.4",
Serge Bazanski32d3eaa2021-02-13 17:42:01 +000068 appserviceIRC: "matrixdotorg/matrix-appservice-irc:release-0.23.0",
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +010069 # That's v0.8.2 - we just don't trust that host to not re-tag images.
70 appserviceTelegram: "dock.mau.dev/tulir/mautrix-telegram@sha256:9e68eaa80c9e4a75d9a09ec92dc4898b12d48390e01efa4de40ce882a6f7e330",
71 wellKnown: "registry.k0.hswaw.net/q3k/wellknown:1611960794-adbf560851a46ad0e58b42f0daad7ef19535687c",
72 },
73
74 # OpenID Connect provider configuration.
75 # Currently only client_secret can be provided as a secretKeyRef.
76 #
77 # https://${cfg.webDomain}/_synapse/oidc/callback needs to be set as
78 # allowed OAuth2/OpenID Connect callback URL
79 #
80 # See: https://github.com/matrix-org/synapse/blob/v1.25.0/docs/openid.md
81 oidc: {
82 enable: false,
83 config: {
84 issuer: error "oidc.config.issuer must be set",
85 client_id: error "oidc.config.client_id must be set",
86 client_secret: error "oidc.config.client_secret must be set",
87
88 # Set this to true when migrating from existing CAS deployment
89 allow_existing_users: false,
90 user_mapping_provider: {
91 config: {
92 localpart_template: '{{ user.sub }}',
93 display_name_template: '{{ user.sub }}',
94 },
95 },
96
97 # Extra configuration required when migrating from
98 # oauth2-cas-proxy bound to https://sso.hackerspace.pl
99 # user_profile_method: "userinfo_endpoint",
100 # client_auth_method: "client_secret_post",
101 },
102 },
103
104 # Central Authentication Scheme, a single-sign-on system. Note: this flow is now called 'SSO' in Matrix, we keep this name for legacy reasons.
105 # Refer to https://matrix.org/docs/spec/client_server/r0.6.1#sso-client-login
106 cas: {
107 # whether to enable the CAS proxy (ie. connect to hswaw sso via OAuth)
108 enable: false,
109 # generate client ID and secret in with your OAuth2 provider, refer to https://www.oauth.com/oauth2-servers/client-registration/client-id-secret/
110 oauth2: {
111 clientID: error "cas.oauth2.clientID must be set",
112 clientSecret: error "cas.oauth2.clientSecret must be set",
113 scope: error "cas.oauth2.scope must be set",
114 authorizeURL: error "cas.oauth2.authorizeURL must be set",
115 tokenURL: error "cas.oauth2.tokenURL must be set",
116 userinfoURL: error "cas.oauth2.userinfoURL must be set",
117 },
118 },
119
120 # Serve /.well-known/matrix configuration endpoints required when using
121 # cfg.webDomain directly as mxid.
122 wellKnown: false,
123 },
124
Piotr Dobrowolskifb119aa2021-01-31 19:07:32 +0100125 # DEPRECATED: this needs to be removed in favor of namespace.Contain() in
126 # modules that depend on this (appservices/instance defintions)
127 metadata(component):: {
128 namespace: cfg.namespace,
129 labels: {
130 "app.kubernetes.io/name": "matrix",
131 "app.kubernetes.io/managed-by": "kubecfg",
132 "app.kubernetes.io/component": component,
133 },
134 },
135
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100136 namespace: kube.Namespace(cfg.namespace),
137
138 postgres3: postgres {
139 cfg+: {
140 namespace: cfg.namespace,
141 appName: "synapse",
142 database: "synapse",
143 username: "synapse",
144 prefix: "waw3-",
145 password: { secretKeyRef: { name: "synapse", key: "postgres_password" } },
146 storageClassName: cfg.storageClassName,
147 storageSize: "100Gi",
148 initdbArgs: "--encoding='UTF8' --lc-collate='C' --lc-ctype='C'",
149 },
150 },
151
152 redis: redis {
153 cfg+: {
154 namespace: cfg.namespace,
155 appName: "synapse",
156 storageClassName: cfg.storageClassName,
157 password: { secretKeyRef: { name: "synapse", key: "redis_password" } },
158 },
159 },
160
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100161 riot: riot {
162 ns: app.namespace,
163 cfg+: {
164 webDomain: cfg.webDomain,
165 serverName: cfg.serverName,
166 image: cfg.images.riot,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100167 },
168 },
169
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100170 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 +0100171 else if cfg.cas.enable then cas {
172 ns: app.namespace,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100173 cfg+: {
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100174 image: cfg.images.casProxy,
175 webDomain: cfg.webDomain,
176 oauth2: cfg.cas.oauth2,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100177 },
178 },
179
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100180 wellKnown: if cfg.wellKnown then wellKnown {
181 ns: app.namespace,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100182 cfg+: {
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100183 image: cfg.images.wellKnown,
184 webDomain: cfg.webDomain,
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100185 },
186 } else {},
187
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100188 synapse: synapse {
189 ns: app.namespace,
190 postgres: app.postgres3,
191 redis: app.redis,
192 appservices: app.appservices,
193 cfg+: app.cfg {
194 image: app.cfg.images.synapse,
195
196 macaroonSecretKey: { secretKeyRef: { name: "synapse", key: "macaroon_secret_key" } },
197 registrationSharedSecret: { secretKeyRef: { name: "synapse", key: "registration_shared_secret" } },
198 workerReplicationSecret: { secretKeyRef: { name: "synapse", key: "worker_replication_secret" } },
199 },
200 },
201
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100202 // Any appservice you add here will require an appservice-X-registration
203 // secret containing a registration.yaml file. Adding something to this
204 // dictionary will cause Synapse to not start until that secret is
205 // available - so change things carefully!
206 // If bootstrapping a new appservice, just keep it out of this dictionary
207 // until it spits you a registration YAML and you feed that to a secret.
208 appservices: {},
209
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100210 ingress: app.namespace.Contain(kube.Ingress("matrix")) {
211 metadata+: {
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100212 annotations+: {
213 "kubernetes.io/tls-acme": "true",
214 "certmanager.k8s.io/cluster-issuer": "letsencrypt-prod",
215 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
216 "nginx.ingress.kubernetes.io/use-regex": "true",
217 },
218 },
219 spec+: {
220 tls: [
221 {
222 hosts: [cfg.webDomain],
223 secretName: "synapse-tls",
224 },
225 ],
226 rules: [
227 {
228 host: cfg.webDomain,
229 http: {
230 paths: [
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100231 { path: path, backend: app.synapse.genericWorker.svc.name_port }
232 for path in app.synapse.genericWorker.paths
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100233 ] + [
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100234 { path: "/", backend: app.riot.svc.name_port },
235 { path: "/_matrix/media/", backend: app.synapse.mediaWorker.svc.name_port },
236 { path: "/_matrix/", backend: app.synapse.main.svc.name_port },
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100237
238 # Used by OpenID Connect login flow
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100239 { path: "/_synapse/", backend: app.synapse.main.svc.name_port },
Piotr Dobrowolski8ec86572021-01-30 13:06:07 +0100240 ] + (if cfg.cas.enable then [
241 { path: "/_cas", backend: app.cas.svc.name_port },
242 ] else []) + (if cfg.wellKnown then [
243 { path: "/.well-known/matrix", backend: app.wellKnown.svc.name_port },
244 ] else [])
245 },
246 }
247 ],
248 },
249 },
250}