Falha ao criar chave de segurança para cada nó do cluster

Preciso configurar um cluster elasticsearch para rodar em um cluster Kubernetes. Criei um CA, salvei em um secret, mapeei este secret para um volume... tudo certinho. Porém não consigo gerar a chave para cada nó para configurar a comunicação ssl dentro do cluster.

Como no ambiente Kubernetes os Pods podem ser mortos e recriados a qualquer momento, fiz um script para ser executado em cada Pod antes do Elasticsearch ser iniciado.

start.sh
#!/bin/bash

. elasticsearch-certutil cert \
	--ca-cert config/certs/ca.crt \
	--ca-key config/certs/ca.key \
	--ca-pass "$CERT_PASSWORD" \
	--out config <<< ""

. elasticsearch-keystore add --stdin \
	xpack.security.transport.ssl.keystore.secure_password <<< "$CERT_PASSWORD"

. elasticsearch-keystore add --stdin \
	xpack.security.transport.ssl.truststore.secure_password <<< "$CERT_PASSWORD"

exec elasticsearch \
"$@"

O comando elasticsearch-certutil dá um erro de "Incomplete Data", ao criar o Pod sem este script e tentar criar o mesmo certificado, obtenho o mesmo erro:

./elasticsearch-certutil cert --ca-cert /usr/share/elasticsearch/config/certs/ca.crt --ca-key /usr/share/elasticsearch/config/certs/ca.key

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.bouncycastle.jcajce.provider.drbg.DRBG (file:/usr/share/elasticsearch/lib/tools/security-cli/bcprov-jdk15on-1.61.jar) to constructor sun.security.provider.Sun()
WARNING: Please consider reporting this to the maintainers of org.bouncycastle.jcajce.provider.drbg.DRBG
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
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 an 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 specify any of the following options:
    * -pem (PEM formatted output)
    * -keep-ca-key (retain generated CA key)
    * -multiple (generate multiple certificates)
    * -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files

Exception in thread "main" java.security.cert.CertificateException: java.io.IOException: Incomplete data
	at java.base/sun.security.provider.X509Factory.engineGenerateCertificates(X509Factory.java:363)
	at java.base/java.security.cert.CertificateFactory.generateCertificates(CertificateFactory.java:478)
	at org.elasticsearch.xpack.core.ssl.CertParsingUtils.readCertificates(CertParsingUtils.java:99)
	at org.elasticsearch.xpack.core.ssl.CertParsingUtils.readCertificates(CertParsingUtils.java:91)
	at org.elasticsearch.xpack.security.cli.CertificateTool$CertificateCommand.loadPemCA(CertificateTool.java:360)
	at org.elasticsearch.xpack.security.cli.CertificateTool$CertificateCommand.getCAInfo(CertificateTool.java:330)
	at org.elasticsearch.xpack.security.cli.CertificateTool$GenerateCertificateCommand.execute(CertificateTool.java:684)
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
	at org.elasticsearch.cli.MultiCommand.execute(MultiCommand.java:77)
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
	at org.elasticsearch.cli.Command.main(Command.java:90)
	at org.elasticsearch.xpack.security.cli.CertificateTool.main(CertificateTool.java:137)
Caused by: java.io.IOException: Incomplete data
	at java.base/sun.security.provider.X509Factory.readOneBlock(X509Factory.java:590)
	at java.base/sun.security.provider.X509Factory.parseX509orPKCS7Cert(X509Factory.java:454)
	at java.base/sun.security.provider.X509Factory.engineGenerateCertificates(X509Factory.java:361)
	... 12 more

Alguém pode me ajudar? O cluster está com as configurações default definidas pelo chart Helm ofiial da Elastic.

Oi @pedro.pacm,

Qual foi o comando que você usou para criar o CA? Pela stacktrace parece que ele está inválido/corrompido.

Olá @luiz.santos,

Eu criei o CA com o seguinte comando:

./elasticsearch/bin/elasticsearch-certutil ca --pem

Mas tive problemas ao criá-lo. Tentei pela primeira vez logado no master, mas ele gerou um arquivo .zip e fiquei impossibilitado de descompactá-lo. A imagem do elasticsearch não contém o programa necessário instalado e não consegui adicioná-lo depois. Também não consegui baixar o arquivo para tratá-lo na minha máquina local.

Para contornar esta situação, levantei uma imagem docker no meu computador, gerei o CA utilizando exatamente o mesmo comando acima, copiei o arquivo .zip para minha máquina local e depois criei o secret no cluster kubernetes com os arquivos correspondentes.