Skip to content

Commit

Permalink
feat: enable tls on grpc server
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddhanta Rath committed Dec 18, 2023
1 parent bdd1da6 commit 1e33ede
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SERVER_WEBSOCKET_WRITE_WAIT_INTERVAL_MS=5000
SERVER_WEBSOCKET_PINGER_SIZE=1

SERVER_GRPC_PORT=8081
SERVER_GRPC_TLS_ENABLED=false

WORKER_BUFFER_CHANNEL_SIZE=5
WORKER_BUFFER_FLUSH_TIMEOUT_MS=5000
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SERVER_WEBSOCKET_WRITE_WAIT_INTERVAL_MS=1000
SERVER_WEBSOCKET_PINGER_SIZE=1

SERVER_GRPC_PORT=8081
SERVER_GRPC_TLS_ENABLED=false

WORKER_BUFFER_CHANNEL_SIZE=5
WORKER_BUFFER_FLUSH_TIMEOUT_MS=5000
Expand Down
14 changes: 11 additions & 3 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ type serverWs struct {
}

type serverGRPC struct {
Port string
Port string
TLSEnabled bool
TLSCertPath string
TLSPublicKey string
}

func serverConfigLoader() {
Expand Down Expand Up @@ -71,9 +74,14 @@ func serverWsConfigLoader() {
}

func serverGRPCConfigLoader() {

viper.SetDefault("SERVER_GRPC_PORT", "8081")
viper.SetDefault("SERVER_GRPC_TLS_ENABLED", false)
viper.SetDefault("SERVER_GRPC_TLS_CERT_PATH", "cert/server.crt")
viper.SetDefault("SERVER_GRPC_TLS_PUBLIC_KEY", "cert/server.key")
ServerGRPC = serverGRPC{
Port: util.MustGetString("SERVER_GRPC_PORT"),
Port: util.MustGetString("SERVER_GRPC_PORT"),
TLSEnabled: util.MustGetBool("SERVER_GRPC_TLS_ENABLED"),
TLSCertPath: util.MustGetString("SERVER_GRPC_TLS_CERT_PATH"),
TLSPublicKey: util.MustGetString("SERVER_GRPC_TLS_PUBLIC_KEY"),
}
}
25 changes: 24 additions & 1 deletion services/grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package grpc

import (
"context"
"crypto/tls"
"fmt"
"google.golang.org/grpc/credentials"
"net"

pbgrpc "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/raccoon/v1beta1/raccoonv1beta1grpc"
Expand All @@ -17,7 +19,7 @@ type Service struct {
}

func NewGRPCService(c collection.Collector) *Service {
server := grpc.NewServer()
server := newGRPCServer()
pbgrpc.RegisterEventServiceServer(server, &Handler{C: c})
return &Service{
s: server,
Expand All @@ -41,3 +43,24 @@ func (s *Service) Shutdown(context.Context) error {
s.s.GracefulStop()
return nil
}

func newGRPCServer() *grpc.Server {
if config.ServerGRPC.TLSEnabled {
return grpc.NewServer(grpc.Creds(loadTLSCredentials()))
}
return grpc.NewServer()
}

func loadTLSCredentials() credentials.TransportCredentials {
serverCert, err := tls.LoadX509KeyPair(config.ServerGRPC.TLSCertPath, config.ServerGRPC.TLSPublicKey)
if err != nil {
panic("failed to load TLS credentials to start grpc server with TLS")
}

config := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.NoClientCert,
}

return credentials.NewTLS(config)
}

0 comments on commit 1e33ede

Please sign in to comment.