#!/bin/bash
# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0
#
# Environment configuration for opensearch-dashboards

# The values for all environment variables will be set in the below order of precedence
# 1. Custom environment variables defined below after Bitnami defaults
# 2. Constants defined in this file (environment variables with no default), i.e. BITNAMI_ROOT_DIR
# 3. Environment variables overridden via external files using *_FILE variables (see below)
# 4. Environment variables set externally (i.e. current Bash context/Dockerfile/userdata)

# Load logging library
# shellcheck disable=SC1090,SC1091
. /opt/bitnami/scripts/liblog.sh

export BITNAMI_ROOT_DIR="/opt/bitnami"
export BITNAMI_VOLUME_DIR="/bitnami"

# Logging configuration
export MODULE="${MODULE:-opensearch-dashboards}"
export BITNAMI_DEBUG="${BITNAMI_DEBUG:-false}"

# By setting an environment variable matching *_FILE to a file path, the prefixed environment
# variable will be overridden with the value specified in that file
opensearch_dashboards_env_vars=(
    OPENSEARCH_DASHBOARDS_OPENSEARCH_URL
    OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER
    OPENSEARCH_DASHBOARDS_HOST
    OPENSEARCH_DASHBOARDS_PORT_NUMBER
    OPENSEARCH_DASHBOARDS_WAIT_READY_MAX_RETRIES
    OPENSEARCH_DASHBOARDS_INITSCRIPTS_START_SERVER
    OPENSEARCH_DASHBOARDS_FORCE_INITSCRIPTS
    OPENSEARCH_DASHBOARDS_DISABLE_STRICT_CSP
    OPENSEARCH_DASHBOARDS_CERTS_DIR
    OPENSEARCH_DASHBOARDS_SERVER_ENABLE_TLS
    OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_LOCATION
    OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_PASSWORD
    OPENSEARCH_DASHBOARDS_SERVER_TLS_USE_PEM
    OPENSEARCH_DASHBOARDS_SERVER_CERT_LOCATION
    OPENSEARCH_DASHBOARDS_SERVER_KEY_LOCATION
    OPENSEARCH_DASHBOARDS_SERVER_KEY_PASSWORD
    OPENSEARCH_DASHBOARDS_PASSWORD
    OPENSEARCH_DASHBOARDS_OPENSEARCH_ENABLE_TLS
    OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_VERIFICATION_MODE
    OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_LOCATION
    OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_PASSWORD
    OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_USE_PEM
    OPENSEARCH_DASHBOARDS_OPENSEARCH_CA_CERT_LOCATION
    OPENSEARCH_URL
    OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER
    OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT
    OPENSEARCH_DASHBOARDS_PORT_NUMBER
    OPENSEARCH_DASHBOARDS_INITSCRIPTS_MAX_RETRIES
)
for env_var in "${opensearch_dashboards_env_vars[@]}"; do
    file_env_var="${env_var}_FILE"
    if [[ -n "${!file_env_var:-}" ]]; then
        if [[ -r "${!file_env_var:-}" ]]; then
            export "${env_var}=$(< "${!file_env_var}")"
            unset "${file_env_var}"
        else
            warn "Skipping export of '${env_var}'. '${!file_env_var:-}' is not readable."
        fi
    fi
done
unset opensearch_dashboards_env_vars
export SERVER_FLAVOR="opensearch-dashboards"

