Merge pull request #5606 from zwtop/master
grpc config add options tcp_client_ca_cert
This commit is contained in:
commit
158901756c
@ -122,6 +122,7 @@ func (c *Config) ValidateV2() error {
|
|||||||
type GRPCConfig struct {
|
type GRPCConfig struct {
|
||||||
Address string `toml:"address"`
|
Address string `toml:"address"`
|
||||||
TCPAddress string `toml:"tcp_address"`
|
TCPAddress string `toml:"tcp_address"`
|
||||||
|
TCPTLSCA string `toml:"tcp_tls_ca"`
|
||||||
TCPTLSCert string `toml:"tcp_tls_cert"`
|
TCPTLSCert string `toml:"tcp_tls_cert"`
|
||||||
TCPTLSKey string `toml:"tcp_tls_key"`
|
TCPTLSKey string `toml:"tcp_tls_key"`
|
||||||
UID int `toml:"uid"`
|
UID int `toml:"uid"`
|
||||||
|
@ -18,8 +18,11 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"expvar"
|
"expvar"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/pprof"
|
"net/http/pprof"
|
||||||
@ -111,11 +114,25 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
|
|||||||
tcpServerOpts := serverOpts
|
tcpServerOpts := serverOpts
|
||||||
if config.GRPC.TCPTLSCert != "" {
|
if config.GRPC.TCPTLSCert != "" {
|
||||||
log.G(ctx).Info("setting up tls on tcp GRPC services...")
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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 (
|
var (
|
||||||
grpcServer = grpc.NewServer(serverOpts...)
|
grpcServer = grpc.NewServer(serverOpts...)
|
||||||
|
Loading…
Reference in New Issue
Block a user