Elastic 安全是非常重要的思劳。沒(méi)有這個(gè)我們的數(shù)據(jù)可以被任何的人進(jìn)行訪問(wèn),串改碧库,刪除钓葫。Elastic Stack 的安全是由 x-pack 所提供的瓤逼。在 Elastic Stack 7.0 版本之前库物,這個(gè)是商用的版本,需要進(jìn)行安裝诱告,并購(gòu)買(mǎi)精居。從Elastic Stack 7.0之后,x-pack 都已經(jīng)在發(fā)布版中靴姿,所以不需要進(jìn)行安裝佛吓。我們只需要進(jìn)行配置就可以了。
在節(jié)點(diǎn)上啟用 Elastic 安全功能
使用基本和試用許可證時(shí)淤刃,默認(rèn)情況下會(huì)禁用 Elasticsearch 安全功能逸贾。 要啟用它們津滞,首先,我們必須停止所以正在運(yùn)行的 Elastic Stack 軟件:
systemctl stop kibana.service
systemctl stop elasticsearch.service
修改 /etc/elasticsearch/elasticsearch.yml配置文件
#開(kāi)啟密碼驗(yàn)證
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.ml.enabled: true
#開(kāi)啟elasticsearch-head的訪問(wèn)
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,Content-Type,X-Requested-with,Content-Length
然后保存退出
重啟elasticsearch服務(wù)
systemctl start elasticsearch.service
為內(nèi)置用戶(hù)編輯創(chuàng)建密碼(ES必須是啟動(dòng)狀態(tài))
在 Elasticsearch 的安裝目錄/usr/share/elasticsearch/
里打入如下的命令:
./bin/elasticsearch-setup-passwords interactive
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_11/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]
你安裝提示進(jìn)行設(shè)置饺蔑。在這個(gè)過(guò)程中選擇你喜歡的密碼來(lái)設(shè)置猾警。
對(duì)于一些用戶(hù)喜歡一個(gè)隨機(jī)的密碼,那么你可以使用如下的方式來(lái)創(chuàng)建你自己密碼:
./bin/elasticsearch-setup-passwords auto
這樣它可以幫我生成一些隨機(jī)的密碼崔慧。
經(jīng)過(guò)上面的配置后惶室,我們可以在 terminal 中使用如下的方式來(lái)進(jìn)行訪問(wèn) Elasticsearch:
curl -u elastic:password 'http://localhost:9200'
或者
curl 'http://elastic:password@localhost:9200'
在這里玄货, elastic 是超級(jí)用戶(hù)的用戶(hù)名松捉,password 是我們?cè)O(shè)置的密碼。我們也可以在瀏覽器中嘗試訪問(wèn) Elasticsearch:
ES開(kāi)啟安全密碼認(rèn)證后可柿,es-head連接方式
es-head連接方式
在web界面訪問(wèn)時(shí),該框由原來(lái)的 http://172.16.3.227:9100替換為 http://172.16.3.227:9100/?auth_user=elastic&auth_password=123456
cors解釋?zhuān)篊ross Origin Resource Sharing 跨域資源共享
http.cors.enabled 是否支持跨域,默認(rèn)為false
http.cors.allow-origin 當(dāng)設(shè)置允許跨域目锭,默認(rèn)為*,表示支持所有域名,如果我們只是允許某些網(wǎng)站能訪問(wèn)键俱,那么可以使用正則表達(dá)式世分。比如只允許本地地址。 /https?:\ /\ /localhost(:[0-9]+)?/
http.cors.max-age 瀏覽器發(fā)送一個(gè)“預(yù)檢”O(jiān)PTIONS請(qǐng)求踪央,以確定CORS設(shè)置畅蹂。最大年齡定義多久的結(jié)果應(yīng)該緩存荣恐。默認(rèn)為1728000(20天)
http.cors.allow-methods 允許跨域的請(qǐng)求方式,默認(rèn)OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers 跨域允許設(shè)置的頭信息少漆,默認(rèn)為X-Requested-With,Content-Type,Content-Length
http.cors.allow-credentials 是否返回設(shè)置的跨域Access-Control-Allow-Credentials頭示损,如果設(shè)置為true,那么會(huì)返回給客戶(hù)端嚷硫。