Setup-passwords command fails due to connection failure

The elasticsearch-setup-passwords command sets passwords for the built-in users by sending user management API requests. If your cluster uses SSL/TLS for the HTTP (REST) interface, the command attempts to establish a connection with the HTTPS protocol. If the connection attempt fails, the command fails.

Symptoms:

  1. Elasticsearch is running HTTPS, but the command fails to detect it and returns the following errors:

    1. Cannot connect to elasticsearch node.
    2. java.net.SocketException: Unexpected end of file from server
    3. ...
    4. ERROR: Failed to connect to elasticsearch at
    5. http://127.0.0.1:9200/_security/_authenticate?pretty.
    6. Is the URL correct and elasticsearch running?
  2. SSL/TLS is configured, but trust cannot be established. The command returns the following errors:

    1. SSL connection to
    2. https://127.0.0.1:9200/_security/_authenticate?pretty
    3. failed: sun.security.validator.ValidatorException:
    4. PKIX path building failed:
    5. sun.security.provider.certpath.SunCertPathBuilderException:
    6. unable to find valid certification path to requested target
    7. Please check the elasticsearch SSL settings under
    8. xpack.security.http.ssl.
    9. ...
    10. ERROR: Failed to establish SSL connection to elasticsearch at
    11. https://127.0.0.1:9200/_security/_authenticate?pretty.
  3. The command fails because hostname verification fails, which results in the following errors:

    1. SSL connection to
    2. https://idp.localhost.test:9200/_security/_authenticate?pretty
    3. failed: java.security.cert.CertificateException:
    4. No subject alternative DNS name matching
    5. elasticsearch.example.com found.
    6. Please check the elasticsearch SSL settings under
    7. xpack.security.http.ssl.
    8. ...
    9. ERROR: Failed to establish SSL connection to elasticsearch at
    10. https://elasticsearch.example.com:9200/_security/_authenticate?pretty.

Resolution:

  1. If your cluster uses TLS/SSL for the HTTP interface but the elasticsearch-setup-passwords command attempts to establish a non-secure connection, use the --url command option to explicitly specify an HTTPS URL. Alternatively, set the xpack.security.http.ssl.enabled setting to true.
  2. If the command does not trust the Elasticsearch server, verify that you configured the xpack.security.http.ssl.certificate_authorities setting or the xpack.security.http.ssl.truststore.path setting.
  3. If hostname verification fails, you can disable this verification by setting xpack.security.http.ssl.verification_mode to certificate.

For more information about these settings, see Security Settings in Elasticsearch.