環境
以下を参考に環境構築を行う。
【Elasticsearch】DockerでElasticsearchを使えるようにする
ロールについて
ユーザーに対して適切な権限を割り当てて、ユーザーが必要なリソースのみにアクセスできるようにする。
簡単に言うと、できることをリストに纏めたもののようなイメージ。
リストはいろんなパターンでいくつも作る事ができる。
そのリストをユーザーに割り当てて、リストに書かれている操作のみが実行できるようになる。
また、ElasticStackではデプロイメント作成時にデフォルトで(既存で作成されている)ロールがいくつか存在します。
Built-in rolesに詳細は纏められています。
それぞれの用途について別の機会に纏めます。
curlでのロール追加
下記のコマンドを利用してreadwrite
という名前のロールを作成します。
curl -u 'user:password' POST 'localhost:9200/_security/role/readwrite' -H 'Content-Type: application/json' -d'
{
"indices" : [
{
"names" : [ "index1", "index2" ],
"privileges" : [ "read", "write" ]
}
]
}'
curl: (6) Could not resolve host: POST
{"role":{"created":true}}%
indices
indices権限リスト。
names
適用されるインデックス(またはインデックス名のパターン)のリスト。
privileges
ロールの所有者が指定されたインデックスに対して持つインデックスレベルの権限。
指定できる値はIndices privilegesで確認できる。
確認方法は下記のコマンドを実行します。
?pretty
はレスポンスを見やすく整形するためにつけてます。
curl -u 'user:password' GET 'localhost:9200/_security/role/readwrite?pretty'
curl: (6) Could not resolve host: GET
{
"readwrite" : {
"cluster" : [ ],
"indices" : [
{
"names" : [
"index1",
"index2"
],
"privileges" : [
"read",
"write"
],
"allow_restricted_indices" : false
}
],
"applications" : [ ],
"run_as" : [ ],
"metadata" : { },
"transient_metadata" : {
"enabled" : true
}
}
}
Kibanaでのロール追加(Kibana→Dev Tools→Console)
KibanaのConsoleにて下記を実行する。
POST /_security/role/readwrite
{
"indices" : [
{
"names" : [ "index1", "index2" ],
"privileges" : [ "read", "write" ]
}
]
}
実行して問題なければ下記のようなレスポンスが返ってくる
{
"role": {
"created": true
}
}
確認方法は下記を実行する
GET /_security/role/readwrite
下記のようなレスポンスが返ってくる
{
"readwrite": {
"cluster": [],
"indices": [
{
"names": [
"index1",
"index2"
],
"privileges": [
"read",
"write"
],
"allow_restricted_indices": false
}
],
"applications": [],
"run_as": [],
"metadata": {},
"transient_metadata": {
"enabled": true
}
}
}
【おまけ】更新する場合
curl、Consoleどちらも同じ対応となります。
具体的には、同じコマンドで更新箇所だけを変更して叩きます。
例えば下記のロールを作ります。
POST /_security/role/readwrite
{
"indices" : [
{
"names" : [ "index1", "index2" ],
"privileges" : [ "read", "write" ]
}
]
}
やっぱりindex2
の権限は不要だとなった場合には下記を実行する
POST /_security/role/readwrite
{
"indices" : [
{
"names" : [ "index1" ], # index2を削除
"privileges" : [ "read", "write" ]
}
]
}
レスポンスにindex2
が無いことを確認
{
"readwrite": {
"cluster": [],
"indices": [
{
"names": [
"index1"
],
"privileges": [
"read",
"write"
],
"allow_restricted_indices": false
}
],
"applications": [],
"run_as": [],
"metadata": {},
"transient_metadata": {
"enabled": true
}
}
}