2023-10-02 17:03:41 +02:00
|
|
|
package conn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2023-12-29 20:54:08 +01:00
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2023-12-10 20:21:54 +01:00
|
|
|
"fmt"
|
2023-10-02 17:03:41 +02:00
|
|
|
"net"
|
2023-12-29 20:54:08 +01:00
|
|
|
"os"
|
2023-10-02 17:03:41 +02:00
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conf"
|
|
|
|
logging "github.com/tim-beatham/smegmesh/pkg/log"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/rpc"
|
2023-10-02 17:03:41 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
)
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
// ConnectionServer manages gRPC server peer connections
|
2023-10-02 17:03:41 +02:00
|
|
|
type ConnectionServer struct {
|
2023-10-24 01:12:38 +02:00
|
|
|
// server an instance of the grpc server
|
2023-12-29 20:54:08 +01:00
|
|
|
server *grpc.Server
|
2023-10-24 01:12:38 +02:00
|
|
|
// the ctrl service to manage node
|
2023-10-02 17:03:41 +02:00
|
|
|
ctrlProvider rpc.MeshCtrlServerServer
|
2023-10-24 01:12:38 +02:00
|
|
|
// the sync service to synchronise nodes
|
2023-10-20 13:41:06 +02:00
|
|
|
syncProvider rpc.SyncServiceServer
|
2023-12-10 20:21:54 +01:00
|
|
|
Conf *conf.DaemonConfiguration
|
2023-10-24 01:12:38 +02:00
|
|
|
listener net.Listener
|
2023-10-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
// NewConnectionServerParams contains params for creating a new connection server
|
2023-10-02 17:03:41 +02:00
|
|
|
type NewConnectionServerParams struct {
|
2023-12-10 20:21:54 +01:00
|
|
|
Conf *conf.DaemonConfiguration
|
2023-10-10 21:14:40 +02:00
|
|
|
CtrlProvider rpc.MeshCtrlServerServer
|
2023-10-20 13:41:06 +02:00
|
|
|
SyncProvider rpc.SyncServiceServer
|
2023-10-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnectionServer: create a new gRPC connection server instance
|
|
|
|
func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer, error) {
|
2023-10-10 21:14:40 +02:00
|
|
|
cert, err := tls.LoadX509KeyPair(params.Conf.CertificatePath, params.Conf.PrivateKeyPath)
|
2023-10-02 17:03:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf("Failed to load key pair: %s\n", err.Error())
|
2023-10-02 17:03:41 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
serverAuth := tls.RequireAndVerifyClientCert
|
|
|
|
|
2023-10-10 21:14:40 +02:00
|
|
|
if params.Conf.SkipCertVerification {
|
2023-10-02 17:03:41 +02:00
|
|
|
serverAuth = tls.RequireAnyClientCert
|
|
|
|
}
|
|
|
|
|
2023-12-29 20:54:08 +01:00
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
if params.Conf.CaCertificatePath == "" {
|
|
|
|
return nil, errors.New("CA Cert is not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := os.ReadFile(params.Conf.CaCertificatePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := certPool.AppendCertsFromPEM(caCert); !ok {
|
|
|
|
return nil, errors.New("could not parse PEM")
|
|
|
|
}
|
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
serverConfig := &tls.Config{
|
|
|
|
ClientAuth: serverAuth,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
2023-12-29 20:54:08 +01:00
|
|
|
ClientCAs: certPool,
|
2023-10-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server := grpc.NewServer(
|
|
|
|
grpc.Creds(credentials.NewTLS(serverConfig)),
|
|
|
|
)
|
|
|
|
|
|
|
|
ctrlProvider := params.CtrlProvider
|
2023-10-20 13:41:06 +02:00
|
|
|
syncProvider := params.SyncProvider
|
2023-10-02 17:03:41 +02:00
|
|
|
|
|
|
|
connServer := ConnectionServer{
|
2023-10-24 01:12:38 +02:00
|
|
|
server: server,
|
|
|
|
ctrlProvider: ctrlProvider,
|
|
|
|
syncProvider: syncProvider,
|
|
|
|
Conf: params.Conf,
|
2023-10-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &connServer, nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
// Listen for incoming requests. Returns an error if something went wrong.
|
2023-10-02 17:03:41 +02:00
|
|
|
func (s *ConnectionServer) Listen() error {
|
|
|
|
rpc.RegisterMeshCtrlServerServer(s.server, s.ctrlProvider)
|
2023-10-20 13:41:06 +02:00
|
|
|
rpc.RegisterSyncServiceServer(s.server, s.syncProvider)
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Conf.GrpcPort))
|
2023-10-24 01:12:38 +02:00
|
|
|
s.listener = lis
|
2023-10-20 13:41:06 +02:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
logging.Log.WriteInfof("GRPC listening on %d\n", s.Conf.GrpcPort)
|
2023-10-02 17:03:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
2023-10-02 17:03:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.server.Serve(lis); err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
2023-10-02 17:03:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-24 01:12:38 +02:00
|
|
|
|
|
|
|
// Close closes the connection server. Returns an error
|
|
|
|
// if something went wrong whilst attempting to close the connection
|
|
|
|
func (c *ConnectionServer) Close() error {
|
|
|
|
var err error = nil
|
|
|
|
c.server.Stop()
|
|
|
|
|
|
|
|
if c.listener != nil {
|
|
|
|
err = c.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|