summary refs log tree commit diff
path: root/nixos/tests/openldap.nix
blob: 47d6a91843f115341bfda2fd4793638387b3f343 (plain) (blame)
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
import ./make-test-python.nix ({ pkgs, ... }: let
  dbContents = ''
    dn: dc=example
    objectClass: domain
    dc: example

    dn: ou=users,dc=example
    objectClass: organizationalUnit
    ou: users
  '';

  ldifConfig = ''
    dn: cn=config
    cn: config
    objectClass: olcGlobal
    olcLogLevel: stats

    dn: cn=schema,cn=config
    cn: schema
    objectClass: olcSchemaConfig

    include: file://${pkgs.openldap}/etc/schema/core.ldif
    include: file://${pkgs.openldap}/etc/schema/cosine.ldif
    include: file://${pkgs.openldap}/etc/schema/inetorgperson.ldif

    dn: olcDatabase={0}config,cn=config
    olcDatabase: {0}config
    objectClass: olcDatabaseConfig
    olcRootDN: cn=root,cn=config
    olcRootPW: configpassword

    dn: olcDatabase={1}mdb,cn=config
    objectClass: olcDatabaseConfig
    objectClass: olcMdbConfig
    olcDatabase: {1}mdb
    olcDbDirectory: /var/db/openldap
    olcDbIndex: objectClass eq
    olcSuffix: dc=example
    olcRootDN: cn=root,dc=example
    olcRootPW: notapassword
  '';

  ldapClientConfig = {
    enable = true;
    loginPam = false;
    nsswitch = false;
    server = "ldap://";
    base = "dc=example";
  };

in {
  name = "openldap";

  nodes.machine = { pkgs, ... }: {
    environment.etc."openldap/root_password".text = "notapassword";

    users.ldap = ldapClientConfig;

    services.openldap = {
      enable = true;
      urlList = [ "ldapi:///" "ldap://" ];
      settings = {
        children = {
          "cn=schema".includes = [
            "${pkgs.openldap}/etc/schema/core.ldif"
            "${pkgs.openldap}/etc/schema/cosine.ldif"
            "${pkgs.openldap}/etc/schema/inetorgperson.ldif"
            "${pkgs.openldap}/etc/schema/nis.ldif"
          ];
          "olcDatabase={0}config" = {
            attrs = {
              objectClass = [ "olcDatabaseConfig" ];
              olcDatabase = "{0}config";
              olcRootDN = "cn=root,cn=config";
              olcRootPW = "configpassword";
            };
          };
          "olcDatabase={1}mdb" = {
            # This tests string, base64 and path values, as well as lists of string values
            attrs = {
              objectClass = [ "olcDatabaseConfig" "olcMdbConfig" ];
              olcDatabase = "{1}mdb";
              olcDbDirectory = "/var/lib/openldap/db";
              olcSuffix = "dc=example";
              olcRootDN = {
                # cn=root,dc=example
                base64 = "Y249cm9vdCxkYz1leGFtcGxl";
              };
              olcRootPW = {
                path = "/etc/openldap/root_password";
              };
            };
          };
        };
      };
    };

    specialisation = {
      declarativeContents.configuration = { ... }: {
        services.openldap.declarativeContents."dc=example" = dbContents;
      };
      mutableConfig.configuration = { ... }: {
        services.openldap = {
          declarativeContents."dc=example" = dbContents;
          mutableConfig = true;
        };
      };
      manualConfigDir = {
        inheritParentConfig = false;
        configuration = { ... }: {
          users.ldap = ldapClientConfig;
          services.openldap = {
            enable = true;
            configDir = "/var/db/slapd.d";
          };
        };
      };
    };
  };
  testScript = { nodes, ... }: let
    specializations = "${nodes.machine.system.build.toplevel}/specialisation";
    changeRootPw = ''
      dn: olcDatabase={1}mdb,cn=config
      changetype: modify
      replace: olcRootPW
      olcRootPW: foobar
    '';
  in ''
    # Test startup with empty DB
    machine.wait_for_unit("openldap.service")

    with subtest("declarative contents"):
      machine.succeed('${specializations}/declarativeContents/bin/switch-to-configuration test')
      machine.wait_for_unit("openldap.service")
      machine.succeed('ldapsearch -LLL -D "cn=root,dc=example" -w notapassword')
      machine.fail('ldapmodify -D cn=root,cn=config -w configpassword -f ${pkgs.writeText "rootpw.ldif" changeRootPw}')

    with subtest("mutable config"):
      machine.succeed('${specializations}/mutableConfig/bin/switch-to-configuration test')
      machine.succeed('ldapsearch -LLL -D "cn=root,dc=example" -w notapassword')
      machine.succeed('ldapmodify -D cn=root,cn=config -w configpassword -f ${pkgs.writeText "rootpw.ldif" changeRootPw}')
      machine.succeed('ldapsearch -LLL -D "cn=root,dc=example" -w foobar')

    with subtest("manual config dir"):
      machine.succeed(
        'mkdir /var/db/slapd.d /var/db/openldap',
        'slapadd -F /var/db/slapd.d -n0 -l ${pkgs.writeText "config.ldif" ldifConfig}',
        'slapadd -F /var/db/slapd.d -n1 -l ${pkgs.writeText "contents.ldif" dbContents}',
        'chown -R openldap:openldap /var/db/slapd.d /var/db/openldap',
        '${specializations}/manualConfigDir/bin/switch-to-configuration test',
      )
      machine.succeed('ldapsearch -LLL -D "cn=root,dc=example" -w notapassword')
      machine.succeed('ldapmodify -D cn=root,cn=config -w configpassword -f ${pkgs.writeText "rootpw.ldif" changeRootPw}')
      machine.succeed('ldapsearch -LLL -D "cn=root,dc=example" -w foobar')
  '';
})