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
|
{ config, pkgs, lib, ... }:
let
inherit (lib)
any attrNames concatMapStringsSep concatStringsSep filterAttrs
genAttrs hasPrefix isString mapAttrsToList mkDefault
mkEnableOption mkIf mkOption mkOptionType mkOverride optionalString
recursiveUpdate ;
inherit (lib.types)
attrsOf bool either enum int lines listOf nullOr package path
str submodule unspecified ;
localIcinga = config.nixsap.apps.icinga2.enable;
cfg = config.nixsap.apps.icingaweb2;
attrs = opts: submodule { options = opts; };
mandatory = t: mkOption { type = t; };
optional = t: mkOption { type = nullOr t; default = null; };
default = d: t: mkOption { type = t; default = d; };
explicit = filterAttrs (n: v: n != "_module" && v != null);
show = v: optionalString (v != null) (toString v);
permission =
let
allowed =
[
"config/authentication/groups"
"config/authentication/roles/show"
"config/authentication/users"
"module"
"monitoring/command"
];
in mkOptionType {
name = "string starting with one of ${concatMapStringsSep ", " (s: ''"${s}"'') allowed}";
check = x: isString x && any (p: hasPrefix p x) allowed;
};
role = attrs {
users = default [] (listOf str);
groups = default [] (listOf str);
permissions = mandatory (listOf permission);
objects = optional str;
};
database = attrs {
db = mandatory str;
host = mandatory str;
passfile = optional path;
port = optional int;
type = mandatory (enum [ "mysql" "pgsql" ]);
user = mandatory str;
};
configIni = pkgs.writeText "config.ini" ''
[global]
show_stacktraces = "${if cfg.stacktrace then "1" else "0"}"
config_backend = "db"
config_resource = "icingaweb2db"
[logging]
level = "${cfg.logLevel}"
${if cfg.log == "syslog" then ''
log = "syslog"
application = "icingaweb2"
'' else ''
log = "file"
file = "${cfg.log}"
''
}
'';
# XXX Livestatus is not supported by IcingaWeb2 (2.1.0)
# https://dev.icinga.org/issues/8254
# "We'll postpone this issue because Icinga 2.4 will introduce
# an API for querying monitoring data. Maybe we drop support
# for Livestatus completely"
modules.monitoring.backendsIni = pkgs.writeText "backends.ini" ''
[icinga2]
type = "ido"
resource = "icinga2db"
'';
modules.monitoring.configIni = pkgs.writeText "config.ini" ''
[security]
protected_customvars = "${concatStringsSep "," cfg.protectedCustomVars}"
'';
modules.monitoring.commandtransportsIni = pkgs.writeText "commandtransports.ini" ''
${optionalString localIcinga ''
[local]
transport = "local"
path = "${config.nixsap.apps.icinga2.commandPipe}"
''
}
'';
groupsIni = pkgs.writeText "groups.ini" (
optionalString (cfg.authentication == "database") ''
[database]
backend = "db"
resource = "icingaweb2db"
''
);
authenticationIni = pkgs.writeText "authentication.ini" (
if cfg.authentication == "sproxy" then ''
[sproxy]
backend = "sproxy"
'' else ''
[database]
backend = "db"
resource = "icingaweb2db"
''
);
rolesIni = pkgs.writeText "roles.ini" ''
[root]
users = "root"
permissions = "config/authentication/roles/show, config/authentication/users/*, config/authentication/groups/*, module/*, monitoring/command/*"
${
concatStringsSep "\n\n" (
mapAttrsToList (n: s: ''
[${n}]
users = "${concatStringsSep ", " s.users}"
groups = "${concatStringsSep ", " s.groups}"
permissions = "${concatStringsSep ", " s.permissions}"
${optionalString (s.objects != null) ''
monitoring/filter/objects = "${s.objects}"
''}
'') (explicit cfg.roles)
)
}
'';
mkResource = name: opts:
''
cat <<'__EOF__'
[${name}]
type = "db"
db = "${opts.type}"
dbname = "${opts.db}"
host = "${opts.host}"
port = "${show opts.port}"
username = "${opts.user}"
__EOF__
${optionalString (opts.passfile != null) ''
pwd=$(cat '${opts.passfile}')
printf 'password="%s"\n' "$pwd"
''}
'';
genResourcesIni = pkgs.writeBashScript "resources" (concatStringsSep "\n" (
mapAttrsToList mkResource (explicit cfg.resources)
));
defaultPool = {
pm.max_children = 10;
pm.max_requests = 1000;
pm.max_spare_servers = 5;
pm.min_spare_servers = 3;
pm.strategy = "dynamic";
};
configureFiles = ''
set -euo pipefail
umask 0277
mkdir -p '${cfg.configDir}'
${pkgs.findutils}/bin/find \
${cfg.configDir} \
-mindepth 1 -maxdepth 1 \
-not -name dashboards \
-not -name preferences \
-exec rm -rf '{}' \; || true
mkdir -p '${cfg.configDir}/dashboards'
mkdir -p '${cfg.configDir}/preferences'
mkdir -p '${cfg.configDir}/enabledModules'
mkdir -p '${cfg.configDir}/modules/monitoring'
ln -sf '${pkgs.icingaweb2}/modules/monitoring' '${cfg.configDir}/enabledModules/monitoring'
${genResourcesIni} > '${cfg.configDir}/resources.ini'
ln -sf '${authenticationIni}' '${cfg.configDir}/authentication.ini'
ln -sf '${configIni}' '${cfg.configDir}/config.ini'
ln -sf '${groupsIni}' '${cfg.configDir}/groups.ini'
ln -sf '${rolesIni}' '${cfg.configDir}/roles.ini'
ln -sf '${modules.monitoring.backendsIni}' \
'${cfg.configDir}/modules/monitoring/backends.ini'
ln -sf '${modules.monitoring.configIni}' \
'${cfg.configDir}/modules/monitoring/config.ini'
ln -sf '${modules.monitoring.commandtransportsIni}' \
'${cfg.configDir}/modules/monitoring/commandtransports.ini'
chmod u=rX,g=,o= '${cfg.configDir}'
chmod -R u=rwX,g=,o= '${cfg.configDir}/dashboards'
chmod -R u=rwX,g=,o= '${cfg.configDir}/preferences'
chown -R icingaweb2:icingaweb2 '${cfg.configDir}'
'';
configureDB = with cfg.resources.icingaweb2db; {
mysql =
let
mkMyCnf = pkgs.writeBashScript "my.cnf.sh" ''
cat <<'__EOF__'
[client]
host = ${host}
${optionalString (port != null) "port = ${toString port}"}
user = ${user}
__EOF__
${optionalString (passfile != null) ''
pwd=$(cat '${passfile}')
printf 'password = %s\n' "$pwd"
''}
'';
mysql = "${pkgs.mysql.client}/bin/mysql";
in pkgs.writeBashScript "configureMySQL" ''
set -euo pipefail
cnf=$(mktemp)
trap 'rm -f "$cnf"' EXIT
chmod 0600 "$cnf"
${mkMyCnf} > "$cnf"
#shellcheck disable=SC2016
while ! ${mysql} --defaults-file="$cnf" -e 'CREATE DATABASE IF NOT EXISTS `${db}`'; do
sleep 5s
done
tt=$(${mysql} --defaults-file="$cnf" -N -e 'SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "${db}"')
if [ "$tt" -eq 0 ]; then
${mysql} --defaults-file="$cnf" -v '${db}' < '${pkgs.icingaweb2}/etc/schema/mysql.schema.sql'
${optionalString (cfg.initialRootPasswordHash != "") ''
#shellcheck disable=SC2016
${mysql} --defaults-file="$cnf" -e \
'INSERT INTO icingaweb_user (name, active, password_hash) VALUES ("root", 1, "${cfg.initialRootPasswordHash}")' '${db}'
''
}
fi
'';
pgsql =
let
psql = pkgs.writeBashScript "icingaweb2pgsql" ''
exec ${pkgs.postgresql}/bin/psql \
-v ON_ERROR_STOP=1 \
--no-password \
--host='${host}' \
--port=${toString port} \
--dbname='${db}' \
--username='${user}' \
"$@"
'';
in pkgs.writeBashScript "configurePgSQL" ''
set -euo pipefail
${optionalString (passfile != null) ''
pgpass=$(mktemp)
trap 'rm -f "$pgpass"' EXIT
chmod 0600 "$cnf"
printf '*:*:*:*:' > "$pgpass"
cat '${passfile}' >> "$pgpass"
export PGPASSFILE="$pgpass"
''}
#shellcheck disable=SC2016
while ! ${psql} -c ';'; do
sleep 5s
done
tt=$(${psql} --tuples-only -c "SELECT count(*) FROM pg_class WHERE relname='icingaweb_user'")
if [ "$tt" -eq 0 ]; then
${psql} --single-transaction -f '${pkgs.icingaweb2}/etc/schema/pgsql.schema.sql'
${optionalString (cfg.initialRootPasswordHash != "") ''
${psql} -c \
"INSERT INTO icingaweb_user (name, active, password_hash) VALUES ('root', 1, '${cfg.initialRootPasswordHash}')"
''
}
fi
'';
};
keys = [ cfg.resources.icingaweb2db.passfile
cfg.resources.icinga2db.passfile ];
in {
options.nixsap.apps.icingaweb2 = {
enable = mkEnableOption "Icinga Web 2";
user = mkOption {
description = ''
The user the PHP-FPM pool runs as. And the owner of files.
'';
default = "icingaweb2";
type = str;
};
nginxServer = mkOption {
type = lines;
default = "";
example = ''
listen 8080;
server_name icinga.example.net;
'';
};
configDir = mkOption {
description = "Where to put config files. This directory will be created if does not exist.";
type = path;
default = "/icingaweb2";
};
php-fpm = {
package = mkOption {
description = "PHP package to use";
type = package;
default = pkgs.php;
};
pool = mkOption {
description = "Options for the PHP FPM pool";
type = attrsOf unspecified;
default = {};
};
};
resources = mkOption {
description = "Composes resources.ini";
type = attrs {
icingaweb2db = mkOption {
description = "Database for Icinga Web 2 settings";
type = database;
};
icinga2db = mkOption {
description = "Icinga2 database (read-only)";
type = database;
};
};
};
authentication = mkOption {
description = ''
Authentication backend: either IcingaWeb2 database or Sproxy.
'';
type = enum [ "sproxy" "database" ];
default = "database";
};
protectedCustomVars = mkOption {
description = ''
Icinga2 custom variables to be masked in WebUI.
This can used for example to hide passwords. Wildcard are allowed.
'';
type = listOf str;
default = [ "*pass*" "*pw*" "community" "http*auth_pair" ];
};
roles = mkOption {
description = "Composes roles.ini";
type = attrsOf role;
default = {};
example = {
devops = {
groups = [ "devops" ];
permissions = [ "module/*" "monitoring/command/*" ];
};
all = {
groups = [ "all" ];
permissions = [ "module/*" ];
objects = "hostgroup_name=Shops";
};
};
};
initialRootPasswordHash = mkOption {
description = ''
Initial root password for icingaweb2db.
Use <literal>openssl passwd -1 mysecret</literal>
to generate this hash. It is used only when database
does not exist. So you may choose not to keep/commit
this hash at all. You better change the root password
after the first login.
'';
type = str;
default = "";
};
stacktrace = mkOption {
description = "whether to show PHP stacktraces";
type = bool;
default = false;
};
log = mkOption {
type = either path (enum [ "syslog" ]);
default = "syslog";
};
logLevel = mkOption {
type = enum [ "INFO" "WARNING" "ERROR" "CRITICAL" "DEBUG" ];
default = "WARNING";
};
};
config = mkIf cfg.enable {
nixsap.deployment.keyrings.root = keys;
users.users.${config.nixsap.apps.nginx.user}.extraGroups = [ cfg.user ];
users.users.icingaweb2.extraGroups = mkIf localIcinga [ config.nixsap.apps.icinga2.commandGroup ];
nixsap.apps.php-fpm.icingaweb2 = mkOverride 0 {
inherit (cfg) user;
inherit (cfg.php-fpm) package;
pool = recursiveUpdate defaultPool cfg.php-fpm.pool;
};
nixsap.apps.nginx.conf.http.servers.icingaweb2 = ''
${cfg.nginxServer}
root ${pkgs.icingaweb2}/public;
index index.php;
try_files $1 $uri $uri/ /index.php$is_args$args;
location ~ ^/index\.php(.*)$ {
fastcgi_pass unix:${config.nixsap.apps.php-fpm.icingaweb2.pool.listen.socket};
fastcgi_index index.php;
include ${pkgs.nginx}/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME ${pkgs.icingaweb2}/public/index.php;
fastcgi_param ICINGAWEB_CONFIGDIR ${cfg.configDir};
fastcgi_param REMOTE_USER $remote_user;
}
'';
systemd.services.icingaweb2cfg = {
description = "configure Icinga Web 2";
after = [ "network.target" "local-fs.target" "keys.target" ];
wants = [ "keys.target" ];
wantedBy = [ "multi-user.target" ];
preStart = configureFiles;
serviceConfig = {
ExecStart = configureDB.${cfg.resources.icingaweb2db.type};
PermissionsStartOnly = true;
RemainAfterExit = true;
User = "icingaweb2";
};
};
};
}
|