about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/auth-mysql.nix
blob: 0ed4b050a69a447d58aab8ea8155f3c3cb133fab (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import ./make-test-python.nix ({ pkgs, lib, ... }:

let
  dbUser = "nixos_auth";
  dbPassword = "topsecret123";
  dbName = "auth";

  mysqlUsername = "mysqltest";
  mysqlPassword = "topsecretmysqluserpassword123";
  mysqlGroup = "mysqlusers";

  localUsername = "localtest";
  localPassword = "topsecretlocaluserpassword123";

  mysqlInit = pkgs.writeText "mysqlInit" ''
      CREATE USER '${dbUser}'@'localhost' IDENTIFIED BY '${dbPassword}';
      CREATE DATABASE ${dbName};
      GRANT ALL PRIVILEGES ON ${dbName}.* TO '${dbUser}'@'localhost';
      FLUSH PRIVILEGES;

      USE ${dbName};
      CREATE TABLE `groups` (
        rowid int(11) NOT NULL auto_increment,
        gid int(11) NOT NULL,
        name char(255) NOT NULL,
        PRIMARY KEY (rowid)
      );

      CREATE TABLE `users` (
        name varchar(255) NOT NULL,
        uid int(11) NOT NULL auto_increment,
        gid int(11) NOT NULL,
        password varchar(255) NOT NULL,
        PRIMARY KEY (uid),
        UNIQUE (name)
      ) AUTO_INCREMENT=5000;

      INSERT INTO `users` (name, uid, gid, password) VALUES
      ('${mysqlUsername}', 5000, 5000, SHA2('${mysqlPassword}', 256));
      INSERT INTO `groups` (name, gid) VALUES ('${mysqlGroup}', 5000);
    '';
in
{
  name = "auth-mysql";
  meta.maintainers = with lib.maintainers; [ netali ];

  nodes.machine =
    { ... }:
    {
      services.mysql = {
        enable = true;
        package = pkgs.mariadb;
        settings.mysqld.bind-address = "127.0.0.1";
        initialScript = mysqlInit;
      };

      users.users.${localUsername} = {
        isNormalUser = true;
        password = localPassword;
      };

      security.pam.services.login.makeHomeDir = true;

      users.mysql = {
        enable = true;
        host = "127.0.0.1";
        user = dbUser;
        database = dbName;
        passwordFile = "${builtins.toFile "dbPassword" dbPassword}";
        pam = {
          table = "users";
          userColumn = "name";
          passwordColumn = "password";
          passwordCrypt = "sha256";
          disconnectEveryOperation = true;
        };
        nss = {
          getpwnam = ''
            SELECT name, 'x', uid, gid, name, CONCAT('/home/', name), "/run/current-system/sw/bin/bash" \
            FROM users \
            WHERE name='%1$s' \
            LIMIT 1
          '';
          getpwuid = ''
            SELECT name, 'x', uid, gid, name, CONCAT('/home/', name), "/run/current-system/sw/bin/bash" \
            FROM users \
            WHERE id=%1$u \
            LIMIT 1
          '';
          getspnam = ''
            SELECT name, password, 1, 0, 99999, 7, 0, -1, 0 \
            FROM users \
            WHERE name='%1$s' \
            LIMIT 1
          '';
          getpwent = ''
            SELECT name, 'x', uid, gid, name, CONCAT('/home/', name), "/run/current-system/sw/bin/bash" \
            FROM users
          '';
          getspent = ''
            SELECT name, password, 1, 0, 99999, 7, 0, -1, 0 \
            FROM users
          '';
          getgrnam = ''
            SELECT name, 'x', gid FROM groups WHERE name='%1$s' LIMIT 1
          '';
          getgrgid = ''
            SELECT name, 'x', gid FROM groups WHERE gid='%1$u' LIMIT 1
          '';
          getgrent = ''
            SELECT name, 'x', gid FROM groups
          '';
          memsbygid = ''
            SELECT name FROM users WHERE gid=%1$u
          '';
          gidsbymem = ''
            SELECT gid FROM users WHERE name='%1$s'
          '';
        };
      };
    };

  testScript = ''
    def switch_to_tty(tty_number):
        machine.fail(f"pgrep -f 'agetty.*tty{tty_number}'")
        machine.send_key(f"alt-f{tty_number}")
        machine.wait_until_succeeds(f"[ $(fgconsole) = {tty_number} ]")
        machine.wait_for_unit(f"getty@tty{tty_number}.service")
        machine.wait_until_succeeds(f"pgrep -f 'agetty.*tty{tty_number}'")


    def try_login(tty_number, username, password):
        machine.wait_until_tty_matches(tty_number, "login: ")
        machine.send_chars(f"{username}\n")
        machine.wait_until_tty_matches(tty_number, f"login: {username}")
        machine.wait_until_succeeds("pgrep login")
        machine.wait_until_tty_matches(tty_number, "Password: ")
        machine.send_chars(f"{password}\n")


    machine.wait_for_unit("multi-user.target")
    machine.wait_for_unit("mysql.service")
    machine.wait_until_succeeds("pgrep -f 'agetty.*tty1'")

    with subtest("Local login"):
        switch_to_tty("2")
        try_login("2", "${localUsername}", "${localPassword}")

        machine.wait_until_succeeds("pgrep -u ${localUsername} bash")
        machine.send_chars("id > local_id.txt\n")
        machine.wait_for_file("/home/${localUsername}/local_id.txt")
        machine.succeed("cat /home/${localUsername}/local_id.txt | grep 'uid=1000(${localUsername}) gid=100(users) groups=100(users)'")

    with subtest("Local incorrect login"):
        switch_to_tty("3")
        try_login("3", "${localUsername}", "wrongpassword")

        machine.wait_until_tty_matches("3", "Login incorrect")
        machine.wait_until_tty_matches("3", "login:")

    with subtest("MySQL login"):
        switch_to_tty("4")
        try_login("4", "${mysqlUsername}", "${mysqlPassword}")

        machine.wait_until_succeeds("pgrep -u ${mysqlUsername} bash")
        machine.send_chars("id > mysql_id.txt\n")
        machine.wait_for_file("/home/${mysqlUsername}/mysql_id.txt")
        machine.succeed("cat /home/${mysqlUsername}/mysql_id.txt | grep 'uid=5000(${mysqlUsername}) gid=5000(${mysqlGroup}) groups=5000(${mysqlGroup})'")

    with subtest("MySQL incorrect login"):
        switch_to_tty("5")
        try_login("5", "${mysqlUsername}", "wrongpassword")

        machine.wait_until_tty_matches("5", "Login incorrect")
        machine.wait_until_tty_matches("5", "login:")
    '';
})