基于x-pack的ES使用者管理(認證)
Elasticsearch安全管理插件通過使用者認證、使用者授權、加密通信、審計日志、IP過濾等多種手段來保證叢集的安全。
領域(realms)和 領域鍊(realms chain)
-
領域(realms)
x-pack的安全插件可以通過使用者名密碼、證書、令牌和其他多種方法實作使用者認證。
Elasticsearch支援通過多種背景服務實作使用者認證,這些背景服務方式通稱為“領域”(realms),常見的realms有:native、ldap、active_directory、file、pki、file、saml、oidc。
-
領域鍊(realms chain)
不同的realms可以組成認證鍊,前一個realms沒有認證通過時,使用後面的realms進行認證,直到所有的realms都沒認證通過時才傳回失敗,隻要一個傳回成功就屬于認證成功。預設的認證鍊中有native和file。
realms分為兩類:内部的realm和外部的realm,native和file都是es自帶的内部realms,不需要和外部互動就能完成使用者認證。
内部的realms隻能在認證鍊中配置一次,比如一個認證鍊中最多有一個native relams和file relams,但是可以有多個ldap realms配置。
-
native realm:
通過native realm管理的使用者資料,存儲在叢集的.secutrity-7索引中,可以通過user api和kibana UI進行人員的管理操作,人員資料在整個叢集有效。
-
file realm:
通過file realm 管理的使用者資料存儲在ES_HOME/config/users和users_roles的配置檔案中,可以通過ES_HOME/bin/elasticsearch-users指令維護,人員資料在node級别有效,需要人為保證叢集中每個節點資料都一緻。
users_roles中的人員角色資料需要角色在前,人員在後,如:
[wangzhen@hellow-pc config]$ cat users_roles
superuser:admin,sa
[wangzhen@hellow-pc config]$ ../bin/elasticsearch-users list
admin : superuser
sa : superuser
支援安全管理的ES版本
Elasticsearch 從6.8.0/7.1.0後xpack 開源,基礎版不需要授權免費使用,支援使用者認證、授權、監控等功能
Elasticsearch 5.X的版本無法免費使用xpack提供的安全功能
内建使用者和系統使用者
Elasticsearch在打開安全配置後,會有系統自帶的管理帳号,稱為内建使用者。同時也有自帶的系統使用者。
系統使用者和内建使用者的差別是:内建使用者可以用來登陸,系統使用者是es叢集内部通信使用的,不能用來登陸,但是可以在審計日志中看到這些使用者産生的事件。
- 内建使用者
- elastic
- kibana
- beats_system
- apm_system
- remote_monitoring_user
- 系統使用者
- _system
- _xpack
- _xpack_security
使用者資料的存儲
當使用Elasticsearch預設的native服務做使用者認證授權時,使用者資料存儲在.security-7 所引中,索引資料可檢視、修改、删除
curl -X GET -i 'http://localhost:9200/.security-7/_search'
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": ".security-7",
"_type": "_doc",
"_id": "reserved-user-kibana",
"_score": 1.0,
"_source": {
"password": "$2a$10$EthtJnnPL9BXDnG2iH8LVedy5s4tOIPrvpZyRSJALw2IIR1ge/jl.",
"enabled": true,
"type": "reserved-user"
}
},...
}]
}
}
如何開啟使用者認證和授權功能
-
第一步,打開安全開關
修改 ES_HOME/config/elasticsearch.yml,添加
xpack.security.enabled: true
-
第二步,初始化内建使用者的密碼
執行 ES_HOME/bin/elasticsearch-set-passwords.sh interactive,通過互動的方式依次設定内建使用者的密碼
- 第三步,登陸
curl -XGET "http://localhost:9200" -u elastic:6789@jkl
{
"name" : "node-1",
"cluster_name" : "my-application",
"cluster_uuid" : "gZRcq4pUTFOdZSRy-JHrFQ",
"version" : {
"number" : "7.2.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "508c38a",
"build_date" : "2019-06-20T15:54:18.811730Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
如何進行使用者增删改(native realm)
- 新增使用者
PUT /_security/user/wangzhen
{
"password": "123456",
"roles": ["superuser"],
"full_name": "wangzhen",
"email":"[email protected]",
"metadata": {
"age":21,
"sex":"男"
}
}
-
修改使用者
和新增API一樣,但是不能修改使用者名和密碼
PUT /_security/user/wangzhen
{
"roles": ["superuser"],
"full_name": "wangzhen",
"email":"[email protected]",
"metadata": {
"age":21,
"sex":"男"
}
}
-
修改使用者密碼
傳回空json結構{ } 代表成功
PUT /_security/user/wangzhen/_password
{
"password":"test"
}
- 删除使用者
DELETE /_security/user/wangzhen
- 禁用/激活使用者
PUT /_security/user/wangzhen/_enable
PUT /_security/user/wangzhen/_disable