# Paths
export BITNAMI_VOLUME_DIR="/bitnami"
export OPENSEARCH_DASHBOARDS_VOLUME_DIR="${BITNAMI_VOLUME_DIR}/opensearch-dashboards"
export SERVER_VOLUME_DIR="$OPENSEARCH_DASHBOARDS_VOLUME_DIR"
export OPENSEARCH_DASHBOARDS_BASE_DIR="${BITNAMI_ROOT_DIR}/opensearch-dashboards"
export SERVER_BASE_DIR="$OPENSEARCH_DASHBOARDS_BASE_DIR"
export OPENSEARCH_DASHBOARDS_CONF_DIR="${SERVER_BASE_DIR}/config"
export SERVER_CONF_DIR="$OPENSEARCH_DASHBOARDS_CONF_DIR"
export OPENSEARCH_DASHBOARDS_DEFAULT_CONF_DIR="${SERVER_BASE_DIR}/config.default"
export SERVER_DEFAULT_CONF_DIR="$OPENSEARCH_DASHBOARDS_DEFAULT_CONF_DIR"
export OPENSEARCH_DASHBOARDS_LOGS_DIR="${SERVER_BASE_DIR}/logs"
export SERVER_LOGS_DIR="$OPENSEARCH_DASHBOARDS_LOGS_DIR"
export OPENSEARCH_DASHBOARDS_TMP_DIR="${SERVER_BASE_DIR}/tmp"
export SERVER_TMP_DIR="$OPENSEARCH_DASHBOARDS_TMP_DIR"
export OPENSEARCH_DASHBOARDS_BIN_DIR="${SERVER_BASE_DIR}/bin"
export SERVER_BIN_DIR="$OPENSEARCH_DASHBOARDS_BIN_DIR"
export OPENSEARCH_DASHBOARDS_PLUGINS_DIR="${SERVER_BASE_DIR}/plugins"
export SERVER_PLUGINS_DIR="$OPENSEARCH_DASHBOARDS_PLUGINS_DIR"
export OPENSEARCH_DASHBOARDS_DEFAULT_PLUGINS_DIR="${SERVER_BASE_DIR}/plugins.default"
export SERVER_DEFAULT_PLUGINS_DIR="$OPENSEARCH_DASHBOARDS_DEFAULT_PLUGINS_DIR"
export OPENSEARCH_DASHBOARDS_DATA_DIR="${SERVER_VOLUME_DIR}/data"
export SERVER_DATA_DIR="$OPENSEARCH_DASHBOARDS_DATA_DIR"
export OPENSEARCH_DASHBOARDS_MOUNTED_CONF_DIR="${SERVER_VOLUME_DIR}/conf"
export SERVER_MOUNTED_CONF_DIR="$OPENSEARCH_DASHBOARDS_MOUNTED_CONF_DIR"
export OPENSEARCH_DASHBOARDS_CONF_FILE="${SERVER_CONF_DIR}/opensearch_dashboards.yml"
export SERVER_CONF_FILE="$OPENSEARCH_DASHBOARDS_CONF_FILE"
export OPENSEARCH_DASHBOARDS_LOG_FILE="${SERVER_LOGS_DIR}/opensearch-dashboards.log"
export SERVER_LOG_FILE="$OPENSEARCH_DASHBOARDS_LOG_FILE"
export OPENSEARCH_DASHBOARDS_PID_FILE="${SERVER_TMP_DIR}/opensearch-dashboards.pid"
export SERVER_PID_FILE="$OPENSEARCH_DASHBOARDS_PID_FILE"
export OPENSEARCH_DASHBOARDS_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export SERVER_INITSCRIPTS_DIR="$OPENSEARCH_DASHBOARDS_INITSCRIPTS_DIR"

# System users (when running with a privileged user)
export OPENSEARCH_DASHBOARDS_DAEMON_USER="opensearch-dashboards"
export SERVER_DAEMON_USER="$OPENSEARCH_DASHBOARDS_DAEMON_USER"
export OPENSEARCH_DASHBOARDS_DAEMON_GROUP="opensearch-dashboards"
export SERVER_DAEMON_GROUP="$OPENSEARCH_DASHBOARDS_DAEMON_GROUP"

# Opensearch Dashboards configuration
OPENSEARCH_DASHBOARDS_OPENSEARCH_URL="${OPENSEARCH_DASHBOARDS_OPENSEARCH_URL:-"${OPENSEARCH_URL:-}"}"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_URL="${OPENSEARCH_DASHBOARDS_OPENSEARCH_URL:-opensearch}"
export SERVER_DB_URL="$OPENSEARCH_DASHBOARDS_OPENSEARCH_URL"
OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER="${OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER:-"${OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER:-}"}"
OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER="${OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER:-"${OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT:-}"}"
OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER="${OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER:-"${OPENSEARCH_DASHBOARDS_PORT_NUMBER:-}"}"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER="${OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER:-9200}"
export SERVER_DB_PORT_NUMBER="$OPENSEARCH_DASHBOARDS_OPENSEARCH_PORT_NUMBER"
export OPENSEARCH_DASHBOARDS_HOST="${OPENSEARCH_DASHBOARDS_HOST:-0.0.0.0}"
export SERVER_HOST="$OPENSEARCH_DASHBOARDS_HOST"
export OPENSEARCH_DASHBOARDS_PORT_NUMBER="${OPENSEARCH_DASHBOARDS_PORT_NUMBER:-5601}"
export SERVER_PORT_NUMBER="$OPENSEARCH_DASHBOARDS_PORT_NUMBER"
OPENSEARCH_DASHBOARDS_WAIT_READY_MAX_RETRIES="${OPENSEARCH_DASHBOARDS_WAIT_READY_MAX_RETRIES:-"${OPENSEARCH_DASHBOARDS_INITSCRIPTS_MAX_RETRIES:-}"}"
export OPENSEARCH_DASHBOARDS_WAIT_READY_MAX_RETRIES="${OPENSEARCH_DASHBOARDS_WAIT_READY_MAX_RETRIES:-30}"
export SERVER_WAIT_READY_MAX_RETRIES="$OPENSEARCH_DASHBOARDS_WAIT_READY_MAX_RETRIES"
export OPENSEARCH_DASHBOARDS_INITSCRIPTS_START_SERVER="${OPENSEARCH_DASHBOARDS_INITSCRIPTS_START_SERVER:-yes}"
export SERVER_INITSCRIPTS_START_SERVER="$OPENSEARCH_DASHBOARDS_INITSCRIPTS_START_SERVER"
export OPENSEARCH_DASHBOARDS_FORCE_INITSCRIPTS="${OPENSEARCH_DASHBOARDS_FORCE_INITSCRIPTS:-no}"
export SERVER_FORCE_INITSCRIPTS="$OPENSEARCH_DASHBOARDS_FORCE_INITSCRIPTS"
export OPENSEARCH_DASHBOARDS_DISABLE_STRICT_CSP="${OPENSEARCH_DASHBOARDS_DISABLE_STRICT_CSP:-no}"
export SERVER_DISABLE_STRICT_CSP="$OPENSEARCH_DASHBOARDS_DISABLE_STRICT_CSP"

