Skip to content

Commit 3114d89

Browse files
author
CommanderKeynes
committed
Add tests for LDAP
1 parent 170343b commit 3114d89

File tree

5 files changed

+218
-7
lines changed

5 files changed

+218
-7
lines changed

.circleci/glauth.cfg

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#################
2+
# glauth.conf
3+
4+
#################
5+
# General configuration.
6+
debug = true
7+
8+
[ldap]
9+
enabled = true
10+
# run on a non privileged port
11+
listen = "0.0.0.0:3893"
12+
tls = false
13+
14+
[ldaps]
15+
enabled = false
16+
#################
17+
# Tracing section controls the tracer configuration
18+
[tracing]
19+
# if enabled is set to false, a no-op tracer will be used
20+
enabled = true
21+
# if both grpcEndpoint and httpEndpoint are unset, the default stdout provider will be used
22+
# TODO add allowGRPCInsecure: right now grpc otlp is using the WithInsecure flag so traffic
23+
# will always go without verifying server certificates
24+
# grpcEndpoint = "otlp.monitoring.io:4317"
25+
# httpEndpoint = "http://otlp.monitoring.io:4318"
26+
#################
27+
# The backend section controls the data store.
28+
[backend]
29+
datastore = "config"
30+
baseDN = "dc=example,dc=com"
31+
nameformat = "cn"
32+
groupformat = "ou"
33+
34+
# If you are using a client that requires reading the root DSE first
35+
# such as SSSD
36+
# anonymousdse = true
37+
38+
## Configure dn format to use structures like
39+
## "uid=serviceuser,cn=svcaccts,$BASEDN" instead of "cn=serviceuser,ou=svcaccts,$BASEDN"
40+
## to help ease migrations from other LDAP systems
41+
# nameformat = "uid"
42+
# groupformat = "cn"
43+
44+
## Configure ssh-key attribute name, default is 'sshPublicKey'
45+
# sshkeyattr = "ipaSshPubKey"
46+
47+
[behaviors]
48+
# Ignore all capabilities restrictions, for instance allowing every user to perform a search
49+
IgnoreCapabilities = false
50+
# Enable a "fail2ban" type backoff mechanism temporarily banning repeated failed login attempts
51+
LimitFailedBinds = true
52+
# How many failed login attempts are allowed before a ban is imposed
53+
NumberOfFailedBinds = 3
54+
# How long (in seconds) is the window for failed login attempts
55+
PeriodOfFailedBinds = 10
56+
# How long (in seconds) is the ban duration
57+
BlockFailedBindsFor = 60
58+
# Clean learnt IP addresses every N seconds
59+
PruneSourceTableEvery = 600
60+
# Clean learnt IP addresses not seen in N seconds
61+
PruneSourcesOlderThan = 600
62+
63+
#################
64+
# The users section contains a hardcoded list of valid users.
65+
# to create a passSHA256: echo -n "mysecret" | openssl dgst -sha256
66+
[[users]]
67+
name = "admin_user"
68+
uidnumber = 5001
69+
primarygroup = 5501
70+
71+
passsha256 = "6478579e37aff45f013e14eeb30b3cc56c72ccdc310123bcdf53e0333e3f416a" # dogood
72+
[[users.customattributes]]
73+
employeetype = ["Intern", "Temp"]
74+
employeenumber = [12345, 54321]
75+
[[users.capabilities]]
76+
action = "search"
77+
object = "dc=example,dc=com"
78+
79+
# This user record shows all of the possible fields available
80+
[[users]]
81+
name = "sharding_user"
82+
givenname="John"
83+
sn="Doe"
84+
85+
uidnumber = 5002
86+
primarygroup = 5501
87+
loginShell = "/bin/sh"
88+
homeDir = "/root"
89+
passsha256 = "6478579e37aff45f013e14eeb30b3cc56c72ccdc310123bcdf53e0333e3f416a" # dogood
90+
sshkeys = ["ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEA3UKCEllO2IZXgqNygiVb+dDLJJwVw3AJwV34t2jzR+/tUNVeJ9XddKpYQektNHsFmY93lJw5QDSbeH/mAC4KPoUM47EriINKEelRbyG4hC/ko/e2JWqEclPS9LP7GtqGmscXXo4JFkqnKw4TIRD52XI9n1syYM9Y8rJ88fjC/Lpn+01AB0paLVIfppJU35t0Ho9doHAEfEvcQA6tcm7FLJUvklAxc8WUbdziczbRV40KzDroIkXAZRjX7vXXhh/p7XBYnA0GO8oTa2VY4dTQSeDAUJSUxbzevbL0ll9Gi1uYaTDQyE5gbn2NfJSqq0OYA+3eyGtIVjFYZgi+txSuhw== rsa-key-20160209"]
91+
passappsha256 = [
92+
"c32255dbf6fd6b64883ec8801f793bccfa2a860f2b1ae1315cd95cdac1338efa", # TestAppPw1
93+
"c9853d5f2599e90497e9f8cc671bd2022b0fb5d1bd7cfff92f079e8f8f02b8d3", # TestAppPw2
94+
"4939efa7c87095dacb5e7e8b8cfb3a660fa1f5edcc9108f6d7ec20ea4d6b3a88", # TestAppPw3
95+
]
96+
97+
#################
98+
# The groups section contains a hardcoded list of valid users.
99+
[[groups]]
100+
name = "superheros"
101+
gidnumber = 5501
102+
103+
[[groups]]
104+
name = "svcaccts"
105+
gidnumber = 5502
106+
107+
[[groups]]
108+
name = "vpn"
109+
gidnumber = 5503
110+
includegroups = [ 5501 ]
111+

