#!/bin/bash # Copyright Broadcom, Inc. All Rights Reserved. # SPDX-License-Identifier: APACHE-2.0 # # Bitnami Pgpool-II library # shellcheck disable=SC1090,SC1091 # Load Generic Libraries . /opt/bitnami/scripts/libfile.sh . /opt/bitnami/scripts/libfs.sh . /opt/bitnami/scripts/liblog.sh . /opt/bitnami/scripts/libnet.sh . /opt/bitnami/scripts/libos.sh . /opt/bitnami/scripts/libservice.sh . /opt/bitnami/scripts/libvalidations.sh ######################## # Validate settings in PGPOOL_* env. variables # Globals: # PGPOOL_* # Arguments: # None # Returns: # None ######################### pgpool_validate() { info "Validating settings in PGPOOL_* env vars..." local error_code=0 # Auxiliary functions print_validation_error() { error "$1" error_code=1 } if [[ -z "$PGPOOL_ADMIN_USERNAME" ]] || [[ -z "$PGPOOL_ADMIN_PASSWORD" ]]; then print_validation_error "The Pgpool administrator user's credentials are mandatory. Set the environment variables PGPOOL_ADMIN_USERNAME and PGPOOL_ADMIN_PASSWORD with the Pgpool administrator user's credentials." fi if [[ "$PGPOOL_SR_CHECK_PERIOD" -gt 0 ]] && { [[ -z "$PGPOOL_SR_CHECK_USER" ]] || [[ -z "$PGPOOL_SR_CHECK_PASSWORD" ]]; }; then print_validation_error "The Streaming Replication Check credentials are mandatory. Set the environment variables PGPOOL_SR_CHECK_USER and PGPOOL_SR_CHECK_PASSWORD with the Streaming Replication Check credentials." fi if [[ -z "$PGPOOL_HEALTH_CHECK_USER" ]] || [[ -z "$PGPOOL_HEALTH_CHECK_PASSWORD" ]]; then print_validation_error "The PostgreSQL health check credentials are mandatory. Set the environment variables PGPOOL_HEALTH_CHECK_USER and PGPOOL_HEALTH_CHECK_PASSWORD with the PostgreSQL health check credentials." fi if is_boolean_yes "$PGPOOL_ENABLE_LDAP" && { [[ -z "${LDAP_URI}" ]] || [[ -z "${LDAP_BASE}" ]] || [[ -z "${LDAP_BIND_DN}" ]] || [[ -z "${LDAP_BIND_PASSWORD}" ]]; }; then print_validation_error "The LDAP configuration is required when LDAP authentication is enabled. Set the environment variables LDAP_URI, LDAP_BASE, LDAP_BIND_DN and LDAP_BIND_PASSWORD with the LDAP configuration." fi if is_boolean_yes "$PGPOOL_ENABLE_LDAP" && (! is_boolean_yes "$PGPOOL_ENABLE_POOL_HBA" || ! is_boolean_yes "$PGPOOL_ENABLE_POOL_PASSWD"); then print_validation_error "pool_hba.conf authentication and pool password should be enabled for LDAP to work. Keep the PGPOOL_ENABLE_POOL_HBA and PGPOOL_ENABLE_POOL_PASSWD environment variables set to 'yes'." fi if is_boolean_yes "$PGPOOL_ENABLE_POOL_PASSWD" && { [[ -z "$PGPOOL_POSTGRES_USERNAME" ]] || [[ -z "$PGPOOL_POSTGRES_PASSWORD" ]]; }; then print_validation_error "The administrator's database credentials are required. Set the environment variables PGPOOL_POSTGRES_USERNAME and PGPOOL_POSTGRES_PASSWORD with the administrator's database credentials." fi if [[ -z "$PGPOOL_BACKEND_NODES" ]]; then print_validation_error "The list of backend nodes cannot be empty. Set the environment variable PGPOOL_BACKEND_NODES with a comma separated list of backend nodes." else read -r -a nodes <<<"$(tr ',;' ' ' <<<"${PGPOOL_BACKEND_NODES}")" for node in "${nodes[@]}"; do read -r -a fields <<<"$(tr ':' ' ' <<<"${node}")" if [[ -z "${fields[0]:-}" ]]; then print_validation_error "Error checking entry '$node', the field 'backend number' must be set!" fi if [[ -z "${fields[1]:-}" ]]; then print_validation_error "Error checking entry '$node', the field 'host' must be set!" fi done fi if is_boolean_yes "$PGPOOL_AUTO_FAILBACK"; then if [[ -z "$PGPOOL_BACKEND_APPLICATION_NAMES" ]]; then print_validation_error "The list of backend application names cannot be empty. Set the environment variable PGPOOL_BACKEND_APPLICATION_NAMES with a comma separated list of backend nodes." fi read -r -a app_name_list <<<"$(tr ',;' ' ' <<<"${PGPOOL_BACKEND_APPLICATION_NAMES}")" read -r -a nodes_list <<<"$(tr ',;' ' ' <<<"${PGPOOL_BACKEND_NODES}")" if [[ ${#app_name_list[@]} -ne ${#nodes_list[@]} ]]; then print_validation_error "PGPOOL_BACKEND_APPLICATION_NAMES and PGPOOL_BACKEND_NODES lists should have the same length" fi fi if [[ -n "$PGPOOL_USER_CONF_FILE" && ! -e "$PGPOOL_USER_CONF_FILE" ]]; then print_validation_error "The provided PGPOOL_USER_CONF_FILE: ${PGPOOL_USER_CONF_FILE} must exist." fi if [[ -n "$PGPOOL_USER_HBA_FILE" && ! -e "$PGPOOL_USER_HBA_FILE" ]]; then print_validation_error "The provided PGPOOL_USER_HBA_FILE: ${PGPOOL_USER_HBA_FILE} must exist." fi local yes_no_values=("PGPOOL_ENABLE_POOL_HBA" "PGPOOL_ENABLE_POOL_PASSWD" "PGPOOL_ENABLE_LOAD_BALANCING" "PGPOOL_ENABLE_STATEMENT_LOAD_BALANCING" "PGPOOL_ENABLE_CONNECTION_CACHE" "PGPOOL_ENABLE_LOG_CONNECTIONS" "PGPOOL_ENABLE_LOG_HOSTNAME" "PGPOOL_ENABLE_LOG_PCP_PROCESSES" "PGPOOL_ENABLE_LOG_PER_NODE_STATEMENT" "PGPOOL_AUTO_FAILBACK") for yn in "${yes_no_values[@]}"; do if ! is_yes_no_value "${!yn}"; then print_validation_error "The values allowed for $yn are: yes or no" fi done local positive_values=("PGPOOL_NUM_INIT_CHILDREN" "PGPOOL_MAX_POOL" "PGPOOL_CHILD_MAX_CONNECTIONS" "PGPOOL_CHILD_LIFE_TIME" "PGPOOL_CONNECTION_LIFE_TIME" "PGPOOL_CLIENT_IDLE_LIMIT" "PGPOOL_HEALTH_CHECK_PERIOD" "PGPOOL_HEALTH_CHECK_TIMEOUT" "PGPOOL_HEALTH_CHECK_MAX_RETRIES" "PGPOOL_HEALTH_CHECK_RETRY_DELAY" "PGPOOL_RESERVED_CONNECTIONS" "PGPOOL_CONNECT_TIMEOUT" "PGPOOL_HEALTH_CHECK_PSQL_TIMEOUT") for p in "${positive_values[@]}"; do if [[ -n "${!p:-}" ]]; then if ! is_positive_int "${!p}"; then print_validation_error "The values allowed for $p: integer greater than 0" fi fi done if ! [[ "$PGPOOL_DISABLE_LOAD_BALANCE_ON_WRITE" =~ ^(off|transaction|trans_transaction|always)$ ]]; then print_validation_error "The values allowed for PGPOOL_DISABLE_LOAD_BALANCE_ON_WRITE: off,transaction,trans_transaction,always" fi if ! is_yes_no_value "$PGPOOL_ENABLE_TLS"; then print_validation_error "The values allowed for PGPOOL_ENABLE_TLS are: yes or no" elif is_boolean_yes "$PGPOOL_ENABLE_TLS"; then # TLS Checks if [[ -z "$PGPOOL_TLS_CERT_FILE" ]]; then print_validation_error "You must provide a X.509 certificate in order to use TLS" elif [[ ! -f "$PGPOOL_TLS_CERT_FILE" ]]; then print_validation_error "The X.509 certificate file in the specified path ${PGPOOL_TLS_CERT_FILE} does not exist" fi if [[ -z "$PGPOOL_TLS_KEY_FILE" ]]; then print_validation_error "You must provide a private key in order to use TLS" elif [[ ! -f "$PGPOOL_TLS_KEY_FILE" ]]; then print_validation_error "The private key file in the specified path ${PGPOOL_TLS_KEY_FILE} does not exist" fi if [[ -z "$PGPOOL_TLS_CA_FILE" ]]; then warn "A CA X.509 certificate was not provided. Client verification will not be performed in TLS connections" elif [[ ! -f "$PGPOOL_TLS_CA_FILE" ]]; then print_validation_error "The CA X.509 certificate file in the specified path ${PGPOOL_TLS_CA_FILE} does not exist" fi if ! is_yes_no_value "$PGPOOL_TLS_PREFER_SERVER_CIPHERS"; then print_validation_error "The values allowed for PGPOOL_TLS_PREFER_SERVER_CIPHERS are: yes or no" fi fi # Check for Authentication method if ! [[ "$PGPOOL_AUTHENTICATION_METHOD" =~ ^(md5|scram-sha-256|trust)$ ]]; then print_validation_error "The values allowed for PGPOOL_AUTHENTICATION_METHOD: md5,scram-sha-256,trust" elif [[ "$PGPOOL_AUTHENTICATION_METHOD" = "trust" ]]; then warn "You set 'trust' as authentication method. For safety reasons, do not use this method in production environments." fi # Check for required environment variables for scram-sha-256 based authentication if [[ "$PGPOOL_AUTHENTICATION_METHOD" = "scram-sha-256" ]]; then # If scram-sha-256 is enabled, pg_pool_password cannot be disabled if ! is_boolean_yes "$PGPOOL_ENABLE_POOL_PASSWD"; then print_validation_error "PGPOOL_ENABLE_POOL_PASSWD cannot be disabled when PGPOOL_AUTHENTICATION_METHOD=scram-sha-256" fi fi # Custom users validations read -r -a custom_users_list <<<"$(tr ',;' ' ' <<<"${PGPOOL_POSTGRES_CUSTOM_USERS}")" read -r -a custom_passwords_list <<<"$(tr ',;' ' ' <<<"${PGPOOL_POSTGRES_CUSTOM_PASSWORDS}")" if [[ ${#custom_users_list[@]} -ne ${#custom_passwords_list[@]} ]]; then print_validation_error "PGPOOL_POSTGRES_CUSTOM_USERS and PGPOOL_POSTGRES_CUSTOM_PASSWORDS lists should have the same length" fi [[ "$error_code" -eq 0 ]] || exit "$error_code" } ######################## # Returns backend nodes info using PCP # Globals: # PGPOOL_* # Returns: # String with backend nodes info ######################### pgpool_nodes_info() { PCPPASSFILE=$(mktemp /tmp/pcppass-XXXXX) export PCPPASSFILE echo "localhost:9898:${PGPOOL_ADMIN_USERNAME}:${PGPOOL_ADMIN_PASSWORD}" >"${PCPPASSFILE}" pcp_node_info -h localhost -U "${PGPOOL_ADMIN_USERNAME}" -p 9898 -a -w 2> /dev/null rm -rf "$PCPPASSFILE" unset PCPPASSFILE } ######################## # Attach a backend node to Pgpool-II # Globals: # PGPOOL_* # Arguments: # $1 - node id # Returns: # None ######################### pgpool_attach_node() { local -r node_id=${1:?node id is missing} echo "Attaching backend node..." PCPPASSFILE=$(mktemp /tmp/pcppass-XXXXX) export PCPPASSFILE echo "localhost:9898:${PGPOOL_ADMIN_USERNAME}:${PGPOOL_ADMIN_PASSWORD}" >"${PCPPASSFILE}" pcp_attach_node -h localhost -U "${PGPOOL_ADMIN_USERNAME}" -p 9898 -n "${node_id}" -w rm -rf "$PCPPASSFILE" unset PCPPASSFILE } ######################## # Check Pgpool-II health and attached offline backends when they are online # Globals: # PGPOOL_* # Arguments: # None # Returns: # 0 when healthy # 1 when unhealthy ######################### pgpool_healthcheck() { local backends node_id node_host node_port # Check backend nodes if backends="$(pgpool_nodes_info)"; then # We're not interested in nodes marked as down|down backends="$(grep -v "down down" <<< "$backends")" # We should also check whether there are discrepancies between Pgpool-II and the actual primary node if grep -e "standby primary" -e "primary standby" <<< "$backends" > /dev/null; then echo "Found inconsistencies in pgpool_status" return 1 fi # Look up backends that are marked offline but being up read -r -a nodes_to_attach <<< "$(grep "down up" <<< "$backends" | awk '{print $1,$2}' | tr ' ' '|' | tr '\n' ' ')" for node in "${nodes_to_attach[@]}"; do node_host=$(echo "$node" | awk -F '|' '{print $1}') node_port=$(echo "$node" | awk -F '|' '{print $2}') node_id=$(grep "$node_host" "$PGPOOL_CONF_FILE" | awk '{print $1}' | sed 's/^backend_hostname//') if [[ $(PGCONNECT_TIMEOUT=3 PGPASSWORD="${PGPOOL_POSTGRES_PASSWORD}" psql -U "${PGPOOL_POSTGRES_USERNAME}" \ -d postgres -h "${node_host}" -p "${node_port}" -tA -c "SELECT 1" || true) == 1 ]]; then # Attach backend if it has come back online pgpool_attach_node "$node_id" fi done else echo "unable to list pool nodes" return 1 fi # Check if Pgpool-II responds to simple queries PGCONNECT_TIMEOUT=$PGPOOL_HEALTH_CHECK_PSQL_TIMEOUT PGPASSWORD="$PGPOOL_POSTGRES_PASSWORD" \ psql -U "$PGPOOL_POSTGRES_USERNAME" -d postgres -h "$PGPOOL_TMP_DIR" -p "$PGPOOL_PORT_NUMBER" \ -tA -c "SELECT 1" 2> /dev/null } ######################## # Create basic pg_hba.conf file # Globals: # PGPOOL_* # Arguments: # None # Returns: # None ######################### pgpool_create_pghba() { local all_authentication="$PGPOOL_AUTHENTICATION_METHOD" is_boolean_yes "$PGPOOL_ENABLE_LDAP" && all_authentication="pam pamservice=pgpool" local postgres_authentication="scram-sha-256" # We avoid using 'trust' for the postgres user even if PGPOOL_AUTHENTICATION_METHOD is set to 'trust' [[ "$PGPOOL_AUTHENTICATION_METHOD" = "md5" ]] && postgres_authentication="md5" info "Generating pg_hba.conf file..." local postgres_auth_line="" if is_boolean_yes "$PGPOOL_ENABLE_POOL_PASSWD"; then postgres_auth_line="host all ${PGPOOL_POSTGRES_USERNAME} all ${postgres_authentication}" fi local sr_check_auth_line="" if [[ -n "$PGPOOL_SR_CHECK_USER" ]]; then sr_check_auth_line="host all ${PGPOOL_SR_CHECK_USER} all ${postgres_authentication}" fi cat >"$PGPOOL_PGHBA_FILE" <>"$PGPOOL_PGHBA_FILE" <>"$PGPOOL_PGHBA_FILE" <>"$PGPOOL_CONF_FILE" <>"$PGPOOL_CONF_FILE" <>> Failover - that will initialize new primary node search!\"" pgpool_set_property "failover_on_backend_error" "$PGPOOL_FAILOVER_ON_BACKEND_ERROR" pgpool_set_property "failover_on_backend_shutdown" "$PGPOOL_FAILOVER_ON_BACKEND_SHUTDOWN" # Keeps searching for a primary node forever when a failover occurs pgpool_set_property "search_primary_node_timeout" "0" pgpool_set_property "disable_load_balance_on_write" "$PGPOOL_DISABLE_LOAD_BALANCE_ON_WRITE" # SSL settings # https://www.pgpool.net/docs/latest/en/html/runtime-ssl.html if is_boolean_yes "$PGPOOL_ENABLE_TLS"; then chmod 600 "$PGPOOL_TLS_KEY_FILE" || warn "Could not set compulsory permissions (600) on file ${PGPOOL_TLS_KEY_FILE}" pgpool_set_property "ssl" "on" # Server ciphers are preferred by default ! is_boolean_yes "$PGPOOL_TLS_PREFER_SERVER_CIPHERS" && pgpool_set_property "ssl_prefer_server_ciphers" "off" [[ -n $PGPOOL_TLS_CA_FILE ]] && pgpool_set_property "ssl_ca_cert" "$PGPOOL_TLS_CA_FILE" pgpool_set_property "ssl_cert" "$PGPOOL_TLS_CERT_FILE" pgpool_set_property "ssl_key" "$PGPOOL_TLS_KEY_FILE" fi # Backend settings read -r -a nodes <<<"$(tr ',;' ' ' <<<"${PGPOOL_BACKEND_NODES}")" if is_boolean_yes "$PGPOOL_AUTO_FAILBACK"; then pgpool_set_property "auto_failback" "on" read -r -a app_name <<<"$(tr ',;' ' ' <<<"${PGPOOL_BACKEND_APPLICATION_NAMES}")" fi for node in "${nodes[@]}"; do pgpool_create_backend_config "$node" "$(is_boolean_yes "$PGPOOL_AUTO_FAILBACK" && echo "${app_name[i]}")" ((i += 1)) done if [[ -f "$PGPOOL_USER_CONF_FILE" ]]; then info "Custom configuration '$PGPOOL_USER_CONF_FILE' detected!. Adding it to the configuration file." cat "$PGPOOL_USER_CONF_FILE" >>"$PGPOOL_CONF_FILE" fi if [[ -f "$PGPOOL_USER_HBA_FILE" ]]; then info "Custom configuration '$PGPOOL_USER_HBA_FILE' detected!. Overwriting the generated hba file." cat "$PGPOOL_USER_HBA_FILE" >"$PGPOOL_PGHBA_FILE" fi } ######################## # Execute postgresql encrypt command # Globals: # PGPOOL_* # Arguments: # $@ - Command to execute # Returns: # String ######################### pgpool_encrypt_execute() { local -a password_encryption_cmd=("pg_md5") # If authentication method for 'all' users is 'trust', we still use # pg_enc to generate encrypted passwords for 'postgres' and 'sr_check' users if [[ "$PGPOOL_AUTHENTICATION_METHOD" =~ ^(scram-sha-256|trust)$ ]]; then if is_file_writable "$PGPOOLKEYFILE"; then # Creating a PGPOOLKEYFILE as it is writeable echo "$PGPOOL_AES_KEY" > "$PGPOOLKEYFILE" # Fix permissions for PGPOOLKEYFILE chmod 0600 "$PGPOOLKEYFILE" fi password_encryption_cmd=("pg_enc" "--key-file=${PGPOOLKEYFILE}") fi "${password_encryption_cmd[@]}" "$@" } ######################## # Generates a password file for local authentication # Globals: # PGPOOL_* # Arguments: # None # Returns: # None ######################### pgpool_generate_password_file() { if is_boolean_yes "$PGPOOL_ENABLE_POOL_PASSWD"; then info "Generating password file for local authentication..." debug_execute pgpool_encrypt_execute -m --config-file="$PGPOOL_CONF_FILE" -u "$PGPOOL_POSTGRES_USERNAME" "$PGPOOL_POSTGRES_PASSWORD" if [[ -n "$PGPOOL_SR_CHECK_USER" ]]; then debug_execute pgpool_encrypt_execute -m --config-file="$PGPOOL_CONF_FILE" -u "$PGPOOL_SR_CHECK_USER" "$PGPOOL_SR_CHECK_PASSWORD" fi if [[ -n "${PGPOOL_POSTGRES_CUSTOM_USERS}" ]]; then read -r -a custom_users_list <<<"$(tr ',;' ' ' <<<"${PGPOOL_POSTGRES_CUSTOM_USERS}")" read -r -a custom_passwords_list <<<"$(tr ',;' ' ' <<<"${PGPOOL_POSTGRES_CUSTOM_PASSWORDS}")" local index=0 for user in "${custom_users_list[@]}"; do debug_execute pgpool_encrypt_execute -m --config-file="$PGPOOL_CONF_FILE" -u "$user" "${custom_passwords_list[$index]}" ((index += 1)) done fi else info "Skip generating password file due to PGPOOL_ENABLE_POOL_PASSWD = no" fi } ######################## # Encrypts a password # Globals: # PGPOOL_* # Arguments: # $1 - password # Returns: # String ######################### pgpool_encrypt_password() { local -r password="${1:?missing password}" if [[ "$PGPOOL_AUTHENTICATION_METHOD" =~ ^(scram-sha-256|trust)$ ]]; then pgpool_encrypt_execute "$password" | grep -o -E "AES.+" | tr -d '\n' else pgpool_encrypt_execute "$password" | tr -d '\n' fi } ######################## # Run custom initialization scripts # Globals: # PGPOOL_* # Arguments: # None # Returns: # None ######################### pgpool_custom_init_scripts() { if [[ -n $(find "$PGPOOL_INITSCRIPTS_DIR/" -type f -name "*.sh") ]]; then info "Loading user's custom files from $PGPOOL_INITSCRIPTS_DIR ..." find "$PGPOOL_INITSCRIPTS_DIR/" -type f -name "*.sh" | sort | while read -r f; do if [[ -x "$f" ]]; then debug "Executing $f" "$f" else debug "Sourcing $f" . "$f" fi done fi } ######################## # Generate a password file for pgpool admin user # Globals: # PGPOOL_* # Arguments: # None # Returns: # None ######################### pgpool_generate_admin_password_file() { info "Generating password file for pgpool admin user..." local passwd passwd=$(pg_md5 "$PGPOOL_ADMIN_PASSWORD") cat >>"$PGPOOL_PCP_CONF_FILE" <