@@ -21,6 +21,54 @@ setup_postgresdb_no_tls() {
21
21
PORT_FORWARD_PID=$!
22
22
}
23
23
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
+
24
72
setup_provider_config_postgres_no_tls () {
25
73
echo_step " creating ProviderConfig for PostgresDb with no TLS"
26
74
local yaml=" $( cat << EOF
40
88
echo " ${yaml} " | " ${KUBECTL} " apply -f -
41
89
}
42
90
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
+
43
140
setup_postgresdb_tests (){
44
141
# install provider resources
45
142
echo_step " creating PostgresDB Database resource"
@@ -59,19 +156,19 @@ echo_step "creating PostgresDB Schema resources"
59
156
" ${KUBECTL} " apply -f ${projectdir} /examples/postgresql/schema.yaml
60
157
61
158
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
63
160
echo_step_completed
64
161
65
162
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
67
164
echo_step_completed
68
165
69
166
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
71
168
echo_step_completed
72
169
73
170
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
75
172
echo_step_completed
76
173
}
77
174
@@ -168,6 +265,22 @@ check_observe_only_database(){
168
265
echo_step_completed
169
266
}
170
267
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
+
171
284
delete_postgresdb_resources (){
172
285
# uninstall
173
286
echo_step " uninstalling ${PROJECT_NAME} "
@@ -179,14 +292,20 @@ delete_postgresdb_resources(){
179
292
180
293
# ----------- cleaning postgres related resources
181
294
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
184
300
185
301
echo_step " uninstalling secret and provider config for postgres"
186
302
" ${KUBECTL} " delete secret postgresdb-creds
187
303
188
304
echo_step " Uninstalling PostgresDB Helm chart from default namespace"
189
305
" ${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
190
309
}
191
310
192
311
integration_tests_postgres () {
@@ -198,4 +317,16 @@ integration_tests_postgres() {
198
317
check_all_roles_privileges
199
318
check_schema_privileges
200
319
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