Debian Codegraph @

src/test/modules/oauth_validator/t/002_client.pl

raw
1 #
2 # Exercises the API for custom OAuth client flows, using the oauth_hook_client
3 # test driver.
4 #
5 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
6 #
7
8 use strict;
9 use warnings FATAL => 'all';
10
11 use JSON::PP qw(encode_json);
12 use MIME::Base64 qw(encode_base64);
13 use PostgreSQL::Test::Cluster;
14 use PostgreSQL::Test::Utils;
15 use Test::More;
16
17 if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\boauth\b/)
18 {
19 plan skip_all =>
20 'Potentially unsafe test oauth not enabled in PG_TEST_EXTRA';
21 }
22
23 #
24 # Cluster Setup
25 #
26
27 my $node = PostgreSQL::Test::Cluster->new('primary');
28 $node->init;
29 $node->append_conf('postgresql.conf', "log_connections = all\n");
30 $node->append_conf('postgresql.conf',
31 "oauth_validator_libraries = 'validator'\n");
32 # Needed to inspect postmaster log after connection failure:
33 $node->append_conf('postgresql.conf', "log_min_messages = debug2");
34 $node->start;
35
36 $node->safe_psql('postgres', 'CREATE USER test;');
37
38 # These tests don't use the builtin flow, and we don't have an authorization
39 # server running, so the address used here shouldn't matter. Use an invalid IP
40 # address, so if there's some cascade of errors that causes the client to
41 # attempt a connection, we'll fail noisily.
42 my $issuer = "https://256.256.256.256";
43 my $scope = "openid postgres";
44
45 unlink($node->data_dir . '/pg_hba.conf');
46 $node->append_conf(
47 'pg_hba.conf', qq{
48 local all test oauth issuer="$issuer" scope="$scope"
49 });
50 $node->reload;
51
52 $node->wait_for_log(qr/reloading configuration files/);
53
54 $ENV{PGOAUTHDEBUG} = "UNSAFE";
55
56 #
57 # Tests
58 #
59
60 my $user = "test";
61 my $base_connstr = $node->connstr() . " user=$user";
62 my $common_connstr =
63 "$base_connstr oauth_issuer=$issuer oauth_client_id=myID";
64
65 sub test
66 {
67 my ($test_name, %params) = @_;
68
69 my $flags = [];
70 if (defined($params{flags}))
71 {
72 $flags = $params{flags};
73 }
74
75 my @cmd = ("oauth_hook_client", @{$flags}, $common_connstr);
76 note "running '" . join("' '", @cmd) . "'";
77
78 my $log_start = -s $node->logfile;
79 my ($stdout, $stderr) = run_command(\@cmd);
80
81 if (defined($params{expected_stdout}))
82 {
83 like($stdout, $params{expected_stdout}, "$test_name: stdout matches");
84 }
85
86 if (defined($params{expected_stderr}))
87 {
88 like($stderr, $params{expected_stderr}, "$test_name: stderr matches");
89 }
90 else
91 {
92 is($stderr, "", "$test_name: no stderr");
93 }
94
95 if (defined($params{log_like}))
96 {
97 # See Cluster::connect_fails(). To avoid races, we have to wait for the
98 # postmaster to flush the log for the finished connection.
99 $node->wait_for_log(
100 qr/DEBUG: (?:00000: )?forked new client backend, pid=(\d+) socket.*DEBUG: (?:00000: )?client backend \(PID \1\) exited with exit code \d/s,
101 $log_start);
102
103 $node->log_check("$test_name: log matches",
104 $log_start, log_like => $params{log_like});
105 }
106 }
107
108 test(
109 "basic synchronous hook can provide a token",
110 flags => [
111 "--token", "my-token",
112 "--expected-uri", "$issuer/.well-known/openid-configuration",
113 "--expected-scope", $scope,
114 ],
115 expected_stdout => qr/connection succeeded/,
116 log_like => [qr/oauth_validator: token="my-token", role="$user"/]);
117
118 if ($ENV{with_libcurl} ne 'yes')
119 {
120 # libpq should help users out if no OAuth support is built in.
121 test(
122 "fails without custom hook installed",
123 flags => ["--no-hook"],
124 expected_stderr =>
125 qr/no OAuth flows are available \(try installing the libpq-oauth package\)/
126 );
127 }
128
129 # connect_timeout should work if the flow doesn't respond.
130 $common_connstr = "$common_connstr connect_timeout=1";
131 test(
132 "connect_timeout interrupts hung client flow",
133 flags => ["--hang-forever"],
134 expected_stderr => qr/failed: timeout expired/);
135
136 # Remove the timeout for later tests.
137 $common_connstr = "$base_connstr oauth_issuer=$issuer oauth_client_id=myID";
138
139 # Test various misbehaviors of the client hook.
140 my @cases = (
141 {
142 flag => "--misbehave=no-hook",
143 expected_error =>
144 qr/user-defined OAuth flow provided neither a token nor an async callback/,
145 },
146 {
147 flag => "--misbehave=fail-async",
148 expected_error => qr/user-defined OAuth flow failed/,
149 },
150 {
151 flag => "--misbehave=no-token",
152 expected_error => qr/user-defined OAuth flow did not provide a token/,
153 },
154 {
155 flag => "--misbehave=no-socket",
156 expected_error =>
157 qr/user-defined OAuth flow did not provide a socket for polling/,
158 });
159
160 foreach my $c (@cases)
161 {
162 test(
163 "hook misbehavior: $c->{'flag'}",
164 flags => [ $c->{'flag'} ],
165 expected_stderr => $c->{'expected_error'});
166 }
167
168 done_testing();