@@ -24,6 +24,9 @@ inputs:
24
24
postgres-version :
25
25
description : The PostgreSQL major version to install. Either "14", "15", or "16".
26
26
default : " 16"
27
+ ssl :
28
+ description : When "true", encrypt connections using SSL (TLS).
29
+ default : " false"
27
30
required : false
28
31
outputs :
29
32
connection-uri :
@@ -32,6 +35,9 @@ outputs:
32
35
service-name :
33
36
description : The service name with connection parameters.
34
37
value : ${{ steps.set-outputs.outputs.service-name }}
38
+ certificate-path :
39
+ description : The path to the server certificate if SSL is on.
40
+ value : ${{ steps.set-outputs.outputs.certificate-path }}
35
41
runs :
36
42
using : composite
37
43
steps :
@@ -132,6 +138,23 @@ runs:
132
138
# directory we have no permissions to (owned by system postgres user).
133
139
echo "unix_socket_directories = ''" >> "$PGDATA/postgresql.conf"
134
140
echo "port = ${{ inputs.port }}" >> "$PGDATA/postgresql.conf"
141
+
142
+ if [ "${{ inputs.ssl }}" = "true" ]; then
143
+ # On Windows, bash runs on top of MSYS2, which automatically converts
144
+ # Unix paths to Windows paths for every argument that appears to be a
145
+ # path. This behavior breaks the openssl invocation because the
146
+ # subject argument is mistakenly converted when it should not be.
147
+ # Therefore, we need to exclude it from the path conversion process
148
+ # by setting the MSYS2_ARG_CONV_EXCL environment variable.
149
+ #
150
+ # https://www.msys2.org/docs/filesystem-paths/#automatic-unix-windows-path-conversion
151
+ export MSYS2_ARG_CONV_EXCL="/CN"
152
+ openssl req -new -x509 -days 365 -nodes -text -subj "/CN=localhost" \
153
+ -out "$PGDATA/server.crt" -keyout "$PGDATA/server.key"
154
+ chmod og-rwx "$PGDATA/server.key" "$PGDATA/server.crt"
155
+ echo "ssl = on" >> "$PGDATA/postgresql.conf"
156
+ fi
157
+
135
158
pg_ctl start --pgdata="$PGDATA"
136
159
137
160
# Save required connection parameters for created superuser to the
@@ -154,6 +177,12 @@ runs:
154
177
password=${{ inputs.password }}
155
178
dbname=${{ inputs.database }}
156
179
EOF
180
+
181
+ if [ "${{ inputs.ssl }}" = "true" ]; then
182
+ echo "sslmode=verify-ca" >> "$PGDATA/pg_service.conf"
183
+ echo "sslrootcert=$PGDATA/server.crt" >> "$PGDATA/pg_service.conf"
184
+ fi
185
+
157
186
echo "PGSERVICEFILE=$PGDATA/pg_service.conf" >> $GITHUB_ENV
158
187
shell : bash
159
188
@@ -173,6 +202,17 @@ runs:
173
202
- name : Set action outputs
174
203
run : |
175
204
CONNECTION_URI="postgresql://${{ inputs.username }}:${{ inputs.password }}@localhost:${{ inputs.port }}/${{ inputs.database }}"
205
+ CERTIFICATE_PATH="$RUNNER_TEMP/pgdata/server.crt"
206
+
207
+ if [ "${{ inputs.ssl }}" = "true" ]; then
208
+ # Although SSLMODE and SSLROOTCERT are specific to libpq options,
209
+ # most third-party drivers also support them. By default libpq
210
+ # prefers SSL but doesn't require it, thus it's important to set
211
+ # these options to ensure SSL is used and the certificate is
212
+ # verified.
213
+ CONNECTION_URI="$CONNECTION_URI?sslmode=verify-ca&sslrootcert=$CERTIFICATE_PATH"
214
+ echo "certificate-path=$CERTIFICATE_PATH" >> $GITHUB_OUTPUT
215
+ fi
176
216
177
217
echo "connection-uri=$CONNECTION_URI" >> $GITHUB_OUTPUT
178
218
echo "service-name=${{ inputs.username }}" >> $GITHUB_OUTPUT
0 commit comments