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>
208 lines
5.8 KiB
Go
208 lines
5.8 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/pem"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"log/slog"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
mglog "github.com/absmach/supermq/logger"
|
|
"github.com/caarlos0/env/v11"
|
|
"github.com/ultravioletrs/cocos/agent/cvms"
|
|
cvmsgrpc "github.com/ultravioletrs/cocos/agent/cvms/api/grpc"
|
|
"github.com/ultravioletrs/cocos/internal"
|
|
"github.com/ultravioletrs/cocos/internal/server"
|
|
grpcserver "github.com/ultravioletrs/cocos/internal/server/grpc"
|
|
"golang.org/x/sync/errgroup"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/reflection"
|
|
)
|
|
|
|
var _ cvmsgrpc.Service = (*svc)(nil)
|
|
|
|
const (
|
|
svcName = "cvms_test_server"
|
|
defaultPort = "7001"
|
|
)
|
|
|
|
var (
|
|
algoPath string
|
|
dataPathString string
|
|
dataPaths []string
|
|
attestedTLSString string
|
|
attestedTLS bool
|
|
pubKeyFile string
|
|
caUrl string
|
|
cvmId string
|
|
clientCAFile string
|
|
)
|
|
|
|
type svc struct {
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func (s *svc) Run(ctx context.Context, ipAddress string, sendMessage cvmsgrpc.SendFunc, authInfo credentials.AuthInfo) {
|
|
s.logger.Debug(fmt.Sprintf("received who am on ip address %s", ipAddress))
|
|
|
|
pubKey, err := os.ReadFile(pubKeyFile)
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("failed to read public key file: %s", err))
|
|
return
|
|
}
|
|
pubPem, _ := pem.Decode(pubKey)
|
|
|
|
var datasets []*cvms.Dataset
|
|
for _, dataPath := range dataPaths {
|
|
if _, err := os.Stat(dataPath); os.IsNotExist(err) {
|
|
s.logger.Error(fmt.Sprintf("data file does not exist: %s", dataPath))
|
|
return
|
|
}
|
|
dataHash, err := internal.Checksum(dataPath)
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("failed to calculate checksum: %s", err))
|
|
return
|
|
}
|
|
|
|
datasets = append(datasets, &cvms.Dataset{Hash: dataHash[:], UserKey: pubPem.Bytes})
|
|
}
|
|
|
|
algoHash, err := internal.Checksum(algoPath)
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("failed to calculate checksum: %s", err))
|
|
return
|
|
}
|
|
|
|
if err := sendMessage(&cvms.ServerStreamMessage{
|
|
Message: &cvms.ServerStreamMessage_RunReq{
|
|
RunReq: &cvms.ComputationRunReq{
|
|
Id: "1",
|
|
Name: "sample computation",
|
|
Description: "sample descrption",
|
|
Datasets: datasets,
|
|
Algorithm: &cvms.Algorithm{Hash: algoHash[:], UserKey: pubPem.Bytes},
|
|
ResultConsumers: []*cvms.ResultConsumer{{UserKey: pubPem.Bytes}},
|
|
AgentConfig: &cvms.AgentConfig{
|
|
Port: "7002",
|
|
AttestedTls: attestedTLS,
|
|
ClientCaFile: clientCAFile,
|
|
},
|
|
},
|
|
},
|
|
}); err != nil {
|
|
s.logger.Error(fmt.Sprintf("failed to send run request: %s", err))
|
|
return
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
flagSet := flag.NewFlagSet("tests/cvms/main.go", flag.ContinueOnError)
|
|
flagSet.StringVar(&algoPath, "algo-path", "", "Path to the algorithm")
|
|
flagSet.StringVar(&pubKeyFile, "public-key-path", "", "Path to the public key file")
|
|
flagSet.StringVar(&attestedTLSString, "attested-tls-bool", "", "Should aTLS be used, must be 'true' or 'false'")
|
|
flagSet.StringVar(&dataPathString, "data-paths", "", "Paths to data sources, list of string separated with commas")
|
|
flagSet.StringVar(&caUrl, "ca-url", "", "URL for certificate authority, must be specified if aTLS is used")
|
|
flagSet.StringVar(&cvmId, "cvm-id", "", "UUID for a CVM, must be specified if aTLS is used")
|
|
flagSet.StringVar(&clientCAFile, "client-ca-file", "", "Client CA root certificate file path")
|
|
|
|
flagSetParseError := flagSet.Parse(os.Args[1:])
|
|
if flagSetParseError != nil {
|
|
log.Fatalf("Error parsing flagas: %v", flagSetParseError)
|
|
}
|
|
|
|
parsingError := !flagSet.Parsed()
|
|
var parsingErrorString strings.Builder
|
|
|
|
parsingErrorString.WriteString("\n")
|
|
|
|
if algoPath == "" {
|
|
parsingErrorString.WriteString("Algorithm path is required\n")
|
|
parsingError = true
|
|
}
|
|
|
|
if pubKeyFile == "" {
|
|
parsingErrorString.WriteString("Public key path is required\n")
|
|
parsingError = true
|
|
}
|
|
|
|
attestedTLSBoolValue, err := strconv.ParseBool(attestedTLSString)
|
|
if err != nil {
|
|
parsingErrorString.WriteString("Attested TLS flag is required and it must be a boolean value\n")
|
|
parsingError = true
|
|
attestedTLS = false
|
|
} else {
|
|
attestedTLS = attestedTLSBoolValue
|
|
}
|
|
|
|
if dataPathString != "" {
|
|
dataPaths = strings.Split(dataPathString, ",")
|
|
}
|
|
|
|
if err == nil && caUrl != "" && !attestedTLS {
|
|
parsingErrorString.WriteString("CA URL is only available with attested TLS\n")
|
|
parsingError = true
|
|
}
|
|
|
|
if err == nil && cvmId != "" && !attestedTLS {
|
|
parsingErrorString.WriteString("CVM UUID is only available with attested TLS\n")
|
|
parsingError = true
|
|
}
|
|
|
|
if parsingError {
|
|
parsingErrorString.WriteString("Usage :\n")
|
|
flagSet.SetOutput(&parsingErrorString)
|
|
flagSet.PrintDefaults()
|
|
log.Fatal(parsingErrorString.String())
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
incomingChan := make(chan *cvms.ClientStreamMessage)
|
|
|
|
logger, err := mglog.New(os.Stdout, "debug")
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
go func() {
|
|
for incoming := range incomingChan {
|
|
fmt.Println(incoming.Message)
|
|
}
|
|
}()
|
|
|
|
registerAgentServiceServer := func(srv *grpc.Server) {
|
|
reflection.Register(srv)
|
|
cvms.RegisterServiceServer(srv, cvmsgrpc.NewServer(incomingChan, &svc{logger: logger}))
|
|
}
|
|
grpcServerConfig := server.ServerConfig{
|
|
BaseConfig: server.BaseConfig{
|
|
Port: defaultPort,
|
|
},
|
|
}
|
|
if err := env.ParseWithOptions(&grpcServerConfig, env.Options{}); err != nil {
|
|
logger.Error(fmt.Sprintf("failed to load %s gRPC client configuration : %s", svcName, err))
|
|
return
|
|
}
|
|
|
|
gs := grpcserver.New(ctx, cancel, svcName, grpcServerConfig, registerAgentServiceServer, logger, nil, caUrl, cvmId)
|
|
|
|
g.Go(func() error {
|
|
return gs.Start()
|
|
})
|
|
|
|
g.Go(func() error {
|
|
return server.StopHandler(ctx, cancel, logger, svcName, gs)
|
|
})
|
|
|
|
if err := g.Wait(); err != nil {
|
|
logger.Error(fmt.Sprintf("%s service terminated: %s", svcName, err))
|
|
}
|
|
}
|