Skip to content

Commit 51dfb3a

Browse files
committed
BREAKING CHANGE: enforce password authentication
It turns out that PostgreSQL comes with weird default that allows passwordless authentication for localhost connections [1]. This essentially means that 'password' input parameter for this action was ignored. The 'setup-postgres' action's primary use case is to be used on CI where most of the time authentication is desired in order to verify that passwords are passed correctly from applications under test. This patch enforces password authentication even for localhost connections, making sure that passwords are verified and not ignored. This will break everyone who previously passed wrong password or didn't pass it at all. [1] https://www.postgresql.org/docs/15/auth-trust.html Fixes: #5
1 parent ae2fb38 commit 51dfb3a

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

action.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ runs:
4141
- name: Setup and start PostgreSQL
4242
run: |
4343
export PGDATA="$RUNNER_TEMP/pgdata"
44+
export PWFILE="$RUNNER_TEMP/pwfile"
45+
46+
# Unfortunately 'initdb' could only receive a password via file on disk
47+
# or prompt to enter on. Prompting is not an option since we're running
48+
# in non-interactive mode.
49+
echo '${{ inputs.password }}' > $PWFILE
4450
4551
# There are couple of reasons why we need to create a new PostgreSQL
4652
# database cluster. First and foremost, we have to create a superuser
@@ -53,6 +59,8 @@ runs:
5359
# [1] https://www.postgresql.org/docs/15/reference-client.html
5460
initdb \
5561
--username="${{ inputs.username }}" \
62+
--pwfile="$PWFILE" \
63+
--auth="scram-sha-256" \
5664
--encoding="UTF-8" \
5765
--locale="en_US.UTF-8" \
5866
--no-instructions
@@ -69,13 +77,14 @@ runs:
6977
# PGHOST is required for Linux/macOS because we turned off unix sockets
7078
# and they use them by default.
7179
#
72-
# PGPORT, PGUSER and PGDATABASE are required because they could be
73-
# parametrized via action input parameters.
80+
# PGPORT, PGUSER, PGPASSWORD and PGDATABASE are required because they
81+
# could be parametrized via action input parameters.
7482
#
7583
# [1] https://www.postgresql.org/docs/15/reference-client.html
7684
echo "PGHOST=localhost" >> $GITHUB_ENV
7785
echo "PGPORT=${{ inputs.port }}" >> $GITHUB_ENV
7886
echo "PGUSER=${{ inputs.username }}" >> $GITHUB_ENV
87+
echo "PGPASSWORD=${{ inputs.username }}" >> $GITHUB_ENV
7988
echo "PGDATABASE=${{ inputs.database }}" >> $GITHUB_ENV
8089
shell: bash
8190

test_action.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_user_create_drop_user(
146146
connection.execute(f"DROP USER {username}")
147147

148148

149-
def test_client_applications(connection_uri, connection_factory):
149+
def test_client_applications(connection_factory: ConnectionFactory, connection_uri: str):
150150
"""Test that PostgreSQL client applications can be used."""
151151

152152
username = "us3rname"
@@ -167,3 +167,26 @@ def test_client_applications(connection_uri, connection_factory):
167167
finally:
168168
subprocess.check_call(["dropdb", database])
169169
subprocess.check_call(["dropuser", username])
170+
171+
172+
def test_auth_wrong_username(connection_factory: ConnectionFactory, connection_uri: str):
173+
"""Test that wrong username is rejected!"""
174+
175+
connection_furl = furl.furl(connection_uri, username="wrong")
176+
177+
with pytest.raises(psycopg.OperationalError) as excinfo:
178+
connection_factory(connection_furl.url)
179+
180+
assert 'password authentication failed for user "wrong"' in str(excinfo.value)
181+
182+
183+
def test_auth_wrong_password(connection_factory: ConnectionFactory, connection_uri: str):
184+
"""Test that wrong password is rejected!"""
185+
186+
connection_furl = furl.furl(connection_uri, password="wrong")
187+
username = connection_furl.username
188+
189+
with pytest.raises(psycopg.OperationalError) as excinfo:
190+
connection_factory(connection_furl.url)
191+
192+
assert f'password authentication failed for user "{username}"' in str(excinfo.value)

0 commit comments

Comments
 (0)