git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #36249 from Ekleog/openldap-module-declarative-contents

Openldap module declarative contents

commit6749f6e26e540cd338f61bdfe6c8bad7a14b4016 authorJörg Thalheim <Mic92@users.noreply.github.com> dateSat 3 Mar 2018, 19:00 (9 years ago) parente6b8eb0b2acbe5
3 files changed+82−6
nixos/modules/services/databases/openldap.nix+46−6 nixos/release.nix+1 nixos/tests/openldap.nix+35
nixos/modules/services/databases/openldap.nix+46−6
@@ -7,8 +7,10 @@ let
77 cfg = config.services.openldap;
88 openldap = pkgs.openldap;
99
10+ dataFile = pkgs.writeText "ldap-contents.ldif" cfg.declarativeContents;
1011 configFile = pkgs.writeText "slapd.conf" cfg.extraConfig;
11−
12+ configOpts = if cfg.configDir == null then "-f ${configFile}"
13+ else "-F ${cfg.configDir}";
1214in
1315
1416{
@@ -81,6 +83,34 @@ in
8183 '''
8284 '';
8385 };
86+
87+ declarativeContents = mkOption {
88+ type = with types; nullOr lines;
89+ default = null;
90+ description = ''
91+ Declarative contents for the LDAP database, in LDIF format.
92+
93+ Note a few facts when using it. First, the database
94+ <emphasis>must</emphasis> be stored in the directory defined by
95+ <code>dataDir</code>. Second, all <code>dataDir</code> will be erased
96+ when starting the LDAP server. Third, modifications to the database
97+ are not prevented, they are just dropped on the next reboot of the
98+ server. Finally, performance-wise the database and indexes are rebuilt
99+ on each server startup, so this will slow down server startup,
100+ especially with large databases.
101+ '';
102+ example = ''
103+ dn: dc=example,dc=org
104+ objectClass: domain
105+ dc: example
106+
107+ dn: ou=users,dc=example,dc=org
108+ objectClass = organizationalUnit
109+ ou: users
110+
111+ # ...
112+ '';
113+ };
84114 };
85115
86116 };
@@ -88,7 +118,7 @@ in
88118
89119 ###### implementation
90120
91− config = mkIf config.services.openldap.enable {
121+ config = mkIf cfg.enable {
92122
93123 environment.systemPackages = [ openldap ];
94124
@@ -98,11 +128,21 @@ in
98128 after = [ "network.target" ];
99129 preStart = ''
100130 mkdir -p /var/run/slapd
101− chown -R ${cfg.user}:${cfg.group} /var/run/slapd
102− mkdir -p ${cfg.dataDir}
103− chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
131+ chown -R "${cfg.user}:${cfg.group}" /var/run/slapd
132+ ${optionalString (cfg.declarativeContents != null) ''
133+ rm -Rf "${cfg.dataDir}"
134+ ''}
135+ mkdir -p "${cfg.dataDir}"
136+ ${optionalString (cfg.declarativeContents != null) ''
137+ ${openldap.out}/bin/slapadd ${configOpts} -l ${dataFile}
138+ ''}
139+ chown -R "${cfg.user}:${cfg.group}" "${cfg.dataDir}"
104140 '';
105− serviceConfig.ExecStart = "${openldap.out}/libexec/slapd -u ${cfg.user} -g ${cfg.group} -d 0 -h \"${concatStringsSep " " cfg.urlList}\" ${if cfg.configDir == null then "-f "+configFile else "-F "+cfg.configDir}";
141+ serviceConfig.ExecStart =
142+ "${openldap.out}/libexec/slapd -d 0 " +
143+ "-u '${cfg.user}' -g '${cfg.group}' " +
144+ "-h '${concatStringsSep " " cfg.urlList}' " +
145+ "${configOpts}";
106146 };
107147
108148 users.extraUsers.openldap =
nixos/release.nix+1
@@ -325,6 +325,7 @@ in rec {
325325 tests.leaps = callTest tests/leaps.nix { };
326326 tests.nsd = callTest tests/nsd.nix {};
327327 tests.openssh = callTest tests/openssh.nix {};
328+ tests.openldap = callTest tests/openldap.nix {};
328329 tests.owncloud = callTest tests/owncloud.nix {};
329330 tests.pam-oath-login = callTest tests/pam-oath-login.nix {};
330331 #tests.panamax = callTestOnTheseSystems ["x86_64-linux"] tests/panamax.nix {};
nixos/tests/openldap.nixnew file+35
@@ -0,0 +1,35 @@
1+import ./make-test.nix {
2+ name = "dovecot";
3+
4+ machine = { pkgs, ... }: {
5+ services.openldap = {
6+ enable = true;
7+ extraConfig = ''
8+ include ${pkgs.openldap}/etc/schema/core.schema
9+ include ${pkgs.openldap}/etc/schema/cosine.schema
10+ include ${pkgs.openldap}/etc/schema/inetorgperson.schema
11+ include ${pkgs.openldap}/etc/schema/nis.schema
12+ database bdb
13+ suffix dc=example
14+ directory /var/db/openldap
15+ rootdn cn=root,dc=example
16+ rootpw notapassword
17+ '';
18+ declarativeContents = ''
19+ dn: dc=example
20+ objectClass: domain
21+ dc: example
22+
23+ dn: ou=users,dc=example
24+ objectClass: organizationalUnit
25+ ou: users
26+ '';
27+ };
28+ };
29+
30+ testScript = ''
31+ $machine->succeed('systemctl status openldap.service');
32+ $machine->waitForUnit('openldap.service');
33+ $machine->succeed('ldapsearch -LLL -D "cn=root,dc=example" -w notapassword -b "dc=example"');
34+ '';
35+}