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>
240 lines
6.0 KiB
Go
240 lines
6.0 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package sdk
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/absmach/supermq/pkg/errors"
|
|
"github.com/ultravioletrs/cocos/agent"
|
|
"github.com/ultravioletrs/cocos/agent/api/grpc"
|
|
"github.com/ultravioletrs/cocos/agent/auth"
|
|
"github.com/ultravioletrs/cocos/pkg/progressbar"
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
type SDK interface {
|
|
Algo(ctx context.Context, algorithm, requirements *os.File, privKey any) error
|
|
Data(ctx context.Context, dataset *os.File, filename string, privKey any) error
|
|
Result(ctx context.Context, privKey any, resultFile *os.File) error
|
|
Attestation(ctx context.Context, reportData [size64]byte, nonce [size32]byte, attType int, attestationFile *os.File) error
|
|
IMAMeasurements(ctx context.Context, resultFile *os.File) ([]byte, error)
|
|
AttestationResult(ctx context.Context, nonce [size32]byte, attType int, attestationFile *os.File) error
|
|
}
|
|
|
|
const (
|
|
size64 = 64
|
|
size32 = 32
|
|
algoProgressBarDescription = "Uploading algorithm"
|
|
dataProgressBarDescription = "Uploading data"
|
|
resultProgressDescription = "Downloading result"
|
|
attestationProgressDescription = "Downloading attestation"
|
|
imaMeasurementsProgressDescription = "Downloading Linux IMA measurements"
|
|
)
|
|
|
|
type agentSDK struct {
|
|
client agent.AgentServiceClient
|
|
}
|
|
|
|
func NewAgentSDK(agentClient agent.AgentServiceClient) SDK {
|
|
return &agentSDK{
|
|
client: agentClient,
|
|
}
|
|
}
|
|
|
|
func (sdk *agentSDK) Algo(ctx context.Context, algorithm, requirements *os.File, privKey any) error {
|
|
md, err := generateMetadata(string(auth.AlgorithmProviderRole), privKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for k, v := range md {
|
|
ctx = metadata.AppendToOutgoingContext(ctx, k, v[0])
|
|
}
|
|
|
|
stream, err := sdk.client.Algo(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pb := progressbar.New(false)
|
|
return pb.SendAlgorithm(algoProgressBarDescription, algorithm, requirements, stream)
|
|
}
|
|
|
|
func (sdk *agentSDK) Data(ctx context.Context, dataset *os.File, filename string, privKey any) error {
|
|
md, err := generateMetadata(string(auth.DataProviderRole), privKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for k, v := range md {
|
|
ctx = metadata.AppendToOutgoingContext(ctx, k, v[0])
|
|
}
|
|
|
|
stream, err := sdk.client.Data(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pb := progressbar.New(false)
|
|
return pb.SendData(dataProgressBarDescription, filename, dataset, stream)
|
|
}
|
|
|
|
func (sdk *agentSDK) Result(ctx context.Context, privKey any, resultFile *os.File) error {
|
|
request := &agent.ResultRequest{}
|
|
|
|
md, err := generateMetadata(string(auth.ConsumerRole), privKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, md)
|
|
stream, err := sdk.client.Result(ctx, request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
incomingmd, err := stream.Header()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileSizeStr := incomingmd.Get(grpc.FileSizeKey)
|
|
|
|
if len(fileSizeStr) == 0 {
|
|
fileSizeStr = append(fileSizeStr, "0")
|
|
}
|
|
|
|
fileSize, err := strconv.Atoi(fileSizeStr[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pb := progressbar.New(true)
|
|
|
|
return pb.ReceiveResult(resultProgressDescription, fileSize, stream, resultFile)
|
|
}
|
|
|
|
func (sdk *agentSDK) Attestation(ctx context.Context, reportData [size64]byte, nonce [size32]byte, attType int, attestationFile *os.File) error {
|
|
request := &agent.AttestationRequest{
|
|
TeeNonce: reportData[:],
|
|
VtpmNonce: nonce[:],
|
|
Type: int32(attType),
|
|
}
|
|
|
|
stream, err := sdk.client.Attestation(ctx, request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
incomingmd, err := stream.Header()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileSizeStr := incomingmd.Get(grpc.FileSizeKey)
|
|
|
|
if len(fileSizeStr) == 0 {
|
|
fileSizeStr = append(fileSizeStr, "0")
|
|
}
|
|
|
|
fileSize, err := strconv.Atoi(fileSizeStr[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pb := progressbar.New(true)
|
|
|
|
return pb.ReceiveAttestation(attestationProgressDescription, fileSize, stream, attestationFile)
|
|
}
|
|
|
|
func (sdk *agentSDK) AttestationResult(ctx context.Context, nonce [size32]byte, attType int, attestationResultFile *os.File) error {
|
|
request := &agent.AttestationResultRequest{
|
|
TokenNonce: nonce[:],
|
|
Type: int32(attType),
|
|
}
|
|
|
|
result, err := sdk.client.AttestationResult(ctx, request)
|
|
if err != nil {
|
|
return errors.Wrap(errors.New("failed to fetch attestation token"), err)
|
|
}
|
|
|
|
_, err = attestationResultFile.Write(result.GetFile())
|
|
if err != nil {
|
|
return errors.Wrap(errors.New("failed to write attestation result to file"), err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sdk *agentSDK) IMAMeasurements(ctx context.Context, resultFile *os.File) ([]byte, error) {
|
|
request := &agent.IMAMeasurementsRequest{}
|
|
|
|
stream, err := sdk.client.IMAMeasurements(ctx, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
incomingmd, err := stream.Header()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fileSizeStr := incomingmd.Get(grpc.FileSizeKey)
|
|
|
|
if len(fileSizeStr) == 0 {
|
|
fileSizeStr = append(fileSizeStr, "0")
|
|
}
|
|
|
|
fileSize, err := strconv.Atoi(fileSizeStr[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pb := progressbar.New(true)
|
|
|
|
return pb.ReceiveIMAMeasurements(imaMeasurementsProgressDescription, fileSize, stream, resultFile)
|
|
}
|
|
|
|
func signData(userID string, privKey crypto.Signer) ([]byte, error) {
|
|
var signature []byte
|
|
var err error
|
|
|
|
switch k := privKey.(type) {
|
|
case ed25519.PrivateKey:
|
|
signature, err = k.Sign(rand.Reader, []byte(userID), crypto.Hash(0))
|
|
case *rsa.PrivateKey, *ecdsa.PrivateKey:
|
|
hash := sha256.Sum256([]byte(userID))
|
|
signature, err = privKey.Sign(rand.Reader, hash[:], crypto.SHA256)
|
|
default:
|
|
return nil, errors.New("unsupported key type")
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return signature, nil
|
|
}
|
|
|
|
func generateMetadata(userID string, privateKey crypto.PrivateKey) (metadata.MD, error) {
|
|
signature, err := signData(userID, privateKey.(crypto.Signer))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
kv := make(map[string]string)
|
|
kv[auth.UserMetadataKey] = userID
|
|
kv[auth.SignatureMetadataKey] = base64.StdEncoding.EncodeToString(signature)
|
|
return metadata.New(kv), nil
|
|
}
|