aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2019-12-08 16:22:56 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2019-12-08 16:22:56 +0100
commit16b80abd57bb215d0e72f3983f997a007743b8fb (patch)
tree19b24a9c9e2334c069721671e4e2a5d53d8efaec
parent6015a3b52c3b155ac444aeb39950c38a5e653101 (diff)
downloadNix-16b80abd57bb215d0e72f3983f997a007743b8fb.tar.gz
Nix-16b80abd57bb215d0e72f3983f997a007743b8fb.tar.zst
Nix-16b80abd57bb215d0e72f3983f997a007743b8fb.zip
Add openldap replication
-rw-r--r--modules/private/databases/openldap/default.nix29
-rw-r--r--modules/private/databases/openldap/eldiron_schemas.nix21
-rw-r--r--modules/private/databases/openldap_replication.nix164
-rw-r--r--modules/private/default.nix1
-rw-r--r--modules/private/monitoring/default.nix23
-rw-r--r--modules/private/monitoring/objects_backup-2.nix33
-rw-r--r--modules/private/monitoring/objects_common.nix1
-rwxr-xr-xmodules/private/monitoring/plugins/check_openldap_replication54
-rw-r--r--modules/private/system/backup-2.nix12
9 files changed, 306 insertions, 32 deletions
diff --git a/modules/private/databases/openldap/default.nix b/modules/private/databases/openldap/default.nix
index 9f72b29..22f6f7b 100644
--- a/modules/private/databases/openldap/default.nix
+++ b/modules/private/databases/openldap/default.nix
@@ -2,22 +2,9 @@
2let 2let
3 cfg = config.myServices.databases.openldap; 3 cfg = config.myServices.databases.openldap;
4 ldapConfig = let 4 ldapConfig = let
5 kerberosSchema = pkgs.fetchurl { 5 eldiron_schemas = pkgs.callPackage ./eldiron_schemas.nix {};
6 url = "https://raw.githubusercontent.com/krb5/krb5/master/src/plugins/kdb/ldap/libkdb_ldap/kerberos.schema";
7 sha256 = "17fnkkf6s3lznsl7wp6914pqsc78d038rh38l638big8z608ksww";
8 };
9 puppetSchema = pkgs.fetchurl {
10 url = "https://raw.githubusercontent.com/puppetlabs/puppet/master/ext/ldap/puppet.schema";
11 sha256 = "11bjf5zfvqlim7p9vddcafs0wiq3v8ys77x8h6fbp9c6bdfh0awh";
12 };
13 in '' 6 in ''
14 include ${pkgs.openldap}/etc/schema/core.schema 7 ${eldiron_schemas}
15 include ${pkgs.openldap}/etc/schema/cosine.schema
16 include ${pkgs.openldap}/etc/schema/inetorgperson.schema
17 include ${pkgs.openldap}/etc/schema/nis.schema
18 include ${puppetSchema}
19 include ${kerberosSchema}
20 include ${./immae.schema}
21 8
22 pidfile ${cfg.pids.pid} 9 pidfile ${cfg.pids.pid}
23 argsfile ${cfg.pids.args} 10 argsfile ${cfg.pids.args}
@@ -33,6 +20,10 @@ let
33 directory ${cfg.dataDir} 20 directory ${cfg.dataDir}
34 overlay memberof 21 overlay memberof
35 22
23 moduleload syncprov
24 overlay syncprov
25 syncprov-checkpoint 100 10
26
36 TLSCertificateFile ${config.security.acme.directory}/ldap/cert.pem 27 TLSCertificateFile ${config.security.acme.directory}/ldap/cert.pem
37 TLSCertificateKeyFile ${config.security.acme.directory}/ldap/key.pem 28 TLSCertificateKeyFile ${config.security.acme.directory}/ldap/key.pem
38 TLSCACertificateFile ${config.security.acme.directory}/ldap/fullchain.pem 29 TLSCACertificateFile ${config.security.acme.directory}/ldap/fullchain.pem
@@ -126,14 +117,6 @@ in
126 users.users.openldap.extraGroups = [ "keys" ]; 117 users.users.openldap.extraGroups = [ "keys" ];
127 networking.firewall.allowedTCPPorts = [ 636 389 ]; 118 networking.firewall.allowedTCPPorts = [ 636 389 ];
128 119
129 services.cron = {
130 systemCronJobs = [
131 ''
132 35 1,13 * * * root ${pkgs.openldap}/bin/slapcat -v -b "dc=immae,dc=eu" -f ${pkgs.writeText "slapd.conf" ldapConfig} -l ${cfg.dataDir}/backup.ldif | ${pkgs.gnugrep}/bin/grep -v "^# id=[0-9a-f]*$"
133 ''
134 ];
135 };
136
137 security.acme.certs."ldap" = config.myServices.databasesCerts // { 120 security.acme.certs."ldap" = config.myServices.databasesCerts // {
138 user = "openldap"; 121 user = "openldap";
139 group = "openldap"; 122 group = "openldap";
diff --git a/modules/private/databases/openldap/eldiron_schemas.nix b/modules/private/databases/openldap/eldiron_schemas.nix
new file mode 100644
index 0000000..7a29988
--- /dev/null
+++ b/modules/private/databases/openldap/eldiron_schemas.nix
@@ -0,0 +1,21 @@
1{ fetchurl, openldap }:
2let
3 kerberosSchema = fetchurl {
4 url = "https://raw.githubusercontent.com/krb5/krb5/master/src/plugins/kdb/ldap/libkdb_ldap/kerberos.schema";
5 sha256 = "17fnkkf6s3lznsl7wp6914pqsc78d038rh38l638big8z608ksww";
6 };
7 puppetSchema = fetchurl {
8 url = "https://raw.githubusercontent.com/puppetlabs/puppet/master/ext/ldap/puppet.schema";
9 sha256 = "11bjf5zfvqlim7p9vddcafs0wiq3v8ys77x8h6fbp9c6bdfh0awh";
10 };
11 schemas = [
12 "${openldap}/etc/schema/core.schema"
13 "${openldap}/etc/schema/cosine.schema"
14 "${openldap}/etc/schema/inetorgperson.schema"
15 "${openldap}/etc/schema/nis.schema"
16 puppetSchema
17 kerberosSchema
18 ./immae.schema
19 ];
20in
21 builtins.concatStringsSep "\n" (map (v: "include ${v}") schemas)
diff --git a/modules/private/databases/openldap_replication.nix b/modules/private/databases/openldap_replication.nix
new file mode 100644
index 0000000..c0c16e6
--- /dev/null
+++ b/modules/private/databases/openldap_replication.nix
@@ -0,0 +1,164 @@
1{ pkgs, config, myconfig, lib, ... }:
2let
3 cfg = config.myServices.databasesReplication.openldap;
4 eldiron_schemas = pkgs.callPackage ./openldap/eldiron_schemas.nix {};
5 ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" ''
6 ${eldiron_schemas}
7 pidfile /run/slapd_${name}/slapd.pid
8 argsfile /run/slapd_${name}/slapd.args
9
10 moduleload back_hdb
11 backend hdb
12 database hdb
13
14 suffix "${hcfg.base}"
15 rootdn "cn=root,${hcfg.base}"
16 directory ${cfg.base}/${name}/openldap
17
18 index objectClass eq
19 index uid pres,eq
20 index entryUUID eq
21
22 include ${config.secrets.location}/openldap_replication/${name}/replication_config
23 '';
24in
25{
26 options.myServices.databasesReplication.openldap = {
27 enable = lib.mkEnableOption "Enable openldap replication";
28 base = lib.mkOption {
29 type = lib.types.path;
30 description = ''
31 Base path to put the replications
32 '';
33 };
34 hosts = lib.mkOption {
35 default = {};
36 description = ''
37 Hosts to backup
38 '';
39 type = lib.types.attrsOf (lib.types.submodule {
40 options = {
41 package = lib.mkOption {
42 type = lib.types.package;
43 default = pkgs.openldap;
44 description = ''
45 Openldap package for this host
46 '';
47 };
48 url = lib.mkOption {
49 type = lib.types.str;
50 description = ''
51 Host to connect to
52 '';
53 };
54 base = lib.mkOption {
55 type = lib.types.str;
56 description = ''
57 Base DN to replicate
58 '';
59 };
60 dn = lib.mkOption {
61 type = lib.types.str;
62 description = ''
63 DN to use
64 '';
65 };
66 password = lib.mkOption {
67 type = lib.types.str;
68 description = ''
69 Password to use
70 '';
71 };
72 };
73 });
74 };
75 };
76
77 config = lib.mkIf cfg.enable {
78 users.users.openldap = {
79 description = "Openldap database user";
80 group = "openldap";
81 uid = config.ids.uids.openldap;
82 extraGroups = [ "keys" ];
83 };
84 users.groups.openldap.gid = config.ids.gids.openldap;
85
86 secrets.keys = lib.flatten (lib.mapAttrsToList (name: hcfg: [
87 {
88 dest = "openldap_replication/${name}/replication_config";
89 user = "openldap";
90 group = "openldap";
91 permissions = "0400";
92 text = ''
93 syncrepl rid=000
94 provider=${hcfg.url}
95 type=refreshAndPersist
96 searchbase="${hcfg.base}"
97 retry="5 10 300 +"
98 attrs="*,+"
99 schemachecking=off
100 bindmethod=simple
101 binddn="${hcfg.dn}"
102 credentials="${hcfg.password}"
103 '';
104 }
105 {
106 dest = "openldap_replication/${name}/replication_password";
107 user = "openldap";
108 group = "openldap";
109 permissions = "0400";
110 text = hcfg.password;
111 }
112 ]) cfg.hosts);
113
114 services.cron = {
115 enable = true;
116 systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
117 let
118 dataDir = "${cfg.base}/${name}/openldap";
119 backupDir = "${cfg.base}/${name}/openldap_backup";
120 backup_script = pkgs.writeScript "backup_openldap_${name}" ''
121 #!${pkgs.stdenv.shell}
122
123 ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iseconds).ldif
124 '';
125 u = pkgs.callPackage ./utils.nix {};
126 cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir);
127 in [
128 "0 22,4,10,16 * * * root ${backup_script}"
129 "0 3 * * * root ${cleanup_script}"
130 ]) cfg.hosts);
131 };
132
133 system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
134 lib.attrsets.nameValuePair "openldap_replication_${name}" {
135 deps = [ "users" "groups" ];
136 text = ''
137 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap
138 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup
139 '';
140 }) cfg.hosts;
141
142 systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
143 let
144 dataDir = "${cfg.base}/${name}/openldap";
145 in
146 lib.attrsets.nameValuePair "openldap_backup_${name}" {
147 description = "Openldap replication for ${name}";
148 wantedBy = [ "multi-user.target" ];
149 after = [ "network.target" ];
150 unitConfig.RequiresMountsFor = dataDir;
151
152 preStart = ''
153 mkdir -p /run/slapd_${name}
154 chown -R "openldap:openldap" /run/slapd_${name}
155 '';
156
157 serviceConfig = {
158 ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}";
159 };
160 }) cfg.hosts;
161 };
162}
163
164
diff --git a/modules/private/default.nix b/modules/private/default.nix
index 1d0f1a9..29bf2af 100644
--- a/modules/private/default.nix
+++ b/modules/private/default.nix
@@ -13,6 +13,7 @@ set = {
13 postgresqlReplication = ./databases/postgresql_replication.nix; 13 postgresqlReplication = ./databases/postgresql_replication.nix;
14 mariadbReplication = ./databases/mariadb_replication.nix; 14 mariadbReplication = ./databases/mariadb_replication.nix;
15 redisReplication = ./databases/redis_replication.nix; 15 redisReplication = ./databases/redis_replication.nix;
16 openldapReplication = ./databases/openldap_replication.nix;
16 17
17 websites = ./websites; 18 websites = ./websites;
18 atenInte = ./websites/aten/integration.nix; 19 atenInte = ./websites/aten/integration.nix;
diff --git a/modules/private/monitoring/default.nix b/modules/private/monitoring/default.nix
index b3f8cbe..e44b127 100644
--- a/modules/private/monitoring/default.nix
+++ b/modules/private/monitoring/default.nix
@@ -22,6 +22,9 @@ let
22 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [ 22 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
23 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb 23 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
24 ]} 24 ]}
25 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
26 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
27 ]}
25 ''; 28 '';
26 toObjects = pkgs.callPackage ./to_objects.nix {}; 29 toObjects = pkgs.callPackage ./to_objects.nix {};
27 commonConfig = { 30 commonConfig = {
@@ -42,7 +45,7 @@ let
42 let 45 let
43 specific_file = ./. + "/objects_" + name + ".nix"; 46 specific_file = ./. + "/objects_" + name + ".nix";
44 in 47 in
45 lib.attrsets.optionalAttrs (builtins.pathExists specific_file) (pkgs.callPackage specific_file {}); 48 lib.attrsets.optionalAttrs (builtins.pathExists specific_file) (pkgs.callPackage specific_file { inherit config; });
46in 49in
47{ 50{
48 options = { 51 options = {
@@ -72,34 +75,38 @@ in
72 } 75 }
73 { 76 {
74 commands = [ 77 commands = [
75 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
76 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; } 78 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
77 ]; 79 ];
78 users = [ "naemon" ]; 80 users = [ "naemon" ];
81 runAs = "ALL";
82 }
83 {
84 commands = [
85 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
86 ];
87 users = [ "naemon" ];
79 runAs = "postgres"; 88 runAs = "postgres";
80 } 89 }
81 { 90 {
82 commands = [ 91 commands = [
83 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; } 92 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
84 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
85 ]; 93 ];
86 users = [ "naemon" ]; 94 users = [ "naemon" ];
87 runAs = "mysql"; 95 runAs = "mysql";
88 } 96 }
89 { 97 {
90 commands = [ 98 commands = [
91 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; } 99 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
92 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
93 ]; 100 ];
94 users = [ "naemon" ]; 101 users = [ "naemon" ];
95 runAs = "redis"; 102 runAs = "openldap";
96 } 103 }
97 { 104 {
98 commands = [ 105 commands = [
99 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; } 106 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
100 ]; 107 ];
101 users = [ "naemon" ]; 108 users = [ "naemon" ];
102 runAs = "backup"; 109 runAs = "redis";
103 } 110 }
104 ]; 111 ];
105 environment.etc."mdadm.conf" = { 112 environment.etc."mdadm.conf" = {
diff --git a/modules/private/monitoring/objects_backup-2.nix b/modules/private/monitoring/objects_backup-2.nix
index c302e45..2b80eee 100644
--- a/modules/private/monitoring/objects_backup-2.nix
+++ b/modules/private/monitoring/objects_backup-2.nix
@@ -1,4 +1,4 @@
1{ ... }: 1{ config, pkgs, ... }:
2{ 2{
3 service = [ 3 service = [
4 { 4 {
@@ -46,5 +46,36 @@
46 use = "local-service"; 46 use = "local-service";
47 check_command = ["check_last_file_date" "/backup2/eldiron/mysql_backup" "7" "mysql"]; 47 check_command = ["check_last_file_date" "/backup2/eldiron/mysql_backup" "7" "mysql"];
48 } 48 }
49 {
50 service_description = "Openldap replication for eldiron is up to date";
51 use = "local-service";
52 check_command = let
53 name = "eldiron";
54 hcfg = config.myServices.databasesReplication.openldap.hosts.eldiron;
55 base = config.myServices.databasesReplication.openldap.base;
56 eldiron_schemas = pkgs.callPackage ../databases/openldap/eldiron_schemas.nix {};
57 ldapConfig = pkgs.writeText "slapd.conf" ''
58 ${eldiron_schemas}
59 moduleload back_hdb
60 backend hdb
61 database hdb
62
63 suffix "${hcfg.base}"
64 directory ${base}/${name}/openldap
65 '';
66 in [
67 "check_openldap_replication"
68 hcfg.url
69 hcfg.dn
70 "${config.secrets.location}/openldap_replication/eldiron/replication_password"
71 hcfg.base
72 ldapConfig
73 ];
74 }
75 {
76 service_description = "Last openldap dump in /backup2/eldiron/openldap_backup is not too old";
77 use = "local-service";
78 check_command = ["check_last_file_date" "/backup2/eldiron/openldap_backup" "7" "openldap"];
79 }
49 ]; 80 ];
50} 81}
diff --git a/modules/private/monitoring/objects_common.nix b/modules/private/monitoring/objects_common.nix
index 1ab9fc3..66fb812 100644
--- a/modules/private/monitoring/objects_common.nix
+++ b/modules/private/monitoring/objects_common.nix
@@ -76,6 +76,7 @@
76 check_ntp = "$USER1$/check_ntp_time -t 30 -q -H 0.arch.pool.ntp.org"; 76 check_ntp = "$USER1$/check_ntp_time -t 30 -q -H 0.arch.pool.ntp.org";
77 check_mysql_replication = "${sudo} -u mysql $USER2$/check_mysql_replication \"$ARG1$\" \"$ARG2$\""; 77 check_mysql_replication = "${sudo} -u mysql $USER2$/check_mysql_replication \"$ARG1$\" \"$ARG2$\"";
78 check_postgresql_replication = "${sudo} -u postgres $USER2$/check_postgres_replication \"$ARG1$\" \"$ARG2$\" \"$ARG3$\""; 78 check_postgresql_replication = "${sudo} -u postgres $USER2$/check_postgres_replication \"$ARG1$\" \"$ARG2$\" \"$ARG3$\"";
79 check_openldap_replication = "${sudo} -u openldap $USER2$/check_openldap_replication \"$ARG1$\" \"$ARG2$\" \"$ARG3$\" \"$ARG4$\" \"$ARG5$\"";
79 check_redis_replication = "${sudo} -u redis $USER2$/check_redis_replication \"$ARG1$\""; 80 check_redis_replication = "${sudo} -u redis $USER2$/check_redis_replication \"$ARG1$\"";
80 check_mailq = "$USER1$/check_mailq -s -w 1 -c 2"; 81 check_mailq = "$USER1$/check_mailq -s -w 1 -c 2";
81 82
diff --git a/modules/private/monitoring/plugins/check_openldap_replication b/modules/private/monitoring/plugins/check_openldap_replication
new file mode 100755
index 0000000..b511ff2
--- /dev/null
+++ b/modules/private/monitoring/plugins/check_openldap_replication
@@ -0,0 +1,54 @@
1#!/bin/bash
2
3STATE_OK=0
4STATE_WARNING=1
5STATE_CRITICAL=2
6STATE_UNKNOWN=3
7
8distant_host="$1"
9replication_dn="$2"
10replication_pw="$3"
11base="$4"
12config="$5"
13
14to_date() {
15 i="$1"
16 i=$(echo "$i" | grep contextCSN | cut -d":" -f2 | sed -e "s/\s//g")
17 i=$(echo "$i" | cut -d"#" -f1)
18 i=$(echo "$i" | cut -d"." -f1)
19 echo "$i"
20}
21
22# ldap
23remote_ldap=$(ldapsearch -H $distant_host -D "$replication_dn" -y "$replication_pw" -b "$base" -s base -LLL contextCSN )
24exit_code_remote=$?
25remote_ldap=$(to_date "$remote_ldap")
26
27# slapcat
28local_ldap=$(slapcat -b "$base" -f "$config" -a "(entryDN=$base)")
29exit_code_local=$?
30local_ldap=$(to_date "$local_ldap")
31
32offset=$(($remote_ldap - $local_ldap))
33
34if [[ $exit_code_remote -ne 0 || $exit_code_local -ne 0 ]]; then
35 echo "UNKNOWN - Impossible to run ldap command"
36 exit $STATE_UNKNOWN
37elif [[ -z "$offset" ]]; then
38 echo "UNKNOWN - No replication found"
39 exit $STATE_UNKNOWN
40else
41 output="Replication lag for openldap is ${offset}s"
42 LC_ALL=C lag=$(printf "%.*f" 0 $lag)
43
44 if [[ $offset -lt 5 ]]; then
45 echo "OK - $output"
46 exit $STATE_OK
47 elif [[ $offset -lt 10 ]]; then
48 echo "WARNING - $output"
49 exit $STATE_WARNING
50 else
51 echo "CRITICAL - $output"
52 exit $STATE_CRITICAL
53 fi
54fi
diff --git a/modules/private/system/backup-2.nix b/modules/private/system/backup-2.nix
index 3d51fa3..1b7f136 100644
--- a/modules/private/system/backup-2.nix
+++ b/modules/private/system/backup-2.nix
@@ -87,6 +87,18 @@
87 }; 87 };
88 }; 88 };
89 }; 89 };
90 openldap = {
91 enable = true;
92 base = "/backup2";
93 hosts = {
94 eldiron = {
95 url = "ldaps://${myconfig.env.ldap.host}:636";
96 dn = myconfig.env.ldap.replication_dn;
97 password = myconfig.env.ldap.replication_pw;
98 base = myconfig.env.ldap.base;
99 };
100 };
101 };
90 }; 102 };
91 103
92 # This value determines the NixOS release with which your system is 104 # This value determines the NixOS release with which your system is