# Opensearch Dashboards server SSL/TLS configuration
export OPENSEARCH_DASHBOARDS_CERTS_DIR="${OPENSEARCH_DASHBOARDS_CERTS_DIR:-${SERVER_CONF_DIR}/certs}"
export SERVER_CERTS_DIR="$OPENSEARCH_DASHBOARDS_CERTS_DIR"
export OPENSEARCH_DASHBOARDS_SERVER_ENABLE_TLS="${OPENSEARCH_DASHBOARDS_SERVER_ENABLE_TLS:-false}"
export SERVER_ENABLE_TLS="$OPENSEARCH_DASHBOARDS_SERVER_ENABLE_TLS"
export OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_LOCATION="${OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_LOCATION:-${SERVER_CERTS_DIR}/server/opensearch-dashboards.keystore.p12}"
export SERVER_KEYSTORE_LOCATION="$OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_LOCATION"
export OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_PASSWORD="${OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_PASSWORD:-}"
export SERVER_KEYSTORE_PASSWORD="$OPENSEARCH_DASHBOARDS_SERVER_KEYSTORE_PASSWORD"
export OPENSEARCH_DASHBOARDS_SERVER_TLS_USE_PEM="${OPENSEARCH_DASHBOARDS_SERVER_TLS_USE_PEM:-false}"
export SERVER_TLS_USE_PEM="$OPENSEARCH_DASHBOARDS_SERVER_TLS_USE_PEM"
export OPENSEARCH_DASHBOARDS_SERVER_CERT_LOCATION="${OPENSEARCH_DASHBOARDS_SERVER_CERT_LOCATION:-${SERVER_CERTS_DIR}/server/tls.crt}"
export SERVER_CERT_LOCATION="$OPENSEARCH_DASHBOARDS_SERVER_CERT_LOCATION"
export OPENSEARCH_DASHBOARDS_SERVER_KEY_LOCATION="${OPENSEARCH_DASHBOARDS_SERVER_KEY_LOCATION:-${SERVER_CERTS_DIR}/server/tls.key}"
export SERVER_KEY_LOCATION="$OPENSEARCH_DASHBOARDS_SERVER_KEY_LOCATION"
export OPENSEARCH_DASHBOARDS_SERVER_KEY_PASSWORD="${OPENSEARCH_DASHBOARDS_SERVER_KEY_PASSWORD:-}"
export SERVER_KEY_PASSWORD="$OPENSEARCH_DASHBOARDS_SERVER_KEY_PASSWORD"

# Opensearch Security configuration
export OPENSEARCH_DASHBOARDS_PASSWORD="${OPENSEARCH_DASHBOARDS_PASSWORD:-}"
export SERVER_PASSWORD="$OPENSEARCH_DASHBOARDS_PASSWORD"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_ENABLE_TLS="${OPENSEARCH_DASHBOARDS_OPENSEARCH_ENABLE_TLS:-false}"
export SERVER_DB_ENABLE_TLS="$OPENSEARCH_DASHBOARDS_OPENSEARCH_ENABLE_TLS"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_VERIFICATION_MODE="${OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_VERIFICATION_MODE:-full}"
export SERVER_DB_TLS_VERIFICATION_MODE="$OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_VERIFICATION_MODE"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_LOCATION="${OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_LOCATION:-${SERVER_CERTS_DIR}/opensearch/opensearch.truststore.p12}"
export SERVER_DB_TRUSTSTORE_LOCATION="$OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_LOCATION"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_PASSWORD="${OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_PASSWORD:-}"
export SERVER_DB_TRUSTSTORE_PASSWORD="$OPENSEARCH_DASHBOARDS_OPENSEARCH_TRUSTSTORE_PASSWORD"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_USE_PEM="${OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_USE_PEM:-false}"
export SERVER_DB_TLS_USE_PEM="$OPENSEARCH_DASHBOARDS_OPENSEARCH_TLS_USE_PEM"
export OPENSEARCH_DASHBOARDS_OPENSEARCH_CA_CERT_LOCATION="${OPENSEARCH_DASHBOARDS_OPENSEARCH_CA_CERT_LOCATION:-${SERVER_CERTS_DIR}/opensearch/ca.crt}"
export SERVER_DB_CA_CERT_LOCATION="$OPENSEARCH_DASHBOARDS_OPENSEARCH_CA_CERT_LOCATION"

# Custom environment variables may be defined below
