<commit_before>#!/bin/bash

function taito::expose_db_ssl_credentials () {
  taito::expose_secret_by_name "${database_instance}-db-ssl.ca"
  database_ssl_ca_path="${secret_value#secret_file:}"
  taito::expose_secret_by_name "${database_instance}-db-ssl.cert"
  database_ssl_cert_path="${secret_value#secret_file:}"
  taito::expose_secret_by_name "${database_instance}-db-ssl.key"
  database_ssl_key_path="${secret_value#secret_file:}"
}
export -f taito::expose_db_ssl_credentials

function taito::expose_db_user_credentials () {
  local print_creds=${1:-false}

  database_app_username="${database_app_username:-${database_name}_app}"
  find_secret_name="db.${database_name}.app"
  # TODO remove if
  if [[ ${taito_version:-} -ge "1" ]]; then
    find_secret_name="${database_app_secret:-${database_name}-db-app.password}"
  fi
  taito::expose_secret_by_name "${find_secret_name}"
  database_app_password="${secret_value}"
  database_app_password_changed="${secret_changed}"

  database_build_username="${database_mgr_username:-${database_name}}"
  find_secret_name="db.${database_name}.build"
  # TODO remove if
  if [[ ${taito_version:-} -ge "1" ]]; then
    find_secret_name="${database_mgr_secret:-${database_name}-db-mgr.password}"
  fi
  taito::expose_secret_by_name "${find_secret_name}"
  database_build_password="${secret_value}"
  database_build_password_changed="${secret_changed}"

  if [[ ${database_viewer_username} ]] && [[ ${database_viewer_secret:-} ]]; then
    database_viewer_username="${database_viewer_username}"
    find_secret_name="${database_viewer_secret}"
    taito::expose_secret_by_name "${find_secret_name}"
    database_viewer_password="${secret_value}"
    database_viewer_password_changed="${secret_changed}"
  fi

  if [[ ${database_default_username} ]] && [[ ${database_default_secret:-} ]]; then
    database_default_username="${database_default_username}"
    find_secret_name="${database_default_secret}"
    taito::expose_secret_by_name "${find_secret_name}"
    database_default_password="${secret_value}"
    # database_default_password_changed="${secret_changed}"
  fi

  if [[ ${taito_target_env} != "local" ]] && \
     [[ ! $database_app_password ]] && \
     [[ ! $database_build_password ]] && \
     [[ ! $database_viewer_password ]] && \
     [[ ! $database_default_password ]] && \
     [[ ${taito_quiet:-} != true ]] && \
     [[ ${print_creds:-} != true ]]; then
    echo -e "${NOTEs}" > /dev/stderr
    echo "WARNING: Failed to determine database passwords. If you have not been" > /dev/stderr
    echo "authenticated, run taito 'auth:${taito_target_env}' and try again." > /dev/stderr
    echo -e "${NOTEe}" > /dev/stderr
  fi

  if [[ ${print_creds:-} == true ]]; then
    echo "app_username=${database_app_username}"
    echo "app_password=${database_app_password}"
    echo "app_password_changed=${database_app_password_changed}"
    echo "mgr_username=${database_build_username}"
    echo "mgr_password=${database_build_password}"
    echo "mgr_password_changed=${database_build_password_changed}"
    echo "viewer_username=${database_viewer_username}"
    echo "viewer_password=${database_viewer_password}"
    echo "viewer_password_changed=${database_viewer_password_changed}"
    echo "default_username=${database_default_username}"
    echo "default_password=${database_default_password}"
    # echo "default_password_changed=${database_default_password_changed}"
  fi
}
export -f taito::expose_db_user_credentials

function taito::get_secret_value_format () {
  if [[ $1 == "random"* ]] ||
     [[ $1 == "manual"* ]] ||
     [[ ! $1 ]]
  then
    echo "literal"
  else
    echo "file"
  fi
}
export -f taito::get_secret_value_format

function taito::get_secret_hint () {
  local secret_name=$1

  # Filter taito_secret_hints the ones that match with secret name
  while IFS='*' read -ra items; do
    local trimmed=$(echo "${items[@]}" | awk '{$1=$1;print}')
    local name="${trimmed%=*}"
    local text="${trimmed##*=}"
    if [[ -n ${name} ]] && [[ ${secret_name} == *"${name}"* ]]; then
      echo "${text}"
    fi
  done <<< "${taito_global_secret_hints:-} ${taito_secret_hints:-}"
}
export -f taito::get_secret_hint