.circleci/pgcat_ldap.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
[general]
3+
host = "0.0.0.0"
4+
port = 6432
5+
admin_username = "admin_user"
6+
admin_password = ""
7+
admin_auth_type = "ldap"
8+
admin_auth_ldapurl = "ldap://127.0.0.1:3893"
9+
admin_auth_ldapsuffix = "@example.com"
10+
11+
[pools.sharded_db.users.0]
12+
username = "sharding_user"
13+
password = "sharding_user"
14+
auth_type = "ldap"
15+
auth_ldapurl = "ldap://127.0.0.1:3893"
16+
auth_ldapsuffix = "@example.com"
17+
pool_size = 10
18+
min_pool_size = 1
19+
pool_mode = "transaction"
20+
21+
[pools.sharded_db.shards.0]
22+
servers = [
23+
[ "127.0.0.1", 5432, "primary" ],
24+
]
25+
database = "shard0"

.circleci/pgcat_trust.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
[general]
3-
43
host = "0.0.0.0"
54
port = 6432
65
admin_username = "admin_user"

tests/docker/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ RUN apt-get update && apt-get install llvm-11 psmisc postgresql-contrib postgres
66
RUN cargo install cargo-binutils rustfilt
77
RUN rustup component add llvm-tools-preview
88
RUN sudo gem install bundler
9+
RUN wget -O /usr/bin/glauth https://github.com/glauth/glauth/releases/download/v2.3.2/glauth-linux-amd64
10+
RUN chmod +x /usr/bin/glauth
911
RUN wget -O toxiproxy-2.4.0.deb https://github.com/Shopify/toxiproxy/releases/download/v2.4.0/toxiproxy_2.4.0_linux_$(dpkg --print-architecture).deb && \
1012
sudo dpkg -i toxiproxy-2.4.0.deb
1113
RUN wget -O go1.21.3.linux-$(dpkg --print-architecture).tar.gz https://go.dev/dl/go1.21.3.linux-$(dpkg --print-architecture).tar.gz && \

tests/python/tests.py

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,43 @@
1111
PGCAT_PORT = "6432"
1212

1313

14-
def pgcat_start():
15-
pg_cat_send_signal(signal.SIGTERM)
16-
os.system("./target/debug/pgcat .circleci/pgcat.toml &")
14+
def ldap_start():
15+
glauth_send_signal(signal.SIGTERM)
16+
os.system("glauth -c .circleci/glauth.cfg &")
1717
time.sleep(2)
1818

