LoginSignup
1
0

【Elasticsearch】ユーザーを追加してみる

Last updated at Posted at 2023-05-12

環境

以下を参考に環境構築を行う。
【Elasticsearch】DockerでElasticsearchを使えるようにする

ユーザーについて

自身で作成するユーザーの他に、ElasticStackではデプロイメント作成時にデフォルトで(既存で作成されている)ユーザーがいくつか存在します。
Built-in usersに詳細は纏められています。
それぞれの用途について別の機会に纏めます。

curlでのユーザー追加

下記のコマンドを利用してjacknichという名前のユーザーを作成します。

curl -u 'user:password' -X POST "localhost:9200/_security/user/jacknich?pretty" -H 'Content-Type: application/json' -d'
{
  "password" : "l0ng-r4nd0m-p@ssw0rd",
  "roles" : [ "admin", "other_role1" ],
  "full_name" : "Jack Nicholson",
  "email" : "jacknich@example.com",
  "metadata" : {
    "intelligence" : 7
  }
}
'
{
  "created" : true
}

確認方法は下記のコマンドを実行します。
?prettyはレスポンスを見やすく整形するためにつけてます。

curl -u 'user:password' GET 'localhost:9200/_security/user/jacknich?pretty'
curl: (6) Could not resolve host: GET
{
  "jacknich" : {
    "username" : "jacknich",
    "roles" : [
      "admin",
      "other_role1"
    ],
    "full_name" : "Jack Nicholson",
    "email" : "jacknich@example.com",
    "metadata" : {
      "intelligence" : 7
    },
    "enabled" : true
  }
}

Kibanaでのユーザー追加(Kibana→Dev Tools→Console)

KibanaのConsoleにて下記を実行する。

POST /_security/user/jacknich
{
  "password" : "l0ng-r4nd0m-p@ssw0rd",
  "roles" : [ "admin", "other_role1" ],
  "full_name" : "Jack Nicholson",
  "email" : "jacknich@example.com",
  "metadata" : {
    "intelligence" : 7
  }
}

実行して問題なければ下記のようなレスポンスが返ってくる

{
  "created": true
}

確認方法は下記を実行する

GET /_security/user/jacknich

下記のようなレスポンスが返ってくる

{
  "jacknich": {
    "username": "jacknich",
    "roles": [
      "admin",
      "other_role1"
    ],
    "full_name": "Jack Nicholson",
    "email": "jacknich@example.com",
    "metadata": {
      "intelligence": 7
    },
    "enabled": true
  }
}

【おまけ】更新する場合

curl、Consoleどちらも同じ対応となります。
具体的には、同じコマンドで更新箇所だけを変更して叩きます。
例えば下記のユーザーを作ります。

POST /_security/user/jacknich
{
  "password" : "l0ng-r4nd0m-p@ssw0rd",
  "roles" : [ "admin", "other_role1" ],
  "full_name" : "Jack Nicholson",
  "email" : "jacknich@example.com",
  "metadata" : {
    "intelligence" : 7
  }
}

やっぱりother_role1の権限は不要だとなった場合には下記を実行する

POST /_security/user/jacknich
{
  "password" : "l0ng-r4nd0m-p@ssw0rd",
  "roles" : [ "admin" ],
  "full_name" : "Jack Nicholson",
  "email" : "jacknich@example.com",
  "metadata" : {
    "intelligence" : 7
  }
}

レスポンスにother_role1が無いことを確認

{
  "jacknich": {
    "username": "jacknich",
    "roles": [
      "admin"
    ],
    "full_name": "Jack Nicholson",
    "email": "jacknich@example.com",
    "metadata": {
      "intelligence": 7
    },
    "enabled": true
  }
}

参考

Create or update users API
Get users API
Enable users API
Disable users API

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0