Debian Codegraph @

src/test/authentication/t/003_peer.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Tests for peer authentication and user name map.
5 # The test is skipped if the platform does not support peer authentication,
6 # and is only able to run with Unix-domain sockets.
7
8 use strict;
9 use warnings FATAL => 'all';
10 use PostgreSQL::Test::Cluster;
11 use PostgreSQL::Test::Utils;
12 use Test::More;
13 if (!$use_unix_sockets)
14 {
15 plan skip_all =>
16 "authentication tests cannot run without Unix-domain sockets";
17 }
18
19 # Delete pg_hba.conf from the given node, add a new entry to it
20 # and then execute a reload to refresh it.
21 sub reset_pg_hba
22 {
23 my $node = shift;
24 my $hba_method = shift;
25
26 unlink($node->data_dir . '/pg_hba.conf');
27 $node->append_conf('pg_hba.conf', "local all all $hba_method");
28 $node->reload;
29 return;
30 }
31
32 # Delete pg_ident.conf from the given node, add a new entry to it
33 # and then execute a reload to refresh it.
34 sub reset_pg_ident
35 {
36 my $node = shift;
37 my $map_name = shift;
38 my $system_user = shift;
39 my $pg_user = shift;
40
41 unlink($node->data_dir . '/pg_ident.conf');
42 $node->append_conf('pg_ident.conf', "$map_name $system_user $pg_user");
43 $node->reload;
44 return;
45 }
46
47 # Test access for a single role, useful to wrap all tests into one.
48 sub test_role
49 {
50 local $Test::Builder::Level = $Test::Builder::Level + 1;
51
52 my ($node, $role, $method, $expected_res, $test_details, %params) = @_;
53 my $status_string = 'failed';
54 $status_string = 'success' if ($expected_res eq 0);
55
56 my $connstr = "user=$role";
57 my $testname =
58 "authentication $status_string for method $method, role $role "
59 . $test_details;
60
61 if ($expected_res eq 0)
62 {
63 $node->connect_ok($connstr, $testname, %params);
64 }
65 else
66 {
67 # No checks of the error message, only the status code.
68 $node->connect_fails($connstr, $testname, %params);
69 }
70 }
71
72 my $node = PostgreSQL::Test::Cluster->new('node');
73 $node->init;
74 $node->append_conf('postgresql.conf', "log_connections = authentication\n");
75 # Needed to allow connect_fails to inspect postmaster log:
76 $node->append_conf('postgresql.conf', "log_min_messages = debug2");
77 $node->start;
78
79 # Set pg_hba.conf with the peer authentication.
80 reset_pg_hba($node, 'peer');
81
82 # Check if peer authentication is supported on this platform.
83 my $log_offset = -s $node->logfile;
84 $node->psql('postgres');
85 if ($node->log_contains(
86 qr/peer authentication is not supported on this platform/,
87 $log_offset))
88 {
89 plan skip_all => 'peer authentication is not supported on this platform';
90 }
91
92 # Add a database role and a group, to use for the user name map.
93 $node->safe_psql('postgres', qq{CREATE ROLE testmapuser LOGIN});
94 $node->safe_psql('postgres', "CREATE ROLE testmapgroup NOLOGIN");
95 $node->safe_psql('postgres', "GRANT testmapgroup TO testmapuser");
96 # Note the double quotes here.
97 $node->safe_psql('postgres', 'CREATE ROLE "testmapgroupliteral\\1" LOGIN');
98 $node->safe_psql('postgres', 'GRANT "testmapgroupliteral\\1" TO testmapuser');
99
100 # Extract as well the system user for the user name map.
101 my $system_user =
102 $node->safe_psql('postgres',
103 q(select (string_to_array(SYSTEM_USER, ':'))[2]));
104
105 # While on it, check the status of huge pages, that can be either on
106 # or off, but never unknown.
107 my $huge_pages_status =
108 $node->safe_psql('postgres', q(SHOW huge_pages_status;));
109 isnt($huge_pages_status, 'unknown', "check huge_pages_status");
110
111 # Tests without the user name map.
112 # Failure as connection is attempted with a database role not mapping
113 # to an authorized system user.
114 test_role(
115 $node, qq{testmapuser}, 'peer', 2,
116 'without user name map',
117 log_like => [qr/Peer authentication failed for user "testmapuser"/]);
118
119 # Tests with a user name map.
120 reset_pg_ident($node, 'mypeermap', $system_user, 'testmapuser');
121 reset_pg_hba($node, 'peer map=mypeermap');
122
123 # Success as the database role matches with the system user in the map.
124 test_role($node, qq{testmapuser}, 'peer', 0, 'with user name map',
125 log_like =>
126 [qr/connection authenticated: identity="$system_user" method=peer/]);
127
128 # Tests with the "all" keyword.
129 reset_pg_ident($node, 'mypeermap', $system_user, 'all');
130
131 test_role(
132 $node,
133 qq{testmapuser},
134 'peer',
135 0,
136 'with keyword "all" as database user in user name map',
137 log_like =>
138 [qr/connection authenticated: identity="$system_user" method=peer/]);
139
140 # Tests with the "all" keyword, but quoted (no effect here).
141 reset_pg_ident($node, 'mypeermap', $system_user, '"all"');
142
143 test_role(
144 $node,
145 qq{testmapuser},
146 'peer',
147 2,
148 'with quoted keyword "all" as database user in user name map',
149 log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);
150
151 # Success as the regexp of the database user matches
152 reset_pg_ident($node, 'mypeermap', $system_user, qq{/^testm.*\$});
153 test_role(
154 $node,
155 qq{testmapuser},
156 'peer',
157 0,
158 'with regexp of database user in user name map',
159 log_like =>
160 [qr/connection authenticated: identity="$system_user" method=peer/]);
161
162 # Failure as the regexp of the database user does not match.
163 reset_pg_ident($node, 'mypeermap', $system_user, qq{/^doesnotmatch.*\$});
164 test_role(
165 $node,
166 qq{testmapuser},
167 'peer',
168 2,
169 'with bad regexp of database user in user name map',
170 log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);
171
172 # Test with regular expression in user name map.
173 # Extract the last 3 characters from the system_user
174 # or the entire system_user (if its length is <= -3).
175 my $regex_test_string = substr($system_user, -3);
176
177 # Success as the system user regular expression matches.
178 reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$},
179 'testmapuser');
180 test_role(
181 $node,
182 qq{testmapuser},
183 'peer',
184 0,
185 'with regexp of system user in user name map',
186 log_like =>
187 [qr/connection authenticated: identity="$system_user" method=peer/]);
188
189 # Success as both regular expressions match.
190 reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$},
191 qq{/^testm.*\$});
192 test_role(
193 $node,
194 qq{testmapuser},
195 'peer',
196 0,
197 'with regexps for both system and database user in user name map',
198 log_like =>
199 [qr/connection authenticated: identity="$system_user" method=peer/]);
200
201 # Success as the regular expression matches and database role is the "all"
202 # keyword.
203 reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$}, 'all');
204 test_role(
205 $node,
206 qq{testmapuser},
207 'peer',
208 0,
209 'with regexp of system user and keyword "all" in user name map',
210 log_like =>
211 [qr/connection authenticated: identity="$system_user" method=peer/]);
212
213 # Success as the regular expression matches and \1 is replaced in the given
214 # subexpression.
215 reset_pg_ident($node, 'mypeermap', qq{/^$system_user(.*)\$}, 'test\1mapuser');
216 test_role(
217 $node,
218 qq{testmapuser},
219 'peer',
220 0,
221 'with regular expression in user name map with \1 replaced',
222 log_like =>
223 [qr/connection authenticated: identity="$system_user" method=peer/]);
224
225 # Success as the regular expression matches and \1 is replaced in the given
226 # subexpression, even if quoted.
227 reset_pg_ident($node, 'mypeermap', qq{/^$system_user(.*)\$},
228 '"test\1mapuser"');
229 test_role(
230 $node,
231 qq{testmapuser},
232 'peer',
233 0,
234 'with regular expression in user name map with quoted \1 replaced',
235 log_like =>
236 [qr/connection authenticated: identity="$system_user" method=peer/]);
237
238 # Failure as the regular expression does not include a subexpression, but
239 # the database user contains \1, requesting a replacement.
240 reset_pg_ident($node, 'mypeermap', qq{/^$system_user\$}, '\1testmapuser');
241 test_role(
242 $node,
243 qq{testmapuser},
244 'peer', 2,
245 'with regular expression in user name map with \1 not replaced',
246 log_like => [
247 qr/regular expression "\^$system_user\$" has no subexpressions as requested by backreference in "\\1testmapuser"/
248 ]);
249
250 # Concatenate system_user to system_user.
251 my $bad_regex_test_string = $system_user . $system_user;
252
253 # Failure as the regexp of system user does not match.
254 reset_pg_ident($node, 'mypeermap', qq{/^.*$bad_regex_test_string\$},
255 'testmapuser');
256 test_role(
257 $node,
258 qq{testmapuser},
259 'peer',
260 2,
261 'with regexp of system user in user name map',
262 log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);
263
264 # Test using a group role match for the database user.
265 reset_pg_ident($node, 'mypeermap', $system_user, '+testmapgroup');
266
267 test_role($node, qq{testmapuser}, 'peer', 0, 'plain user with group',
268 log_like =>
269 [qr/connection authenticated: identity="$system_user" method=peer/]);
270
271 test_role(
272 $node, qq{testmapgroup}, 'peer', 2,
273 'group user with group',
274 log_like => [qr/role "testmapgroup" is not permitted to log in/]);
275
276 # Now apply quotes to the group match, nullifying its effect.
277 reset_pg_ident($node, 'mypeermap', $system_user, '"+testmapgroup"');
278 test_role(
279 $node,
280 qq{testmapuser},
281 'peer',
282 2,
283 'plain user with quoted group name',
284 log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);
285
286 # Test using a regexp for the system user, with a group membership
287 # check for the database user.
288 reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$},
289 '+testmapgroup');
290
291 test_role(
292 $node,
293 qq{testmapuser},
294 'peer',
295 0,
296 'regexp of system user as group member',
297 log_like =>
298 [qr/connection authenticated: identity="$system_user" method=peer/]);
299
300 test_role(
301 $node,
302 qq{testmapgroup},
303 'peer',
304 2,
305 'regexp of system user as non-member of group',
306 log_like => [qr/role "testmapgroup" is not permitted to log in/]);
307
308 # Test that membership checks and regexes will use literal \1 instead of
309 # replacing it, as subexpression replacement is not allowed in this case.
310 reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string(.*)\$},
311 '+testmapgroupliteral\\1');
312
313 test_role(
314 $node,
315 qq{testmapuser},
316 'peer',
317 0,
318 'membership check with literal \1',
319 log_like =>
320 [qr/connection authenticated: identity="$system_user" method=peer/]);
321
322 # Do the same with a quoted regular expression for the database user this
323 # time. No replacement of \1 is done.
324 reset_pg_ident(
325 $node, 'mypeermap',
326 qq{/^.*$regex_test_string(.*)\$},
327 '"/^testmapgroupliteral\\\\1$"');
328
329 test_role(
330 $node,
331 'testmapgroupliteral\\\\1',
332 'peer',
333 0,
334 'regexp of database user with literal \1',
335 log_like =>
336 [qr/connection authenticated: identity="$system_user" method=peer/]);
337
338 done_testing();