阅读 464

gRPC: 如何开启 TLS/SSL?

介绍

本文将介绍如何在 gRPC 微服务中开启 TLS/SSL,我就是我们常说的 https。

我们将会使用 rk-boot 来启动 gRPC 服务。

请访问如下地址获取完整教程:

  • rkdev.info/cn

  • rkdocs.netlify.app/cn (备用)

生成 Self-Signed Certificate

用户可以从各大云厂商购买证书,或者使用 cfssl 创建自定义证书。

我们介绍如何在本地生成证书。

1.下载 cfssl & cfssljson 命令行

推荐使用 rk 命令行来下载。

$ go get -u github.com/rookie-ninja/rk/cmd/rk $ rk install cfssl $ rk install cfssljson 复制代码

官网下载

$ go get github.com/cloudflare/cfssl/cmd/cfssl $ go get github.com/cloudflare/cfssl/cmd/cfssljson 复制代码

2.生成 CA

$ cfssl print-defaults config > ca-config.json $ cfssl print-defaults csr > ca-csr.json 复制代码

根据需要修改 ca-config.json 和 ca-csr.json。

$ cfssl gencert -initca ca-csr.json | cfssljson -bare ca - 复制代码

3.生成服务端证书

server.csrserver.pemserver-key.pem 将会被生成。

$ cfssl gencert -config ca-config.json -ca ca.pem -ca-key ca-key.pem -profile www csr.json | cfssljson -bare server 复制代码

安装

go get github.com/rookie-ninja/rk-boot 复制代码

快速开始

rk-boot 支持通过如下方式让 gRPC 服务获取证书。

  • 本地文件系统

  • 远程文件系统

  • Consul

  • ETCD

我们先看看如何从本地获取证书并启动。

1.创建 boot.yaml

在这个例子中,我们只启动服务端的证书。其中,locale 用于区分不同环境下 cert。

请参考之前的文章了解详情:GRPC: 基于云原生环境,区分配置文件

--- cert:   - name: "local-cert"                     # Required     provider: "localFs"                    # Required, etcd, consul, localFs, remoteFs are supported options     locale: "*::*::*::*"                   # Required, default: ""     serverCertPath: "cert/server.pem"      # Optional, default: "", path of certificate on local FS     serverKeyPath: "cert/server-key.pem"   # Optional, default: "", path of certificate on local FS grpc:   - name: greeter     port: 8080     enabled: true     enableReflection: true     cert:       ref: "local-cert"                    # Enable grpc TLS     commonService:       enabled: true 复制代码

2.创建 main.go

package main import ( "context" "github.com/rookie-ninja/rk-boot" ) // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) } 复制代码

3.文件夹结构

. ├── boot.yaml ├── cert │   ├── server-key.pem │   └── server.pem ├── go.mod ├── go.sum └── main.go 1 directory, 6 files 复制代码

4.启动 main.go

$ go run main.go 复制代码

5.验证

  • 发送 Restful 请求

$ curl -X GET --insecure https://localhost:8080/rk/v1/healthy                  {"healthy":true} 复制代码

  • 发送 grpc 请求

$ grpcurl -insecure localhost:8080 rk.api.v1.RkCommonService.Healthy {   "healthy": true } 复制代码

架构

参数介绍

1.从本地读取证书

配置项详情需要默认值
cert.localFs.name本地文件系统获取器名称""
cert.localFs.locale遵从 locale: <realm>::<region>::<az>::<domain>""
cert.localFs.serverCertPath服务器证书路径""
cert.localFs.serverKeyPath服务器证书密钥路径""
cert.localFs.clientCertPath客户端证书路径""
cert.localFs.clientCertPath客户端证书密钥路径""
  • 例子

--- cert:   - name: "local-cert"                     # Required     description: "Description of entry"    # Optional     provider: "localFs"                    # Required, etcd, consul, localFs, remoteFs are supported options     locale: "*::*::*::*"                   # Required, default: ""     serverCertPath: "cert/server.pem"      # Optional, default: "", path of certificate on local FS     serverKeyPath: "cert/server-key.pem"   # Optional, default: "", path of certificate on local FS grpc:   - name: greeter     port: 8080     enabled: true     enableReflection: true     cert:       ref: "local-cert"                    # Enable grpc TLS 复制代码

