Skip to content

Commit b134569

Browse files
committed
test(postgresql): e2e for TLS options
Adds e2e tests for the functionality of providing the TLS options by testing with TLS turned on on the server side using the `tls.enabled=true` and `tls.autoGenerated=true` PostgreSQL Chart parameters. To make the tests as robust as possible: port forwarding is not used, but rather exposing the server port via NodePort; timeouts are increased on kubectl wait's, from 2 min to 3 min. The test iself reuses parts of the existing tests, and adds one additional check that asserts if TLS is used.
1 parent 0f2afad commit b134569

File tree

2 files changed

+147
-7
lines changed

2 files changed

+147
-7
lines changed

cluster/local/integration_tests.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ nodes:
9696
extraMounts:
9797
- hostPath: "${cache_path}/"
9898
containerPath: /cache
99+
extraPortMappings:
100+
- containerPort: 5432
101+
hostPort: 5432
102+
kubeadmConfigPatches:
103+
- |
104+
kind: ClusterConfiguration
105+
apiServer:
106+
extraArgs:
107+
"service-node-port-range": "1-65535"
99108
EOF
100109
)"
101110
echo "${config}" | "${KIND}" create cluster --name="${K8S_CLUSTER}" --wait=5m --image="${node_image}" --config=-

cluster/local/postgresdb_functions.sh

Lines changed: 138 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ setup_postgresdb_no_tls() {
2121
PORT_FORWARD_PID=$!
2222
}
2323

24+
setup_postgresdb_tls() {
25+
echo_step "Installing PostgresDB Helm chart into default namespace"
26+
postgres_root_pw=$(LC_ALL=C tr -cd "A-Za-z0-9" </dev/urandom | head -c 32)
27+
28+
"${HELM}" repo update
29+
"${HELM}" install postgresdb bitnami/postgresql \
30+
--version 11.9.1 \
31+
--set global.postgresql.auth.postgresPassword="${postgres_root_pw}" \
32+
--set volumePermissions.enabled=true \
33+
--set tls.enabled=true \
34+
--set tls.autoGenerated=true \
35+
--set primary.extraEnvVars[0].name=POSTGRESQL_TLS_CA_FILE \
36+
--set primary.extraEnvVars[0].value=/opt/bitnami/postgresql/certs/ca.crt \
37+
--set primary.pgHbaConfiguration='local all all trust
38+
hostssl all all 0.0.0.0/0 md5
39+
hostssl all all ::/0 md5' \
40+
--wait
41+
42+
# Access via NodePort, the port-forward gets a connection reset after first use and will be no longer available
43+
"${KUBECTL}" expose pod postgresdb-postgresql-0 \
44+
--name=postgres-nodeport \
45+
--type=NodePort \
46+
--selector='statefulset.kubernetes.io/pod-name=postgresdb-postgresql-0' \
47+
--overrides '{"apiVersion":"v1","spec":{"ports":[{"port":5432,"protocol":"TCP","targetPort":5432,"nodePort":5432}]}}'
48+
"${KUBECTL}" wait service/postgres-nodeport --timeout 2m --for=jsonpath='{.status.loadBalancer}'
49+
50+
# double check that PostgreSQL is accessible via the NodePort
51+
echo_step "Waiting for PostgreSQL to become accessible"
52+
while ! pg_isready -h localhost -p 5432; do
53+
echo -n .
54+
sleep 0.5
55+
done
56+
echo_step_completed
57+
58+
echo_step "Setting up client connection secrets"
59+
"${KUBECTL}" create secret generic postgresdb-creds \
60+
--from-literal username="postgres" \
61+
--from-literal password="${postgres_root_pw}" \
62+
--from-literal endpoint="postgresdb-postgresql.default.svc.cluster.local" \
63+
--from-literal port="5432"
64+
65+
# copy the TLS secret to crossplane-system namespace for the provider to access
66+
"${KUBECTL}" get secret postgresdb-postgresql-crt -o yaml \
67+
| "${KUBECTL}" patch --patch='{"metadata":{"namespace":"crossplane-system"}}' -o yaml --dry-run=client -f - \
68+
| "${KUBECTL}" apply -f -
69+
echo_step_completed
70+
}
71+
2472
setup_provider_config_postgres_no_tls() {
2573
echo_step "creating ProviderConfig for PostgresDb with no TLS"
2674
local yaml="$( cat <<EOF
@@ -40,6 +88,55 @@ EOF
4088
echo "${yaml}" | "${KUBECTL}" apply -f -
4189
}
4290

