Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 051b096

Browse files
committed
Refactor TAP test authentication/001_password.pl
The test is changed to test for connection strings rather than specific roles, and the reset logic of pg_hba.conf is extended so as the database and user name entries can be directly specified. This is aimed at being used as a base for more test scenarios of pg_hba.conf and authentication paths. Author: Bertrand Drouvot, Michael Paquier Discussion: https://postgr.es/m/Yz0xO0emJ+mxtj2a@paquier.xyz
1 parent d8df67b commit 051b096

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,30 @@
2424
sub reset_pg_hba
2525
{
2626
my $node = shift;
27+
my $database = shift;
28+
my $role = shift;
2729
my $hba_method = shift;
2830

2931
unlink($node->data_dir . '/pg_hba.conf');
3032
# just for testing purposes, use a continuation line
31-
$node->append_conf('pg_hba.conf', "local all all\\\n $hba_method");
33+
$node->append_conf('pg_hba.conf',
34+
"local $database $role\\\n $hba_method");
3235
$node->reload;
3336
return;
3437
}
3538

36-
# Test access for a single role, useful to wrap all tests into one. Extra
37-
# named parameters are passed to connect_ok/fails as-is.
38-
sub test_role
39+
# Test access for a connection string, useful to wrap all tests into one.
40+
# Extra named parameters are passed to connect_ok/fails as-is.
41+
sub test_conn
3942
{
4043
local $Test::Builder::Level = $Test::Builder::Level + 1;
4144

42-
my ($node, $role, $method, $expected_res, %params) = @_;
45+
my ($node, $connstr, $method, $expected_res, %params) = @_;
4346
my $status_string = 'failed';
4447
$status_string = 'success' if ($expected_res eq 0);
4548

46-
my $connstr = "user=$role";
4749
my $testname =
48-
"authentication $status_string for method $method, role $role";
50+
"authentication $status_string for method $method, connstr $connstr";
4951

5052
if ($expected_res eq 0)
5153
{
@@ -81,10 +83,10 @@ sub test_role
8183

8284
# For "trust" method, all users should be able to connect. These users are not
8385
# considered to be authenticated.
84-
reset_pg_hba($node, 'trust');
85-
test_role($node, 'scram_role', 'trust', 0,
86+
reset_pg_hba($node, 'all', 'all', 'trust');
87+
test_conn($node, 'user=scram_role', 'trust', 0,
8688
log_unlike => [qr/connection authenticated:/]);
87-
test_role($node, 'md5_role', 'trust', 0,
89+
test_conn($node, 'user=md5_role', 'trust', 0,
8890
log_unlike => [qr/connection authenticated:/]);
8991

9092
# SYSTEM_USER is null when not authenticated.
@@ -106,40 +108,40 @@ sub test_role
106108
);
107109

108110
# For plain "password" method, all users should also be able to connect.
109-
reset_pg_hba($node, 'password');
110-
test_role($node, 'scram_role', 'password', 0,
111+
reset_pg_hba($node, 'all', 'all', 'password');
112+
test_conn($node, 'user=scram_role', 'password', 0,
111113
log_like =>
112114
[qr/connection authenticated: identity="scram_role" method=password/]);
113-
test_role($node, 'md5_role', 'password', 0,
115+
test_conn($node, 'user=md5_role', 'password', 0,
114116
log_like =>
115117
[qr/connection authenticated: identity="md5_role" method=password/]);
116118

117119
# For "scram-sha-256" method, user "scram_role" should be able to connect.
118-
reset_pg_hba($node, 'scram-sha-256');
119-
test_role(
120+
reset_pg_hba($node, 'all', 'all', 'scram-sha-256');
121+
test_conn(
120122
$node,
121-
'scram_role',
123+
'user=scram_role',
122124
'scram-sha-256',
123125
0,
124126
log_like => [
125127
qr/connection authenticated: identity="scram_role" method=scram-sha-256/
126128
]);
127-
test_role($node, 'md5_role', 'scram-sha-256', 2,
129+
test_conn($node, 'user=md5_role', 'scram-sha-256', 2,
128130
log_unlike => [qr/connection authenticated:/]);
129131

130132
# Test that bad passwords are rejected.
131133
$ENV{"PGPASSWORD"} = 'badpass';
132-
test_role($node, 'scram_role', 'scram-sha-256', 2,
134+
test_conn($node, 'user=scram_role', 'scram-sha-256', 2,
133135
log_unlike => [qr/connection authenticated:/]);
134136
$ENV{"PGPASSWORD"} = 'pass';
135137

136138
# For "md5" method, all users should be able to connect (SCRAM
137139
# authentication will be performed for the user with a SCRAM secret.)
138-
reset_pg_hba($node, 'md5');
139-
test_role($node, 'scram_role', 'md5', 0,
140+
reset_pg_hba($node, 'all', 'all', 'md5');
141+
test_conn($node, 'user=scram_role', 'md5', 0,
140142
log_like =>
141143
[qr/connection authenticated: identity="scram_role" method=md5/]);
142-
test_role($node, 'md5_role', 'md5', 0,
144+
test_conn($node, 'user=md5_role', 'md5', 0,
143145
log_like =>
144146
[qr/connection authenticated: identity="md5_role" method=md5/]);
145147

@@ -164,13 +166,13 @@ sub test_role
164166

165167
# Tests for channel binding without SSL.
166168
# Using the password authentication method; channel binding can't work
167-
reset_pg_hba($node, 'password');
169+
reset_pg_hba($node, 'all', 'all', 'password');
168170
$ENV{"PGCHANNELBINDING"} = 'require';
169-
test_role($node, 'scram_role', 'scram-sha-256', 2);
171+
test_conn($node, 'user=scram_role', 'scram-sha-256', 2);
170172
# SSL not in use; channel binding still can't work
171-
reset_pg_hba($node, 'scram-sha-256');
173+
reset_pg_hba($node, 'all', 'all', 'scram-sha-256');
172174
$ENV{"PGCHANNELBINDING"} = 'require';
173-
test_role($node, 'scram_role', 'scram-sha-256', 2);
175+
test_conn($node, 'user=scram_role', 'scram-sha-256', 2);
174176

175177
# Test .pgpass processing; but use a temp file, don't overwrite the real one!
176178
my $pgpassfile = "${PostgreSQL::Test::Utils::tmp_check}/pgpass";
@@ -187,15 +189,15 @@ sub test_role
187189
!);
188190
chmod 0600, $pgpassfile or die;
189191

190-
reset_pg_hba($node, 'password');
191-
test_role($node, 'scram_role', 'password from pgpass', 0);
192-
test_role($node, 'md5_role', 'password from pgpass', 2);
192+
reset_pg_hba($node, 'all', 'all', 'password');
193+
test_conn($node, 'user=scram_role', 'password from pgpass', 0);
194+
test_conn($node, 'user=md5_role', 'password from pgpass', 2);
193195

194196
append_to_file(
195197
$pgpassfile, qq!
196198
*:*:*:md5_role:p\\ass
197199
!);
198200

199-
test_role($node, 'md5_role', 'password from pgpass', 0);
201+
test_conn($node, 'user=md5_role', 'password from pgpass', 0);
200202

201203
done_testing();

0 commit comments

Comments
 (0)