tangled
alpha
login
or
join now
pyrox.dev
/
nixpkgs
0
fork
atom
lol
0
fork
atom
overview
issues
pulls
pipelines
nixos/mysql-auth: add VM-Test
Netali
3 years ago
e23ace62
1a35b5aa
+178
2 changed files
expand all
collapse all
unified
split
nixos
tests
all-tests.nix
auth-mysql.nix
+1
nixos/tests/all-tests.nix
···
41
41
apparmor = handleTest ./apparmor.nix {};
42
42
atd = handleTest ./atd.nix {};
43
43
atop = handleTest ./atop.nix {};
44
44
+
auth-mysql = handleTest ./auth-mysql.nix {};
44
45
avahi = handleTest ./avahi.nix {};
45
46
avahi-with-resolved = handleTest ./avahi.nix { networkd = true; };
46
47
babeld = handleTest ./babeld.nix {};
+177
nixos/tests/auth-mysql.nix
···
1
1
+
import ./make-test-python.nix ({ pkgs, lib, ... }:
2
2
+
3
3
+
let
4
4
+
dbUser = "nixos_auth";
5
5
+
dbPassword = "topsecret123";
6
6
+
dbName = "auth";
7
7
+
8
8
+
mysqlUsername = "mysqltest";
9
9
+
mysqlPassword = "topsecretmysqluserpassword123";
10
10
+
mysqlGroup = "mysqlusers";
11
11
+
12
12
+
localUsername = "localtest";
13
13
+
localPassword = "topsecretlocaluserpassword123";
14
14
+
15
15
+
mysqlInit = pkgs.writeText "mysqlInit" ''
16
16
+
CREATE USER '${dbUser}'@'localhost' IDENTIFIED BY '${dbPassword}';
17
17
+
CREATE DATABASE ${dbName};
18
18
+
GRANT ALL PRIVILEGES ON ${dbName}.* TO '${dbUser}'@'localhost';
19
19
+
FLUSH PRIVILEGES;
20
20
+
21
21
+
USE ${dbName};
22
22
+
CREATE TABLE `groups` (
23
23
+
rowid int(11) NOT NULL auto_increment,
24
24
+
gid int(11) NOT NULL,
25
25
+
name char(255) NOT NULL,
26
26
+
PRIMARY KEY (rowid)
27
27
+
);
28
28
+
29
29
+
CREATE TABLE `users` (
30
30
+
name varchar(255) NOT NULL,
31
31
+
uid int(11) NOT NULL auto_increment,
32
32
+
gid int(11) NOT NULL,
33
33
+
password varchar(255) NOT NULL,
34
34
+
PRIMARY KEY (uid),
35
35
+
UNIQUE (name)
36
36
+
) AUTO_INCREMENT=5000;
37
37
+
38
38
+
INSERT INTO `users` (name, uid, gid, password) VALUES
39
39
+
('${mysqlUsername}', 5000, 5000, SHA2('${mysqlPassword}', 256));
40
40
+
INSERT INTO `groups` (name, gid) VALUES ('${mysqlGroup}', 5000);
41
41
+
'';
42
42
+
in
43
43
+
{
44
44
+
name = "auth-mysql";
45
45
+
meta.maintainers = with lib.maintainers; [ netali ];
46
46
+
47
47
+
nodes.machine =
48
48
+
{ ... }:
49
49
+
{
50
50
+
services.mysql = {
51
51
+
enable = true;
52
52
+
package = pkgs.mariadb;
53
53
+
settings.mysqld.bind-address = "127.0.0.1";
54
54
+
initialScript = mysqlInit;
55
55
+
};
56
56
+
57
57
+
users.users.${localUsername} = {
58
58
+
isNormalUser = true;
59
59
+
password = localPassword;
60
60
+
};
61
61
+
62
62
+
security.pam.services.login.makeHomeDir = true;
63
63
+
64
64
+
users.mysql = {
65
65
+
enable = true;
66
66
+
host = "127.0.0.1";
67
67
+
user = dbUser;
68
68
+
database = dbName;
69
69
+
passwordFile = "${builtins.toFile "dbPassword" dbPassword}";
70
70
+
pam = {
71
71
+
table = "users";
72
72
+
userColumn = "name";
73
73
+
passwordColumn = "password";
74
74
+
passwordCrypt = "sha256";
75
75
+
disconnectEveryOperation = true;
76
76
+
};
77
77
+
nss = {
78
78
+
getpwnam = ''
79
79
+
SELECT name, 'x', uid, gid, name, CONCAT('/home/', name), "/run/current-system/sw/bin/bash" \
80
80
+
FROM users \
81
81
+
WHERE name='%1$s' \
82
82
+
LIMIT 1
83
83
+
'';
84
84
+
getpwuid = ''
85
85
+
SELECT name, 'x', uid, gid, name, CONCAT('/home/', name), "/run/current-system/sw/bin/bash" \
86
86
+
FROM users \
87
87
+
WHERE id=%1$u \
88
88
+
LIMIT 1
89
89
+
'';
90
90
+
getspnam = ''
91
91
+
SELECT name, password, 1, 0, 99999, 7, 0, -1, 0 \
92
92
+
FROM users \
93
93
+
WHERE name='%1$s' \
94
94
+
LIMIT 1
95
95
+
'';
96
96
+
getpwent = ''
97
97
+
SELECT name, 'x', uid, gid, name, CONCAT('/home/', name), "/run/current-system/sw/bin/bash" \
98
98
+
FROM users
99
99
+
'';
100
100
+
getspent = ''
101
101
+
SELECT name, password, 1, 0, 99999, 7, 0, -1, 0 \
102
102
+
FROM users
103
103
+
'';
104
104
+
getgrnam = ''
105
105
+
SELECT name, 'x', gid FROM groups WHERE name='%1$s' LIMIT 1
106
106
+
'';
107
107
+
getgrgid = ''
108
108
+
SELECT name, 'x', gid FROM groups WHERE gid='%1$u' LIMIT 1
109
109
+
'';
110
110
+
getgrent = ''
111
111
+
SELECT name, 'x', gid FROM groups
112
112
+
'';
113
113
+
memsbygid = ''
114
114
+
SELECT name FROM users WHERE gid=%1$u
115
115
+
'';
116
116
+
gidsbymem = ''
117
117
+
SELECT gid FROM users WHERE name='%1$s'
118
118
+
'';
119
119
+
};
120
120
+
};
121
121
+
};
122
122
+
123
123
+
testScript = ''
124
124
+
def switch_to_tty(tty_number):
125
125
+
machine.fail(f"pgrep -f 'agetty.*tty{tty_number}'")
126
126
+
machine.send_key(f"alt-f{tty_number}")
127
127
+
machine.wait_until_succeeds(f"[ $(fgconsole) = {tty_number} ]")
128
128
+
machine.wait_for_unit(f"getty@tty{tty_number}.service")
129
129
+
machine.wait_until_succeeds(f"pgrep -f 'agetty.*tty{tty_number}'")
130
130
+
131
131
+
132
132
+
def try_login(tty_number, username, password):
133
133
+
machine.wait_until_tty_matches(tty_number, "login: ")
134
134
+
machine.send_chars(f"{username}\n")
135
135
+
machine.wait_until_tty_matches(tty_number, f"login: {username}")
136
136
+
machine.wait_until_succeeds("pgrep login")
137
137
+
machine.wait_until_tty_matches(tty_number, "Password: ")
138
138
+
machine.send_chars(f"{password}\n")
139
139
+
140
140
+
141
141
+
machine.wait_for_unit("multi-user.target")
142
142
+
machine.wait_for_unit("mysql.service")
143
143
+
machine.wait_until_succeeds("pgrep -f 'agetty.*tty1'")
144
144
+
145
145
+
with subtest("Local login"):
146
146
+
switch_to_tty("2")
147
147
+
try_login("2", "${localUsername}", "${localPassword}")
148
148
+
149
149
+
machine.wait_until_succeeds("pgrep -u ${localUsername} bash")
150
150
+
machine.send_chars("id > local_id.txt\n")
151
151
+
machine.wait_for_file("/home/${localUsername}/local_id.txt")
152
152
+
machine.succeed("cat /home/${localUsername}/local_id.txt | grep 'uid=1000(${localUsername}) gid=100(users) groups=100(users)'")
153
153
+
154
154
+
with subtest("Local incorrect login"):
155
155
+
switch_to_tty("3")
156
156
+
try_login("3", "${localUsername}", "wrongpassword")
157
157
+
158
158
+
machine.wait_until_tty_matches("3", "Login incorrect")
159
159
+
machine.wait_until_tty_matches("3", "login:")
160
160
+
161
161
+
with subtest("MySQL login"):
162
162
+
switch_to_tty("4")
163
163
+
try_login("4", "${mysqlUsername}", "${mysqlPassword}")
164
164
+
165
165
+
machine.wait_until_succeeds("pgrep -u ${mysqlUsername} bash")
166
166
+
machine.send_chars("id > mysql_id.txt\n")
167
167
+
machine.wait_for_file("/home/${mysqlUsername}/mysql_id.txt")
168
168
+
machine.succeed("cat /home/${mysqlUsername}/mysql_id.txt | grep 'uid=5000(${mysqlUsername}) gid=5000(${mysqlGroup}) groups=5000(${mysqlGroup})'")
169
169
+
170
170
+
with subtest("MySQL incorrect login"):
171
171
+
switch_to_tty("5")
172
172
+
try_login("5", "${mysqlUsername}", "wrongpassword")
173
173
+
174
174
+
machine.wait_until_tty_matches("5", "Login incorrect")
175
175
+
machine.wait_until_tty_matches("5", "login:")
176
176
+
'';
177
177
+
})