Skip to content

Commit 081bfc1

Browse files
committed
Check error messages in SSL tests
In tests that check whether a connection fails, also check the error message. That makes sure that the connection was rejected for the right reason. This discovered that two tests had their connection failing for the wrong reason. One test failed because pg_hba.conf was not set up to allow that user, one test failed because the client key file did not have the right permissions. Fix those tests and add a new one that is really supposed to check the file permission issue. Reviewed-by: Michael Paquier <[email protected]>
1 parent bc1adc6 commit 081bfc1

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

src/test/ssl/ServerSetup.pm

+16-26
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,35 @@ use Test::More;
2727
use Exporter 'import';
2828
our @EXPORT = qw(
2929
configure_test_server_for_ssl
30-
run_test_psql
3130
switch_server_cert
3231
test_connect_fails
3332
test_connect_ok
3433
);
3534

3635
# Define a couple of helper functions to test connecting to the server.
3736

38-
# Attempt connection to server with given connection string.
39-
sub run_test_psql
40-
{
41-
my $connstr = $_[0];
42-
43-
my $cmd = [
44-
'psql', '-X', '-A', '-t', '-c', "SELECT \$\$connected with $connstr\$\$",
45-
'-d', "$connstr" ];
46-
47-
my $result = run_log($cmd);
48-
return $result;
49-
}
50-
5137
# The first argument is a base connection string to use for connection.
5238
# The second argument is a complementary connection string.
5339
sub test_connect_ok
5440
{
55-
my $common_connstr = $_[0];
56-
my $connstr = $_[1];
57-
my $test_name = $_[2];
41+
my ($common_connstr, $connstr, $test_name) = @_;
5842

59-
ok(run_test_psql("$common_connstr $connstr"), $test_name);
43+
my $cmd = [
44+
'psql', '-X', '-A', '-t', '-c', "SELECT \$\$connected with $connstr\$\$",
45+
'-d', "$common_connstr $connstr" ];
46+
47+
command_ok($cmd, $test_name);
6048
}
6149

6250
sub test_connect_fails
6351
{
64-
my $common_connstr = $_[0];
65-
my $connstr = $_[1];
66-
my $test_name = $_[2];
52+
my ($common_connstr, $connstr, $expected_stderr, $test_name) = @_;
53+
54+
my $cmd = [
55+
'psql', '-X', '-A', '-t', '-c', "SELECT \$\$connected with $connstr\$\$",
56+
'-d', "$common_connstr $connstr" ];
6757

68-
ok(!run_test_psql("$common_connstr $connstr"), $test_name);
58+
command_fails_like($cmd, $expected_stderr, $test_name);
6959
}
7060

7161
# Copy a set of files, taking into account wildcards
@@ -169,12 +159,12 @@ sub configure_hba_for_ssl
169159
print $hba
170160
"# TYPE DATABASE USER ADDRESS METHOD\n";
171161
print $hba
172-
"hostssl trustdb ssltestuser $serverhost/32 $authmethod\n";
162+
"hostssl trustdb all $serverhost/32 $authmethod\n";
173163
print $hba
174-
"hostssl trustdb ssltestuser ::1/128 $authmethod\n";
164+
"hostssl trustdb all ::1/128 $authmethod\n";
175165
print $hba
176-
"hostssl certdb ssltestuser $serverhost/32 cert\n";
166+
"hostssl certdb all $serverhost/32 cert\n";
177167
print $hba
178-
"hostssl certdb ssltestuser ::1/128 cert\n";
168+
"hostssl certdb all ::1/128 cert\n";
179169
close $hba;
180170
}

