Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to solve the problem if es password contains @ #77

Open
aishuhaon opened this issue Aug 7, 2020 · 5 comments
Open

How to solve the problem if es password contains @ #77

aishuhaon opened this issue Aug 7, 2020 · 5 comments

Comments

@aishuhaon
Copy link

aishuhaon commented Aug 7, 2020

How to solve the problem if es password contains @

  • ex:elastic:abc@123@127.0.0.1:9200
  • elastic version : 7.7.0
@yoesoff
Copy link

yoesoff commented Oct 21, 2020

I think you can just url_encode it using https://www.urlencoder.org/

@18855441015
Copy link

I set the url property like “http://${ELASTIC_NAME}:${ELASTIC_PASSWORD}@${ELASTIC_URL}/_bulk”,used System's environment variable to configure es's parameters Dynamically,my password contains '@' too, how to deal with this kind of problem,I would appreciate it if you could answer。

@frdrolland
Copy link

Hi,

To fix that I made an URL encoding as yoesoff said, but there was still an issue because Elasticsearch rejects the authentication with 401 error.

Reason is that in class com.internetitem.logback.elasticsearch.config.BasicAuthentication, login/password is read from URL and passed to basic auth header without decoding the password.

So my solution was to implement a custom BasicAuthentication class to decode the password before encoding it in base64 and putting it in Authorization header.
This custom BasicAuthentication class can be changed in logback configuration, by changing value of :
<authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" /><authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" />

@leopold7
Copy link

Thx to frdrolland. This is my code and it works for me.

  1. Encoding your password as yoesoff said, then your url like elastic:abc%40123@127.0.0.1:9200
  2. Changing your logback configuration such as <authentication class="com.xxx.config.BasicAuthentication" />
  3. Decoding your password and encoding it
package com.xxx.config;

import com.internetitem.logback.elasticsearch.config.Authentication;
import com.internetitem.logback.elasticsearch.util.Base64;
import org.yaml.snakeyaml.util.UriEncoder;

import java.net.HttpURLConnection;

public class BasicAuthentication implements Authentication {
    public BasicAuthentication() {
    }

    public void addAuth(HttpURLConnection urlConnection, String body) {
        String userInfo = urlConnection.getURL().getUserInfo();
        if (userInfo != null) {
            String basicAuth = "Basic " + Base64.encode(UriEncoder.decode(userInfo).getBytes());
            urlConnection.setRequestProperty("Authorization", basicAuth);
        }
    }
}

There you go ~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants