self.ent_search_es_config

in lib/app/config.rb [70:130]


  def self.ent_search_es_config
    ent_search_config_path = ENV['ENT_SEARCH_CONFIG_PATH']
    unless ent_search_config_path
      Utility::Logger.info('ENT_SEARCH_CONFIG_PATH is not found, use connector service config.')
      return nil
    end

    Utility::Logger.info("Found ENT_SEARCH_CONFIG_PATH, loading ent-search config from #{ent_search_config_path}")
    ent_search_config = begin
      YAML.load_file(ent_search_config_path)
    rescue StandardError => e
      Utility::Logger.error("Failed to load ent-search config #{ent_search_config_path}: #{e.message}")
      return nil
    end

    unless ent_search_config.is_a?(Hash)
      Utility::Logger.error("Invalid ent-search config: #{ent_search_config.inspect}")
      return nil
    end

    host = ent_search_config['elasticsearch.host'] || ent_search_config.dig('elasticsearch', 'host')
    username = ent_search_config['elasticsearch.username'] || ent_search_config.dig('elasticsearch', 'username')
    password = ent_search_config['elasticsearch.password'] || ent_search_config.dig('elasticsearch', 'password')

    missing_fields = []
    missing_fields << 'elasticsearch.host' unless host
    missing_fields << 'elasticsearch.username' unless username
    missing_fields << 'elasticsearch.password' unless password
    if missing_fields.any?
      Utility::Logger.error("Incomplete elasticsearch config, missing #{missing_fields.join(', ')}")
      return nil
    end

    uri = begin
      URI.parse(host)
    rescue URI::InvalidURIError => e
      Utility::Logger.error("Failed to parse elasticsearch host #{host}: #{e.message}")
      return nil
    end

    unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
      Utility::Logger.error("Invalid elasticsearch host #{host}, it must be a http or https URI.")
      return nil
    end

    headers = ent_search_config['elasticsearch.headers'] || ent_search_config.dig('elasticsearch', 'headers')

    {
      :hosts => [
        {
          scheme: uri.scheme,
          user: username,
          password: password,
          host: uri.host,
          port: uri.port
        }
      ],
      :headers => headers
    }
  end