elastic / elasticsearch-rs

Official Elasticsearch Rust Client
https://www.elastic.co/guide/en/elasticsearch/client/rust-api/current/index.html
Apache License 2.0
695 stars 70 forks source link

Problem with SSL #229

Open FrederickFrance opened 9 months ago

FrederickFrance commented 9 months ago

Hi all,

I'm a newbie with Python and Elasticsearch. I'm trying to create a client from host, username and password.

With Python, the original code is:

import os
from elasticsearch import Elasticsearch

# create connector for local elastic store
def local_elastic():

    return Elasticsearch(
        hosts=[
            {
                "host": os.environ.get("ES_URL", "localhost"),
                "port": os.environ.get("ES_PORT", 9200),
            }
        ],
        timeout=600,
    )

# create connector with remote elastic store
def remote_elastic():

    es_username, es_password = os.environ.get("ES_USERNAME", None), os.environ.get(
        "ES_PASSWORD", None
    )
    hosts = [{"host": os.environ["ES_URL"], "port": os.environ.get("ES_PORT", 9200)}]
    if es_username and es_password:
        return Elasticsearch(
            hosts=hosts,
            http_auth=(es_username, es_password),
            use_ssl=True,
            verify_certs=False,
            ssl_show_warn=False,
            timeout=600,
        )
    return Elasticsearch(hosts=hosts, timeout=600)

For the moment, I transform it to:

use std::time::Duration;

use elasticsearch::{
    http::transport::{SingleNodeConnectionPool, TransportBuilder},
    Elasticsearch,
};
use url::Url;

pub fn local_elastic(host: String, port: u16) -> anyhow::Result<Elasticsearch> {
    Ok(Elasticsearch::new(
        TransportBuilder::new(SingleNodeConnectionPool::new(Url::parse(&format!(
            "{host}:{port}"
        ))?))
        .timeout(Duration::from_secs(600))
        .build()?,
    ))
}

pub fn remote_elastic(
    host: String,
    port: u16,
    username: Option<String>,
    password: Option<String>,
) -> anyhow::Result<Elasticsearch> {

    match (username, password) {
        (Some(u), Some(p)) => {
            let t = TransportBuilder::new(SingleNodeConnectionPool::new(Url::parse(&format!(
                "{host}:{port}"
            ))?));

            Ok(Elasticsearch::new(
                t.timeout(Duration::from_secs(600))
                    .auth(elasticsearch::auth::Credentials::Basic(u, p))
                    .build()?,
            ))
        }
        _ => local_elastic(host, port),
    }
}

Even with the documentation, I'm unable to understand how to adapt use_ssl=True and ssl_show_warn=False.

Could someone help me ?

Regards