mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
906d7877b2
* Implement gRPC server with TLS and mTLS support - Added gRPC server implementation in pkg/server/grpc. - Introduced server configuration options for TLS and mTLS. - Implemented health check service for gRPC. - Created tests for server initialization, startup, and shutdown scenarios. - Added mock server for testing purposes. - Implemented graceful shutdown handling for the server. - Included documentation for the server package. Signed-off-by: SammyOina <sammyoina@gmail.com> * Add TLS and ATLS support to gRPC and HTTP clients; refactor security handling Signed-off-by: SammyOina <sammyoina@gmail.com> * Refactor server configuration structure to use Config instead of BaseConfig Signed-off-by: SammyOina <sammyoina@gmail.com> * Fix comments for consistency and clarity in TLS-related code Signed-off-by: SammyOina <sammyoina@gmail.com> * Add comprehensive tests for TLS and ATLS configurations in clients package Signed-off-by: SammyOina <sammyoina@gmail.com> * Refactor file permission constants in client tests to use octal notation Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add tests for HTTP server's TLS configuration and lifecycle management Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for TLS certificate handling and configuration Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for HTTP client configuration and transport Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor AttestationReportSize constant declaration for clarity Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor client configuration structure and update gRPC client implementations - Consolidated client configuration types into a unified structure with BaseConfig. - Introduced AttestedClientConfig and StandardClientConfig for specific use cases. - Updated gRPC client creation functions to utilize new configuration types. - Refactored tests to align with the new configuration structure. - Removed redundant ClientConfiguration interface and related methods. - Simplified TLS configuration loading logic for both standard and attested clients. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor client configuration structure and TLS handling - Introduced StandardClientConfig to replace BaseConfig, simplifying client configuration. - Updated AttestedClientConfig to embed StandardClientConfig instead of BaseConfig. - Modified ClientConfiguration interface to use Config() method instead of GetBaseConfig(). - Refactored various client tests to accommodate changes in configuration structure. - Added new TLS handling functions to support basic and attested TLS configurations. - Implemented comprehensive tests for TLS loading and configuration validation. - Removed deprecated methods and unnecessary code related to BaseConfig. Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com> Signed-off-by: Sammy Oina <sammyoina@gmail.com>
106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
type Server interface {
|
|
Start() error
|
|
Stop() error
|
|
}
|
|
|
|
type ServerConfiguration interface {
|
|
GetBaseConfig() ServerConfig
|
|
}
|
|
|
|
type Config struct {
|
|
Host string `env:"HOST" envDefault:"localhost"`
|
|
Port string `env:"PORT" envDefault:"7001"`
|
|
ServerCAFile string `env:"SERVER_CA_CERTS" envDefault:""`
|
|
CertFile string `env:"SERVER_CERT" envDefault:""`
|
|
KeyFile string `env:"SERVER_KEY" envDefault:""`
|
|
ClientCAFile string `env:"CLIENT_CA_CERTS" envDefault:""`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Config
|
|
}
|
|
type AgentConfig struct {
|
|
ServerConfig
|
|
AttestedTLS bool `env:"ATTESTED_TLS" envDefault:"false"`
|
|
}
|
|
|
|
type BaseServer struct {
|
|
Ctx context.Context
|
|
Cancel context.CancelFunc
|
|
Name string
|
|
Address string
|
|
Config ServerConfiguration
|
|
Logger *slog.Logger
|
|
Protocol string
|
|
}
|
|
|
|
func (s ServerConfig) GetBaseConfig() ServerConfig {
|
|
return s
|
|
}
|
|
|
|
func (a AgentConfig) GetBaseConfig() ServerConfig {
|
|
return a.ServerConfig
|
|
}
|
|
|
|
func NewBaseServer(
|
|
ctx context.Context, cancel context.CancelFunc, name string, config ServerConfiguration, logger *slog.Logger,
|
|
) BaseServer {
|
|
cfg := config.GetBaseConfig()
|
|
address := fmt.Sprintf("%s:%s", cfg.Host, cfg.Port)
|
|
|
|
return BaseServer{
|
|
Ctx: ctx,
|
|
Cancel: cancel,
|
|
Name: name,
|
|
Address: address,
|
|
Config: config,
|
|
Logger: logger,
|
|
}
|
|
}
|
|
|
|
func StopHandler(ctx context.Context, cancel context.CancelFunc, logger *slog.Logger, svcName string, servers ...Server) error {
|
|
var err error
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT)
|
|
select {
|
|
case sig := <-c:
|
|
defer cancel()
|
|
err = stopAllServer(servers...)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("%s service error during shutdown: %v", svcName, err))
|
|
}
|
|
logger.Info(fmt.Sprintf("%s service shutdown by signal: %s", svcName, sig))
|
|
return err
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func stopAllServer(servers ...Server) error {
|
|
var errs []error
|
|
for _, server := range servers {
|
|
if err := server.Stop(); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return fmt.Errorf("encountered errors while stopping servers: %v", errs)
|
|
}
|
|
|
|
return nil
|
|
}
|