@@ -60,6 +60,46 @@ wait_rows() {
6060 return 1
6161}
6262
63+ check_log_secrets () {
64+ if [[ " $1 " == * " $password " * || " $1 " == * " $replication_password " * ]]; then
65+ printf >&2 ' PostgreSQL logs exposed a configured password\n'
66+ return 1
67+ fi
68+ }
69+
70+ check_startup_logs () {
71+ local container=" $1 "
72+ shift
73+ local started_at logs message attempt
74+ started_at=" $( docker inspect --format ' {{.State.StartedAt}}' " $container " ) "
75+ for (( attempt= 0 ; attempt< 90 ; attempt++ )) ; do
76+ logs=" $( docker logs --since " $started_at " " $container " 2>&1 ) "
77+ check_log_secrets " $logs "
78+ if awk '
79+ /\[INFO\] PostgreSQL cluster \((master|slave)\): Starting PostgreSQL on port / { started = 1; ready = 0; next }
80+ started && /database system is ready to accept/ { ready = 1 }
81+ END { exit !ready }
82+ ' <<< " $logs" ; then
83+ break
84+ fi
85+ sleep 1
86+ done
87+ if (( attempt == 90 )) ; then
88+ printf >&2 ' PostgreSQL engine readiness log is missing after the current startup: %s\n' " $container "
89+ return 1
90+ fi
91+ if ! grep -Eq ' ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z \[INFO\] PostgreSQL cluster \((master|slave)\): ' <<< " $logs" ; then
92+ printf >&2 ' Structured PostgreSQL startup log is missing: %s\n' " $container "
93+ return 1
94+ fi
95+ for message in " $@ " ; do
96+ if ! grep -Fq " $message " <<< " $logs" ; then
97+ printf >&2 ' PostgreSQL startup log is missing for %s: %s\n' " $container " " $message "
98+ return 1
99+ fi
100+ done
101+ }
102+
63103docker image inspect " $image " > /dev/null
64104major=" $( docker run --rm " $image " bash -ec ' printf "%s" "$PG_MAJOR"' ) "
65105case " $major " in
@@ -87,6 +127,7 @@ docker run -d --name "$master" --network "$network" --network-alias pg-master \
87127 -e " POSTGRESQL_REPLICATION_PASSWORD=$replication_password " \
88128 -v " ${volumes[0]} :/bitnami/postgresql" -v " ${volumes[1]} :/bitnami/postgresql/conf" " $image " > /dev/null
89129wait_healthy " $master "
130+ check_startup_logs " $master " ' Initializing master database' ' Configuring primary replication account' ' Startup configuration complete'
90131[[ " $( sql " $master " 55432 ' SHOW shared_buffers' ) " == 32MB ]]
91132[[ " $( docker exec " $master " id -u) " == 1001 ]]
92133[[ " $( sql " $master " 55432 " SELECT current_setting('server_version_num')::integer / 10000" ) " == " $major " ]]
@@ -109,6 +150,7 @@ docker exec "$master" bash -ec '
109150'
110151docker restart " $master " > /dev/null
111152wait_healthy " $master "
153+ check_startup_logs " $master " ' Using existing master data; skipping initialization and base backup' ' Configuring primary replication account'
112154[[ " $( sql " $master " 55432 ' SELECT system_identifier FROM pg_control_system()' ) " == " $master_id " ]]
113155[[ " $( docker exec -e " PGPASSWORD=$password " " $master " psql -h 127.0.0.1 -p 55432 -U postgres -d postgres -Atqc ' SELECT 1' ) " == 1 ]]
114156if docker exec -e " PGPASSWORD=$password " " $master " psql -h " $master_ip " -p 55432 -U postgres -d postgres -Atqc ' SELECT 1' > /dev/null 2>&1 ; then
@@ -123,6 +165,7 @@ docker run -d --name "$slave" --network "$network" \
123165 -e " POSTGRESQL_REPLICATION_PASSWORD=$replication_password " \
124166 -v " ${volumes[2]} :/bitnami/postgresql" -v " ${volumes[3]} :/bitnami/postgresql/conf" " $image " > /dev/null
125167wait_healthy " $slave "
168+ check_startup_logs " $slave " ' Cloning standby database from primary; waiting for base backup' ' Standby base backup complete' ' Configuring standby replication connection'
126169[[ " $( sql " $slave " 55433 ' SELECT pg_is_in_recovery()' ) " == t ]]
127170[[ " $( sql " $slave " 55433 ' SHOW shared_buffers' ) " == 48MB ]]
128171[[ " $( sql " $slave " 55433 ' SELECT system_identifier FROM pg_control_system()' ) " == " $master_id " ]]
@@ -145,9 +188,11 @@ docker stop "$slave" >/dev/null
145188sql " $master " 55432 " INSERT INTO cluster_probe VALUES (3, 'offline-replica');" > /dev/null
146189docker start " $slave " > /dev/null
147190wait_healthy " $slave "
191+ check_startup_logs " $slave " ' Using existing slave data; skipping initialization and base backup' ' Configuring standby replication connection'
148192wait_rows 3
149193docker restart " $master " > /dev/null
150194wait_healthy " $master "
195+ check_startup_logs " $master " ' Using existing master data; skipping initialization and base backup' ' Configuring primary replication account'
151196sql " $master " 55432 " INSERT INTO cluster_probe VALUES (4, 'restarted-primary');" > /dev/null
152197wait_rows 4
153198[[ " $( docker exec " $slave " stat -c ' %u:%g:%a' /bitnami/postgresql/conf/replication.pgpass) " == 1001:1001:600 ]]
@@ -173,6 +218,10 @@ if docker run --name "$prefix-failed" --network "$network" \
173218 printf >&2 ' Clone unexpectedly succeeded against a closed port\n'
174219 exit 1
175220fi
221+ failed_started_at=" $( docker inspect --format ' {{.State.StartedAt}}' " $prefix -failed" ) "
222+ failed_logs=" $( docker logs --since " $failed_started_at " " $prefix -failed" 2>&1 ) "
223+ check_log_secrets " $failed_logs "
224+ grep -Fq ' [ERROR] PostgreSQL cluster (slave): Standby base backup failed' <<< " $failed_logs"
176225[[ " $( docker run --rm -v " ${volumes[7]} :/conf" " $image " cat /conf/user-file) " == ' preserve configuration' ]]
177226[[ -z " $( docker run --rm -v " ${volumes[6]} :/bitnami/postgresql" " $image " find /bitnami/postgresql -maxdepth 1 -name ' .clone.*' -print) " ]]
178227
187236[[ " $( docker run --rm -v " ${volumes[0]} :/bitnami/postgresql" " $image " cat /bitnami/postgresql/data/PG_VERSION) " == " $major " ]]
188237docker start " $master " > /dev/null
189238wait_healthy " $master "
239+ check_startup_logs " $master " ' Using existing master data; skipping initialization and base backup'
190240[[ " $( sql " $master " 55432 ' SELECT system_identifier FROM pg_control_system()' ) " == " $master_id " ]]
191241[[ " $( sql " $master " 55432 ' SELECT count(*) FROM cluster_probe' ) " == 4 ]]
192242
@@ -204,6 +254,7 @@ docker run -d --user root --name "$master" --network "$network" --network-alias
204254 -e " POSTGRESQL_REPLICATION_PASSWORD=$replication_password " \
205255 -v " ${volumes[0]} :/bitnami/postgresql" -v " ${volumes[1]} :/bitnami/postgresql/conf" " $image " > /dev/null
206256wait_healthy " $master "
257+ check_startup_logs " $master " ' Preparing directory permissions and switching to the postgres user' ' Using existing master data; skipping initialization and base backup' ' [WARN] PostgreSQL cluster (master): Authentication configuration is missing'
207258[[ " $( docker exec " $master " stat -c ' %u:%g' /bitnami/postgresql) " == 1001:1001 ]]
208259[[ " $( docker exec " $master " stat -c ' %u' /proc/1) " == 1001 ]]
209260[[ " $( docker exec " $master " grep -Fxc ' local all all peer' /bitnami/postgresql/conf/pg_hba.conf) " == 1 ]]
0 commit comments