summaryrefslogtreecommitdiff
path: root/src/test/authentication/t/003_peer.pl
blob: a6be651ea7f420fce85853e9636fdd008abec31c (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

# Copyright (c) 2021-2023, PostgreSQL Global Development Group

# Tests for peer authentication and user name map.
# The test is skipped if the platform does not support peer authentication,
# and is only able to run with Unix-domain sockets.

use strict;
use warnings;
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
if (!$use_unix_sockets)
{
	plan skip_all =>
	  "authentication tests cannot run without Unix-domain sockets";
}

# Delete pg_hba.conf from the given node, add a new entry to it
# and then execute a reload to refresh it.
sub reset_pg_hba
{
	my $node       = shift;
	my $hba_method = shift;

	unlink($node->data_dir . '/pg_hba.conf');
	$node->append_conf('pg_hba.conf', "local all all $hba_method");
	$node->reload;
	return;
}

# Delete pg_ident.conf from the given node, add a new entry to it
# and then execute a reload to refresh it.
sub reset_pg_ident
{
	my $node        = shift;
	my $map_name    = shift;
	my $system_user = shift;
	my $pg_user     = shift;

	unlink($node->data_dir . '/pg_ident.conf');
	$node->append_conf('pg_ident.conf', "$map_name $system_user $pg_user");
	$node->reload;
	return;
}

# Test access for a single role, useful to wrap all tests into one.
sub test_role
{
	local $Test::Builder::Level = $Test::Builder::Level + 1;

	my ($node, $role, $method, $expected_res, $test_details, %params) = @_;
	my $status_string = 'failed';
	$status_string = 'success' if ($expected_res eq 0);

	my $connstr = "user=$role";
	my $testname =
	  "authentication $status_string for method $method, role $role "
	  . $test_details;

	if ($expected_res eq 0)
	{
		$node->connect_ok($connstr, $testname, %params);
	}
	else
	{
		# No checks of the error message, only the status code.
		$node->connect_fails($connstr, $testname, %params);
	}
}

# Find $pattern in log file of $node.
sub find_in_log
{
	my ($node, $offset, $pattern) = @_;

	my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $offset);
	return 0 if (length($log) <= 0);

	return $log =~ m/$pattern/;
}

my $node = PostgreSQL::Test::Cluster->new('node');
$node->init;
$node->append_conf('postgresql.conf', "log_connections = on\n");
$node->start;

# Set pg_hba.conf with the peer authentication.
reset_pg_hba($node, 'peer');

# Check if peer authentication is supported on this platform.
my $log_offset = -s $node->logfile;
$node->psql('postgres');
if (find_in_log(
		$node, $log_offset,
		qr/peer authentication is not supported on this platform/))
{
	plan skip_all => 'peer authentication is not supported on this platform';
}

# Add a database role and a group, to use for the user name map.
$node->safe_psql('postgres', qq{CREATE ROLE testmapuser LOGIN});
$node->safe_psql('postgres', "CREATE ROLE testmapgroup NOLOGIN");
$node->safe_psql('postgres', "GRANT testmapgroup TO testmapuser");
# Note the double quotes here.
$node->safe_psql('postgres', 'CREATE ROLE "testmapgroupliteral\\1" LOGIN');
$node->safe_psql('postgres', 'GRANT "testmapgroupliteral\\1" TO testmapuser');

# Extract as well the system user for the user name map.
my $system_user =
  $node->safe_psql('postgres',
	q(select (string_to_array(SYSTEM_USER, ':'))[2]));

# Tests without the user name map.
# Failure as connection is attempted with a database role not mapping
# to an authorized system user.
test_role(
	$node, qq{testmapuser}, 'peer', 2,
	'without user name map',
	log_like => [qr/Peer authentication failed for user "testmapuser"/]);

# Tests with a user name map.
reset_pg_ident($node, 'mypeermap', $system_user, 'testmapuser');
reset_pg_hba($node, 'peer map=mypeermap');

