Skip to content

Commit

Permalink
feat(grpc): add TLS support (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
buzzy authored Oct 21, 2023
1 parent 855abf4 commit 80509be
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
"crypto/tls"
"fmt"
"net"
"runtime/debug"
Expand All @@ -20,6 +21,7 @@ import (
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -139,10 +141,32 @@ func RunGRPCServer(ctx context.Context, cfg *config.ConfYaml) error {
grpc_recovery.UnaryServerInterceptor(recoveryOpt),
}

s := grpc.NewServer(
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
)
var s *grpc.Server

if cfg.Core.SSL && cfg.Core.CertPath != "" && cfg.Core.KeyPath != "" {
tlsCert, err := tls.LoadX509KeyPair(cfg.Core.CertPath, cfg.Core.KeyPath)
if err != nil {
logx.LogError.Error("failed to load tls cert file: ", err)
return err
}

tlsConfig := &tls.Config{

Check failure on line 153 in rpc/server.go

View workflow job for this annotation

GitHub Actions / lint

G402: TLS MinVersion too low. (gosec)
Certificates: []tls.Certificate{tlsCert},
ClientAuth: tls.NoClientCert,
}

s = grpc.NewServer(
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
)
} else {
s = grpc.NewServer(
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
)
}

rpcSrv := NewServer(cfg)
proto.RegisterGorushServer(s, rpcSrv)
proto.RegisterHealthServer(s, rpcSrv)
Expand Down

0 comments on commit 80509be

Please sign in to comment.