91+
setup_provider_config_postgres_tls() {
92+
echo_step "creating ProviderConfig for PostgresDb with TLS"
93+
local yaml="$(cat <<EOF
94+
apiVersion: pkg.crossplane.io/v1beta1
95+
kind: DeploymentRuntimeConfig
96+
metadata:
97+
name: postgres-tls
98+
spec:
99+
deploymentTemplate:
100+
spec:
101+
selector: {}
102+
template:
103+
spec:
104+
containers:
105+
- name: package-runtime
106+
args:
107+
- --debug
108+
volumeMounts:
109+
- mountPath: /certs/postgres
110+
name: postgresql-tls
111+
readOnly: true
112+
volumes:
113+
- name: postgresql-tls
114+
secret:
115+
secretName: postgresdb-postgresql-crt
116+
defaultMode: 420
117+
items:
118+
- key: ca.crt
119+
path: ca.crt
120+
---
121+
apiVersion: postgresql.sql.crossplane.io/v1alpha1
122+
kind: ProviderConfig
123+
metadata:
124+
name: default
125+
spec:
126+
sslRootCert: /certs/postgres/ca.crt
127+
credentials:
128+
source: PostgreSQLConnectionSecret
129+
connectionSecretRef:
130+
namespace: default
131+
name: postgresdb-creds
132+
EOF
133+
)"
134+
echo "${yaml}" | "${KUBECTL}" apply -f -
135+
136+
# make use of the postgres-tls DeploymentRuntimeConfig
137+
"${KUBECTL}" patch providers.pkg.crossplane.io/provider-sql --type=json --patch='[{"op":"add","path":"/spec/runtimeConfigRef","value":{"name":"postgres-tls"}}]'
138+
}
139+
43140
setup_postgresdb_tests(){
44141
# install provider resources
45142
echo_step "creating PostgresDB Database resource"
@@ -59,19 +156,19 @@ echo_step "creating PostgresDB Schema resources"
59156
"${KUBECTL}" apply -f ${projectdir}/examples/postgresql/schema.yaml
60157

61158
echo_step "check if Role is ready"
62-
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/postgresql/role.yaml
159+
"${KUBECTL}" wait --timeout 3m --for condition=Ready -f ${projectdir}/examples/postgresql/role.yaml
63160
echo_step_completed
64161

65162
echo_step "check if database is ready"
66-
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/postgresql/database.yaml
163+
"${KUBECTL}" wait --timeout 3m --for condition=Ready -f ${projectdir}/examples/postgresql/database.yaml
67164
echo_step_completed
68165

69166
echo_step "check if grant is ready"
70-
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/postgresql/grant.yaml
167+
"${KUBECTL}" wait --timeout 3m --for condition=Ready -f ${projectdir}/examples/postgresql/grant.yaml
71168
echo_step_completed
72169

73170
echo_step "check if schema is ready"
74-
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/postgresql/schema.yaml
171+
"${KUBECTL}" wait --timeout 3m --for condition=Ready -f ${projectdir}/examples/postgresql/schema.yaml
75172
echo_step_completed
76173
}
77174

@@ -168,6 +265,22 @@ check_observe_only_database(){
168265
echo_step_completed
169266
}
170267

268+
check_tls_used() {
269+
echo_step "check if TLS is used to connect"
270+
local tls
271+
tls="$(PGPASSWORD="${postgres_root_pw}" psql -h localhost -p 5432 -U postgres -wtAc "select pg_ssl.ssl FROM pg_stat_ssl pg_ssl JOIN pg_stat_activity pg_sa ON pg_ssl.pid = pg_sa.pid;")"
272+
273+
if [[ "${tls}" == t ]]; then
274+
echo "Connected using TLS"
275+
echo_info "OK"
276+
else
277+
echo "Did not connect using TLS"
278+
echo_error "Not OK"
279+
fi
280+
281+
echo_step_completed
282+
}
283+
171284
delete_postgresdb_resources(){
172285
# uninstall
173286
echo_step "uninstalling ${PROJECT_NAME}"
@@ -179,14 +292,20 @@ delete_postgresdb_resources(){
179292

180293
# ----------- cleaning postgres related resources
181294

182-
echo_step "kill port-forwarding"
183-
kill $PORT_FORWARD_PID
295+
if [[ -n "${PORT_FORWARD_PID}" ]]; then
296+
echo_step "kill port-forwarding"
297+
kill $PORT_FORWARD_PID
298+
unset PORT_FORWARD_PID
299+
fi
184300

185301
echo_step "uninstalling secret and provider config for postgres"
186302
"${KUBECTL}" delete secret postgresdb-creds
187303

188304
echo_step "Uninstalling PostgresDB Helm chart from default namespace"
189305
"${HELM}" uninstall postgresdb
306+
307+
# make sure to delete the PVC, otherwise the password will be reused from the first installation
308+
"${KUBECTL}" delete --ignore-not-found=true pvc data-postgresdb-postgresql-0
190309
}
191310

192311
integration_tests_postgres() {
@@ -198,4 +317,16 @@ integration_tests_postgres() {
198317
check_all_roles_privileges
199318
check_schema_privileges
200319
delete_postgresdb_resources
201-
}
320+
321+
tls_tests() {
322+
local PGSSLMODE=require
323+
setup_postgresdb_tls
324+
setup_provider_config_postgres_tls
325+
check_tls_used
326+
setup_observe_only_database
327+
setup_postgresdb_tests
328+
check_all_roles_privileges
329+
delete_postgresdb_resources
330+
}
331+
tls_tests
332+
}

0 commit comments

Comments
 (0)