はじめに
Elasticsearchがversion8になってからcurlコマンドが実行できなくなったことはありませんか?
$ curl -s http://localhost:9200/
実行結果
{
   "error":{
      "root_cause":[
         {
            "type":"security_exception",
            "reason":"missing authentication credentials for REST request [/]",
            "header":{
               "WWW-Authenticate":[
                  "Basic realm=\"security\" charset=\"UTF-8\"",
                  "ApiKey"
               ]
            }
         }
      ],
      "type":"security_exception",
      "reason":"missing authentication credentials for REST request [/]",
      "header":{
         "WWW-Authenticate":[
            "Basic realm=\"security\" charset=\"UTF-8\"",
            "ApiKey"
         ]
      }
   },
   "status":401
}
今回はそれの解決方法です.
検証環境
- Ubuntu 22.04.2 LTS
- 
lsb_release -a1 
 - 
 - Elasticsearch 8.10.3
 - curl 7.81.0-1ubuntu1.14
- 
apt list --installed | grep curl2 
 - 
 - Python 3.10.12
python3 -V
 
解決方法
解決方法はElasticsearchのユーザー名とパスワードを入力することです.
下にElasticsearchのパスワードが分からない人向けのパスワードのリセットの方法を載せますね3.
Elasticsearchのパスワードのリセット
Elasticsearchのパスワードをリセットするコマンド
$ sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
実行結果
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y
Password for the [elastic] user successfully reset.
New value: <新しいパスワード>
$ export ELASTIC_PASSWORD="<新しいパスワード>"
注意:exportはshellから抜けると代入した変数が消えます.
確認
curlを用いて疎通確認をする.
$ curl -u elastic:$ELASTIC_PASSWORD -s http://localhost:9200/
実行結果
{
  "name" : "<ホスト名>",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "<クラスターのID>",
  "version" : {
    "number" : "8.10.3",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "c63272efed16b5a1c25f3ce500715b7fddf9a9fb",
    "build_date" : "2023-10-05T10:15:55.152563867Z",
    "build_snapshot" : false,
    "lucene_version" : "9.7.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}
これでElasticsearchのAPIをcurlコマンドで使うことができます.
Pythonで確認(おまけ)
curlでやったことと同様のことをPythonでやってみます.requestsモジュールでBasic認証をしています4.
elastic_requests.py
import requests
from requests.auth import HTTPBasicAuth
import json
elastic_url = 'http://localhost:9200/'
# Basic Authentication
# https://requests.readthedocs.io/en/latest/user/authentication/
basic = HTTPBasicAuth('elastic', '<新しいパスワード>')
headers = {
    'Content-Type': 'application/json',
}
response = requests.get(elastic_url, auth=basic, headers=headers)
print(json.dumps(response.json(), indent=4))
$ python3 elastic_requests.py
実行するとcurlと同じ結果が得られます.
実行結果
{
  "name" : "<ホスト名>",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "<クラスターのID>",
  "version" : {
    "number" : "8.10.3",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "c63272efed16b5a1c25f3ce500715b7fddf9a9fb",
    "build_date" : "2023-10-05T10:15:55.152563867Z",
    "build_snapshot" : false,
    "lucene_version" : "9.7.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}