Debian Codegraph @

src/test/authentication/t/001_password.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Set of tests for authentication and pg_hba.conf. The following password
5 # methods are checked through this test:
6 # - Plain
7 # - MD5-encrypted
8 # - SCRAM-encrypted
9 # This test can only run with Unix-domain sockets.
10 #
11 # There's also a few tests of the log_connections GUC here.
12
13 use strict;
14 use warnings FATAL => 'all';
15 use PostgreSQL::Test::Cluster;
16 use PostgreSQL::Test::Utils;
17 use Test::More;
18 if (!$use_unix_sockets)
19 {
20 plan skip_all =>
21 "authentication tests cannot run without Unix-domain sockets";
22 }
23
24 # Delete pg_hba.conf from the given node, add a new entry to it
25 # and then execute a reload to refresh it.
26 sub reset_pg_hba
27 {
28 my $node = shift;
29 my $database = shift;
30 my $role = shift;
31 my $hba_method = shift;
32
33 unlink($node->data_dir . '/pg_hba.conf');
34 # just for testing purposes, use a continuation line
35 $node->append_conf('pg_hba.conf',
36 "local $database $role\\\n $hba_method");
37 $node->reload;
38 return;
39 }
40
41 # Test access for a connection string, useful to wrap all tests into one.
42 # Extra named parameters are passed to connect_ok/fails as-is.
43 sub test_conn
44 {
45 local $Test::Builder::Level = $Test::Builder::Level + 1;
46
47 my ($node, $connstr, $method, $expected_res, %params) = @_;
48 my $status_string = 'failed';
49 $status_string = 'success' if ($expected_res eq 0);
50
51 my $testname =
52 "authentication $status_string for method $method, connstr $connstr";
53
54 if ($expected_res eq 0)
55 {
56 $node->connect_ok($connstr, $testname, %params);
57 }
58 else
59 {
60 # No checks of the error message, only the status code.
61 $node->connect_fails($connstr, $testname, %params);
62 }
63 }
64
65 # Initialize primary node
66 my $node = PostgreSQL::Test::Cluster->new('primary');
67 $node->init;
68 $node->append_conf('postgresql.conf', "log_connections = on\n");
69 # Needed to allow connect_fails to inspect postmaster log:
70 $node->append_conf('postgresql.conf', "log_min_messages = debug2");
71 $node->start;
72
73 # Test behavior of log_connections GUC
74 #
75 # There wasn't another test file where these tests obviously fit, and we don't
76 # want to incur the cost of spinning up a new cluster just to test one GUC.
77
78 # Make a database for the log_connections tests to avoid test fragility if
79 # other tests are added to this file in the future
80 $node->safe_psql('postgres', "CREATE DATABASE test_log_connections");
81
82 my $log_connections =
83 $node->safe_psql('test_log_connections', q(SHOW log_connections;));
84 is($log_connections, 'on', qq(check log connections has expected value 'on'));
85
86 $node->connect_ok(
87 'test_log_connections',
88 qq(log_connections 'on' works as expected for backwards compatibility),
89 log_like => [
90 qr/connection received/,
91 qr/connection authenticated/,
92 qr/connection authorized: user=\S+ database=test_log_connections/,
93 ],
94 log_unlike => [ qr/connection ready/, ],);
95
96 $node->safe_psql(
97 'test_log_connections',
98 q[ALTER SYSTEM SET log_connections = receipt,authorization,setup_durations;
99 SELECT pg_reload_conf();]);
100
101 $node->connect_ok(
102 'test_log_connections',
103 q(log_connections with subset of specified options logs only those aspects),
104 log_like => [
105 qr/connection received/,
106 qr/connection authorized: user=\S+ database=test_log_connections/,
107 qr/connection ready/,
108 ],
109 log_unlike => [ qr/connection authenticated/, ],);
110
111 $node->safe_psql('test_log_connections',
112 qq(ALTER SYSTEM SET log_connections = 'all'; SELECT pg_reload_conf();));
113
114 $node->connect_ok(
115 'test_log_connections',
116 qq(log_connections 'all' logs all available connection aspects),
117 log_like => [
118 qr/connection received/,
119 qr/connection authenticated/,
120 qr/connection authorized: user=\S+ database=test_log_connections/,
121 qr/connection ready/,
122 ],);
123
124 # Authentication tests
125
126 # could fail in FIPS mode
127 my $md5_works = ($node->psql('postgres', "select md5('')") == 0);
128
129 # Create 3 roles with different password methods for each one. The same
130 # password is used for all of them.
131 is( $node->psql(
132 'postgres',
133 "SET password_encryption='scram-sha-256'; CREATE ROLE scram_role LOGIN PASSWORD 'pass';"
134 ),
135 0,
136 'created user with SCRAM password');
137 is( $node->psql(
138 'postgres',
139 "SET password_encryption='md5'; CREATE ROLE md5_role LOGIN PASSWORD 'pass';"
140 ),
141 $md5_works ? 0 : 3,
142 'created user with md5 password');
143 # Set up a table for tests of SYSTEM_USER.
144 $node->safe_psql(
145 'postgres',
146 "CREATE TABLE sysuser_data (n) AS SELECT NULL FROM generate_series(1, 10);
147 GRANT ALL ON sysuser_data TO scram_role;");
148 $ENV{"PGPASSWORD"} = 'pass';
149
150 # Create a role that contains a comma to stress the parsing.
151 $node->safe_psql('postgres',
152 q{SET password_encryption='scram-sha-256'; CREATE ROLE "scram,role" LOGIN PASSWORD 'pass';}
153 );
154
155 # Create a role with a non-default iteration count
156 $node->safe_psql(
157 'postgres',
158 "SET password_encryption='scram-sha-256';
159 SET scram_iterations=1024;
160 CREATE ROLE scram_role_iter LOGIN PASSWORD 'pass';
161 RESET scram_iterations;"
162 );
163
164 my $res = $node->safe_psql(
165 'postgres',
166 "SELECT substr(rolpassword,1,19)
167 FROM pg_authid
168 WHERE rolname = 'scram_role_iter'");
169 is($res, 'SCRAM-SHA-256$1024:', 'scram_iterations in server side ROLE');
170
171 # If we don't have IO::Pty, forget it, because IPC::Run depends on that
172 # to support pty connections. Also skip if IPC::Run isn't at least 0.98
173 # as earlier version cause the session to time out.
174 SKIP:
175 {
176 skip "IO::Pty and IPC::Run >= 0.98 required", 1
177 unless eval { require IO::Pty; IPC::Run->VERSION('0.98'); };
178
179 # Alter the password on the created role using \password in psql to ensure
180 # that clientside password changes use the scram_iterations value when
181 # calculating SCRAM secrets.
182 my $session = $node->interactive_psql('postgres');
183
184 $session->set_query_timer_restart();
185 $session->query("SET password_encryption='scram-sha-256';");
186 $session->query("SET scram_iterations=42;");
187 $session->query_until(qr/Enter new password/,
188 "\\password scram_role_iter\n");
189 $session->query_until(qr/Enter it again/, "pass\n");
190 $session->query_until(qr/postgres=# /, "pass\n");
191 $session->quit;
192
193 $res = $node->safe_psql(
194 'postgres',
195 "SELECT substr(rolpassword,1,17)
196 FROM pg_authid
197 WHERE rolname = 'scram_role_iter'");
198 is($res, 'SCRAM-SHA-256$42:',
199 'scram_iterations in psql \password command');
200 }
201
202 # Create a database to test regular expression.
203 $node->safe_psql('postgres', "CREATE database regex_testdb;");
204
205 # For "trust" method, all users should be able to connect.
206 reset_pg_hba($node, 'all', 'all', 'trust');
207 test_conn($node, 'user=scram_role', 'trust', 0,
208 log_like =>
209 [qr/connection authenticated: user="scram_role" method=trust/]);
210 SKIP:
211 {
212 skip "MD5 not supported" unless $md5_works;
213 test_conn($node, 'user=md5_role', 'trust', 0,
214 log_like =>
215 [qr/connection authenticated: user="md5_role" method=trust/]);
216 }
217
218 # SYSTEM_USER is null when not authenticated.
219 $res = $node->safe_psql('postgres', "SELECT SYSTEM_USER IS NULL;");
220 is($res, 't', "users with trust authentication use SYSTEM_USER = NULL");
221
222 # Test SYSTEM_USER with parallel workers when not authenticated.
223 $res = $node->safe_psql(
224 'postgres', qq(
225 SET min_parallel_table_scan_size TO 0;
226 SET parallel_setup_cost TO 0;
227 SET parallel_tuple_cost TO 0;
228 SET max_parallel_workers_per_gather TO 2;
229
230 SELECT bool_and(SYSTEM_USER IS NOT DISTINCT FROM n) FROM sysuser_data;),
231 connstr => "user=scram_role");
232 is($res, 't',
233 "users with trust authentication use SYSTEM_USER = NULL in parallel workers"
234 );
235
236 # Explicitly specifying an empty require_auth (the default) should always
237 # succeed.
238 $node->connect_ok("user=scram_role require_auth=",
239 "empty require_auth succeeds");
240
241 # All these values of require_auth should fail, as trust is expected.
242 $node->connect_fails(
243 "user=scram_role require_auth=gss",
244 "GSS authentication required, fails with trust auth",
245 expected_stderr =>
246 qr/authentication method requirement "gss" failed: server did not complete authentication/
247 );
248 $node->connect_fails(
249 "user=scram_role require_auth=sspi",
250 "SSPI authentication required, fails with trust auth",
251 expected_stderr =>
252 qr/authentication method requirement "sspi" failed: server did not complete authentication/
253 );
254 $node->connect_fails(
255 "user=scram_role require_auth=password",
256 "password authentication required, fails with trust auth",
257 expected_stderr =>
258 qr/authentication method requirement "password" failed: server did not complete authentication/
259 );
260 $node->connect_fails(
261 "user=scram_role require_auth=md5",
262 "MD5 authentication required, fails with trust auth",
263 expected_stderr =>
264 qr/authentication method requirement "md5" failed: server did not complete authentication/
265 );
266 $node->connect_fails(
267 "user=scram_role require_auth=scram-sha-256",
268 "SCRAM authentication required, fails with trust auth",
269 expected_stderr =>
270 qr/authentication method requirement "scram-sha-256" failed: server did not complete authentication/
271 );
272 $node->connect_fails(
273 "user=scram_role require_auth=password,scram-sha-256",
274 "password and SCRAM authentication required, fails with trust auth",
275 expected_stderr =>
276 qr/authentication method requirement "password,scram-sha-256" failed: server did not complete authentication/
277 );
278
279 # These negative patterns of require_auth should succeed.
280 $node->connect_ok("user=scram_role require_auth=!gss",
281 "GSS authentication can be forbidden, succeeds with trust auth");
282 $node->connect_ok("user=scram_role require_auth=!sspi",
283 "SSPI authentication can be forbidden, succeeds with trust auth");
284 $node->connect_ok("user=scram_role require_auth=!password",
285 "password authentication can be forbidden, succeeds with trust auth");
286 $node->connect_ok("user=scram_role require_auth=!md5",
287 "md5 authentication can be forbidden, succeeds with trust auth");
288 $node->connect_ok("user=scram_role require_auth=!scram-sha-256",
289 "SCRAM authentication can be forbidden, succeeds with trust auth");
290 $node->connect_ok(
291 "user=scram_role require_auth=!password,!scram-sha-256",
292 "multiple authentication types forbidden, succeeds with trust auth");
293
294 # require_auth=[!]none should interact correctly with trust auth.
295 $node->connect_ok("user=scram_role require_auth=none",
296 "all authentication types forbidden, succeeds with trust auth");
297 $node->connect_fails(
298 "user=scram_role require_auth=!none",
299 "any authentication types required, fails with trust auth",
300 expected_stderr => qr/server did not complete authentication/);
301
302 # Negative and positive require_auth methods can't be mixed.
303 $node->connect_fails(
304 "user=scram_role require_auth=scram-sha-256,!md5",
305 "negative require_auth methods cannot be mixed with positive ones",
306 expected_stderr =>
307 qr/negative require_auth method "!md5" cannot be mixed with non-negative methods/
308 );
309 $node->connect_fails(
310 "user=scram_role require_auth=!password,!none,scram-sha-256",
311 "positive require_auth methods cannot be mixed with negative one",
312 expected_stderr =>
313 qr/require_auth method "scram-sha-256" cannot be mixed with negative methods/
314 );
315
316 # require_auth methods cannot have duplicated values.
317 $node->connect_fails(
318 "user=scram_role require_auth=password,md5,password",
319 "require_auth methods cannot include duplicates, positive case",
320 expected_stderr =>
321 qr/require_auth method "password" is specified more than once/);
322 $node->connect_fails(
323 "user=scram_role require_auth=!password,!md5,!password",
324 "require_auth methods cannot be duplicated, negative case",
325 expected_stderr =>
326 qr/require_auth method "!password" is specified more than once/);
327 $node->connect_fails(
328 "user=scram_role require_auth=none,md5,none",
329 "require_auth methods cannot be duplicated, none case",
330 expected_stderr =>
331 qr/require_auth method "none" is specified more than once/);
332 $node->connect_fails(
333 "user=scram_role require_auth=!none,!md5,!none",
334 "require_auth methods cannot be duplicated, !none case",
335 expected_stderr =>
336 qr/require_auth method "!none" is specified more than once/);
337 $node->connect_fails(
338 "user=scram_role require_auth=scram-sha-256,scram-sha-256",
339 "require_auth methods cannot be duplicated, scram-sha-256 case",
340 expected_stderr =>
341 qr/require_auth method "scram-sha-256" is specified more than once/);
342 $node->connect_fails(
343 "user=scram_role require_auth=!scram-sha-256,!scram-sha-256",
344 "require_auth methods cannot be duplicated, !scram-sha-256 case",
345 expected_stderr =>
346 qr/require_auth method "!scram-sha-256" is specified more than once/);
347
348 # Unknown value defined in require_auth.
349 $node->connect_fails(
350 "user=scram_role require_auth=none,abcdefg",
351 "unknown require_auth methods are rejected",
352 expected_stderr => qr/invalid require_auth value: "abcdefg"/);
353
354 # For plain "password" method, all users should also be able to connect.
355 reset_pg_hba($node, 'all', 'all', 'password');
356 test_conn($node, 'user=scram_role', 'password', 0,
357 log_like =>
358 [qr/connection authenticated: identity="scram_role" method=password/]);
359 SKIP:
360 {
361 skip "MD5 not supported" unless $md5_works;
362 test_conn($node, 'user=md5_role', 'password', 0,
363 log_like =>
364 [qr/connection authenticated: identity="md5_role" method=password/]
365 );
366 }
367
368 # require_auth succeeds here with a plaintext password.
369 $node->connect_ok("user=scram_role require_auth=password",
370 "password authentication required, works with password auth");
371 $node->connect_ok("user=scram_role require_auth=!none",
372 "any authentication required, works with password auth");
373 $node->connect_ok(
374 "user=scram_role require_auth=scram-sha-256,password,md5",
375 "multiple authentication types required, works with password auth");
376
377 # require_auth fails for other authentication types.
378 $node->connect_fails(
379 "user=scram_role require_auth=md5",
380 "md5 authentication required, fails with password auth",
381 expected_stderr =>
382 qr/authentication method requirement "md5" failed: server requested a cleartext password/
383 );
384 $node->connect_fails(
385 "user=scram_role require_auth=scram-sha-256",
386 "SCRAM authentication required, fails with password auth",
387 expected_stderr =>
388 qr/authentication method requirement "scram-sha-256" failed: server requested a cleartext password/
389 );
390 $node->connect_fails(
391 "user=scram_role require_auth=none",
392 "all authentication forbidden, fails with password auth",
393 expected_stderr =>
394 qr/authentication method requirement "none" failed: server requested a cleartext password/
395 );
396
397 # Disallowing password authentication fails, even if requested by server.
398 $node->connect_fails(
399 "user=scram_role require_auth=!password",
400 "password authentication forbidden, fails with password auth",
401 expected_stderr => qr/server requested a cleartext password/);
402 $node->connect_fails(
403 "user=scram_role require_auth=!password,!md5,!scram-sha-256",
404 "multiple authentication types forbidden, fails with password auth",
405 expected_stderr =>
406 qr/ method requirement "!password,!md5,!scram-sha-256" failed: server requested a cleartext password/
407 );
408
409 # For "scram-sha-256" method, user "scram_role" should be able to connect.
410 reset_pg_hba($node, 'all', 'all', 'scram-sha-256');
411 test_conn(
412 $node,
413 'user=scram_role',
414 'scram-sha-256',
415 0,
416 log_like => [
417 qr/connection authenticated: identity="scram_role" method=scram-sha-256/
418 ]);
419 test_conn(
420 $node,
421 'user=scram_role_iter',
422 'scram-sha-256',
423 0,
424 log_like => [
425 qr/connection authenticated: identity="scram_role_iter" method=scram-sha-256/
426 ]);
427 test_conn($node, 'user=md5_role', 'scram-sha-256', 2,
428 log_unlike => [qr/connection authenticated:/]);
429
430 # require_auth should succeed with SCRAM when it is required.
431 $node->connect_ok(
432 "user=scram_role require_auth=scram-sha-256",
433 "SCRAM authentication required, works with SCRAM auth");
434 $node->connect_ok("user=scram_role require_auth=!none",
435 "any authentication required, works with SCRAM auth");
436 $node->connect_ok(
437 "user=scram_role require_auth=password,scram-sha-256,md5",
438 "multiple authentication types required, works with SCRAM auth");
439
440 # Authentication fails for other authentication types.
441 $node->connect_fails(
442 "user=scram_role require_auth=password",
443 "password authentication required, fails with SCRAM auth",
444 expected_stderr =>
445 qr/authentication method requirement "password" failed: server requested SASL authentication/
446 );
447 $node->connect_fails(
448 "user=scram_role require_auth=md5",
449 "md5 authentication required, fails with SCRAM auth",
450 expected_stderr =>
451 qr/authentication method requirement "md5" failed: server requested SASL authentication/
452 );
453 $node->connect_fails(
454 "user=scram_role require_auth=none",
455 "all authentication forbidden, fails with SCRAM auth",
456 expected_stderr =>
457 qr/authentication method requirement "none" failed: server requested SASL authentication/
458 );
459
460 # Authentication fails if SCRAM authentication is forbidden.
461 $node->connect_fails(
462 "user=scram_role require_auth=!scram-sha-256",
463 "SCRAM authentication forbidden, fails with SCRAM auth",
464 expected_stderr => qr/server requested SCRAM-SHA-256 authentication/);
465 $node->connect_fails(
466 "user=scram_role require_auth=!password,!md5,!scram-sha-256",
467 "multiple authentication types forbidden, fails with SCRAM auth",
468 expected_stderr => qr/server requested SCRAM-SHA-256 authentication/);
469
470 # Test that bad passwords are rejected.
471 $ENV{"PGPASSWORD"} = 'badpass';
472 test_conn($node, 'user=scram_role', 'scram-sha-256', 2,
473 log_unlike => [qr/connection authenticated:/]);
474 $ENV{"PGPASSWORD"} = 'pass';
475
476 # For "md5" method, all users should be able to connect (SCRAM
477 # authentication will be performed for the user with a SCRAM secret.)
478 reset_pg_hba($node, 'all', 'all', 'md5');
479 test_conn($node, 'user=scram_role', 'md5', 0,
480 log_like =>
481 [qr/connection authenticated: identity="scram_role" method=md5/]);
482 SKIP:
483 {
484 skip "MD5 not supported" unless $md5_works;
485 test_conn($node, 'user=md5_role', 'md5', 0,
486 log_like =>
487 [qr/connection authenticated: identity="md5_role" method=md5/]);
488 }
489
490 # require_auth succeeds with SCRAM required.
491 $node->connect_ok(
492 "user=scram_role require_auth=scram-sha-256",
493 "SCRAM authentication required, works with SCRAM auth");
494 $node->connect_ok("user=scram_role require_auth=!none",
495 "any authentication required, works with SCRAM auth");
496 $node->connect_ok(
497 "user=scram_role require_auth=md5,scram-sha-256,password",
498 "multiple authentication types required, works with SCRAM auth");
499
500 # Authentication fails if other types are required.
501 $node->connect_fails(
502 "user=scram_role require_auth=password",
503 "password authentication required, fails with SCRAM auth",
504 expected_stderr =>
505 qr/authentication method requirement "password" failed: server requested SASL authentication/
506 );
507 $node->connect_fails(
508 "user=scram_role require_auth=md5",
509 "MD5 authentication required, fails with SCRAM auth",
510 expected_stderr =>
511 qr/authentication method requirement "md5" failed: server requested SASL authentication/
512 );
513 $node->connect_fails(
514 "user=scram_role require_auth=none",
515 "all authentication types forbidden, fails with SCRAM auth",
516 expected_stderr =>
517 qr/authentication method requirement "none" failed: server requested SASL authentication/
518 );
519
520 # Authentication fails if SCRAM is forbidden.
521 $node->connect_fails(
522 "user=scram_role require_auth=!scram-sha-256",
523 "password authentication forbidden, fails with SCRAM auth",
524 expected_stderr =>
525 qr/authentication method requirement "!scram-sha-256" failed: server requested SCRAM-SHA-256 authentication/
526 );
527 $node->connect_fails(
528 "user=scram_role require_auth=!password,!md5,!scram-sha-256",
529 "multiple authentication types forbidden, fails with SCRAM auth",
530 expected_stderr =>
531 qr/authentication method requirement "!password,!md5,!scram-sha-256" failed: server requested SCRAM-SHA-256 authentication/
532 );
533
534 # Test SYSTEM_USER <> NULL with parallel workers.
535 $node->safe_psql(
536 'postgres',
537 "TRUNCATE sysuser_data;
538 INSERT INTO sysuser_data SELECT 'md5:scram_role' FROM generate_series(1, 10);",
539 connstr => "user=scram_role");
540 $res = $node->safe_psql(
541 'postgres', qq(
542 SET min_parallel_table_scan_size TO 0;
543 SET parallel_setup_cost TO 0;
544 SET parallel_tuple_cost TO 0;
545 SET max_parallel_workers_per_gather TO 2;
546
547 SELECT bool_and(SYSTEM_USER IS NOT DISTINCT FROM n) FROM sysuser_data;),
548 connstr => "user=scram_role");
549 is($res, 't',
550 "users with md5 authentication use SYSTEM_USER = md5:role in parallel workers"
551 );
552
553 # Tests for channel binding without SSL.
554 # Using the password authentication method; channel binding can't work
555 reset_pg_hba($node, 'all', 'all', 'password');
556 $ENV{"PGCHANNELBINDING"} = 'require';
557 test_conn($node, 'user=scram_role', 'scram-sha-256', 2);
558 # SSL not in use; channel binding still can't work
559 reset_pg_hba($node, 'all', 'all', 'scram-sha-256');
560 $ENV{"PGCHANNELBINDING"} = 'require';
561 test_conn($node, 'user=scram_role', 'scram-sha-256', 2);
562
563 # Test .pgpass processing; but use a temp file, don't overwrite the real one!
564 my $pgpassfile = "${PostgreSQL::Test::Utils::tmp_check}/pgpass";
565
566 delete $ENV{"PGPASSWORD"};
567 delete $ENV{"PGCHANNELBINDING"};
568 $ENV{"PGPASSFILE"} = $pgpassfile;
569
570 unlink($pgpassfile);
571 append_to_file(
572 $pgpassfile, qq!
573 # This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file. This very long comment is just here to exercise handling of long lines in the file.
574 *:*:postgres:scram_role:pass:this is not part of the password.
575 !);
576 chmod 0600, $pgpassfile or die;
577
578 reset_pg_hba($node, 'all', 'all', 'password');
579 test_conn($node, 'user=scram_role', 'password from pgpass', 0);
580 test_conn($node, 'user=md5_role', 'password from pgpass', 2);
581
582 append_to_file(
583 $pgpassfile, qq!
584 *:*:*:scram_role:p\\ass
585 *:*:*:scram,role:p\\ass
586 !);
587
588 test_conn($node, 'user=scram_role', 'password from pgpass', 0);
589
590 # Testing with regular expression for username. The third regexp matches.
591 reset_pg_hba($node, 'all', '/^.*nomatch.*$, baduser, /^scr.*$', 'password');
592 test_conn(
593 $node,
594 'user=scram_role',
595 'password, matching regexp for username',
596 0,
597 log_like =>
598 [qr/connection authenticated: identity="scram_role" method=password/]);
599
600 # The third regex does not match anymore.
601 reset_pg_hba($node, 'all', '/^.*nomatch.*$, baduser, /^sc_r.*$', 'password');
602 test_conn($node, 'user=scram_role',
603 'password, non matching regexp for username',
604 2, log_unlike => [qr/connection authenticated:/]);
605
606 # Test with a comma in the regular expression. In this case, the use of
607 # double quotes is mandatory so as this is not considered as two elements
608 # of the user name list when parsing pg_hba.conf.
609 reset_pg_hba($node, 'all', '"/^.*m,.*e$"', 'password');
610 test_conn(
611 $node,
612 'user=scram,role',
613 'password, matching regexp for username',
614 0,
615 log_like =>
616 [qr/connection authenticated: identity="scram,role" method=password/]);
617
618 # Testing with regular expression for dbname. The third regex matches.
619 reset_pg_hba($node, '/^.*nomatch.*$, baddb, /^regex_t.*b$', 'all',
620 'password');
621 test_conn(
622 $node,
623 'user=scram_role dbname=regex_testdb',
624 'password, matching regexp for dbname',
625 0,
626 log_like =>
627 [qr/connection authenticated: identity="scram_role" method=password/]);
628
629 # The third regexp does not match anymore.
630 reset_pg_hba($node, '/^.*nomatch.*$, baddb, /^regex_t.*ba$',
631 'all', 'password');
632 test_conn(
633 $node,
634 'user=scram_role dbname=regex_testdb',
635 'password, non matching regexp for dbname',
636 2, log_unlike => [qr/connection authenticated:/]);
637
638 unlink($pgpassfile);
639 delete $ENV{"PGPASSFILE"};
640
641 note "Authentication tests with specific HBA policies on roles";
642
643 # Create database and roles for membership tests
644 reset_pg_hba($node, 'all', 'all', 'trust');
645 # Database and root role names match for "samerole" and "samegroup".
646 $node->safe_psql('postgres', "CREATE DATABASE regress_regression_group;");
647 $node->safe_psql(
648 'postgres',
649 qq{CREATE ROLE regress_regression_group LOGIN PASSWORD 'pass';
650 CREATE ROLE regress_member LOGIN SUPERUSER IN ROLE regress_regression_group PASSWORD 'pass';
651 CREATE ROLE regress_not_member LOGIN SUPERUSER PASSWORD 'pass';});
652
653 # Test role with exact matching, no members allowed.
654 $ENV{"PGPASSWORD"} = 'pass';
655 reset_pg_hba($node, 'all', 'regress_regression_group', 'scram-sha-256');
656 test_conn(
657 $node,
658 'user=regress_regression_group',
659 'scram-sha-256',
660 0,
661 log_like => [
662 qr/connection authenticated: identity="regress_regression_group" method=scram-sha-256/
663 ]);
664 test_conn(
665 $node,
666 'user=regress_member',
667 'scram-sha-256',
668 2,
669 log_unlike => [
670 qr/connection authenticated: identity="regress_member" method=scram-sha-256/
671 ]);
672 test_conn(
673 $node,
674 'user=regress_not_member',
675 'scram-sha-256',
676 2,
677 log_unlike => [
678 qr/connection authenticated: identity="regress_not_member" method=scram-sha-256/
679 ]);
680
681 # Test role membership with '+', where all the members are allowed
682 # to connect.
683 reset_pg_hba($node, 'all', '+regress_regression_group', 'scram-sha-256');
684 test_conn(
685 $node,
686 'user=regress_regression_group',
687 'scram-sha-256',
688 0,
689 log_like => [
690 qr/connection authenticated: identity="regress_regression_group" method=scram-sha-256/
691 ]);
692 test_conn(
693 $node,
694 'user=regress_member',
695 'scram-sha-256',
696 0,
697 log_like => [
698 qr/connection authenticated: identity="regress_member" method=scram-sha-256/
699 ]);
700 test_conn(
701 $node,
702 'user=regress_not_member',
703 'scram-sha-256',
704 2,
705 log_unlike => [
706 qr/connection authenticated: identity="regress_not_member" method=scram-sha-256/
707 ]);
708
709 # Test role membership is respected for samerole
710 $ENV{"PGDATABASE"} = 'regress_regression_group';
711 reset_pg_hba($node, 'samerole', 'all', 'scram-sha-256');
712 test_conn(
713 $node,
714 'user=regress_regression_group',
715 'scram-sha-256',
716 0,
717 log_like => [
718 qr/connection authenticated: identity="regress_regression_group" method=scram-sha-256/
719 ]);
720 test_conn(
721 $node,
722 'user=regress_member',
723 'scram-sha-256',
724 0,
725 log_like => [
726 qr/connection authenticated: identity="regress_member" method=scram-sha-256/
727 ]);
728 test_conn(
729 $node,
730 'user=regress_not_member',
731 'scram-sha-256',
732 2,
733 log_unlike => [
734 qr/connection authenticated: identity="regress_not_member" method=scram-sha-256/
735 ]);
736
737 # Test role membership is respected for samegroup
738 reset_pg_hba($node, 'samegroup', 'all', 'scram-sha-256');
739 test_conn(
740 $node,
741 'user=regress_regression_group',
742 'scram-sha-256',
743 0,
744 log_like => [
745 qr/connection authenticated: identity="regress_regression_group" method=scram-sha-256/
746 ]);
747 test_conn(
748 $node,
749 'user=regress_member',
750 'scram-sha-256',
751 0,
752 log_like => [
753 qr/connection authenticated: identity="regress_member" method=scram-sha-256/
754 ]);
755 test_conn(
756 $node,
757 'user=regress_not_member',
758 'scram-sha-256',
759 2,
760 log_unlike => [
761 qr/connection authenticated: identity="regress_not_member" method=scram-sha-256/
762 ]);
763
764 done_testing();