5-Elasticsearchのインストール及び設定（TLS設定）

はてなブログに投稿しました。

https://gitya107.hatenablog.com/entry/2018/05/05/165226

そのうちこちらにも投稿します。

# はじめに
Elasticsearchの内部通信を暗号化する設定となります。
X-PackのGOLDライセンスを利用する場合でセキュリティを有効にする場合は、TLSが必須となります。
(SSLは任意です。）

# 認証局の構築及びCA証明書・プライベートキーの作成
Elasticsearchに認証局や証明書発行の機能がありますので、そちらを利用します。
複数インスタンス時はどれか1台が認証局となり、発行した証明書・プライベートキーをすべてのインスタンスに配布します。

CA証明書・プライベートキーの作成を実施します。

```
# /usr/share/elasticsearch/bin/x-pack/certutil ca
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.
Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority
By default the 'ca' mode produces a single PKCS#12 output file which holds:
    * The CA certificate
    * The CA's private key
If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key
Please enter the desired output file [elastic-stack-ca.p12]: Enterを押下
Enter password for elastic-stack-ca.p12 : パスワードを入力
```

# インスタンス向け証明書及びプライベートキーの作成
インスタンス向け証明書及びプライベートキーの作成を実施します。

```
# /usr/share/elasticsearch/bin/x-pack/certutil cert --ca elastic-stack-ca.p12
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'cert' mode generates X.509 certificate and private keys.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file
    * An instance is any piece of the Elastic Stack that requires a SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.
    * All certificates generated by this tool will be signed by a certificate authority (CA).
    * The tool can automatically generate a new CA for you, or you can provide your own with the
         -ca or -ca-cert command line options.
By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate
If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the instance certificate, the key and the CA certificate
If you elect to generate multiple instances certificates, the output will be a zip file
containing all the generated certificates
Enter password for CA (elastic-stack-ca.p12) : CAで設定したパスワードを入力
Please enter the desired output file [elastic-certificates.p12]:Enterを押下
Enter password for elastic-certificates.p12 : 任意のパスワードを入力
Certificates written to /root/elastic-certificates.p12
This file should be properly secured as it contains the private key for
your instance.
This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.
For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.
#
```

# 証明書及びプライベートキーの配置
本手順を全インスタンスに実施します。

(1)証明書を配置するディレクトリの作成

```
# mkdir /etc/elasticsearch/certificate
```

(2)ディレクトリの権限等を修正します。

```
# chgrp elasticsearch /etc/elasticsearch/certificate
# chmod o-rx /etc/elasticsearch/certificate
```

(3)SCP等で全インスタンスに証明書及びプライベートキー(elastic-stack-ca.p12 , elastic-certificates.p12 )を配布します。
　また、(1)で作成したディレクトリにファイルを移動します。

(4)ファイル権限等を修正します。

```
chgrp elasticsearch /etc/elasticsearch/elastic-stack-ca.p12 /etc/elasticsearch/elastic-certificates.p12
chmod 640 /etc/elasticsearch/elastic-stack-ca.p12 /etc/elasticsearch/elastic-certificates.p12
```

(5)キーストアの設定を実施します。
・CA

```
# /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
Enter value for xpack.security.transport.ssl.keystore.secure_password:   elastic-stack-ca.p12作成時のパスワード
```

・インスタンス

```
# /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
Enter value for xpack.security.transport.ssl.truststore.secure_password:  elastic-certificates.p12 作成時のパスワード
```