Skip to content

Commit 8212c83

Browse files
committed
Add TAP tests for LDAP connection parameter lookup
Add TAP tests that tests the LDAP Lookup of Connection Parameters functionality in libpq. Prior to this commit, LDAP test coverage only existed for the server-side authentication functionality and for connection service file with parameters directly specified in the file. The tests included here test a pg_service.conf that contains a link to an LDAP system that contains all of the connection parameters. Author: Andrew Jackson <andrewjackson947@gmail.com> Discussion: https://www.postgresql.org/message-id/CAKK5BkHixcivSCA9pfd_eUp7wkLRhvQ6OtGLAYrWC%3Dk7E76LDQ%40mail.gmail.com
1 parent 296cba2 commit 8212c83

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

src/test/ldap/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tests += {
88
'tests': [
99
't/001_auth.pl',
1010
't/002_bindpasswd.pl',
11+
't/003_ldap_connection_param_lookup.pl',
1112
],
1213
'env': {
1314
'with_ldap': ldap.found() ? 'yes' : 'no',
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
2+
# Copyright (c) 2025, PostgreSQL Global Development Group
3+
4+
use strict;
5+
use warnings FATAL => 'all';
6+
7+
use FindBin;
8+
use lib "$FindBin::RealBin/..";
9+
10+
use File::Copy;
11+
use LdapServer;
12+
use PostgreSQL::Test::Utils;
13+
use PostgreSQL::Test::Cluster;
14+
use Test::More;
15+
16+
if ($ENV{with_ldap} ne 'yes')
17+
{
18+
plan skip_all => 'LDAP not supported by this build';
19+
}
20+
elsif (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bldap\b/)
21+
{
22+
plan skip_all =>
23+
'Potentially unsafe test LDAP not enabled in PG_TEST_EXTRA';
24+
}
25+
elsif (!$LdapServer::setup)
26+
{
27+
plan skip_all => $LdapServer::setup_error;
28+
}
29+
30+
# This tests scenarios related to the service name and the service file,
31+
# for the connection options and their environment variables.
32+
my $dummy_node = PostgreSQL::Test::Cluster->new('dummy_node');
33+
$dummy_node->init;
34+
35+
my $node = PostgreSQL::Test::Cluster->new('node');
36+
$node->init;
37+
$node->start;
38+
39+
note "setting up LDAP server";
40+
41+
my $ldap_rootpw = 'secret';
42+
my $ldap = LdapServer->new($ldap_rootpw, 'anonymous'); # use anonymous auth
43+
$ldap->ldapadd_file('authdata.ldif');
44+
$ldap->ldapsetpw('uid=test1,dc=example,dc=net', 'secret1');
45+
$ldap->ldapsetpw('uid=test2,dc=example,dc=net', 'secret2');
46+
47+
# Windows vs non-Windows: CRLF vs LF for the file's newline, relying on
48+
# the fact that libpq uses fgets() when reading the lines of a service file.
49+
my $newline = $windows_os ? "\r\n" : "\n";
50+
51+
my $td = PostgreSQL::Test::Utils::tempdir;
52+
53+
# create ldap file based on postgres connection info
54+
my $ldif_valid = "$td/connection_params.ldif";
55+
append_to_file($ldif_valid, "version:1");
56+
append_to_file($ldif_valid, $newline);
57+
append_to_file($ldif_valid, "dn:cn=mydatabase,dc=example,dc=net");
58+
append_to_file($ldif_valid, $newline);
59+
append_to_file($ldif_valid, "changetype:add");
60+
append_to_file($ldif_valid, $newline);
61+
append_to_file($ldif_valid, "objectclass:top");
62+
append_to_file($ldif_valid, $newline);
63+
append_to_file($ldif_valid, "objectclass:device");
64+
append_to_file($ldif_valid, $newline);
65+
append_to_file($ldif_valid, "cn:mydatabase");
66+
append_to_file($ldif_valid, $newline);
67+
append_to_file($ldif_valid, "description:host=");
68+
append_to_file($ldif_valid, $node->host);
69+
append_to_file($ldif_valid, $newline);
70+
append_to_file($ldif_valid, "description:port=");
71+
append_to_file($ldif_valid, $node->port);
72+
73+
$ldap->ldapadd_file($ldif_valid);
74+
75+
my ($ldap_server, $ldap_port, $ldaps_port, $ldap_url,
76+
$ldaps_url, $ldap_basedn, $ldap_rootdn
77+
) = $ldap->prop(qw(server port s_port url s_url basedn rootdn));
78+
79+
# don't bother to check the server's cert (though perhaps we should)
80+
$ENV{'LDAPTLS_REQCERT'} = "never";
81+
82+
note "setting up PostgreSQL instance";
83+
84+
# Create the set of service files used in the tests.
85+
86+
# File that includes a valid service name, that uses a decomposed
87+
# connection string for its contents, split on spaces.
88+
my $srvfile_valid = "$td/pg_service_valid.conf";
89+
append_to_file($srvfile_valid, "[my_srv]");
90+
append_to_file($srvfile_valid, $newline);
91+
append_to_file($srvfile_valid, "ldap://localhost:");
92+
append_to_file($srvfile_valid, $ldap_port);
93+
append_to_file($srvfile_valid,
94+
"/dc=example,dc=net?description?one?(cn=mydatabase)");
95+
96+
# File defined with no contents, used as default value for
97+
# PGSERVICEFILE, so that no lookup is attempted in the user's home
98+
# directory.
99+
my $srvfile_empty = "$td/pg_service_empty.conf";
100+
append_to_file($srvfile_empty, '');
101+
102+
# Default service file in PGSYSCONFDIR.
103+
my $srvfile_default = "$td/pg_service.conf";
104+
105+
# Missing service file.
106+
my $srvfile_missing = "$td/pg_service_missing.conf";
107+
108+
# Set the fallback directory lookup of the service file to the
109+
# temporary directory of this test. PGSYSCONFDIR is used if the
110+
# service file defined in PGSERVICEFILE cannot be found, or when a
111+
# service file is found but not the service name.
112+
local $ENV{PGSYSCONFDIR} = $td;
113+
114+
# Force PGSERVICEFILE to a default location, so as this test never
115+
# tries to look at a home directory. This value needs to remain at
116+
# the top of this script before running any tests, and should never be
117+
# changed.
118+
local $ENV{PGSERVICEFILE} = "$srvfile_empty";
119+
120+
# Checks combinations of service name and a valid service file.
121+
{
122+
local $ENV{PGSERVICEFILE} = $srvfile_valid;
123+
124+
$dummy_node->connect_ok(
125+
'service=my_srv',
126+
'connection with correct "service" string and PGSERVICEFILE',
127+
sql => "SELECT 'connect1_1'",
128+
expected_stdout => qr/connect1_1/);
129+
130+
$dummy_node->connect_ok(
131+
'postgres://?service=my_srv',
132+
'connection with correct "service" URI and PGSERVICEFILE',
133+
sql => "SELECT 'connect1_2'",
134+
expected_stdout => qr/connect1_2/);
135+
136+
$dummy_node->connect_fails(
137+
'service=undefined-service',
138+
'connection with incorrect "service" string and PGSERVICEFILE',
139+
expected_stderr =>
140+
qr/definition of service "undefined-service" not found/);
141+
142+
local $ENV{PGSERVICE} = 'my_srv';
143+
144+
$dummy_node->connect_ok(
145+
'',
146+
'connection with correct PGSERVICE and PGSERVICEFILE',
147+
sql => "SELECT 'connect1_3'",
148+
expected_stdout => qr/connect1_3/);
149+
150+
local $ENV{PGSERVICE} = 'undefined-service';
151+
152+
$dummy_node->connect_fails(
153+
'',
154+
'connection with incorrect PGSERVICE and PGSERVICEFILE',
155+
expected_stdout =>
156+
qr/definition of service "undefined-service" not found/);
157+
}
158+
159+
# Checks case of incorrect service file.
160+
{
161+
local $ENV{PGSERVICEFILE} = $srvfile_missing;
162+
163+
$dummy_node->connect_fails(
164+
'service=my_srv',
165+
'connection with correct "service" string and incorrect PGSERVICEFILE',
166+
expected_stderr =>
167+
qr/service file ".*pg_service_missing.conf" not found/);
168+
}
169+
170+
# Checks case of service file named "pg_service.conf" in PGSYSCONFDIR.
171+
{
172+
# Create copy of valid file
173+
my $srvfile_default = "$td/pg_service.conf";
174+
copy($srvfile_valid, $srvfile_default);
175+
176+
$dummy_node->connect_ok(
177+
'service=my_srv',
178+
'connection with correct "service" string and pg_service.conf',
179+
sql => "SELECT 'connect2_1'",
180+
expected_stdout => qr/connect2_1/);
181+
182+
$dummy_node->connect_ok(
183+
'postgres://?service=my_srv',
184+
'connection with correct "service" URI and default pg_service.conf',
185+
sql => "SELECT 'connect2_2'",
186+
expected_stdout => qr/connect2_2/);
187+
188+
$dummy_node->connect_fails(
189+
'service=undefined-service',
190+
'connection with incorrect "service" string and default pg_service.conf',
191+
expected_stderr =>
192+
qr/definition of service "undefined-service" not found/);
193+
194+
local $ENV{PGSERVICE} = 'my_srv';
195+
196+
$dummy_node->connect_ok(
197+
'',
198+
'connection with correct PGSERVICE and default pg_service.conf',
199+
sql => "SELECT 'connect2_3'",
200+
expected_stdout => qr/connect2_3/);
201+
202+
local $ENV{PGSERVICE} = 'undefined-service';
203+
204+
$dummy_node->connect_fails(
205+
'',
206+
'connection with incorrect PGSERVICE and default pg_service.conf',
207+
expected_stdout =>
208+
qr/definition of service "undefined-service" not found/);
209+
210+
# Remove default pg_service.conf.
211+
unlink($srvfile_default);
212+
}
213+
214+
$node->teardown_node;
215+
216+
done_testing();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy