]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/databases/openldap_replication.nix
Use attrs for secrets instead of lists
[perso/Immae/Config/Nix.git] / modules / private / databases / openldap_replication.nix
CommitLineData
ab8f306d 1{ pkgs, config, lib, ... }:
16b80abd
IB
2let
3 cfg = config.myServices.databasesReplication.openldap;
4 eldiron_schemas = pkgs.callPackage ./openldap/eldiron_schemas.nix {};
5 ldapConfig = hcfg: name: pkgs.writeText "slapd.conf" ''
5400b9b6
IB
6 include ${pkgs.openldap}/etc/schema/core.schema
7 include ${pkgs.openldap}/etc/schema/cosine.schema
8 include ${pkgs.openldap}/etc/schema/inetorgperson.schema
9 include ${pkgs.openldap}/etc/schema/nis.schema
16b80abd
IB
10 ${eldiron_schemas}
11 pidfile /run/slapd_${name}/slapd.pid
12 argsfile /run/slapd_${name}/slapd.args
13
14 moduleload back_hdb
15 backend hdb
16 database hdb
17
18 suffix "${hcfg.base}"
19 rootdn "cn=root,${hcfg.base}"
20 directory ${cfg.base}/${name}/openldap
21
22 index objectClass eq
23 index uid pres,eq
24 index entryUUID eq
25
da30ae4f 26 include ${config.secrets.fullPaths."openldap_replication/${name}/replication_config"}
16b80abd
IB
27 '';
28in
29{
30 options.myServices.databasesReplication.openldap = {
31 enable = lib.mkEnableOption "Enable openldap replication";
32 base = lib.mkOption {
33 type = lib.types.path;
34 description = ''
35 Base path to put the replications
36 '';
37 };
38 hosts = lib.mkOption {
39 default = {};
40 description = ''
41 Hosts to backup
42 '';
43 type = lib.types.attrsOf (lib.types.submodule {
44 options = {
45 package = lib.mkOption {
46 type = lib.types.package;
47 default = pkgs.openldap;
48 description = ''
49 Openldap package for this host
50 '';
51 };
52 url = lib.mkOption {
53 type = lib.types.str;
54 description = ''
55 Host to connect to
56 '';
57 };
58 base = lib.mkOption {
59 type = lib.types.str;
60 description = ''
61 Base DN to replicate
62 '';
63 };
64 dn = lib.mkOption {
65 type = lib.types.str;
66 description = ''
67 DN to use
68 '';
69 };
70 password = lib.mkOption {
71 type = lib.types.str;
72 description = ''
73 Password to use
74 '';
75 };
76 };
77 });
78 };
79 };
80
81 config = lib.mkIf cfg.enable {
82 users.users.openldap = {
83 description = "Openldap database user";
84 group = "openldap";
85 uid = config.ids.uids.openldap;
86 extraGroups = [ "keys" ];
87 };
88 users.groups.openldap.gid = config.ids.gids.openldap;
89
4c4652aa
IB
90 secrets.keys = lib.listToAttrs (lib.flatten (lib.mapAttrsToList (name: hcfg: [
91 (lib.nameValuePair "openldap_replication/${name}/replication_config" {
16b80abd
IB
92 user = "openldap";
93 group = "openldap";
94 permissions = "0400";
95 text = ''
96 syncrepl rid=000
97 provider=${hcfg.url}
98 type=refreshAndPersist
99 searchbase="${hcfg.base}"
100 retry="5 10 300 +"
101 attrs="*,+"
102 schemachecking=off
103 bindmethod=simple
104 binddn="${hcfg.dn}"
105 credentials="${hcfg.password}"
106 '';
4c4652aa
IB
107 })
108 (lib.nameValuePair "openldap_replication/${name}/replication_password" {
16b80abd
IB
109 user = "openldap";
110 group = "openldap";
111 permissions = "0400";
112 text = hcfg.password;
4c4652aa
IB
113 })
114 ]) cfg.hosts));
16b80abd
IB
115
116 services.cron = {
117 enable = true;
118 systemCronJobs = lib.flatten (lib.mapAttrsToList (name: hcfg:
119 let
120 dataDir = "${cfg.base}/${name}/openldap";
121 backupDir = "${cfg.base}/${name}/openldap_backup";
122 backup_script = pkgs.writeScript "backup_openldap_${name}" ''
123 #!${pkgs.stdenv.shell}
124
4c853ba6 125 ${hcfg.package}/bin/slapcat -b "${hcfg.base}" -f ${ldapConfig hcfg name} -l ${backupDir}/$(${pkgs.coreutils}/bin/date -Iminutes).ldif
16b80abd
IB
126 '';
127 u = pkgs.callPackage ./utils.nix {};
128 cleanup_script = pkgs.writeScript "cleanup_openldap_${name}" (u.exponentialDumps "ldif" backupDir);
129 in [
130 "0 22,4,10,16 * * * root ${backup_script}"
131 "0 3 * * * root ${cleanup_script}"
132 ]) cfg.hosts);
133 };
134
135 system.activationScripts = lib.attrsets.mapAttrs' (name: hcfg:
136 lib.attrsets.nameValuePair "openldap_replication_${name}" {
137 deps = [ "users" "groups" ];
138 text = ''
139 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap
140 install -m 0700 -o openldap -g openldap -d ${cfg.base}/${name}/openldap_backup
141 '';
142 }) cfg.hosts;
143
144 systemd.services = lib.attrsets.mapAttrs' (name: hcfg:
145 let
146 dataDir = "${cfg.base}/${name}/openldap";
147 in
148 lib.attrsets.nameValuePair "openldap_backup_${name}" {
149 description = "Openldap replication for ${name}";
150 wantedBy = [ "multi-user.target" ];
151 after = [ "network.target" ];
152 unitConfig.RequiresMountsFor = dataDir;
153
154 preStart = ''
155 mkdir -p /run/slapd_${name}
156 chown -R "openldap:openldap" /run/slapd_${name}
157 '';
158
159 serviceConfig = {
160 ExecStart = "${hcfg.package}/libexec/slapd -d 0 -u openldap -g openldap -f ${ldapConfig hcfg name}";
161 };
162 }) cfg.hosts;
163 };
164}
165
166