mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
8eb1fac9ad
* Refactor and update dependencies in the project - Updated go.sum to replace `github.com/absmach/magistrala` with `github.com/absmach/supermq` across various modules. - Removed VSock configuration from environment variables and QEMU arguments. - Updated QEMU configuration and related tests to remove references to guest CID and VSock. - Added new HTTP transport layer for API endpoints in the manager. - Introduced Prometheus monitoring configuration with alert rules and Alertmanager setup. - Updated service and VM interfaces to remove unused methods and references. - Refactored tests to align with the new structure and dependencies. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add MaxVMs configuration and enforce limit on VM creation Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add comprehensive tests for HTTP transport handlers and endpoints Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Add test case for exceeding maximum number of VMs in TestRun Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Improve error handling in TestHandlerWithCustomRouter to ensure response writing is checked Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Update dependencies to latest versions - Upgrade cel.dev/expr from v0.23.0 to v0.24.0 - Upgrade github.com/absmach/supermq from v0.16.0 to v0.17.0 - Upgrade github.com/cenkalti/backoff from v4.3.0 to v5.0.2 - Upgrade github.com/cncf/xds/go to v0.0.0-20250501225837-2ac532fd4443 - Upgrade github.com/go-chi/chi/v5 from v5.2.1 to v5.2.2 - Upgrade github.com/go-jose/go-jose/v3 from v3.0.3 to v3.0.4 - Upgrade github.com/gofrs/uuid/v5 from v5.3.0 to v5.3.2 - Upgrade github.com/prometheus/client_golang from v1.22.0 to v1.23.0 - Upgrade github.com/prometheus/client_model from v0.6.1 to v0.6.2 - Upgrade github.com/prometheus/common from v0.62.0 to v0.65.0 - Upgrade github.com/prometheus/procfs from v0.15.1 to v0.16.1 - Upgrade go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp from v0.60.0 to v0.62.0 - Upgrade go.opentelemetry.io/otel/exporters/otlp/otlptrace from v1.36.0 to v1.37.0 - Upgrade golang.org/x/crypto from v0.39.0 to v0.40.0 - Upgrade golang.org/x/sys from v0.33.0 to v0.34.0 - Upgrade golang.org/x/text from v0.26.0 to v0.27.0 - Upgrade golang.org/x/time from v0.11.0 to v0.12.0 - Upgrade google.golang.org/grpc from v1.73.0 to v1.74.2 Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: Sammy Oina <sammyoina@gmail.com>
289 lines
7.8 KiB
Go
289 lines
7.8 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"log"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
mglog "github.com/absmach/supermq/logger"
|
|
"github.com/absmach/supermq/pkg/prometheus"
|
|
"github.com/caarlos0/env/v11"
|
|
"github.com/ultravioletrs/cocos/agent"
|
|
"github.com/ultravioletrs/cocos/agent/api"
|
|
"github.com/ultravioletrs/cocos/agent/cvms"
|
|
cvmsapi "github.com/ultravioletrs/cocos/agent/cvms/api/grpc"
|
|
"github.com/ultravioletrs/cocos/agent/cvms/server"
|
|
"github.com/ultravioletrs/cocos/agent/events"
|
|
agentlogger "github.com/ultravioletrs/cocos/internal/logger"
|
|
"github.com/ultravioletrs/cocos/pkg/attestation"
|
|
"github.com/ultravioletrs/cocos/pkg/attestation/azure"
|
|
"github.com/ultravioletrs/cocos/pkg/attestation/tdx"
|
|
"github.com/ultravioletrs/cocos/pkg/attestation/vtpm"
|
|
pkggrpc "github.com/ultravioletrs/cocos/pkg/clients/grpc"
|
|
cvmsgrpc "github.com/ultravioletrs/cocos/pkg/clients/grpc/cvm"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
const (
|
|
svcName = "agent"
|
|
defSvcGRPCPort = "7002"
|
|
retryInterval = 5 * time.Second
|
|
envPrefixCVMGRPC = "AGENT_CVM_GRPC_"
|
|
storageDir = "/var/lib/cocos/agent"
|
|
)
|
|
|
|
type config struct {
|
|
LogLevel string `env:"AGENT_LOG_LEVEL" envDefault:"debug"`
|
|
Vmpl int `env:"AGENT_VMPL" envDefault:"2"`
|
|
AgentGrpcHost string `env:"AGENT_GRPC_HOST" envDefault:"0.0.0.0"`
|
|
CAUrl string `env:"AGENT_CVM_CA_URL" envDefault:""`
|
|
CVMId string `env:"AGENT_CVM_ID" envDefault:""`
|
|
AgentMaaURL string `env:"AGENT_MAA_URL" envDefault:"https://sharedeus2.eus2.attest.azure.net"`
|
|
AgentOSBuild string `env:"AGENT_OS_BUILD" envDefault:"UVC"`
|
|
AgentOSDistro string `env:"AGENT_OS_DISTRO" envDefault:"UVC"`
|
|
AgentOSType string `env:"AGENT_OS_TYPE" envDefault:"UVC"`
|
|
}
|
|
|
|
func main() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
var cfg config
|
|
if err := env.Parse(&cfg); err != nil {
|
|
log.Fatalf("failed to load %s configuration : %s", svcName, err)
|
|
}
|
|
|
|
var exitCode int
|
|
defer mglog.ExitWithError(&exitCode)
|
|
|
|
var level slog.Level
|
|
if err := level.UnmarshalText([]byte(cfg.LogLevel)); err != nil {
|
|
log.Println(err)
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
eventsLogsQueue := make(chan *cvms.ClientStreamMessage, 1000)
|
|
|
|
handler := agentlogger.NewProtoHandler(os.Stdout, &slog.HandlerOptions{Level: level}, eventsLogsQueue)
|
|
logger := slog.New(handler)
|
|
|
|
eventSvc, err := events.New(svcName, eventsLogsQueue)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("failed to create events service %s", err.Error()))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
var provider attestation.Provider
|
|
ccPlatform := attestation.CCPlatform()
|
|
|
|
azureConfig := azure.NewEnvConfigFromAgent(
|
|
cfg.AgentOSBuild,
|
|
cfg.AgentOSType,
|
|
cfg.AgentOSDistro,
|
|
cfg.AgentMaaURL,
|
|
)
|
|
azure.InitializeDefaultMAAVars(azureConfig)
|
|
|
|
switch ccPlatform {
|
|
case attestation.SNP:
|
|
provider = vtpm.NewProvider(false, uint(cfg.Vmpl))
|
|
case attestation.SNPvTPM:
|
|
provider = vtpm.NewProvider(true, uint(cfg.Vmpl))
|
|
case attestation.Azure:
|
|
provider = azure.NewProvider()
|
|
case attestation.TDX:
|
|
provider = tdx.NewProvider()
|
|
case attestation.NoCC:
|
|
logger.Info("TEE device not found")
|
|
provider = &attestation.EmptyProvider{}
|
|
}
|
|
|
|
cvmGrpcConfig := pkggrpc.CVMClientConfig{}
|
|
if err := env.ParseWithOptions(&cvmGrpcConfig, env.Options{Prefix: envPrefixCVMGRPC}); err != nil {
|
|
logger.Error(fmt.Sprintf("failed to load %s gRPC client configuration : %s", svcName, err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
cvmGRPCClient, cvmsClient, err := cvmsgrpc.NewCVMClient(cvmGrpcConfig)
|
|
if err != nil {
|
|
logger.Error(err.Error())
|
|
exitCode = 1
|
|
return
|
|
}
|
|
defer cvmGRPCClient.Close()
|
|
|
|
reconnectFn := func(ctx context.Context) (pkggrpc.Client, cvms.Service_ProcessClient, error) {
|
|
grpcClient, newClient, err := cvmsgrpc.NewCVMClient(cvmGrpcConfig)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
// Don't defer close here as we want to keep the connection open
|
|
|
|
pc, err := newClient.Process(ctx)
|
|
if err != nil {
|
|
grpcClient.Close()
|
|
return nil, nil, err
|
|
}
|
|
return grpcClient, pc, nil
|
|
}
|
|
|
|
pc, err := cvmsClient.Process(ctx)
|
|
if err != nil {
|
|
logger.Error(err.Error())
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
if cfg.Vmpl < 0 || cfg.Vmpl > 3 {
|
|
logger.Error("vmpl level must be in a range [0, 3]")
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
svc := newService(ctx, logger, eventSvc, provider, cfg.Vmpl)
|
|
|
|
if err := os.MkdirAll(storageDir, 0o755); err != nil {
|
|
logger.Error(fmt.Sprintf("failed to create storage directory: %s", err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
mc, err := cvmsapi.NewClient(pc, svc, eventsLogsQueue, logger, server.NewServer(logger, svc, cfg.AgentGrpcHost, cfg.CAUrl, cfg.CVMId), storageDir, reconnectFn, cvmGRPCClient)
|
|
if err != nil {
|
|
logger.Error(err.Error())
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
g.Go(func() error {
|
|
ch := make(chan os.Signal, 1)
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
defer signal.Stop(ch)
|
|
|
|
select {
|
|
case <-ch:
|
|
logger.Info("Received signal, shutting down...")
|
|
cancel()
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
})
|
|
|
|
g.Go(func() error {
|
|
return mc.Process(ctx, cancel)
|
|
})
|
|
|
|
attest, certSerialNumber, err := attestationFromCert(ctx, cvmGrpcConfig.ClientCert, svc)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("failed to get attestation: %s", err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
if ccPlatform == attestation.Azure {
|
|
azureAttestationResult, azureCertSerialNumber, err := azureAttestationFromCert(ctx, cvmGrpcConfig.ClientCert, svc)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("failed to get attestation: %s", err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
eventsLogsQueue <- &cvms.ClientStreamMessage{
|
|
Message: &cvms.ClientStreamMessage_AzureAttestationResult{
|
|
AzureAttestationResult: &cvms.AzureAttestationResponse{
|
|
File: azureAttestationResult,
|
|
CertSerialNumber: azureCertSerialNumber,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
eventsLogsQueue <- &cvms.ClientStreamMessage{
|
|
Message: &cvms.ClientStreamMessage_VTPMattestationReport{
|
|
VTPMattestationReport: &cvms.AttestationResponse{
|
|
File: attest,
|
|
CertSerialNumber: certSerialNumber,
|
|
},
|
|
},
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
|
logger.Error(fmt.Sprintf("%s service terminated: %s", svcName, err))
|
|
}
|
|
}
|
|
|
|
func newService(ctx context.Context, logger *slog.Logger, eventSvc events.Service, provider attestation.Provider, vmpl int) agent.Service {
|
|
svc := agent.New(ctx, logger, eventSvc, provider, vmpl)
|
|
|
|
svc = api.LoggingMiddleware(svc, logger)
|
|
counter, latency := prometheus.MakeMetrics(svcName, "api")
|
|
svc = api.MetricsMiddleware(svc, counter, latency)
|
|
|
|
return svc
|
|
}
|
|
|
|
func attestationFromCert(ctx context.Context, certFilePath string, svc agent.Service) ([]byte, string, error) {
|
|
if certFilePath == "" {
|
|
return nil, "", nil
|
|
}
|
|
|
|
certFile, err := os.ReadFile(certFilePath)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
certPem, _ := pem.Decode(certFile)
|
|
certx509, err := x509.ParseCertificate(certPem.Bytes)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
nonceSNP := sha512.Sum512(certFile)
|
|
nonceVTPM := sha256.Sum256(certFile)
|
|
attest, err := svc.Attestation(ctx, nonceSNP, nonceVTPM, attestation.SNPvTPM)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
return attest, certx509.SerialNumber.String(), nil
|
|
}
|
|
|
|
func azureAttestationFromCert(ctx context.Context, certFilePath string, svc agent.Service) ([]byte, string, error) {
|
|
if certFilePath == "" {
|
|
return nil, "", nil
|
|
}
|
|
|
|
certFile, err := os.ReadFile(certFilePath)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
certPem, _ := pem.Decode(certFile)
|
|
certx509, err := x509.ParseCertificate(certPem.Bytes)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
nonceAzure := sha256.Sum256(certFile)
|
|
attestation, err := svc.AttestationResult(ctx, nonceAzure, attestation.AzureToken)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
return attestation, certx509.SerialNumber.String(), nil
|
|
}
|