elastic / elasticsearch-rs

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

[BUG] There is no obvious way to support a reverse proxy #48

Closed bjorn-ove closed 4 years ago

bjorn-ove commented 4 years ago

Describe the bug When running towards elasticsearch behind a reverse proxy where the url is something like http://10.1.2.3/es/ I haven't found a way to use it. The reason for this appears to be the way Url::join works. When given a path that starts with / it removes everything after the domain and adds it to the root.

To Reproduce

    // The logging from reqwuest shows how the url is not as expected
    let client = Elasticsearch::new(Transport::single_node("http://10.1.2.3/es/")?);
    let search = client.search(SearchParts::Index(&["my-index"]));
    let res = search.send().await?;
    // [2020-01-14T10:25:31Z DEBUG reqwest::async_impl::response] Response: '200 OK' for https://10.1.2.3/my-index/_search

    // Digging further shows the issue is likely in how Url::join works
    let url = url::Url::parse("http://10.1.2.3/hello/").unwrap();
    assert_eq!(url.join("world").unwrap().as_str(), "http://10.1.2.3/hello/world");
    assert_eq!(url.join("/world").unwrap().as_str(), "http://10.1.2.3/world");

    // Note: The trailing backslash is important or it will be consithered a file name
    let url = url::Url::parse("http://10.1.2.3/hello/again").unwrap();
    assert_eq!(url.join("world").unwrap().as_str(), "http://10.1.2.3/hello/world");
    assert_eq!(url.join("/world").unwrap().as_str(), "http://10.1.2.3/world");

Expected behavior The url used should be https://10.1.2.3/es/my-index/_search

Environment (please complete the following information):

bjorn-ove commented 4 years ago

A work-around that works for my particular use-case right now is to add es/ as a prefix to the index. I doubt this will cut it when I extend my usage though.

russcam commented 4 years ago

Thanks for opening @BearOve. I’ll have a chance to properly take a look at this next week when I’m back, but it looks like the generated API urls should omit the leading forward slash for `url::join()1st behaviour to work as expected.

russcam commented 4 years ago

I've opened #50 to address