16
16
use TestLib;
17
17
use PostgresNode;
18
18
use Test::More;
19
+ use Time::HiRes qw( usleep) ;
19
20
20
21
if ($ENV {with_gssapi } eq ' yes' )
21
22
{
22
- plan tests => 18 ;
23
+ plan tests => 34 ;
23
24
}
24
25
else
25
26
{
74
75
my $kdc_pidfile = " ${TestLib::tmp_check} /krb5kdc.pid" ;
75
76
my $keytab = " ${TestLib::tmp_check} /krb5.keytab" ;
76
77
78
+ my $dbname = ' postgres' ;
79
+ my $username = ' test1' ;
80
+ my $application = ' 001_auth.pl' ;
81
+
77
82
note " setting up Kerberos" ;
78
83
79
84
my ($stdout , $krb5_version );
160
165
$node -> init;
161
166
$node -> append_conf(' postgresql.conf' , " listen_addresses = '$hostaddr '" );
162
167
$node -> append_conf(' postgresql.conf' , " krb_server_keyfile = '$keytab '" );
168
+ $node -> append_conf(' postgresql.conf' , " logging_collector = on" );
169
+ $node -> append_conf(' postgresql.conf' , " log_connections = on" );
163
170
$node -> start;
164
171
165
172
$node -> safe_psql(' postgres' , ' CREATE USER test1;' );
169
176
# Test connection success or failure, and if success, that query returns true.
170
177
sub test_access
171
178
{
172
- my ($node , $role , $query , $expected_res , $gssencmode , $test_name ) = @_ ;
179
+ my ($node , $role , $query , $expected_res , $gssencmode , $test_name , $expect_log_msg ) = @_ ;
173
180
174
181
# need to connect over TCP/IP for Kerberos
175
182
my ($res , $stdoutres , $stderrres ) = $node -> psql(
@@ -192,6 +199,33 @@ sub test_access
192
199
{
193
200
is($res , $expected_res , $test_name );
194
201
}
202
+
203
+ # Verify specified log message is logged in the log file.
204
+ if ($expect_log_msg ne ' ' )
205
+ {
206
+ my $current_logfiles = slurp_file($node -> data_dir . ' /current_logfiles' );
207
+ note " current_logfiles = $current_logfiles " ;
208
+ like($current_logfiles , qr | ^stderr log/postgresql-.*log$ | ,
209
+ ' current_logfiles is sane' );
210
+
211
+ my $lfname = $current_logfiles ;
212
+ $lfname =~ s / ^stderr // ;
213
+ chomp $lfname ;
214
+
215
+ # might need to retry if logging collector process is slow...
216
+ my $max_attempts = 180 * 10;
217
+ my $first_logfile ;
218
+ for (my $attempts = 0; $attempts < $max_attempts ; $attempts ++)
219
+ {
220
+ $first_logfile = slurp_file($node -> data_dir . ' /' . $lfname );
221
+ last if $first_logfile =~ m /\Q $expect_log_msg \E / ;
222
+ usleep(100_000);
223
+ }
224
+
225
+ like($first_logfile , qr /\Q $expect_log_msg \E / ,
226
+ ' found expected log file content' );
227
+ }
228
+
195
229
return ;
196
230
}
197
231
@@ -223,11 +257,11 @@ sub test_query
223
257
qq{ host all all $hostaddr /32 gss map=mymap} );
224
258
$node -> restart;
225
259
226
- test_access($node , ' test1' , ' SELECT true' , 2, ' ' , ' fails without ticket' );
260
+ test_access($node , ' test1' , ' SELECT true' , 2, ' ' , ' fails without ticket' , ' ' );
227
261
228
262
run_log [ $kinit , ' test1' ], \$test1_password or BAIL_OUT($? );
229
263
230
- test_access($node , ' test1' , ' SELECT true' , 2, ' ' , ' fails without mapping' );
264
+ test_access($node , ' test1' , ' SELECT true' , 2, ' ' , ' fails without mapping' , ' ' );
231
265
232
266
$node -> append_conf(' pg_ident.conf' , qq{ mymap /^(.*)\@ $realm \$ \\ 1} );
233
267
$node -> restart;
@@ -238,42 +272,49 @@ sub test_query
238
272
' SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
239
273
0,
240
274
' ' ,
241
- ' succeeds with mapping with default gssencmode and host hba' );
275
+ ' succeeds with mapping with default gssencmode and host hba' ,
276
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=yes, principal=test1\@ $realm )"
277
+ );
278
+
242
279
test_access(
243
280
$node ,
244
- " test1" ,
281
+ ' test1' ,
245
282
' SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
246
283
0,
247
- " gssencmode=prefer" ,
248
- " succeeds with GSS-encrypted access preferred with host hba" );
284
+ ' gssencmode=prefer' ,
285
+ ' succeeds with GSS-encrypted access preferred with host hba' ,
286
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=yes, principal=test1\@ $realm )"
287
+ );
249
288
test_access(
250
289
$node ,
251
- " test1" ,
290
+ ' test1' ,
252
291
' SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
253
292
0,
254
- " gssencmode=require" ,
255
- " succeeds with GSS-encrypted access required with host hba" );
293
+ ' gssencmode=require' ,
294
+ ' succeeds with GSS-encrypted access required with host hba' ,
295
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=yes, principal=test1\@ $realm )"
296
+ );
256
297
257
298
# Test that we can transport a reasonable amount of data.
258
299
test_query(
259
300
$node ,
260
- " test1" ,
301
+ ' test1' ,
261
302
' SELECT * FROM generate_series(1, 100000);' ,
262
303
qr / ^1\n .*\n 1024\n .*\n 9999\n .*\n 100000$ / s ,
263
- " gssencmode=require" ,
264
- " receiving 100K lines works" );
304
+ ' gssencmode=require' ,
305
+ ' receiving 100K lines works' );
265
306
266
307
test_query(
267
308
$node ,
268
- " test1" ,
309
+ ' test1' ,
269
310
" CREATE TABLE mytab (f1 int primary key);\n "
270
311
. " COPY mytab FROM STDIN;\n "
271
312
. join (" \n " , (1 .. 100000))
272
313
. " \n\\ .\n "
273
314
. " SELECT COUNT(*) FROM mytab;" ,
274
315
qr / ^100000$ / s ,
275
- " gssencmode=require" ,
276
- " sending 100K lines works" );
316
+ ' gssencmode=require' ,
317
+ ' sending 100K lines works' );
277
318
278
319
unlink ($node -> data_dir . ' /pg_hba.conf' );
279
320
$node -> append_conf(' pg_hba.conf' ,
@@ -282,20 +323,24 @@ sub test_query
282
323
283
324
test_access(
284
325
$node ,
285
- " test1" ,
326
+ ' test1' ,
286
327
' SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
287
328
0,
288
- " gssencmode=prefer" ,
289
- " succeeds with GSS-encrypted access preferred and hostgssenc hba" );
329
+ ' gssencmode=prefer' ,
330
+ ' succeeds with GSS-encrypted access preferred and hostgssenc hba' ,
331
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=yes, principal=test1\@ $realm )"
332
+ );
290
333
test_access(
291
334
$node ,
292
- " test1" ,
335
+ ' test1' ,
293
336
' SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
294
337
0,
295
- " gssencmode=require" ,
296
- " succeeds with GSS-encrypted access required and hostgssenc hba" );
297
- test_access($node , " test1" , ' SELECT true' , 2, " gssencmode=disable" ,
298
- " fails with GSS encryption disabled and hostgssenc hba" );
338
+ ' gssencmode=require' ,
339
+ ' succeeds with GSS-encrypted access required and hostgssenc hba' ,
340
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=yes, principal=test1\@ $realm )"
341
+ );
342
+ test_access($node , ' test1' , ' SELECT true' , 2, ' gssencmode=disable' ,
343
+ ' fails with GSS encryption disabled and hostgssenc hba' , ' ' );
299
344
300
345
unlink ($node -> data_dir . ' /pg_hba.conf' );
301
346
$node -> append_conf(' pg_hba.conf' ,
@@ -304,21 +349,24 @@ sub test_query
304
349
305
350
test_access(
306
351
$node ,
307
- " test1" ,
352
+ ' test1' ,
308
353
' SELECT gss_authenticated and not encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
309
354
0,
310
- " gssencmode=prefer" ,
311
- " succeeds with GSS-encrypted access preferred and hostnogssenc hba, but no encryption"
355
+ ' gssencmode=prefer' ,
356
+ ' succeeds with GSS-encrypted access preferred and hostnogssenc hba, but no encryption' ,
357
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=no, principal=test1\@ $realm )"
312
358
);
313
- test_access($node , " test1" , ' SELECT true' , 2, " gssencmode=require" ,
314
- " fails with GSS-encrypted access required and hostnogssenc hba" );
359
+ test_access($node , ' test1' , ' SELECT true' , 2, ' gssencmode=require' ,
360
+ ' fails with GSS-encrypted access required and hostnogssenc hba' , ' ' );
315
361
test_access(
316
362
$node ,
317
- " test1" ,
363
+ ' test1' ,
318
364
' SELECT gss_authenticated and not encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
319
365
0,
320
- " gssencmode=disable" ,
321
- " succeeds with GSS encryption disabled and hostnogssenc hba" );
366
+ ' gssencmode=disable' ,
367
+ ' succeeds with GSS encryption disabled and hostnogssenc hba' ,
368
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=no, principal=test1\@ $realm )"
369
+ );
322
370
323
371
truncate ($node -> data_dir . ' /pg_ident.conf' , 0);
324
372
unlink ($node -> data_dir . ' /pg_hba.conf' );
@@ -332,4 +380,6 @@ sub test_query
332
380
' SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid();' ,
333
381
0,
334
382
' ' ,
335
- ' succeeds with include_realm=0 and defaults' );
383
+ ' succeeds with include_realm=0 and defaults' ,
384
+ " connection authorized: user=$username database=$dbname application_name=$application GSS (authenticated=yes, encrypted=yes, principal=test1\@ $realm )"
385
+ );
0 commit comments