src/test/ssl/ssl/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/*.old
22
/new_certs_dir/
3-
/client_tmp.key
3+
/client*_tmp.key

src/test/ssl/t/001_ssltests.pl

+39-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use warnings;
33
use PostgresNode;
44
use TestLib;
5-
use Test::More tests => 40;
5+
use Test::More tests => 62;
66
use ServerSetup;
77
use File::Copy;
88

@@ -20,6 +20,14 @@
2020
# of the key stored in the code tree and update its permissions.
2121
copy("ssl/client.key", "ssl/client_tmp.key");
2222
chmod 0600, "ssl/client_tmp.key";
23+
copy("ssl/client-revoked.key", "ssl/client-revoked_tmp.key");
24+
chmod 0600, "ssl/client-revoked_tmp.key";
25+
26+
# Also make a copy of that explicitly world-readable. We can't
27+
# necessarily rely on the file in the source tree having those
28+
# permissions.
29+
copy("ssl/client.key", "ssl/client_wrongperms_tmp.key");
30+
chmod 0644, "ssl/client_wrongperms_tmp.key";
2331

2432
#### Part 0. Set up the server.
2533

@@ -48,33 +56,40 @@
4856

4957
# The server should not accept non-SSL connections.
5058
test_connect_fails($common_connstr, "sslmode=disable",
59+
qr/\Qno pg_hba.conf entry\E/,
5160
"server doesn't accept non-SSL connections");
5261

5362
# Try without a root cert. In sslmode=require, this should work. In verify-ca
5463
# or verify-full mode it should fail.
5564
test_connect_ok($common_connstr, "sslrootcert=invalid sslmode=require",
5665
"connect without server root cert sslmode=require");
5766
test_connect_fails($common_connstr, "sslrootcert=invalid sslmode=verify-ca",
67+
qr/root certificate file "invalid" does not exist/,
5868
"connect without server root cert sslmode=verify-ca");
5969
test_connect_fails($common_connstr, "sslrootcert=invalid sslmode=verify-full",
70+
qr/root certificate file "invalid" does not exist/,
6071
"connect without server root cert sslmode=verify-full");
6172

6273
# Try with wrong root cert, should fail. (We're using the client CA as the
6374
# root, but the server's key is signed by the server CA.)
6475
test_connect_fails($common_connstr,
6576
"sslrootcert=ssl/client_ca.crt sslmode=require",
77+
qr/SSL error/,
6678
"connect with wrong server root cert sslmode=require");
6779
test_connect_fails($common_connstr,
6880
"sslrootcert=ssl/client_ca.crt sslmode=verify-ca",
81+
qr/SSL error/,
6982
"connect with wrong server root cert sslmode=verify-ca");
7083
test_connect_fails($common_connstr,
7184
"sslrootcert=ssl/client_ca.crt sslmode=verify-full",
85+
qr/SSL error/,
7286
"connect with wrong server root cert sslmode=verify-full");
7387

7488
# Try with just the server CA's cert. This fails because the root file
7589
# must contain the whole chain up to the root CA.
7690
test_connect_fails($common_connstr,
7791
"sslrootcert=ssl/server_ca.crt sslmode=verify-ca",
92+
qr/SSL error/,
7893
"connect with server CA cert, without root CA");
7994

8095
# And finally, with the correct root cert.
@@ -107,6 +122,7 @@
107122
# A CRL belonging to a different CA is not accepted, fails
108123
test_connect_fails($common_connstr,
109124
"sslrootcert=ssl/root+server_ca.crt sslmode=verify-ca sslcrl=ssl/client.crl",
125+
qr/SSL error/,
110126
"CRL belonging to a different CA");
111127

112128
# With the correct CRL, succeeds (this cert is not revoked)
@@ -124,9 +140,9 @@
124140
test_connect_ok($common_connstr, "sslmode=verify-ca host=wronghost.test",
125141
"mismatch between host name and server certificate sslmode=verify-ca");
126142
test_connect_fails($common_connstr, "sslmode=verify-full host=wronghost.test",
143+
qr/\Qserver certificate for "common-name.pg-ssltest.test" does not match host name "wronghost.test"\E/,
127144
"mismatch between host name and server certificate sslmode=verify-full");
128145

129-
130146
# Test Subject Alternative Names.
131147
switch_server_cert($node, 'server-multiple-alt-names');
132148

@@ -141,9 +157,11 @@
141157
"host name matching with X.509 Subject Alternative Names wildcard");
142158

143159
test_connect_fails($common_connstr, "host=wronghost.alt-name.pg-ssltest.test",
160+
qr/\Qserver certificate for "dns1.alt-name.pg-ssltest.test" (and 2 other names) does not match host name "wronghost.alt-name.pg-ssltest.test"\E/,
144161
"host name not matching with X.509 Subject Alternative Names");
145162
test_connect_fails($common_connstr,
146163
"host=deep.subdomain.wildcard.pg-ssltest.test",
164+
qr/\Qserver certificate for "dns1.alt-name.pg-ssltest.test" (and 2 other names) does not match host name "deep.subdomain.wildcard.pg-ssltest.test"\E/,
147165
"host name not matching with X.509 Subject Alternative Names wildcard");
148166

149167
# Test certificate with a single Subject Alternative Name. (this gives a
@@ -157,9 +175,11 @@
157175
"host name matching with a single X.509 Subject Alternative Name");
158176

159177
test_connect_fails($common_connstr, "host=wronghost.alt-name.pg-ssltest.test",
178+
qr/\Qserver certificate for "single.alt-name.pg-ssltest.test" does not match host name "wronghost.alt-name.pg-ssltest.test"\E/,
160179
"host name not matching with a single X.509 Subject Alternative Name");
161180
test_connect_fails($common_connstr,
162181
"host=deep.subdomain.wildcard.pg-ssltest.test",
182+
qr/\Qserver certificate for "single.alt-name.pg-ssltest.test" does not match host name "deep.subdomain.wildcard.pg-ssltest.test"\E/,
163183
"host name not matching with a single X.509 Subject Alternative Name wildcard");
164184

165185
# Test server certificate with a CN and SANs. Per RFCs 2818 and 6125, the CN
@@ -174,6 +194,7 @@
174194
test_connect_ok($common_connstr, "host=dns2.alt-name.pg-ssltest.test",
175195
"certificate with both a CN and SANs 2");
176196
test_connect_fails($common_connstr, "host=common-name.pg-ssltest.test",
197+
qr/\Qserver certificate for "dns1.alt-name.pg-ssltest.test" (and 1 other name) does not match host name "common-name.pg-ssltest.test"\E/,
177198
"certificate with both a CN and SANs ignores CN");
178199

179200
# Finally, test a server certificate that has no CN or SANs. Of course, that's
@@ -187,6 +208,7 @@
187208
"server certificate without CN or SANs sslmode=verify-ca");
188209
test_connect_fails($common_connstr,
189210
"sslmode=verify-full host=common-name.pg-ssltest.test",
211+
qr/could not get server's host name from server certificate/,
190212
"server certificate without CN or SANs sslmode=verify-full");
191213

192214
# Test that the CRL works
@@ -201,6 +223,7 @@
201223
"connects without client-side CRL");
202224
test_connect_fails($common_connstr,
203225
"sslrootcert=ssl/root+server_ca.crt sslmode=verify-ca sslcrl=ssl/root+server.crl",
226+
qr/SSL error/,
204227
"does not connect with client-side CRL");
205228

206229
### Part 2. Server-side tests.
@@ -215,21 +238,30 @@
215238
# no client cert
216239
test_connect_fails($common_connstr,
217240
"user=ssltestuser sslcert=invalid",
241+
qr/connection requires a valid client certificate/,
218242
"certificate authorization fails without client cert");
219243

220244
# correct client cert
221245
test_connect_ok($common_connstr,
222246
"user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key",
223247
"certificate authorization succeeds with correct client cert");
224248

249+
# client key with wrong permissions
250+
test_connect_fails($common_connstr,
251+
"user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client_wrongperms_tmp.key",
252+
qr!\Qprivate key file "ssl/client_wrongperms_tmp.key" has group or world access\E!,
253+
"certificate authorization fails because of file permissions");
254+
225255
# client cert belonging to another user
226256
test_connect_fails($common_connstr,
227257
"user=anotheruser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key",
258+
qr/certificate authentication failed for user "anotheruser"/,
228259
"certificate authorization fails with client cert belonging to another user");
229260

230261
# revoked client cert
231262
test_connect_fails($common_connstr,
232-
"user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=ssl/client-revoked.key",
263+
"user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=ssl/client-revoked_tmp.key",
264+
qr/SSL error/,
233265
"certificate authorization fails with revoked client cert");
234266

235267
# intermediate client_ca.crt is provided by client, and isn't in server's ssl_ca_file
@@ -241,7 +273,10 @@
241273
"sslmode=require sslcert=ssl/client+client_ca.crt",
242274
"intermediate client certificate is provided by client");
243275
test_connect_fails($common_connstr, "sslmode=require sslcert=ssl/client.crt",
276+
qr/SSL error/,
244277
"intermediate client certificate is missing");
245278

246279
# clean up
247-
unlink "ssl/client_tmp.key";
280+
unlink("ssl/client_tmp.key",
281+
"ssl/client_wrongperms_tmp.key",
282+
"ssl/client-revoked_tmp.key");

src/test/ssl/t/002_scram.pl

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use warnings;
55
use PostgresNode;
66
use TestLib;
7-
use Test::More tests => 5;
7+
use Test::More tests => 6;
88
use ServerSetup;
99
use File::Copy;
1010

@@ -59,8 +59,10 @@
5959
{
6060
test_connect_fails($common_connstr,
6161
"scram_channel_binding=tls-server-end-point",
62+
qr/unsupported SCRAM channel-binding type/,
6263
"SCRAM authentication with tls-server-end-point as channel binding");
6364
}
6465
test_connect_fails($common_connstr,
6566
"scram_channel_binding=not-exists",
67+
qr/unsupported SCRAM channel-binding type/,
6668
"SCRAM authentication with invalid channel binding");

0 commit comments

Comments
 (0)