# Reads secret info to environment variables. The secret in question is
# determined by the given ${secret_index}"
function taito::expose_secret_by_index () {
  local secret_index=${1}

  # TODO: refactor
  secret_namespace_var="secret_namespace_${secret_index}"
  secret_namespace=${!secret_namespace_var}

  secret_name_var="secret_name_${secret_index}"
  secret_name=${!secret_name_var}

  secret_value_var="secret_value_${secret_index}"
  secret_value=${!secret_value_var}
  secret_value_var2="secret_value_${secret_name//[-.]/_}"

  secret_default_value_var="default_secret_value_${secret_index}"
  secret_default_value=${!secret_default_value_var}

  secret_method_var="secret_method_${secret_index}"
  secret_method=${!secret_method_var}
  secret_method_var2="secret_method_${secret_name//[-.]/_}"

  secret_orig_method_var="secret_orig_method_${secret_index}"
  secret_orig_method=${!secret_orig_method_var}
  secret_orig_method_var2="secret_orig_method_${secret_name//[-.]/_}"
  secret_orig_method=${secret_orig_method:-$secret_method}

  secret_value_format=$(taito::get_secret_value_format "${secret_method}")
  if [[ ${secret_value_format} == "file" ]]; then
    # if [[ ${secret_value} == "secret_file:"* ]]; then
    secret_filename=${secret_value#secret_file:}
  else
    secret_filename=""
  fi

  secret_changed_var="secret_changed_${secret_index}"
  secret_changed=${!secret_changed_var}

  if [[ ${secret_orig_method} == *"/"* ]]; then
    secret_source_namespace="${secret_orig_method##*/}"
  else
    secret_source_namespace="${secret_namespace}"
  fi
}
export -f taito::expose_secret_by_index

function taito::expose_secret_by_name () {
  local find_secret_name=${1}
  local print_creds=${2:-false}

  local found_index=-1
  local secret_index=0
  local secret_names=(${taito_secret_names})
  for secret_name in "${secret_names[@]}"
  do
    taito::expose_secret_by_index ${secret_index}
    if [[ ${secret_name} == "${find_secret_name}" ]]; then
      found_index=${secret_index}
      break
    fi
    secret_index=$((${secret_index}+1))
  done

  if [[ ${found_index} != "-1" ]]; then
    secret_index=${found_index}
    taito::expose_secret_by_index ${secret_index}
  fi

  if [[ ${print_creds} == true ]]; then
    echo "TODO print secret details"
  fi
}<commit_msg>fix: clear secret value if not found<commit_after>#!/bin/bash

function taito::expose_db_ssl_credentials () {
  taito::expose_secret_by_name "${database_instance}-db-ssl.ca"
  database_ssl_ca_path="${secret_value#secret_file:}"
  taito::expose_secret_by_name "${database_instance}-db-ssl.cert"
  database_ssl_cert_path="${secret_value#secret_file:}"
  taito::expose_secret_by_name "${database_instance}-db-ssl.key"
  database_ssl_key_path="${secret_value#secret_file:}"
}
export -f taito::expose_db_ssl_credentials

function taito::expose_db_user_credentials () {
  local print_creds=${1:-false}

  database_app_username="${database_app_username:-${database_name}_app}"
  find_secret_name="db.${database_name}.app"
  # TODO remove if
  if [[ ${taito_version:-} -ge "1" ]]; then
    find_secret_name="${database_app_secret:-${database_name}-db-app.password}"
  fi
  taito::expose_secret_by_name "${find_secret_name}"
  database_app_password="${secret_value}"
  database_app_password_changed="${secret_changed}"

  database_build_username="${database_mgr_username:-${database_name}}"
  find_secret_name="db.${database_name}.build"
  # TODO remove if
  if [[ ${taito_version:-} -ge "1" ]]; then
    find_secret_name="${database_mgr_secret:-${database_name}-db-mgr.password}"
  fi
  taito::expose_secret_by_name "${find_secret_name}"
  database_build_password="${secret_value}"
  database_build_password_changed="${secret_changed}"

  if [[ ${database_viewer_username} ]] && [[ ${database_viewer_secret:-} ]]; then
    database_viewer_username="${database_viewer_username}"
    find_secret_name="${database_viewer_secret}"
    taito::expose_secret_by_name "${find_secret_name}"
    database_viewer_password="${secret_value}"
    database_viewer_password_changed="${secret_changed}"
  fi

  if [[ ${database_default_username} ]] && [[ ${database_default_secret:-} ]]; then
    database_default_username="${database_default_username}"
    find_secret_name="${database_default_secret}"
    taito::expose_secret_by_name "${find_secret_name}"
    database_default_password="${secret_value}"
    # database_default_password_changed="${secret_changed}"
  fi

  if [[ ${taito_target_env} != "local" ]] && \
     [[ ! $database_app_password ]] && \
     [[ ! $database_build_password ]] && \
     [[ ! $database_viewer_password ]] && \
     [[ ! $database_default_password ]] && \
     [[ ${taito_quiet:-} != true ]] && \
     [[ ${print_creds:-} != true ]]; then
    echo -e "${NOTEs}" > /dev/stderr
    echo "WARNING: Failed to determine database passwords. If you have not been" > /dev/stderr
    echo "authenticated, run taito 'auth:${taito_target_env}' and try again." > /dev/stderr
    echo -e "${NOTEe}" > /dev/stderr
  fi

  if [[ ${print_creds:-} == true ]]; then
    echo "app_username=${database_app_username}"
    echo "app_password=${<PASSWORD>}"
    echo "app_password_changed=${database_app_password_changed}"
    echo "mgr_username=${database_build_username}"
    echo "mgr_password=${<PASSWORD>}"
    echo "mgr_password_changed=${database_build_password_changed}"
    echo "viewer_username=${database_viewer_username}"
    echo "viewer_password=${<PASSWORD>}"
    echo "viewer_password_changed=${database_viewer_password_changed}"
    echo "default_username=${database_default_username}"
    echo "default_password=${<PASSWORD>}"
    # echo "default_password_changed=${database_default_password_changed}"
  fi
}
export -f taito::expose_db_user_credentials

function taito::get_secret_value_format () {
  if [[ $1 == "random"* ]] ||
     [[ $1 == "manual"* ]] ||
     [[ ! $1 ]]
  then
    echo "literal"
  else
    echo "file"
  fi
}
export -f taito::get_secret_value_format

function taito::get_secret_hint () {
  local secret_name=$1

  # Filter taito_secret_hints the ones that match with secret name
  while IFS='*' read -ra items; do
    local trimmed=$(echo "${items[@]}" | awk '{$1=$1;print}')
    local name="${trimmed%=*}"
    local text="${trimmed##*=}"
    if [[ -n ${name} ]] && [[ ${secret_name} == *"${name}"* ]]; then
      echo "${text}"
    fi
  done <<< "${taito_global_secret_hints:-} ${taito_secret_hints:-}"
}
export -f taito::get_secret_hint

# Reads secret info to environment variables. The secret in question is
# determined by the given ${secret_index}"
function taito::expose_secret_by_index () {
  local secret_index=${1}

  # TODO: refactor
  secret_namespace_var="secret_namespace_${secret_index}"
  secret_namespace=${!secret_namespace_var}

  secret_name_var="secret_name_${secret_index}"
  secret_name=${!secret_name_var}

  secret_value_var="secret_value_${secret_index}"
  secret_value=${!secret_value_var}
  secret_value_var2="secret_value_${secret_name//[-.]/_}"

  secret_default_value_var="default_secret_value_${secret_index}"
  secret_default_value=${!secret_default_value_var}

  secret_method_var="secret_method_${secret_index}"
  secret_method=${!secret_method_var}
  secret_method_var2="secret_method_${secret_name//[-.]/_}"

  secret_orig_method_var="secret_orig_method_${secret_index}"
  secret_orig_method=${!secret_orig_method_var}
  secret_orig_method_var2="secret_orig_method_${secret_name//[-.]/_}"
  secret_orig_method=${secret_orig_method:-$secret_method}

  secret_value_format=$(taito::get_secret_value_format "${secret_method}")
  if [[ ${secret_value_format} == "file" ]]; then
    # if [[ ${secret_value} == "secret_file:"* ]]; then
    secret_filename=${secret_value#secret_file:}
  else
    secret_filename=""
  fi

  secret_changed_var="secret_changed_${secret_index}"
  secret_changed=${!secret_changed_var}

  if [[ ${secret_orig_method} == *"/"* ]]; then
    secret_source_namespace="${secret_orig_method##*/}"
  else
    secret_source_namespace="${secret_namespace}"
  fi
}
export -f taito::expose_secret_by_index

function taito::expose_secret_by_name () {
  local find_secret_name=${1}
  local print_creds=${2:-false}

  local found_index=-1
  local secret_index=0
  local secret_names=(${taito_secret_names})
  for secret_name in "${secret_names[@]}"
  do
    taito::expose_secret_by_index ${secret_index}
    if [[ ${secret_name} == "${find_secret_name}" ]]; then
      found_index=${secret_index}
      break
    fi
    secret_index=$((${secret_index}+1))
  done

  secret_index=${found_index/-1/notfound}
  taito::expose_secret_by_index ${secret_index}

  if [[ ${print_creds} == true ]]; then
    echo "TODO print secret details"
  fi
}
{'fraction_non_alphanumeric': 0.1533390211742806, 'fraction_numerical': 0.002947335763592647, 'mean_word_length': 5.825833774483854, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4851964', 'n_tokens_mistral': 4386, 'n_tokens_neox': 4303, 'n_words': 639}