1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
concatMapStringsSep
filter
filterAttrs
getExe
getExe'
isAttrs
isList
mapAttrs
mkEnableOption
mkIf
mkOption
mkPackageOption
optional
types
;
cfg = config.services.matrix-authentication-service;
format = pkgs.formats.yaml { };
filterRecursiveNull =
o:
if isAttrs o then
mapAttrs (_: v: filterRecursiveNull v) (filterAttrs (_: v: v != null) o)
else if isList o then
map filterRecursiveNull (filter (v: v != null) o)
else
o;
# remove null values from the final configuration
finalSettings =
let
pruned = filterRecursiveNull cfg.settings;
in
if pruned ? upstream_oauth2 && pruned.upstream_oauth2 == { } then
removeAttrs pruned [ "upstream_oauth2" ]
else
pruned;
configFile = format.generate "config.yaml" finalSettings;
extraConfigFiles = lib.imap0 (
i: _: "\${CREDENTIALS_DIRECTORY}/config-${toString i}"
) cfg.extraConfigFiles;
runtimeConfig = "/run/matrix-authentication-service/config.yaml";
in
{
meta.maintainers = with lib.maintainers; [
eymeric
flashonfire
mkoppmann
skowalak
];
meta.teams = [ lib.teams.matrix ];
options.services.matrix-authentication-service = {
enable = mkEnableOption "Matrix Authentication Service";
package = mkPackageOption pkgs "matrix-authentication-service" { };
settings = mkOption {
default = { };
description = ''
The primary mas configuration. See the
[configuration reference](https://element-hq.github.io/matrix-authentication-service/usage/configuration.html)
for possible values.
Secrets should be passed in by using the `extraConfigFiles` option.
'';
type = types.submodule {
freeformType = format.type;
options = {
http.public_base = mkOption {
type = types.str;
default = "http://[::]:8080/";
description = ''
Public URL base used when building absolute public URLs.
'';
};
http.trusted_proxies = mkOption {
type = types.listOf types.str;
default = [
"127.0.0.1/8"
"::1/128"
];
description = ''
MAS can infer the client IP address from the X-Forwarded-For header. It will trust the value for this header only if the request comes from a trusted reverse proxy listed here.
'';
};
http.listeners = mkOption {
type = types.listOf (
types.submodule {
freeformType = format.type;
options = {
name = mkOption {
type = types.str;
example = "web";
description = ''
The name of the listener, used in logs and metrics.
'';
};
proxy_protocol = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable the PROXY protocol on the listener.
'';
};
resources = mkOption {
type = types.listOf (
types.submodule {
freeformType = format.type;
options = {
name = mkOption {
type = types.str;
description = ''
Serve the given resource.
'';
};
};
}
);
description = ''
List of resources to serve.
'';
};
binds = mkOption {
type = types.listOf (
types.submodule {
freeformType = format.type;
options = {
host = mkOption {
type = types.nullOr types.str;
description = ''
Listen on the given host.
'';
};
port = mkOption {
type = types.nullOr types.port;
description = ''
Listen on the given port.
'';
};
};
}
);
description = ''
List of addresses and ports to listen to.
'';
};
};
}
);
default = [
{
name = "web";
resources = [
{ name = "discovery"; }
{ name = "human"; }
{ name = "oauth"; }
{ name = "compat"; }
{ name = "graphql"; }
{ name = "assets"; }
];
binds = [
{
host = "0.0.0.0";
port = 8080;
}
];
proxy_protocol = false;
}
{
name = "internal";
resources = [
{ name = "health"; }
];
binds = [
{
host = "0.0.0.0";
port = 8081;
}
];
proxy_protocol = false;
}
];
description = ''
Each listener can serve multiple resources, and listen on multiple TCP ports or UNIX sockets.
'';
};
database.uri = mkOption {
type = types.str;
default = "postgresql:///matrix-authentication-service?host=/run/postgresql";
description = ''
The postgres connection string.
Refer to <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING>.
If you need to put secrets in the uri, please use the `extraConfigFiles` option.
'';
};
database.max_connections = mkOption {
type = types.ints.unsigned;
default = 10;
description = ''
Maximum number of connections for the connection pool.
'';
};
database.min_connections = mkOption {
type = types.ints.unsigned;
default = 0;
description = ''
Minimum number of connections for the connection pool.
'';
};
database.connect_timeout = mkOption {
type = types.ints.unsigned;
default = 30;
description = ''
Connection timeout for the connection pool.
'';
};
database.idle_timeout = mkOption {
type = types.ints.unsigned;
default = 600;
description = ''
Idle timeout for the connection pool.
'';
};
database.max_lifetime = mkOption {
type = types.ints.unsigned;
default = 1800;
description = ''
Maximum lifetime for the connection pool.
'';
};
passwords.enabled = mkOption {
type = types.bool;
default = true;
description = ''
Whether to enable the password database. If disabled, users will only be able to log in using upstream OIDC providers.
'';
};
passwords.schemes = mkOption {
type = types.listOf (
types.submodule {
freeformType = format.type;
options = {
version = mkOption {
type = types.ints.unsigned;
description = ''
Password scheme version.
'';
};
algorithm = mkOption {
type = types.str;
description = ''
Password scheme algorithm.
'';
};
};
}
);
default = [
{
version = 1;
algorithm = "argon2id";
}
];
description = ''
List of password hashing schemes being used. Only change this if you know what you're doing.
'';
};
passwords.minimum_complexity = mkOption {
type = types.enum [
0
1
2
3
4
];
default = 3;
description = ''
Minimum complexity required for passwords, estimated by the zxcvbn algorithm.
Must be between 0 and 4, default is 3. See <https://github.com/dropbox/zxcvbn#usage> for more information.
'';
};
matrix.homeserver = mkOption {
type = types.str;
default = "";
description = ''
Corresponds to the server_name in the Synapse configuration file.
'';
};
matrix.endpoint = mkOption {
type = types.str;
default = "";
description = ''
The URL to which the homeserver is accessible from the service.
'';
};
upstream_oauth2.providers = mkOption {
default = null;
type = types.nullOr (
types.listOf (
types.submodule {
freeformType = format.type;
options = {
id = mkOption {
type = types.nullOr types.str;
example = "01H8PKNWKKRPCBW4YGH1RWV279";
default = null;
description = ''
Unique id for the provider, must be a ULID, and can be generated using online tools like <https://www.ulidtools.com>.
'';
};
};
}
)
);
description = ''
Configuration of upstream providers
'';
};
};
};
};
createDatabase = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable and configure `services.postgresql` to ensure that the database user `matrix-authentication-service`
and the database `matrix-authentication-service` exist.
'';
};
extraConfigFiles = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Extra config files to include.
The configuration files will be included based on the command line
argument --config. This allows to configure secrets without
having to go through the Nix store, e.g. based on deployment keys if
NixOps is in use.
'';
};
serviceDependencies = mkOption {
type = types.listOf types.str;
default = optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnit;
defaultText = lib.literalExpression ''
lib.optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnit
'';
description = ''
List of Systemd services to require and wait for when starting the application service,
such as the Matrix homeserver if it's running on the same host.
'';
};
credentials = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
default = { };
description = ''
Mapping of credential name to source file-path. Exposed to the unit via LoadCredential and
readable inside the service at `''${CREDENTIALS_DIRECTORY}/<name>`.
For example:
```
services.matrix-authentication-service.credentials."synapse-secret" = "/run/agenix/synapse-shared";
services.matrix-authentication-service.settings.matrix.secret_file =
"\''${CREDENTIALS_DIRECTORY}/synapse-secret";
```
'';
};
};
config = mkIf cfg.enable {
services.postgresql = mkIf cfg.createDatabase {
enable = true;
ensureDatabases = [ "matrix-authentication-service" ];
ensureUsers = [
{
name = "matrix-authentication-service";
ensureDBOwnership = true;
}
];
};
systemd.services.matrix-authentication-service = rec {
after = optional cfg.createDatabase "postgresql.service" ++ cfg.serviceDependencies;
wants = after;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
DynamicUser = true;
LoadCredential =
(lib.imap0 (i: path: "config-${toString i}:${path}") cfg.extraConfigFiles)
++ (lib.mapAttrsToList (name: path: "${name}:${path}") cfg.credentials);
ExecStartPre = pkgs.writeShellScript "mas-prepare" ''
${getExe' pkgs.gettext "envsubst"} \
'$CREDENTIALS_DIRECTORY' \
< ${configFile} \
> /run/matrix-authentication-service/config.yaml
${getExe cfg.package} config check --config ${runtimeConfig} \
${concatMapStringsSep " " (x: "--config ${x}") extraConfigFiles}
'';
ExecStart = ''
${getExe cfg.package} server --config ${runtimeConfig} \
${concatMapStringsSep " " (x: "--config ${x}") extraConfigFiles}
'';
Restart = "on-failure";
RestartSec = "1s";
# Security Hardening
CapabilityBoundingSet = "";
AmbientCapabilities = "";
LockPersonality = true;
NoNewPrivileges = true;
PrivateTmp = true;
PrivateDevices = true;
PrivateMounts = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "invisible";
ProcSubset = "pid";
ProtectSystem = "strict";
RemoveIPC = true;
RestrictAddressFamilies = [
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallErrorNumber = "EPERM";
SystemCallFilter = [
"@system-service"
];
UMask = "0077";
# Working and state directories
StateDirectory = "matrix-authentication-service";
StateDirectoryMode = "0700";
RuntimeDirectory = "matrix-authentication-service";
WorkingDirectory = "/var/lib/matrix-authentication-service";
};
};
};
}
|