ElasticSearch安全-賬號密碼驗證


JAVA 8

elasticsearch-7.14.0 on Windows 10

Spring Boot 2.5.3

---

 

目錄

1、開啟ES的賬號密碼訪問

2、修改Kibana的ES賬號密碼

3、Spring Boot項目啟動訪問ES

4、更多操作

參考文檔

 

相關ES官方文檔

1、Secure the Elastic Stack

password protect access、Transport Layer Security (TLS)、role-based access control、IP filtering、auditing……

2、Security APIs

角色管理、用戶管理,其它

 

1、開啟ES的賬號密碼訪問

在使用的7.14版本ES中,無需密碼即可訪問。

根據 參考文檔1 的介紹,開啟了ES的賬號密碼登錄功能。來自博客園

步驟如下:

1)修改配置文件config\elasticsearch.yml

添加:

# ---------------------------------- Security -----------------------------------
xpack.security.enabled: true

2)執行bin\elasticsearch.bat

啟動ES后,執行下面的命令訪問ES:訪問失敗,權限不足

D:\WS\es\bin>curl localhost:9200
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials 
for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],
"type":"security_exception","reason":"missing authentication credentials for REST request [/]",
"header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}

3)執行bin\elasticsearch-setup-passwords.bat interactive

interactive adj.  互相作用的,相互影響的; [計]交互式的; 互動的;

交互式地設置 多個賬號的密碼。

這些賬號(6個)包括:elastic、apm_system、kibana_system、logstash_system、beats_system、remote_monitoring_user

設置多個用戶密碼
D:\WS\es\bin>elasticsearch-setup-passwords.bat interactive
"warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME"
Future versions of Elasticsearch will require Java 11; your Java version from [D:\Program Files\Java\jdk1.8.0_202\jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y


Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]

D:\WS\es\bin>

 

注,為了簡便,將它們都設置為了 123456

 

設置賬號密碼后,驗證使用賬號密碼是否能訪問ES:兩種格式,都成功。

賬號密碼訪問ES
# 第一種格式:命令中沒有 密碼
D:\WS\es\bin>curl localhost:9200 -u elastic
Enter host password for user 'elastic':
{
  "name" : "DESKTOP-BDNTQQ3",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "mACMicetS3-YnNTGbiDLXA",
  "version" : {
    "number" : "7.14.0",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
    "build_date" : "2021-07-29T20:49:32.864135063Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

# 第二種格式:命令中有密碼
D:\WS\es\bin>curl localhost:9200 -u elastic:123456

六個賬號都能訪問成功。來自博客園

 

2、修改Kibana的ES賬號密碼

在Kibana的配置文件kibana.yml中,默認是不使用賬號密碼訪問ES的。

默認情況下啟動Kibana,出現下面的錯誤信息:

log   [10:24:50.678] [error][savedobjects-service] Unable to retrieve version information 
from Elasticsearch nodes. security_exception: [security_exception] Reason: missing authentication 
credentials for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]

此時訪問Kibana:Kibana server is not ready yet

修改Kibana的配置中的ES賬號密碼信息:

elasticsearch.username: "kibana_system"
elasticsearch.password: "123456"

再次啟動Kibana:啟動成功。

訪問Kibana:彈出登錄對話框。

使用ES的 kibana_system 賬號登錄,不過,登錄成功了,但提示沒有權限

怎么添加這個權限呢?看了一些文檔,沒有找到想要的答案……

……若干分鍾后……

上面使用的賬號 kibana_system來訪問Kibana,提示權限不足。

更改為使用ES賬號 elastic 來訪問Kibana,訪問成功

登錄后,還可以修改自己的密碼:

打開主菜單的Management->Stack Management,可以看到更多管理信息。來自博客園

在其中的 Security 中,可以看到 Users、Roles的信息。

從上面的Users表可以看到,賬號elastic的角色是 superuser,而之前沒有訪問權限的 Kibana_system 的角色是 kibana_system,這才導致了訪問權限不足,無法進入系統。

新增用戶 也可以在這個頁面,當然,也可以使用RESTful API來對用戶、角色進行管理。

 

3、Spring Boot項目啟動訪問ES

未配置賬號密碼啟動:啟動失敗

配置文件中添加下面的配置:

# 賬號、密碼
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=123456

再次啟動:啟動成功。

訪問應用的接口:訪問成功。

 

4、更多操作

修改賬號密碼

Kibana中操作:

獲取所有用戶
GET /_security/user

獲取單個用戶elastic
GET /_security/user/elastic

修改用戶密碼(修改自己的密碼后,需要重新登錄)
POST /_security/user/elastic/_password
{
  "password": "123456"
}

 

重置賬號密碼

忘記賬號elastic這個超級賬號的密碼了,此時,需要重置。

按照 參考文檔1 的步驟執行即可。

步驟如下:

1、停止ES

2、修改ES的配置文件elasticsearch.yml

取消:xpack.security.enabled: true

3、重啟ES

檢查ES的索引:有一個名為 .security-7 的

4、刪除這個索引 .security-7

curl -XDELETE localhost:9200/.security-7
{"acknowledged":true}

刪除成功。

此時,ES就回到了沒有賬號密碼階段了,又可以按照第一章的建立賬號密碼了。來自博客園

 

添加賬號

官文:

Create or update users API

 

獲取角色:

GET /_security/role

獲取賬號:

GET /_security/user

 

添加一個 角色為 superuser 的賬號:lib

# Kibana
POST /_security/user/lib
{
  "password" : "123456",
  "roles" : [ "superuser"  ],
  "full_name" : "library",
  "email" : "lib@example.com",
  "metadata" : {
    "startfrom" : 20210927
  }
}
響應:
{
  "created" : true
}

# 查看
GET /_security/user/lib
響應:
{
  "lib" : {
    "username" : "lib",
    "roles" : [
      "superuser"
    ],
    "full_name" : "library",
    "email" : "lib@example.com",
    "metadata" : {
      "startfrom" : 20210927
    },
    "enabled" : true
  }
}

使用S.B應用驗證,成功。

 

刪除賬號

使用 elastic這個 superuser 賬號去 刪除上面的lib:來自博客園

DELETE /_security/user/lib
響應:
{
  "found" : true
}


GET /_security/user/lib
響應:
{ }

GET /_security/user
也沒找到 lib 賬號。

 

》》》全文完《《《

 

ES的賬號密碼驗證搞完,不過,看了官文,ES還有更多安全措施可以做,后面再深入。來自博客園

對了SMAL是什么?回頭了解下,感覺畢OAuth2 還高級啊(認證與授權協議對比:OAuth2、OpenID、SMAL

 

參考文檔

1、如何給ElasticSearch設置用戶名和密碼

作者:Vino

2、

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM