Skip to content

Commit 7111ff2

Browse files
committed
Add tests for query_auth reload functionality
This change adds tests for query_auth configuration reload and also does some refactoring. - Now postgres containers in `docker-compose` are set to use md5 auth. Note that this still uses scram if the password is stored using scram. - Current tests are: - Test that activating the functionality can be added without restarting. - Test that with a failing `query_auth`, clear text passwords are used. - Test that with a correct `query_auth` and without clear text passwords, auth works as expected. - Test that when we change a password in postgres and reload, the new password is obtained, and the pool rotated. - Test that we can use and ENV var to set `auth_query_password`.
1 parent 2702ee4 commit 7111ff2

File tree

8 files changed

+114
-25
lines changed

8 files changed

+114
-25
lines changed

.circleci/pgcat.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ tls_private_key = ".circleci/server.key"
5050
admin_username = "admin_user"
5151
admin_password = "admin_pass"
5252

53-
auth_query = "SELECT * FROM public.user_lookup('$1');"
54-
auth_query_user = "md5_auth_user"
55-
auth_query_password = "secret"
56-
auth_query_database = "postgres"
53+
# auth_query = "SELECT * FROM public.user_lookup('$1');"
54+
# auth_query_user = "md5_auth_user"
55+
# auth_query_password = "secret"
56+
# auth_query_database = "postgres"
5757

5858
# pool
5959
# configs are structured as pool.<pool_name>

.circleci/query_auth_test.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
# Query auth test
3+
4+
set -e
5+
set -o xtrace
6+
7+
export LOCAL_IP=$(hostname -i)
8+
9+
# In config file we have this commented:
10+
# [general]
11+
# ...
12+
# auth_query = "SELECT * FROM public.user_lookup('$1');"
13+
# auth_query_user = "md5_auth_user"
14+
# auth_query_password = "secret"
15+
# auth_query_database = "postgres"
16+
# ...
17+
18+
# Before (sets up auth_query in postgres and pgcat)
19+
PGDATABASE=postgres PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_auth_setup.sql
20+
sed -i 's/^# auth_query/auth_query/' .circleci/pgcat.toml
21+
22+
# TEST_WRONG_AUTH_QUERY BEGIN
23+
# When auth_query fails...
24+
PGDATABASE=postgres \
25+
PGPASSWORD=postgres \
26+
psql -e -h 127.0.0.1 -p 5432 -U postgres -c "REVOKE ALL ON FUNCTION public.user_lookup(text) FROM public, md5_auth_user;"
27+
28+
kill -SIGHUP $(pgrep pgcat) # Reload config
29+
sleep 0.2
30+
31+
# ... we can still connect.
32+
echo "When query_auth_config is wrong, we fall back to passwords set in cleartext."
33+
psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT 1'
34+
35+
# After
36+
PGDATABASE=postgres \
37+
PGPASSWORD=postgres \
38+
psql -e -h 127.0.0.1 -p 5432 -U postgres -c "GRANT EXECUTE ON FUNCTION public.user_lookup(text) TO md5_auth_user;"
39+
# TEST_WRONG_AUTH_QUERY END
40+
41+
# TEST_AUTH_QUERY BEGIN
42+
# When no passwords are specified in config file...
43+
sed -i 's/^password =/# password =/' .circleci/pgcat.toml
44+
kill -SIGHUP $(pgrep pgcat) # Reload config
45+
sleep 0.2
46+
47+
# ... we can still connect
48+
echo "When no passwords are specified in config file, and query_auth is set, we can still connect"
49+
psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT 1'
50+
# TEST_AUTH_QUERY END
51+
52+
# TEST_AUTH_QUERY_WITH_ENV_VAR BEGIN
53+
# When no passwords are specified in config file...
54+
sed -i 's/^password =/# password =/' .circleci/pgcat.toml
55+
# ... and no auth_query_password is set...
56+
sed -i 's/^auth_query_password =/# auth_query_password =/' .circleci/pgcat.toml
57+
kill -SIGTERM $(pgrep pgcat)
58+
export PGCAT_AUTH_QUERY_PASSWORD=secret
59+
start_pgcat "info"
60+
61+
# ... we can still connect
62+
echo "When no passwords are specified in config file, and query_auth is set using env var for password we can still connect"
63+
psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT 1'
64+
# TEST_AUTH_QUERY_WITH_ENV_VAR END
65+
66+
# TEST_PASSWORD_CHANGE BEGIN
67+
# When we change the password of a user in postgres...
68+
PGDATABASE=postgres \
69+
PGPASSWORD=postgres \
70+
psql -e -h 127.0.0.1 -p 5432 -U postgres \
71+
-c "ALTER USER sharding_user WITH ENCRYPTED PASSWORD 'md5b47a59331e93a520d20e90fc8a3355a4'; --- another_sharding_password"
72+
73+
# ... and we reload the config...
74+
kill -SIGHUP $(pgrep pgcat) # Reload config
75+
sleep 0.2
76+
77+
# ... we can connect using the new password
78+
echo "When we change pass in postgres and reload the config, the new hash is fetched."
79+
PGPASSWORD=another_sharding_password psql -U sharding_user -h "${LOCAL_IP}" -p 6432 -c 'SELECT 1'
80+
# TEST_PASSWORD_CHANGE END
81+
82+
# After
83+
PGDATABASE=postgres PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_auth_teardown.sql
84+
sed -i 's/^auth_query/# auth_query/' .circleci/pgcat.toml
85+
sed -i 's/^# password =/password =/' .circleci/pgcat.toml
86+
87+
kill -SIGHUP $(pgrep pgcat)
88+
sleep 0.2