# Success as the database role matches with the system user in the map.
test_role($node, qq{testmapuser}, 'peer', 0, 'with user name map',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Tests with the "all" keyword.
reset_pg_ident($node, 'mypeermap', $system_user, 'all');

test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with keyword "all" as database user in user name map',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Tests with the "all" keyword, but quoted (no effect here).
reset_pg_ident($node, 'mypeermap', $system_user, '"all"');

test_role(
	$node,
	qq{testmapuser},
	'peer',
	2,
	'with quoted keyword "all" as database user in user name map',
	log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);

# Success as the regexp of the database user matches
reset_pg_ident($node, 'mypeermap', $system_user, qq{/^testm.*\$});
test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with regexp of database user in user name map',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Failure as the regexp of the database user does not match.
reset_pg_ident($node, 'mypeermap', $system_user, qq{/^doesnotmatch.*\$});
test_role(
	$node,
	qq{testmapuser},
	'peer',
	2,
	'with bad regexp of database user in user name map',
	log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);

# Test with regular expression in user name map.
# Extract the last 3 characters from the system_user
# or the entire system_user (if its length is <= -3).
my $regex_test_string = substr($system_user, -3);

# Success as the system user regular expression matches.
reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$},
	'testmapuser');
test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with regexp of system user in user name map',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Success as both regular expressions match.
reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$},
	qq{/^testm.*\$});
test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with regexps for both system and database user in user name map',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Success as the regular expression matches and database role is the "all"
# keyword.
reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$}, 'all');
test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with regexp of system user and keyword "all" in user name map',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Success as the regular expression matches and \1 is replaced in the given
# subexpression.
reset_pg_ident($node, 'mypeermap', qq{/^$system_user(.*)\$}, 'test\1mapuser');
test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with regular expression in user name map with \1 replaced',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Success as the regular expression matches and \1 is replaced in the given
# subexpression, even if quoted.
reset_pg_ident($node, 'mypeermap', qq{/^$system_user(.*)\$},
	'"test\1mapuser"');
test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'with regular expression in user name map with quoted \1 replaced',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Failure as the regular expression does not include a subexpression, but
# the database user contains \1, requesting a replacement.
reset_pg_ident($node, 'mypeermap', qq{/^$system_user\$}, '\1testmapuser');
test_role(
	$node,
	qq{testmapuser},
	'peer', 2,
	'with regular expression in user name map with \1 not replaced',
	log_like => [
		qr/regular expression "\^$system_user\$" has no subexpressions as requested by backreference in "\\1testmapuser"/
	]);

# Concatenate system_user to system_user.
my $bad_regex_test_string = $system_user . $system_user;

# Failure as the regexp of system user does not match.
reset_pg_ident($node, 'mypeermap', qq{/^.*$bad_regex_test_string\$},
	'testmapuser');
test_role(
	$node,
	qq{testmapuser},
	'peer',
	2,
	'with regexp of system user in user name map',
	log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);

# Test using a group role match for the database user.
reset_pg_ident($node, 'mypeermap', $system_user, '+testmapgroup');

test_role($node, qq{testmapuser}, 'peer', 0, 'plain user with group',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

test_role(
	$node, qq{testmapgroup}, 'peer', 2,
	'group user with group',
	log_like => [qr/role "testmapgroup" is not permitted to log in/]);

# Now apply quotes to the group match, nullifying its effect.
reset_pg_ident($node, 'mypeermap', $system_user, '"+testmapgroup"');
test_role(
	$node,
	qq{testmapuser},
	'peer',
	2,
	'plain user with quoted group name',
	log_like => [qr/no match in usermap "mypeermap" for user "testmapuser"/]);

# Test using a regexp for the system user, with a group membership
# check for the database user.
reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string\$},
	'+testmapgroup');

test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'regexp of system user as group member',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

test_role(
	$node,
	qq{testmapgroup},
	'peer',
	2,
	'regexp of system user as non-member of group',
	log_like => [qr/role "testmapgroup" is not permitted to log in/]);

# Test that membership checks and regexes will use literal \1 instead of
# replacing it, as subexpression replacement is not allowed in this case.
reset_pg_ident($node, 'mypeermap', qq{/^.*$regex_test_string(.*)\$},
	'+testmapgroupliteral\\1');

test_role(
	$node,
	qq{testmapuser},
	'peer',
	0,
	'membership check with literal \1',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

# Do the same with a quoted regular expression for the database user this
# time.  No replacement of \1 is done.
reset_pg_ident(
	$node, 'mypeermap',
	qq{/^.*$regex_test_string(.*)\$},
	'"/^testmapgroupliteral\\\\1$"');

test_role(
	$node,
	'testmapgroupliteral\\\\1',
	'peer',
	0,
	'regexp of database user with literal \1',
	log_like =>
	  [qr/connection authenticated: identity="$system_user" method=peer/]);

done_testing();