Skip to content

Commit

Permalink
183164582 Test mysql using containers (#717)
Browse files Browse the repository at this point in the history
* Experiment running system-tests for mysql inside containers

[#183164582]

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>

* Build database-backup-restore and fix echo commands

[#183164582]

Also replace some additional commands performed on BOSH VM

* Ensure we invoke `bash -c` for every `go.Command`

[#183164582]

Running os commands using goexec is not identical to running
those commands in a shell such as bash.
Some examples of known things to require a bash shell:
- Pipes
- Redirects

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>

* Create workflow to run MySQL tests on PRs

[#183164582]

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>

* Generate TLS certificates and shared them across containers

[#183164582]

Mysql service is the one in charge of creating the certificates. To make these
generated certificates available to the container running the tests we use a
shared volume named `mysql-certs` which we mount in both containers.

* Support testing several MYSQL versions using GHAction matrix

[#183164582]

For this we need to pass the MYSQL version we want to test as a Docker build arg

* Remove from matrix failing unsupported versions of MYSQL

[#183164582]

There are chances that the versions removed could also work
with our current release but they need some packages not included by default in
the Dockerimage. Doing the extra effort to make the pass is irrelevant to the
BOSH release itself, since the filesystem is not identical to what will be included
in the VM.

We might need to investigate how to reduce this filesystem mismatch if possible.

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>

* WIP try to improve some docs-type things

[#183164582]

Signed-off-by: Gareth Smith <sgareth@vmware.com>

* Remove unneded folder intialization steps

[#183164582]

They seem to have inconsistent behaviour on different operating systems

* Restore folder initialization and add it to system-mysql Dockerfile too

[#183164582]

* Print database logs in case of test failures

[#183164582]

* Reorganize VOLUME declarations to overcome a possible bug in Docker

[#183164582]

docker/compose#3270 (comment)

* Replace chmod with chown to better express the intent of the operation

[#183164582]

* Revert "Replace chmod with chown to better express the intent of the operation"

This reverts commit e102885.

It seems `chmod root:root` counteracts `chmod mysql:mysql` causing a race condition
between the two containers. It seems we are better off using `chmod 777` here.

* Improve debugging experience by mounting the whole repo instead of a subfolder

[#183164582]

By doing this any error message in the logs would point to the exact path where
the problem occurred instead of a docker-mounted path in an arbitrary location.

This change pursues reducing the knowledge burden introduced by the use of Docker
by trying to make the Docker integration as thin and transparent as possible.
Ideally, not requiring any Docker knowledge to be able to work with the codebase.

* Improve unit-tests debug experience by mounting full repo instead of subfolder

[#183164582]

By doing this any error message in the logs would point to the exact path where
the problem occurred instead of a docker-mounted path in an arbitrary location.

This change pursues reducing the knowledge burden introduced by the use of Docker
by trying to make the Docker integration as thin and transparent as possible.
Ideally, not requiring any Docker knowledge to be able to work with the codebase.

* Refactor system-db-tests to use a similar approach to unit-tests

[#183164582]

Some goals of this refactor were:
- Don't have an explicit ENTRYPOINT in Dockerfile, specify it in docker-compose
- Move most of the boilerplate required by the tests to the tests folder
- Remove hardcoded paths in boilerplate environment variables required by tests
- Make scripts/run-system-db-tests-mysqsl more self-container and self-documenting
- Flatten the path to start adding tests for other databases such as Postgres
- Flatten the path to add some tests for testing the version detection logic

* Simplify TestRunner.Dockerfile and `backup`/`restore` invocations

[#183164582]

Original tests made use of two auxiliary scripts `backup`/`restore` to
set the required environment variables every time they were used.
Since these variables are static and never change we added these vars
to the `run-system-db-tests-mysql.bash` script itself and invoke the
`database-backup-restore` binary directly instead from the tests.

* Reduce certs handling in `Dockerfile` and `docker-compose.yml`

[#183164582]

By aglutinating the three previous variables each one pointing to
a different certificate into a single variable pointing to a folder
we should contain the three files.
These files are expected to always be named the same so there isn't
a need to specify the full path for each of them individually.

* Fix errors introduced in latest commit

[#183164582]

Some of the environment variables which previously we thought were
not needed explicitly by the tests were in fact required for mysql
to successfully establish a connection using TLS certificates.

* Add MySQL5.7 binary as it is used to determine version of db

[#183164582]

By looking at the source-code we determined that mysql5.7 binary
is used to connect with the mysql database and ask for its version.
Only later, the corresponding binary will be choosed and used.

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>

* Add tests for different versions of MariaDB

[#183164582]

In this case we didn't copy the binary from the mariadb Docker
image because it had unsatisfied dependencies (such as libssl3)

* Trigger PR workflows

* Show database logs on error to facilitate debugging

[#183164582]

* Workaround permission issues when specifying Volume in Dockerfile

[#183164582]

* Try triggering tests again

[#183164582]

* Use chmod 777 both in TestsRunner and BackingDB Dockerfiles

[#183164582]

* Fix mysql5.7-debian tests and make mysql tests more precise

[#183164582]

Since bbr-sdk-release always uses 5.7 at least once (to query the db version)
we always need to specify MYSQL_CLIENT_5_7_PATH.
Since we are using GitHub matrixes to tests several MySQL versions using the
same scripts, it seems difficult to use the MYSQL_VERSION variable to download
only the required binaries since we will need some conditional logic to populate
the right ENV variables.

For that reason this commit simply downloads the latest MySQL 8.0 and MySQL 5.7
binaries and populates the corresponding ENV variables every time, without caring
which specific MYSQL_VERSION we are currently testing.

* Add Postgres system tests

[#183164582]

* Fix typo in Postgres system tests workflow

[#183164582]

* Test compilation against diff stemcells and improve system tests

[#183164582]

To test compilation against diff stemcells we use multistage Docker containers
to download bosh-lite warden tarballs and using the rootfs included in the tarball
to create a container FROM scratch.

Inside that dockerized stemcell we run the release packaging scripts which allows
testing compilation against different stemcell using GHActions matrix strategies.

Now that we are able to compile the release directly in GHActions, we can improve
system tests by leveraging the binaries built on-the-fly from the blobs.
This approach has huge benefits compared to the previous one:
- Tests are more true to reality
- Bosh blobs are fully tested (instead of testing just the GO code)
- Compilation is being tested against different stemcells

* Use `backup` and `restore` scripts from the /var/vcap/jobs folder

[#183164582]

Also, compiled database-backup-restorer using its packaging script
instead of manually running golang during the `run-system-db-tests` scripts
and refactored tests to use the binary located in /var/vcap/packages

* Reduce verbosity of the logs

[#183164582]

* Add ubuntu-xenial tests except for MySQL 8.0 which is unsupported

[#183164582]

* Run postgres_tls and postgres_mutual_tls test suites in containers

[#183164582]

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>

* Fix Postgres TLS and MUTUAL_TLS test suites

[#183164582]

* Ensure system-db-postgres-backing-db database gets cleaned up after each job

[#183164582]

* Fix ENABLE_TLS var not being passed to docker-compose run commands

[#183164582]

All tests were running the basic Postgres tests instead of TLS and MUTUAL

* Run dockerize-release as a separate step from the tests

[#183164582]

This change will help us differentiate how much time is spent in the actual tests versus
compiling the release itself. This should serve as a first step towards optimizing builds

* Prevent running Postgres TLS and MTLS tests for 9.4-alpine

[#183164582]

postgres:9.4-alpine doesn't have openssl package installed by default
which causes the database to error before start and prevents tests from running.

Testing Postgres 9.4 without TLS/MTLS should be enough for now to ensure this
version is supported. Patching postgres Dockerfile to add openssl is possible
but might no be needed or desirable since Postgres 9.4 reached EOGS in February 2020

* Disable 9.4-alpine for Mutual TLS not normal tests

[#183164582]

* Move dockerfiles from .github/actions to ci/

[#183164582]

Signed-off-by: Diego Lemos <dlemos@vmware.com>

* Restore tests and guard new BOSH-less functionality under a flag

[#183164582]

* Redirect stderr to stdout when running with flag RUN_TESTS_WITHOUT_BOSH

[#183164582]

The tests expected error messages to be available in stdout. Probably because when
running the commands by SSHing into a BOSH VM the errors are retreived from Stdout

Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com>
Signed-off-by: Gareth Smith <sgareth@vmware.com>
Signed-off-by: Diego Lemos <dlemos@vmware.com>
Co-authored-by: Gareth Smith <sgareth@vmware.com>
Co-authored-by: Diego Lemos <dlemos@vmware.com>
Co-authored-by: Cryogenics CI Bot <mapbu-cryogenics@groups.vmware.com>
  • Loading branch information
4 people committed Oct 25, 2022
1 parent 5e68a5f commit 616c9c9
Show file tree
Hide file tree
Showing 20 changed files with 638 additions and 36 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/system-db-tests.yml
@@ -0,0 +1,106 @@
on:
pull_request:
branches:
- main

jobs:
system-db-mariadb:
name: System DB Tests for MariaDB
runs-on: ubuntu-latest
strategy:
matrix:
stemcell-name: [ubuntu-bionic, ubuntu-jammy, ubuntu-xenial]
mariadb-version: [10.9-jammy, 10.7-focal, 10.5-focal, 10.2-bionic]

steps:
- uses: actions/checkout@v3
with:
path: repo

- name: Dockerize BOSH Release (Compilation Test)
run: |
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
docker-compose -f repo/docker-compose.yml run dockerize-release
- name: Run System Tests for MariaDB
run: |
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
MARIADB_VERSION="${{ matrix.mariadb-version }}" \
MARIADB_PASSWORD="$(head /dev/urandom | md5sum | cut -f1 -d" ")" \
docker-compose -f repo/docker-compose.yml run system-db-mariadb || (docker-compose -f repo/docker-compose.yml logs system-db-mariadb-backing-db && exit 1)
system-db-mysql:
name: System DB Tests for MySQL
runs-on: ubuntu-latest
strategy:
matrix:
stemcell-name: [ubuntu-bionic, ubuntu-jammy, ubuntu-xenial]
mysql-version: [8.0-debian, 8.0-oracle, 5.7-debian]
exclude:
- stemcell-name: ubuntu-xenial
mysql-version: 8.0-debian
- stemcell-name: ubuntu-xenial
mysql-version: 8.0-oracle

steps:
- uses: actions/checkout@v3
with:
path: repo

- name: Dockerize BOSH Release (Compilation Test)
run: |
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
docker-compose -f repo/docker-compose.yml run dockerize-release
- name: Run System Tests for MySQL
run: |
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
MYSQL_VERSION="${{ matrix.mysql-version }}" \
MYSQL_PASSWORD="$(head /dev/urandom | md5sum | cut -f1 -d" ")" \
docker-compose -f repo/docker-compose.yml run system-db-mysql || (docker-compose -f repo/docker-compose.yml logs system-db-mysql-backing-db && exit 1)
system-db-postgres:
name: System DB Tests for Postgres
runs-on: ubuntu-latest
strategy:
matrix:
stemcell-name: [ubuntu-bionic, ubuntu-jammy, ubuntu-xenial]
postgres-version: [13-bullseye, 11-bullseye, 10-bullseye, 9.6-bullseye, 9.4-alpine]

steps:
- uses: actions/checkout@v3
with:
path: repo

- name: Dockerize BOSH Release (Compilation Test)
run: |
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
docker-compose -f repo/docker-compose.yml run dockerize-release
- name: Run System Tests for Postgres
run: |
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
POSTGRES_VERSION="${{ matrix.postgres-version }}" \
POSTGRES_PASSWORD="$(head /dev/urandom | md5sum | cut -f1 -d" ")" \
docker-compose -f repo/docker-compose.yml run system-db-postgres || (docker-compose -f repo/docker-compose.yml logs system-db-postgres-backing-db && exit 1)
docker-compose -f repo/docker-compose.yml down -v --remove-orphans --rmi local
- name: Run System Tests for Postgres with TLS
if: ${{ matrix.postgres-version != '9.4-alpine' }}
run: |
ENABLE_TLS="yes" \
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
POSTGRES_VERSION="${{ matrix.postgres-version }}" \
POSTGRES_PASSWORD="$(head /dev/urandom | md5sum | cut -f1 -d" ")" \
docker-compose -f repo/docker-compose.yml run system-db-postgres || (docker-compose -f repo/docker-compose.yml logs system-db-postgres-backing-db && exit 1)
docker-compose -f repo/docker-compose.yml down -v --remove-orphans --rmi local
- name: Run System Tests for Postgres with MUTUAL_TLS
if: ${{ matrix.postgres-version != '9.4-alpine' }}
run: |
ENABLE_TLS="mutual" \
STEMCELL_NAME="${{ matrix.stemcell-name }}" \
POSTGRES_VERSION="${{ matrix.postgres-version }}" \
POSTGRES_PASSWORD="$(head /dev/urandom | md5sum | cut -f1 -d" ")" \
docker-compose -f repo/docker-compose.yml run system-db-postgres || (docker-compose -f repo/docker-compose.yml logs system-db-postgres-backing-db && exit 1)
docker-compose -f repo/docker-compose.yml down -v --remove-orphans --rmi local
97 changes: 97 additions & 0 deletions ci/dockerfiles/dockerize-release/Dockerfile
@@ -0,0 +1,97 @@
#########################################################################
# Find, download and uncompress latest version of $STEMCELL_NAME from Bosh.io
#########################################################################
FROM alpine as uncompressed-stemcell
ARG STEMCELL_NAME
RUN apk add curl --no-cache
RUN STEMCELL_URL="$(curl -L https://bosh.io/stemcells | grep -io "https:\/\/.*warden-boshlite-${STEMCELL_NAME}-go_agent.tgz")" \
&& curl -o /stemcell.tgz -L ${STEMCELL_URL}
RUN mkdir -p /wrapper && tar xf /stemcell.tgz -C /wrapper
RUN mkdir -p /stemcell && tar xf /wrapper/image -C /stemcell

#########################################################################
# Run bosh create-release and uncompress tarball which contains blobs and vendored packages
#########################################################################
FROM alpine as uncompressed-release
RUN apk add bash curl git ca-certificates --no-cache
RUN curl -o /bin/bosh -L "https://github.com/cloudfoundry/bosh-cli/releases/download/v7.0.1/bosh-cli-7.0.1-linux-amd64" && chmod +x /bin/bosh

COPY . /release-repo
WORKDIR /release-repo

RUN git submodule update --init --recursive
RUN bosh create-release --force --tarball /release.tgz
RUN mkdir -p /release && tar -xf /release.tgz -C /release

WORKDIR /release/packages
RUN bash -c 'for tgz in *.tgz ; do \
filename=$(basename -- "${tgz}") ; \
ext="${filename##*.}" ; \
noext="${filename%.*}" ; \
\
mkdir -p "${noext}" ; \
tar -xf "${tgz}" -C "${noext}" ; \
rm "${tgz}" ; \
echo "${tgz} extracted!" ; \
done'

WORKDIR /release/jobs
RUN bash -c 'for tgz in *.tgz ; do \
filename=$(basename -- "${tgz}") ; \
ext="${filename##*.}" ; \
noext="${filename%.*}" ; \
\
mkdir -p "${noext}" ; \
tar -xf "${tgz}" -C "${noext}" ; \
rm "${tgz}" ; \
echo "${tgz} extracted!" ; \
done'

#########################################################################
# Mount alongside stemcell and uncompressed release files and run packaging scripts
#########################################################################
FROM scratch as compiled-release
COPY --from=uncompressed-stemcell /stemcell/ /
COPY --from=uncompressed-release /release /release

ENV PACKAGES="libpcre2 \
libopenssl1 \
database-backup-restorer-mariadb \
database-backup-restorer-boost \
database-backup-restorer-mysql-5.6 \
database-backup-restorer-mysql-5.7 \
database-backup-restorer-mysql-8.0 \
database-backup-restorer-postgres-9.4 \
database-backup-restorer-postgres-9.6 \
database-backup-restorer-postgres-10 \
database-backup-restorer-postgres-10 \
database-backup-restorer-postgres-11 \
database-backup-restorer-postgres-13 \
golang-1-linux \
database-backup-restorer"

WORKDIR /release/packages
RUN bash -c 'for pkg in ${PACKAGES}; do \
export BOSH_INSTALL_TARGET="/var/vcap/packages/${pkg}" ; \
cd "/release/packages/${pkg}" ; \
mkdir -p "${BOSH_INSTALL_TARGET}" ; \
chmod +x packaging ; \
./packaging ; \
done'


WORKDIR /release/jobs
RUN mkdir -p /var/vcap/jobs/database-backup-restorer/bin \
&& cp database-backup-restorer/templates/backup /var/vcap/jobs/database-backup-restorer/bin/backup \
&& cp database-backup-restorer/templates/restore /var/vcap/jobs/database-backup-restorer/bin/restore \
&& chmod +x /var/vcap/jobs/database-backup-restorer/bin/backup \
&& chmod +x /var/vcap/jobs/database-backup-restorer/bin/restore

#########################################################################
# Ready to run, clean, filesystem with the stemcell and compiled packages in place
#########################################################################
FROM scratch
COPY --from=uncompressed-stemcell /stemcell/ /
COPY --from=compiled-release /var/vcap/ /var/vcap/
ENTRYPOINT ["echo", "Successfully dockerized release!"]

@@ -1,7 +1,6 @@
FROM golang:1.19.1

VOLUME /goproject
VOLUME /backup-and-restore-sdk-release

RUN go install github.com/onsi/ginkgo/ginkgo@latest

ENTRYPOINT /goproject/scripts/run-unit-tests.bash
11 changes: 11 additions & 0 deletions ci/dockerfiles/run-system-db-tests/Dockerfile
@@ -0,0 +1,11 @@
FROM scratch
COPY --from=dockerize-release / /

VOLUME /backup-and-restore-sdk-release

RUN mkdir -p /tls-certs && chmod -R 777 /tls-certs
VOLUME /tls-certs
# https://boxboat.com/2017/01/23/volumes-and-dockerfiles-dont-mix/

ENV PATH=/var/vcap/packages/golang-1-linux/bin/:${PATH}
RUN GOBIN=/usr/local/bin/ go install github.com/onsi/ginkgo/ginkgo@latest
9 changes: 9 additions & 0 deletions ci/dockerfiles/run-system-db-tests/mariadb/Dockerfile
@@ -0,0 +1,9 @@
ARG MARIADB_VERSION
FROM mariadb:$MARIADB_VERSION

RUN mkdir -p /tls-certs && chmod -R 777 /tls-certs
VOLUME /tls-certs
# https://boxboat.com/2017/01/23/volumes-and-dockerfiles-dont-mix/

RUN mkdir -p /etc/mysql/mariadb.conf.d/ && chown mysql: /etc/mysql/mariadb.conf.d/
ADD enable_tls.sh /docker-entrypoint-initdb.d/
42 changes: 42 additions & 0 deletions ci/dockerfiles/run-system-db-tests/mariadb/enable_tls.sh
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

set -euo pipefail

# https://portal2portal.blogspot.com/2021/09/openssl-get-your-subject-right.html
# Whatever method you use to generate the certificate and key files, the Common Name
# value used for the server and client certificates/keys must each differ from the
# Common Name value used for the CA certificate.
# Otherwise, the certificate and key files will not work for servers compiled using OpenSSL.

openssl genrsa 2048 > /tls-certs/ca-key.pem
SUBJ="/C=DK/CN=CertificateAuthority/ST=SomeLand/L=SomeLocality/S=SomeProvince/O=SomeOrganization"
openssl req -new -x509 -nodes -days 3650 -key /tls-certs/ca-key.pem -subj "$SUBJ" > /tls-certs/ca-cert.pem

SUBJ="/C=IE/CN=ServerCert/ST=SomeOtherLand/L=SomeOtherLocality/S=SomeOtherProvince/O=SomeOtherOrganization"
openssl req -newkey rsa:2048 -days 3650 -nodes -keyout /tls-certs/server-key.pem -subj "$SUBJ" > /tls-certs/server-req.pem
openssl x509 -req -in /tls-certs/server-req.pem -days 3650 -CA /tls-certs/ca-cert.pem -CAkey /tls-certs/ca-key.pem -set_serial 01 > /tls-certs/server-cert.pem
openssl rsa -in /tls-certs/server-key.pem -out /tls-certs/server-key.pem

SUBJ="/C=CB/CN=ClientCert/ST=EvenOtherLand/L=EvenOtherLocality/S=EvenOtherProvince/O=EvenOtherOrganization"
openssl req -newkey rsa:2048 -days 3650 -nodes -keyout /tls-certs/client-key.pem -subj "$SUBJ" > /tls-certs/client-req.pem
openssl x509 -req -in /tls-certs/client-req.pem -days 3650 -CA /tls-certs/ca-cert.pem -CAkey /tls-certs/ca-key.pem -set_serial 01 > /tls-certs/client-cert.pem
openssl rsa -in /tls-certs/client-key.pem -out /tls-certs/client-key.pem

# https://mariadb.com/kb/en/mariadb-ssl-connection-issues/
# The core of the issue, you've used exactly the same information both for the client and the server certificate and OpenSSL doesn't like that
openssl verify -CAfile /tls-certs/ca-cert.pem /tls-certs/server-cert.pem /tls-certs/client-cert.pem

mkdir -p /etc/mysql/mariadb.conf.d/
cat << EOF > /etc/mysql/mariadb.conf.d/tls.cnf
[mysqld]
ssl-ca=/tls-certs/ca-cert.pem
ssl-cert=/tls-certs/server-cert.pem
ssl-key=/tls-certs/server-key.pem
bind-address= *
[client]
ssl-cert=/tls-certs/client-cert.pem
ssl-key=/tls-certs/client-key.pem
EOF

10 changes: 10 additions & 0 deletions ci/dockerfiles/run-system-db-tests/mysql/Dockerfile
@@ -0,0 +1,10 @@
ARG MYSQL_VERSION
FROM mysql:$MYSQL_VERSION

RUN mkdir -p /tls-certs && chmod -R 777 /tls-certs
VOLUME /tls-certs
# https://boxboat.com/2017/01/23/volumes-and-dockerfiles-dont-mix/

RUN mkdir -p /etc/mysql/mysql.conf.d/ && chown mysql: /etc/mysql/mysql.conf.d/
ADD enable_mysql_tls.sh /docker-entrypoint-initdb.d/

17 changes: 17 additions & 0 deletions ci/dockerfiles/run-system-db-tests/mysql/enable_mysql_tls.sh
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -euo pipefail

mysql_ssl_rsa_setup
cp /var/lib/mysql/ca.pem /tls-certs/
cp /var/lib/mysql/server-cert.pem /tls-certs/
cp /var/lib/mysql/server-key.pem /tls-certs/

mkdir -p /etc/mysql/mysql.conf.d/
cat << EOF > /etc/mysql/mysql.conf.d/ssl.cnf
[mysqld]
ssl-ca=/var/lib/mysql/ca.pem
ssl-cert=/var/lib/mysql/server-cert.pem
ssl-key=/var/lib/mysql/server-key.pem
EOF

9 changes: 9 additions & 0 deletions ci/dockerfiles/run-system-db-tests/postgres/Dockerfile
@@ -0,0 +1,9 @@
ARG POSTGRES_VERSION
FROM postgres:$POSTGRES_VERSION

RUN mkdir -p /tls-certs && chmod -R 777 /tls-certs
VOLUME /tls-certs
# https://boxboat.com/2017/01/23/volumes-and-dockerfiles-dont-mix/

ADD enable_tls.sh /docker-entrypoint-initdb.d/
RUN touch /docker-entrypoint-initdb.d/enable-tls.sql && chmod 777 /docker-entrypoint-initdb.d/enable-tls.sql
51 changes: 51 additions & 0 deletions ci/dockerfiles/run-system-db-tests/postgres/enable_tls.sh
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

set -euo pipefail

ENABLE_TLS="${ENABLE_TLS:-no}"

if [[ "${ENABLE_TLS}" == "yes" || "${ENABLE_TLS}" = "mutual" ]]; then
# https://portal2portal.blogspot.com/2021/09/openssl-get-your-subject-right.html
# Whatever method you use to generate the certificate and key files, the Common Name
# value used for the server and client certificates/keys must each differ from the
# Common Name value used for the CA certificate.
# Otherwise, the certificate and key files will not work for servers compiled using OpenSSL.

openssl genrsa 2048 > /tls-certs/ca-key.pem
SUBJ="/C=DK/CN=CertificateAuthority/ST=SomeLand/L=SomeLocality/S=SomeProvince/O=SomeOrganization"
openssl req -new -x509 -nodes -days 3650 -key /tls-certs/ca-key.pem -subj "$SUBJ" > /tls-certs/ca-cert.pem

# Notice that we are using `system-db-postgres-backing-db`
# which is the same name we give to the Postgres backing db service
# this is needed for the plsql Mutual TLS to work correctly
SUBJ="/C=IE/CN=system-db-postgres-backing-db/ST=SomeOtherLand/L=SomeOtherLocality/S=SomeOtherProvince/O=SomeOtherOrganization"
openssl req -newkey rsa:2048 -days 3650 -nodes -keyout /tls-certs/server-key.pem -subj "$SUBJ" > /tls-certs/server-req.pem
openssl x509 -req -in /tls-certs/server-req.pem -days 3650 -CA /tls-certs/ca-cert.pem -CAkey /tls-certs/ca-key.pem -set_serial 01 > /tls-certs/server-cert.pem
openssl rsa -in /tls-certs/server-key.pem -out /tls-certs/server-key.pem

SUBJ="/C=CB/CN=ClientCert/ST=EvenOtherLand/L=EvenOtherLocality/S=EvenOtherProvince/O=EvenOtherOrganization"
openssl req -newkey rsa:2048 -days 3650 -nodes -keyout /tls-certs/client-key.pem -subj "$SUBJ" > /tls-certs/client-req.pem
openssl x509 -req -in /tls-certs/client-req.pem -days 3650 -CA /tls-certs/ca-cert.pem -CAkey /tls-certs/ca-key.pem -set_serial 01 > /tls-certs/client-cert.pem
openssl rsa -in /tls-certs/client-key.pem -out /tls-certs/client-key.pem

# https://mariadb.com/kb/en/mariadb-ssl-connection-issues/
# The core of the issue, you've used exactly the same information both for the client and the server certificate and OpenSSL doesn't like that
openssl verify -CAfile /tls-certs/ca-cert.pem /tls-certs/server-cert.pem /tls-certs/client-cert.pem

cat << 'EOF' > /docker-entrypoint-initdb.d/enable-tls.sql
ALTER SYSTEM SET ssl_ca_file TO '/tls-certs/ca-cert.pem';
ALTER SYSTEM SET ssl_cert_file TO '/tls-certs/server-cert.pem';
ALTER SYSTEM SET ssl_key_file TO '/tls-certs/server-key.pem';
ALTER SYSTEM SET ssl TO 'ON';
EOF
fi

if [[ "${ENABLE_TLS}" == "yes" ]]; then
cat << 'EOF' > /var/lib/postgresql/data/pg_hba.conf
hostssl all all 0.0.0.0/0 md5
EOF
elif [[ "${ENABLE_TLS}" == "mutual" ]]; then
cat << 'EOF' > /var/lib/postgresql/data/pg_hba.conf
hostssl all all 0.0.0.0/0 md5 clientcert=1
EOF
fi

0 comments on commit 616c9c9

Please sign in to comment.