【问题标题】:How do I properly generate the .pem file(s ?) needed for the grpc java auth example?如何正确生成 grpc java auth 示例所需的 .pem 文件?
【发布时间】:2017-03-17 08:20:53
【问题描述】:

对于一些背景,我正在关注this guide,因为我正在使用 auth 构建一些 grpc 微服务。

我正在尝试运行非常基本的 java 示例,而我生成的密钥不允许发生 ssl 连接。

这是我的服务器代码。

server = NettyServer.forPort(8050)
    .useTransportSecurity(
        new File(serverCert)
        new File(serverKey)
    )
    .addService(
        new TestService(); // my implementation of the grpc service
    )
    .build();

server.start();

这是我的客户端代码

channel = NettyChannelBuilder.forAddress("localhost", 8050)
    .sslContext(
        GrpcSslContexts
            .forClient()
            .trustManager(
                new File(serverCert)
            ).build()
    ).build();

现在,当我去生成密钥时

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

输入一些关于我自己的信息后,我最终得到了 2 个文件

>> ls 
key.pem cert.pem

在我上面的代码中,serverKey 指的是“key.pem”,serverCert 指的是“cert.pem”

我可以毫无问题地启动服务器。当我尝试与客户端连接时,出现以下错误

INFO: [ManagedChannelImpl@6ddf90b0] Created with target localhost:8050
Nov 03, 2016 11:25:16 AM getapi.example.TestClient greet
INFO: Will try to greet world ... 
Nov 03, 2016 11:25:17 AM getapi.example.TestClient greet
WARNING: RPC failed: Status{code=UNAVAILABLE, description=null, cause=javax.net.ssl.SSLHandshakeException: General OpenSslEngine problem}
Nov 03, 2016 11:25:17 AM io.grpc.internal.ManagedChannelImpl maybeTerminateChannel
INFO: [ManagedChannelImpl@6ddf90b0] Terminated

最相关的行似乎是这个

javax.net.ssl.SSLHandshakeException: General OpenSslEngine problem

我承认,除了对公钥密码学非常基本的了解之外,我基本上对密钥/证书/pem 文件一无所知。

有人能解释一下如何正确生成 grpc 所期望的密钥文件吗?

【问题讨论】:

  • 我有一个可行的答案。我非常接近。当我创建我的密钥时,我使用我的 IP 作为公用名,而不是“localhost”。明天我有能力时会正确回答。
  • SSL 设置看起来有些问题(不是您的配置本身)。例如,可能需要使用不同版本的 tcnative。该状态的cause 可能有更多信息。您可以在失败状态下运行status.getCause().printStackTrace() 并发布输出吗?

标签: java ssl public-key-encryption pem grpc


【解决方案1】:

我创建的密钥不正确。这是 openssl 命令

openssl req -x509 -days 365 -nodes -newkey rsa:1024 -keyout key.pem -out cert.pem

当它询问通用名称时,我最初输入了我的 IP 地址。这是不正确的。

我不能代表所有用例,但这里要做的适当的事情是使用“localhost”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多