Debian Codegraph @

src/test/authentication/t/006_login_trigger.pl

raw
1 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
2
3 # Tests of authentication via login trigger. Mostly for rejection via
4 # exception, because this scenario cannot be covered with *.sql/*.out regress
5 # tests.
6
7 use strict;
8 use warnings FATAL => 'all';
9
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 # Execute a psql command and compare its output towards given regexps
20 sub psql_command
21 {
22 local $Test::Builder::Level = $Test::Builder::Level + 1;
23 my ($node, $sql, $expected_ret, $test_name, %params) = @_;
24
25 my $connstr;
26 if (defined($params{connstr}))
27 {
28 $connstr = $params{connstr};
29 }
30 else
31 {
32 $connstr = '';
33 }
34
35 # Execute command
36 my ($ret, $stdout, $stderr) =
37 $node->psql('postgres', $sql, connstr => "$connstr");
38
39 # Check return code
40 is($ret, $expected_ret, "$test_name: exit code $expected_ret");
41
42 # Check stdout
43 if (defined($params{log_like}))
44 {
45 my @log_like = @{ $params{log_like} };
46 while (my $regex = shift @log_like)
47 {
48 like($stdout, $regex, "$test_name: log matches");
49 }
50 }
51 if (defined($params{log_unlike}))
52 {
53 my @log_unlike = @{ $params{log_unlike} };
54 while (my $regex = shift @log_unlike)
55 {
56 unlike($stdout, $regex, "$test_name: log unmatches");
57 }
58 }
59 if (defined($params{log_exact}))
60 {
61 is($stdout, $params{log_exact}, "$test_name: log equals");
62 }
63
64 # Check stderr
65 if (defined($params{err_like}))
66 {
67 my @err_like = @{ $params{err_like} };
68 while (my $regex = shift @err_like)
69 {
70 like($stderr, $regex, "$test_name: err matches");
71 }
72 }
73 if (defined($params{err_unlike}))
74 {
75 my @err_unlike = @{ $params{err_unlike} };
76 while (my $regex = shift @err_unlike)
77 {
78 unlike($stderr, $regex, "$test_name: err unmatches");
79 }
80 }
81 if (defined($params{err_exact}))
82 {
83 is($stderr, $params{err_exact}, "$test_name: err equals");
84 }
85
86 return;
87 }
88
89 # New node
90 my $node = PostgreSQL::Test::Cluster->new('main');
91 $node->init(extra => [ '--locale=C', '--encoding=UTF8' ]);
92 $node->append_conf(
93 'postgresql.conf', q{
94 wal_level = 'logical'
95 max_replication_slots = 4
96 max_wal_senders = 4
97 });
98 $node->start;
99
100 # Create temporary roles and log table
101 psql_command(
102 $node, 'CREATE ROLE regress_alice WITH LOGIN;
103 CREATE ROLE regress_mallory WITH LOGIN;
104 CREATE TABLE user_logins(id serial, who text);
105 GRANT SELECT ON user_logins TO public;
106 ', 0, 'create tmp objects',
107 log_exact => '',
108 err_exact => ''),
109 ;
110
111 # Create login event function and trigger
112 psql_command(
113 $node,
114 'CREATE FUNCTION on_login_proc() RETURNS event_trigger AS $$
115 BEGIN
116 INSERT INTO user_logins (who) VALUES (SESSION_USER);
117 IF SESSION_USER = \'regress_mallory\' THEN
118 RAISE EXCEPTION \'Hello %! You are NOT welcome here!\', SESSION_USER;
119 END IF;
120 RAISE NOTICE \'Hello %! You are welcome!\', SESSION_USER;
121 END;
122 $$ LANGUAGE plpgsql SECURITY DEFINER;
123 ', 0, 'create trigger function',
124 log_exact => '',
125 err_exact => '');
126
127 psql_command(
128 $node,
129 'CREATE EVENT TRIGGER on_login_trigger '
130 . 'ON login EXECUTE PROCEDURE on_login_proc();', 0,
131 'create event trigger',
132 log_exact => '',
133 err_exact => '');
134 psql_command(
135 $node, 'ALTER EVENT TRIGGER on_login_trigger ENABLE ALWAYS;', 0,
136 'alter event trigger',
137 log_exact => '',
138 err_like => [qr/You are welcome/]);
139
140 # Check the two requests were logged via login trigger
141 psql_command(
142 $node, 'SELECT COUNT(*) FROM user_logins;', 0, 'select count',
143 log_exact => '2',
144 err_like => [qr/You are welcome/]);
145
146 # Try to login as allowed Alice. We don't check the Mallory login, because
147 # FATAL error could cause a timing-dependant panic of IPC::Run.
148 psql_command(
149 $node, 'SELECT 1;', 0, 'try regress_alice',
150 connstr => 'user=regress_alice',
151 log_exact => '1',
152 err_like => [qr/You are welcome/],
153 err_unlike => [qr/You are NOT welcome/]);
154
155 # Check that Alice's login record is here
156 psql_command(
157 $node, 'SELECT * FROM user_logins;', 0, 'select *',
158 log_like => [qr/3\|regress_alice/],
159 log_unlike => [qr/regress_mallory/],
160 err_like => [qr/You are welcome/]);
161
162 # Check total number of successful logins so far
163 psql_command(
164 $node, 'SELECT COUNT(*) FROM user_logins;', 0, 'select count',
165 log_exact => '5',
166 err_like => [qr/You are welcome/]);
167
168 # Cleanup the temporary stuff
169 psql_command(
170 $node, 'DROP EVENT TRIGGER on_login_trigger;', 0,
171 'drop event trigger',
172 log_exact => '',
173 err_like => [qr/You are welcome/]);
174 psql_command(
175 $node, 'DROP TABLE user_logins;
176 DROP FUNCTION on_login_proc;
177 DROP ROLE regress_mallory;
178 DROP ROLE regress_alice;
179 ', 0, 'cleanup',
180 log_exact => '',
181 err_exact => '');
182
183 done_testing();