19-
def pgcat_trust_start():
19+
20+
def pgcat_start_inner(config_path: str):
2021
pg_cat_send_signal(signal.SIGTERM)
21-
os.system("./target/debug/pgcat .circleci/pgcat_trust.toml &")
22+
os.system(f"./target/debug/pgcat {config_path} &")
2223
time.sleep(2)
2324

25+
def pgcat_start():
26+
pgcat_start_inner(config_path='.circleci/pgcat.toml')
27+
28+
29+
def pgcat_trust_start():
30+
pgcat_start_inner(config_path='.circleci/pgcat_trust.toml')
31+
32+
33+
def pgcat_ldap_start():
34+
pgcat_start_inner(config_path='.circleci/pgcat_ldap.toml')
35+
36+
def glauth_send_signal(signal: signal.Signals):
37+
try:
38+
for proc in psutil.process_iter(["pid", "name"]):
39+
if "glauth" == proc.name():
40+
os.kill(proc.pid, signal)
41+
except Exception as e:
42+
# The process can be gone when we send this signal
43+
print(e)
44+
45+
if signal == signal.SIGTERM:
46+
# Returns 0 if pgcat process exists
47+
time.sleep(2)
48+
if not os.system('pgrep glauth'):
49+
raise Exception("glauth not closed after SIGTERM")
50+
2451

2552
def pg_cat_send_signal(signal: signal.Signals):
2653
try:
@@ -51,14 +78,36 @@ def connect_db_trust(
5178
db = "sharded_db"
5279

5380
conn = psycopg2.connect(
54-
f"postgres://{user}:@{PGCAT_HOST}:{PGCAT_PORT}/{db}?application_name=testing_pgcat",
81+
f"postgres://{user}@{PGCAT_HOST}:{PGCAT_PORT}/{db}?application_name=testing_pgcat",
5582
connect_timeout=2,
5683
)
5784
conn.autocommit = autocommit
5885
cur = conn.cursor()
5986

6087
return (conn, cur)
6188

89+
def connect_db_ldap(
90+
autocommit: bool = True,
91+
admin: bool = False,
92+
) -> Tuple[psycopg2.extensions.connection, psycopg2.extensions.cursor]:
93+
94+
if admin:
95+
user = "admin_user"
96+
password = "dogood"
97+
db = "pgcat"
98+
else:
99+
user = "sharding_user"
100+
password = "dogood"
101+
db = "sharded_db"
102+
103+
conn = psycopg2.connect(
104+
f"postgres://{user}:{password}@{PGCAT_HOST}:{PGCAT_PORT}/{db}?application_name=testing_pgcat",
105+
connect_timeout=2,
106+
)
107+
conn.autocommit = autocommit
108+
cur = conn.cursor()
109+
110+
return (conn, cur)
62111

63112
def connect_db(
64113
autocommit: bool = True,
@@ -104,6 +153,30 @@ def test_normal_trust_auth():
104153
print(res)
105154
cleanup_conn(conn, cur)
106155

156+
def test_normal_ldap_auth():
157+
conn, cur = connect_db_ldap(autocommit=False)
158+
cur.execute("SELECT 1")
159+
res = cur.fetchall()
160+
print(res)
161+
cleanup_conn(conn, cur)
162+
163+
def test_admin_ldap_auth():
164+
conn, cur = connect_db_ldap(admin=True)
165+
cur.execute("SHOW POOLS")
166+
res = cur.fetchall()
167+
print(res)
168+
cleanup_conn(conn, cur)
169+
170+
def test_ldap():
171+
ldap_start()
172+
pgcat_ldap_start()
173+
174+
test_admin_ldap_auth()
175+
test_normal_ldap_auth()
176+
177+
glauth_send_signal(signal.SIGTERM)
178+
pg_cat_send_signal(signal.SIGTERM)
179+
107180

108181
def test_trust():
109182
pgcat_trust_start()
@@ -370,3 +443,4 @@ def test_shutdown_logic():
370443
test_shutdown_logic()
371444

372445
test_trust()
446+
test_ldap()

0 commit comments

Comments
 (0)