Search code examples
apache-calcite

Where to set calcite elasticsearch username/password?


I am trying to use Apache Calcite to connect to ElasticSearch, and am running into problems setting the Username Password.

I have tried to config username/password with operand(based on JSON), with Properties(DriverManager.getConnection(String url, Properties config)) and with DriverManager.getConnection(String url, username, password), but have not been able to get any of them to work.

I have previously posted the question to Apache JIRA

My Code is as follows

Trying to set username password with properties

Properties config = new Properties();
config.put("lex", "JAVA");
String sql = "select * from index";

try (Connection con = DriverManager.getConnection("jdbc:calcite:model=src/main/resources/es.json", config)) {
    try (Statement stmt = con.createStatement()) {
        try (ResultSet rs = stmt.executeQuery(sql)) {
            printRs(rs);
        }
    }
}

Trying to set it with operand

{
  "version": "1.0",
  "defaultSchema": "elasticsearch",
  "schemas": [
    {
      "type": "custom",
      "name": "elasticsearch",
      "factory": "org.apache.calcite.adapter.elasticsearch.ElasticsearchSchemaFactory",
      "operand": {
        "coordinates": "{'192.168.133.104': 9200}",
        "jdbcUser": "elastic",
        "jdbcPassword": "elastic"
      }
    }
  ]
}

and throw Exception

{
    "error":{
        "root_cause":[
            {
                "type":"security_exception",
                "reason":"missing authentication token for REST request [/_alias]",
                "header":{
                    "WWW-Authenticate":"Basic realm="security" charset="UTF-8""
                }
            }
        ],
        "type":"security_exception",
        "reason":"missing authentication token for REST request [/_alias]",
        "header":{
            "WWW-Authenticate":"Basic realm="security" charset="UTF-8""
        }
    },
    "status":401
}

Solution

  • This was not possible at the time this question has asked, but this functionality has since been added and will be included in the next release of Calcite (1.26.0). Alongside the coordinates key in the configuration, you can add username and password to specify authentication parameters.