summaryrefslogtreecommitdiffstats
path: root/nixos/tests/sssd-ldap.nix
blob: f60232945adc44b89b891169f7571f2cb845e02c (plain)
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
let
  dbDomain = "example.org";
  dbSuffix = "dc=example,dc=org";

  ldapRootUser = "admin";
  ldapRootPassword = "foobar";

  testUser = "alice";
  testPassword = "foobar";
  testNewPassword = "barfoo";
in
import ./make-test-python.nix (
  { pkgs, ... }:
  {
    name = "sssd-ldap";

    meta = with pkgs.lib.maintainers; {
      maintainers = [
        bbigras
        s1341
      ];
    };

    nodes.machine =
      { pkgs, ... }:
      {
        security.pam.services.systemd-user.makeHomeDir = true;
        environment.etc."cert.pem".text = builtins.readFile ./common/acme/server/acme.test.cert.pem;
        environment.etc."key.pem".text = builtins.readFile ./common/acme/server/acme.test.key.pem;
        services.openldap = {
          enable = true;
          urlList = [
            "ldap:///"
            "ldaps:///"
          ];
          settings = {
            attrs = {
              olcTLSCACertificateFile = "/etc/cert.pem";
              olcTLSCertificateFile = "/etc/cert.pem";
              olcTLSCertificateKeyFile = "/etc/key.pem";
              olcTLSCipherSuite = "HIGH:MEDIUM:+3DES:+RC4:+aNULL";
              olcTLSCRLCheck = "none";
              olcTLSVerifyClient = "never";
              olcTLSProtocolMin = "3.1";
            };
            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={1}mdb" = {
                attrs = {
                  objectClass = [
                    "olcDatabaseConfig"
                    "olcMdbConfig"
                  ];
                  olcDatabase = "{1}mdb";
                  olcDbDirectory = "/var/lib/openldap/db";
                  olcSuffix = dbSuffix;
                  olcRootDN = "cn=${ldapRootUser},${dbSuffix}";
                  olcRootPW = ldapRootPassword;
                  olcAccess = [
                    # custom access rules for userPassword attributes
                    ''
                      {0}to attrs=userPassword
                                        by self write
                                        by anonymous auth
                                        by * none''

                    # allow read on anything else
                    ''
                      {1}to *
                                        by * read''
                  ];
                };
              };
            };
          };
          declarativeContents = {
            ${dbSuffix} = ''
              dn: ${dbSuffix}
              objectClass: top
              objectClass: dcObject
              objectClass: organization
              o: ${dbDomain}

              dn: ou=posix,${dbSuffix}
              objectClass: top
              objectClass: organizationalUnit

              dn: ou=accounts,ou=posix,${dbSuffix}
              objectClass: top
              objectClass: organizationalUnit

              dn: uid=${testUser},ou=accounts,ou=posix,${dbSuffix}
              objectClass: person
              objectClass: posixAccount
              userPassword: ${testPassword}
              homeDirectory: /home/${testUser}
              loginShell: /run/current-system/sw/bin/bash
              uidNumber: 1234
              gidNumber: 1234
              cn: ""
              sn: ""
            '';
          };
        };

        services.sssd = {
          enable = true;
          # just for testing purposes, don't put this into the Nix store in production!
          environmentFile = "${pkgs.writeText "ldap-root" "LDAP_BIND_PW=${ldapRootPassword}"}";
          settings = {
            sssd = {
              config_file_version = 2;
              services = "nss, pam, sudo";
              domains = dbDomain;
            };

            "domain/${dbDomain}" = {
              auth_provider = "ldap";
              id_provider = "ldap";
              ldap_uri = "ldaps://127.0.0.1:636";
              ldap_tls_reqcert = "allow";
              ldap_tls_cacert = "/etc/cert.pem";
              ldap_search_base = dbSuffix;
              ldap_default_bind_dn = "cn=${ldapRootUser},${dbSuffix}";
              ldap_default_authtok_type = "password";
              ldap_default_authtok = "$LDAP_BIND_PW";
            };
          };
        };
      };

    testScript = ''
      machine.start()
      machine.wait_for_unit("openldap.service")
      machine.wait_for_unit("sssd.service")
      result = machine.execute("getent passwd ${testUser}")
      if result[0] == 0:
        assert "${testUser}" in result[1]
      else:
        machine.wait_for_console_text("Backend is online")
        machine.succeed("getent passwd ${testUser}")

      with subtest("Log in as ${testUser}"):
          machine.wait_until_tty_matches("1", "login: ")
          machine.send_chars("${testUser}\n")
          machine.wait_until_tty_matches("1", "login: ${testUser}")
          machine.wait_until_succeeds("pgrep login")
          machine.wait_until_tty_matches("1", "Password: ")
          machine.send_chars("${testPassword}\n")
          machine.wait_until_succeeds("pgrep -u ${testUser} bash")
          machine.send_chars("touch done\n")
          machine.wait_for_file("/home/${testUser}/done")

      with subtest("Change ${testUser}'s password"):
          machine.send_chars("passwd\n")
          machine.wait_until_tty_matches("1", "Current Password: ")
          machine.send_chars("${testPassword}\n")
          machine.wait_until_tty_matches("1", "New Password: ")
          machine.send_chars("${testNewPassword}\n")
          machine.wait_until_tty_matches("1", "Reenter new Password: ")
          machine.send_chars("${testNewPassword}\n")
          machine.wait_until_tty_matches("1", "passwd: password updated successfully")

      with subtest("Log in as ${testUser} with new password in virtual console 2"):
          machine.send_key("alt-f2")
          machine.wait_until_succeeds("[ $(fgconsole) = 2 ]")
          machine.wait_for_unit("getty@tty2.service")
          machine.wait_until_succeeds("pgrep -f 'agetty.*tty2'")

          machine.wait_until_tty_matches("2", "login: ")
          machine.send_chars("${testUser}\n")
          machine.wait_until_tty_matches("2", "login: ${testUser}")
          machine.wait_until_succeeds("pgrep login")
          machine.wait_until_tty_matches("2", "Password: ")
          machine.send_chars("${testNewPassword}\n")
          machine.wait_until_succeeds("pgrep -u ${testUser} bash")
          machine.send_chars("touch done2\n")
          machine.wait_for_file("/home/${testUser}/done2")
    '';
  }
)