2.从远程文件服务读取证书

配置项详情需要默认值
cert.remoteFs.name远程文件服务获取器名称""
cert.remoteFs.locale遵从 locale:<realm>::<region>::<az>::<domain>""
cert.remoteFs.endpoint远程地址: http://x.x.x.x 或者 x.x.x.xN/A
cert.remoteFs.basicAuthBasic auth: user:pass.""
cert.remoteFs.serverCertPath服务器证书路径""
cert.remoteFs.serverKeyPath服务器证书密钥路径""
cert.remoteFs.clientCertPath客户端证书路径""
cert.remoteFs.clientCertPath客户端证书密钥路径""
  • 例子

--- cert:   - name: "remote-cert"                    # Required     description: "Description of entry"    # Optional     provider: "remoteFs"                   # Required, etcd, consul, localFs, remoteFs are supported options     endpoint: "localhost:8081"             # Required, both http://x.x.x.x or x.x.x.x are acceptable     locale: "*::*::*::*"                   # Required, default: ""     serverCertPath: "cert/server.pem"      # Optional, default: "", path of certificate on local FS     serverKeyPath: "cert/server-key.pem"   # Optional, default: "", path of certificate on local FS grpc:   - name: greeter     port: 8080     enabled: true     cert:       ref: "remote-cert"                   # Enable grpc TLS 复制代码

3.从 Consul 读取证书

配置项详情需要默认值
cert.consul.nameConsul 获取器名称""
cert.consul.locale遵从 locale: <realm>::<region>::<az>::<domain>""
cert.consul.endpointConsul 地址: http://x.x.x.x or x.x.x.xN/A
cert.consul.datacenterConsul 数据中心""
cert.consul.tokenConsul 访问密钥""
cert.consul.basicAuthConsul Basic auth,格式:user:pass.""
cert.consul.serverCertPath服务器证书路径""
cert.consul.serverKeyPath服务器证书密钥路径""
cert.consul.clientCertPath服务器证书密钥路径""
cert.consul.clientCertPath服务器证书密钥路径""
  • 例子

--- cert:   - name: "consul-cert"                    # Required     provider: "consul"                     # Required, etcd, consul, localFS, remoteFs are supported options     description: "Description of entry"    # Optional     locale: "*::*::*::*"                   # Required, ""     endpoint: "localhost:8500"             # Required, http://x.x.x.x or x.x.x.x both acceptable.     datacenter: "dc1"                      # Optional, default: "", consul datacenter     serverCertPath: "server.pem"           # Optional, default: "", key of value in consul     serverKeyPath: "server-key.pem"        # Optional, default: "", key of value in consul grpc:   - name: greeter     port: 8080     enabled: true     cert:       ref: "consul-cert"                   # Enable grpc TLS 复制代码

4.从 ETCD 读取证书

配置项详情需要默认值
cert.etcd.nameETCD 获取器名称""
cert.etcd.locale遵从 locale:  <realm>::<region>::<az>::<domain>""
cert.etcd.endpointETCD 地址:http://x.x.x.x or x.x.x.xN/A
cert.etcd.basicAuthETCD basic auth,格式:user:pass.""
cert.etcd.serverCertPath服务器证书路径""
cert.etcd.serverKeyPath服务器证书路径""
cert.etcd.clientCertPath客户端证书路径""
cert.etcd.clientCertPath客户端证书密钥路径""
  • 例子

--- cert:   - name: "etcd-cert"                      # Required     description: "Description of entry"    # Optional     provider: "etcd"                       # Required, etcd, consul, localFs, remoteFs are supported options     locale: "*::*::*::*"                   # Required, default: ""     endpoint: "localhost:2379"             # Required, http://x.x.x.x or x.x.x.x both acceptable.     serverCertPath: "server.pem"           # Optional, default: "", key of value in etcd     serverKeyPath: "server-key.pem"        # Optional, default: "", key of value in etcd grpc:   - name: greeter     port: 8080     enabled: true     cert:       ref: "etcd-cert"                   # Enable grpc TLS


作者:尹东勋
链接:https://juejin.cn/post/7023359445308538887

文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