简体   繁体   English

导出 java ca 证书,以便它可以从 go 中使用

[英]Export java ca certificate so that it is usable from go

I need to integrate java service with go app, which can be configured to specify the CA certificate file to use when checking TLS.我需要将java服务与go app集成,可以配置为指定检查TLS时使用的CA证书文件。 I'm not familiar with go.我对 go 不熟悉。 The Java rest endpoint is secured with self-signed certificate. Java rest 端点使用自签名证书进行保护。

I'm trying to export it using keytool like this:我正在尝试使用这样的keytool导出它:

keytool -export -alias rootca -file ca.crt -keystore cacerts

The ca.crt file is checkable with keytool itself with command: ca.crt文件可通过 keytool 本身使用以下命令进行检查:

keytool -printcert -v -file ca.crt

Now when I try to use this file in go app, which is prometheus by passing tls_config with ca_cert value pointing to exported file I get an error:现在,当我尝试使用中去应用这个文件,这是普罗米修斯通过传递tls_configca_cert价值指向导出的文件,我得到一个错误:

err="error creating HTTP client: unable to use specified CA cert /etc/prometheus/ssl/ca.crt" err="错误创建 HTTP 客户端:无法使用指定的 CA 证书 /etc/prometheus/ssl/ca.crt"

The Prometheus is using the the go's standart crypto/tls so my questions is: Prometheus 使用的是 go 的标准crypto/tls所以我的问题是:

  • Is the way I exported Java file is correct and usable?我导出的 Java 文件的方式是否正确可用?
  • How can I translate exported Java CA file to the format that Go will understand and use?如何将导出的 Java CA 文件转换为 Go 能够理解和使用的格式?

Per the man page for keytool , under the -exportcert command:根据keytool的手册页,在-exportcert命令下:

The certificate is by default output in binary encoding.默认情况下,证书以二进制编码输出。

The encoding in question is DER which is a binary representation of the certificate data.有问题的编码是DER ,它是证书数据的二进制表示。

However, the Go standard library expects PEM encoding which is just base64 of the DER data plus some header/footer.然而,Go 标准库需要PEM编码,它只是 DER 数据的 base64 加上一些页眉/页脚。

You have (at least) two options:您(至少)有两个选择:

Tell keytool to export in PEM format告诉keytool以 PEM 格式导出

This is done using the -rfc flag to the -exportcerts command.这是使用-exportcerts命令的-rfc标志完成的。 Per the man page:根据手册页:

If the -rfc option is specified, then the output in the printable encoding format defined by the Internet RFC 1421 Certificate Encoding Standard.如果指定了 -rfc 选项,则以 Internet RFC 1421 证书编码标准定义的可打印编码格式输出。

RFC1421 is the one for PEM. RFC1421是 PEM 的一种。

Convert from DER to PEM从 DER 转换为 PEM

This can be done using openssl using the following:这可以使用 openssl 使用以下方法完成:

openssl -in exported_file.der -inform DER -out ca.crt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM