Merge pull request #5606 from zwtop/master

grpc config add options tcp_client_ca_cert
This commit is contained in:
Maksym Pavlenko 2021-07-26 20:57:29 -07:00 committed by GitHub
commit 158901756c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -122,6 +122,7 @@ func (c *Config) ValidateV2() error {
type GRPCConfig struct {
Address string `toml:"address"`
TCPAddress string `toml:"tcp_address"`
TCPTLSCA string `toml:"tcp_tls_ca"`
TCPTLSCert string `toml:"tcp_tls_cert"`
TCPTLSKey string `toml:"tcp_tls_key"`
UID int `toml:"uid"`

View File

@ -18,8 +18,11 @@ package server
import (
"context"
"crypto/tls"
"crypto/x509"
"expvar"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/pprof"
@ -111,11 +114,25 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
tcpServerOpts := serverOpts
if config.GRPC.TCPTLSCert != "" {
log.G(ctx).Info("setting up tls on tcp GRPC services...")
creds, err := credentials.NewServerTLSFromFile(config.GRPC.TCPTLSCert, config.GRPC.TCPTLSKey)
tlsCert, err := tls.LoadX509KeyPair(config.GRPC.TCPTLSCert, config.GRPC.TCPTLSKey)
if err != nil {
return nil, err
}
tcpServerOpts = append(tcpServerOpts, grpc.Creds(creds))
tlsConfig := &tls.Config{Certificates: []tls.Certificate{tlsCert}}
if config.GRPC.TCPTLSCA != "" {
caCertPool := x509.NewCertPool()
caCert, err := ioutil.ReadFile(config.GRPC.TCPTLSCA)
if err != nil {
return nil, errors.Wrap(err, "failed to load CA file")
}
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
tcpServerOpts = append(tcpServerOpts, grpc.Creds(credentials.NewTLS(tlsConfig)))
}
var (
grpcServer = grpc.NewServer(serverOpts...)