|
| 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 |
0 commit comments