@@ -24,6 +24,9 @@ inputs:
2424 postgres-version :
2525 description : The PostgreSQL major version to install. Either "14", "15", or "16".
2626 default : " 16"
27+ ssl :
28+ description : When "true", encrypt connections using SSL (TLS).
29+ default : " false"
2730 required : false
2831outputs :
2932 connection-uri :
@@ -32,6 +35,9 @@ outputs:
3235 service-name :
3336 description : The service name with connection parameters.
3437 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 }}
3541runs :
3642 using : composite
3743 steps :
@@ -132,6 +138,23 @@ runs:
132138 # directory we have no permissions to (owned by system postgres user).
133139 echo "unix_socket_directories = ''" >> "$PGDATA/postgresql.conf"
134140 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+
135158 pg_ctl start --pgdata="$PGDATA"
136159
137160 # Save required connection parameters for created superuser to the
@@ -154,6 +177,12 @@ runs:
154177 password=${{ inputs.password }}
155178 dbname=${{ inputs.database }}
156179 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+
157186 echo "PGSERVICEFILE=$PGDATA/pg_service.conf" >> $GITHUB_ENV
158187 shell : bash
159188
@@ -173,6 +202,17 @@ runs:
173202 - name : Set action outputs
174203 run : |
175204 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
176216
177217 echo "connection-uri=$CONNECTION_URI" >> $GITHUB_OUTPUT
178218 echo "service-name=${{ inputs.username }}" >> $GITHUB_OUTPUT
0 commit comments