在為 Elasticsearch 采集數(shù)據(jù)時(shí),我們經(jīng)常使用到 Filebeat 及 Logstash。在我們之前的很多教程中,我們通常不為 Filebeat 和 Logstash 之前的通信做安全配置。
如何為 Filebeat 及 Logstash 直接建立安全的鏈接?這個(gè)在很多的情況下是非常有用的。在我之前的文章 “Elasticsearch:為日志分析設(shè)置安全的 Elasticsearch 管道”,我有涉及到這個(gè)問題。最主要的是,它是針對(duì) Elastic Stack 8.x 而設(shè)計(jì)的。在今天的文章中,我將針對(duì)最普通的情況來進(jìn)行展示。
本指南提供了有關(guān)使用 OpenSSL 生成和配置 SSL 證書的詳細(xì)說明,以增強(qiáng) Logstash 和 Filebeat 之間通信的安全性。 概述的步驟包括為證書頒發(fā)機(jī)構(gòu) (CA) 生成私鑰、為 CA 創(chuàng)建自簽名 X509 證書以及為 Logstash 和 Filebeat 生成密鑰和證書。 還包括使用這些證書的 Filebeat 和 Logstash 的配置步驟。
為 CA 生成私鑰
# Generate a private key for the Certificate Authority (CA)
openssl genrsa 2048 > /path/to/your/ca-key.pem
$ pwd
/Users/liuxg/demos/filebeat_logstash
$ openssl genrsa 2048 > ca-key.pem
$ ls
ca-key.pem
為 CA 生成 X509 證書
# Generate a self-signed X509 certificate for the CA
openssl req -new -x509 -nodes -days 365000 \
-key /path/to/your/ca-key.pem \
-out /path/to/your/ca-cert.pem
openssl req -new -x509 -nodes -days 365000 -key ca-key.pem -out ca-cert.pem
$ pwd
/Users/liuxg/demos/filebeat_logstash
$ ls
ca-key.pem
$ openssl req -new -x509 -nodes -days 365000 -key ca-key.pem -out ca-cert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Elastic
Organizational Unit Name (eg, section) []:Community
Common Name (e.g. server FQDN or YOUR name) []:Xiaoguo
Email Address []:x@y.com
$ ls
ca-cert.pem ca-key.pem
為 Logstash 和 Filebeat 創(chuàng)建證書和密鑰
# Generate a private key and certificate for Logstash and Filebeat using the CA
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /path/to/your/server.key \
-out /path/to/your/server.crt \
-CA /path/to/your/ca-cert.pem \
-CAkey /path/to/your/ca-key.pem
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ./server.key \
-out ./server.crt \
-CA ./ca-cert.pem \
-CAkey ./ca-key.pem
$ pwd
/Users/liuxg/demos/filebeat_logstash
$ ls
ca-cert.pem ca-key.pem
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
> -keyout ./server.key \
> -out ./server.crt \
> -CA ./ca-cert.pem \
> -CAkey ./ca-key.pem
........+...+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...........+..+.+...+.........+..+.............+...+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..........+..........+.....+...+.........+.+..+......+.......+...............+..+...+.......+...+...+.....+......+......+.......+..+....+..+..........+..+......+......+.+..+.+..............+...+.......+..+...+.......+...+........+...+......+....+..+.......+.....+...............+......+................+......+...+......+.....+.+..+...+..........+........+................+...............+......+..+.+..+.........+...+.......+......+...+.....+.+...............+...+........+...+.........+...+.........+....+.....+.+........+...+.........+..........+......+..+.............+.....+....+...+..+.............+......+......+.........+..+...+....+...+............+...+..+.+......+.....+.+..+...+..................+.....................+....+..............+.+..+..........+..+...+....+...+......+.................+...+....+........+...+.+...........+..........+.....+...............+....+......+..............+...+......+......+...+.......+...+.....+...+.............+..+.......+.....+.+..+....+...+......+.....+..........+..+.........+.........+.+........+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.........+......+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*........+.........+.........+.....+.+.....+.+.........+..+....+........+...+.......+........+...+......+...+...+............+..........+...+..+..........+.....+.............+..+...+...+....+...+..............+....+..+......................+..+...+...+...+.+.........+......+...+..+.+...+.....+.........+...+...+.+...+..+.+........+............+..........+..+.+.....................+......+........+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Elastic
Organizational Unit Name (eg, section) []:Community
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:x@y.com
按照同樣的方法,我們生成相應(yīng)的 client 證書:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout client.key \
-out client.crt \
-CA ./ca-cert.pem \
-CAkey ./ca-key.pem
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
> -keyout ./server.key \
> -out ./server.crt \
> -CA ./ca-cert.pem \
> -CAkey ./ca-key.pem
........+...+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...........+..+.+...+.........+..+.............+...+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..........+..........+.....+...+.........+.+..+......+.......+...............+..+...+.......+...+...+.....+......+......+.......+..+....+..+..........+..+......+......+.+..+.+..............+...+.......+..+...+.......+...+........+...+......+....+..+.......+.....+...............+......+................+......+...+......+.....+.+..+...+..........+........+................+...............+......+..+.+..+.........+...+.......+......+...+.....+.+...............+...+........+...+.........+...+.........+....+.....+.+........+...+.........+..........+......+..+.............+.....+....+...+..+.............+......+......+.........+..+...+....+...+............+...+..+.+......+.....+.+..+...+..................+.....................+....+..............+.+..+..........+..+...+....+...+......+.................+...+....+........+...+.+...........+..........+.....+...............+....+......+..............+...+......+......+...+.......+...+.....+...+.............+..+.......+.....+.+..+....+...+......+.....+..........+..+.........+.........+.+........+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.........+......+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*........+.........+.........+.....+.+.....+.+.........+..+....+........+...+.......+........+...+......+...+...+............+..........+...+..+..........+.....+.............+..+...+...+....+...+..............+....+..+......................+..+...+...+...+.+.........+......+...+..+.+...+.....+.........+...+...+.+...+..+.+........+............+..........+..+.+.....................+......+........+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Elastic
Organizational Unit Name (eg, section) []:Community
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:x@y.com
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
> -keyout client.key \
> -out client.crt \
> -CA ./ca-cert.pem \
> -CAkey ./ca-key.pem
....+...+.+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+.........+........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*......+....+.....+....+...+...+..+..................+.+..............+.+.....................+...+..+.............+..+.......+..+......+............+......+.+......+.........+.........+......+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
..+.....+..................+.+..+............+.+..+....+......+...+.....+.......+...+.....+......+...+.............+........+.+.....+....+..+.......+......+..+....+...+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+.....+......+.+...+..+....+.....+.............+........+.......+...+............+.....+..................+.......+..+.+.........+...............+.....+.+..+...+...+.+......+.....+.......+.....+...+.........+......+....+...+...........+....+...........+......+.........+....+.....+......+......+.+.....+..........+.....+............+.+......+...+...+...+..+.......+..+...+......+.+......+.....+....+...+..+...............+....+...............+..+.........+...+.+.....+................+.....+.+...+......+.....+......+.......+..+...+......+.+......+...+.....+.........+......................+...+.....+...+...+......+.........+.+.....+....+.....+...............+....+...+.....+............+.+..+.......+..+...+...+............+..........+..+................+..+...+...+.+...+........+...+.+...........+.........+......+..................+....+.....+............+....+.....+......+....+..+....+.....+.+.....+...+...............+....+.....+..........+..+................+..+............+.........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Elastic
Organizational Unit Name (eg, section) []:Community
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:x@y.com
$ ls
ca-cert.pem filebeat-8.12.0-darwin-aarch64.tar.gz
ca-key.pem logstash-8.12.0
client.crt logstash-8.12.0-darwin-aarch64.tar.gz
client.key server.crt
filebeat-8.12.0-darwin-aarch64 server.key
配置 Filebeat(或任何 Beat)
output.logstash:
hosts: ["localhost:5044"]
# Specify the path to the CA certificate for verification
ssl.certificate_authorities: ["/path/to/your/ca-cert.pem"]
# Specify the path to the client certificate
ssl.certificate: "/path/to/your/server.crt"
# Specify the path to the client private key
ssl.key: "/path/to/your/server.key"
配置 Logstash
input {
beats {
port => 5044
ssl => true
# Specify the path to the CA certificate for verification
ssl_certificate_authorities => ["/path/to/your/ca-cert.pem"]
# Specify the path to the server certificate
ssl_certificate => "/path/to/your/server.crt"
# Specify the path to the server private key
ssl_key => "/path/to/your/server.key"
# Set peer verification mode to force_peer
ssl_verify_mode => "force_peer"
}
}
確保將 /path/to/your/ 替換為密鑰和證書的實(shí)際所需路徑。
測(cè)試
我們?cè)诋?dāng)前的目錄下安裝 Filebeat 及 Logstash:
filebeat_ssl.yml
$ pwd
/Users/liuxg/demos/filebeat_logstash
$ ls
ca-cert.pem logstash-8.12.0
ca-key.pem logstash-8.12.0-darwin-aarch64.tar.gz
filebeat-8.12.0-darwin-aarch64 server.crt
filebeat-8.12.0-darwin-aarch64.tar.gz server.key
$ cd filebeat-8.12.0-darwin-aarch64
$ ls filebeat_ssl.yml
filebeat_ssl.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- ./logfile.log
output.logstash:
hosts: ["localhost:5044"]
ssl.certificate_authorities: ["../ca-cert.pem"]
ssl.certificate: "../server.crt"
ssl.key: "../server.key"
logging:
level: info
to_files: true
files:
path: .
name: filebeat.log
keepfiles: 7
permissions: 0644
我們?cè)?filebeat 安裝目錄下創(chuàng)建一個(gè)叫做 logfile.log 的文件。它的內(nèi)容如下:
logfile.log
This is a test log
logstash.conf
input {
beats {
port => 5044
ssl => true
ssl_certificate_authorities => "../ca-cert.pem"
ssl_certificate => "../server.crt"
ssl_key => "../server.key"
ssl_verify_mode => "force_peer"
}
}
output {
stdout { codec => rubydebug }
file {
path => "../outputfile.log"
}
}
我們首先運(yùn)行 logstash:
./bin/logstash -f logstash.conf
我們?cè)俳酉聛磉\(yùn)行 Filebeat:
我們回到 Logstash 運(yùn)行的 terminal 中進(jìn)行查看:
文章來源:http://www.zghlxwxcb.cn/news/detail-831593.html
從上面的輸出中,我們可以看到日志被正確地采集到 Logstash。我們可以更進(jìn)一步輸出到 Elasticsearch。在這里,我們就不再累述了。文章來源地址http://www.zghlxwxcb.cn/news/detail-831593.html
到了這里,關(guān)于安全通信設(shè)置:使用 OpenSSL 為 Logstash 和 Filebeat 提供 SSL 證書的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!