.circleci/run_tests.sh

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,8 @@ curl --fail localhost:9930/metrics
4343
export PGPASSWORD=sharding_user
4444
export PGDATABASE=sharded_db
4545

46-
# Query auth test, we check that without passwords (and query auth) we can connect.
47-
PGDATABASE=postgres PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_auth_setup.sql
48-
sed -i 's/^password =/# password =/' .circleci/pgcat.toml
49-
kill -SIGTERM $(pgrep pgcat) # restart config
50-
start_pgcat "info"
51-
psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT 1'
52-
53-
# Query auth test, we check that when we have issues executing auth_query, passwords are used.
54-
# Also, that reload fetches new passwords
55-
sed -i 's/^# password =/password =/' .circleci/pgcat.toml
56-
PGDATABASE=postgres PGPASSWORD=postgres psql -e -h 127.0.0.1 -p 5432 -U postgres -f tests/sharding/query_auth_wrong_setup.sql
57-
kill -SIGTERM $(pgrep pgcat) # restart config
58-
start_pgcat "info"
59-
psql -U sharding_user -h 127.0.0.1 -p 6432 -c 'SELECT 1'
46+
# Query auth test
47+
source .circleci/query_auth_test.sh
6048

6149
# pgbench test
6250
pgbench -U sharding_user -i -h 127.0.0.1 -p 6432

tests/docker/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
POSTGRES_USER: postgres
88
POSTGRES_DB: postgres
99
POSTGRES_PASSWORD: postgres
10-
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
10+
POSTGRES_HOST_AUTH_METHOD: md5
1111
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all", "-p", "5432"]
1212
pg2:
1313
image: postgres:14

tests/sharding/query_auth_setup.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
--- Sets up query_auth test config:
2+
--- - Change sharding_user password to use md5.
3+
--- - Adds a new user and a function to perform auth_query.
4+
5+
ALTER ROLE sharding_user ENCRYPTED PASSWORD 'md5fa9d23e5a874c61a91bf37e1e4a9c86e'; --- sharding_user
6+
CREATE ROLE md5_auth_user ENCRYPTED PASSWORD 'md54ab2c5d00339c4b2a4e921d2dc4edec7' LOGIN; --- secret
7+
18
CREATE OR REPLACE FUNCTION public.user_lookup(in i_username text, out uname text, out phash text)
29
RETURNS record AS $$
310
BEGIN
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- Tears down query_auth test config:
2+
--- - Change password for sharding_user to use scram instead of md5.
3+
--- - Drops auth query function and user.
4+
5+
ALTER ROLE sharding_user ENCRYPTED PASSWORD 'sharding_user' LOGIN;
6+
7+
REVOKE ALL ON FUNCTION public.user_lookup(text) FROM public, md5_auth_user;
8+
DROP FUNCTION public.user_lookup(in i_username text, out uname text, out phash text);
9+
DROP ROLE md5_auth_user;

tests/sharding/query_auth_wrong_setup.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/sharding/query_routing_setup.sql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ CREATE TABLE data (
5555
DROP ROLE IF EXISTS sharding_user;
5656
DROP ROLE IF EXISTS other_user;
5757
DROP ROLE IF EXISTS simple_user;
58-
DROP ROLE IF EXISTS md5_auth_user;
59-
CREATE ROLE sharding_user ENCRYPTED PASSWORD 'md5fa9d23e5a874c61a91bf37e1e4a9c86e' LOGIN; --- sharding_user
60-
CREATE ROLE other_user ENCRYPTED PASSWORD 'md5cd25b4d66ff35640188a196a4267533d' LOGIN; --- other_user
61-
CREATE ROLE simple_user ENCRYPTED PASSWORD 'md53c3a1ee0ead8c6c15d61b909b9dca980' LOGIN; -- simple_user
62-
CREATE ROLE md5_auth_user ENCRYPTED PASSWORD 'md54ab2c5d00339c4b2a4e921d2dc4edec7' LOGIN; --- secret
58+
CREATE ROLE sharding_user ENCRYPTED PASSWORD 'sharding_user' LOGIN;
59+
CREATE ROLE other_user ENCRYPTED PASSWORD 'other_user' LOGIN;
60+
CREATE ROLE simple_user ENCRYPTED PASSWORD 'simple_user' LOGIN;
6361

6462
GRANT CONNECT ON DATABASE shard0 TO sharding_user;
6563
GRANT CONNECT ON DATABASE shard1 TO sharding_user;

0 commit comments

Comments
 (0)