#!/usr/bin/env bash set -euo pipefail namespace="${1:?usage: $0 }" site="${2:?usage: $0 }" app_username="${3:?usage: $0 }" : "${AGENT_POSTGRES_ADMIN_PASSWORD:?set AGENT_POSTGRES_ADMIN_PASSWORD}" : "${AGENT_POSTGRES_APP_PASSWORD:?set AGENT_POSTGRES_APP_PASSWORD}" database_name="agent-db-${site}" postgres_host="agent-${site}-postgres-service" secret_name="agent-${site}-postgres-credentials" escaped_app_password=${AGENT_POSTGRES_APP_PASSWORD//\"/\"\"} connection_string="Host=${postgres_host};Port=5432;Username=${app_username};Password=\"${escaped_app_password}\";Database=${database_name}" credentials_directory=$(mktemp -d) trap 'rm -rf -- "${credentials_directory}"' EXIT chmod 700 "${credentials_directory}" printf '%s' "${AGENT_POSTGRES_ADMIN_PASSWORD}" > "${credentials_directory}/admin-password" printf '%s' "${AGENT_POSTGRES_APP_PASSWORD}" > "${credentials_directory}/app-password" printf '%s' "${connection_string}" > "${credentials_directory}/connection-string" kubectl --namespace "${namespace}" create secret generic "${secret_name}" \ --from-file=admin-password="${credentials_directory}/admin-password" \ --from-file=app-password="${credentials_directory}/app-password" \ --from-file=connection-string="${credentials_directory}/connection-string" \ --dry-run=client \ --output=yaml \ | kubectl apply --filename=- echo "Updated ${namespace}/${secret_name}; restart the Agent Deployment to consume changed values."