mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
4b27b98edb
* Refactor attestation handling: rename AttestationResult to AzureAttestationToken - Updated the protobuf definition to change azureAttestationResponse to azureAttestationToken. - Refactored the Service interface and its implementation to replace AttestationResult with AzureAttestationToken. - Modified mock functions and tests to reflect the new naming and functionality. - Adjusted CLI commands to use the new AzureAttestationToken method. - Removed the AzureToken constant from the attestation package as it is no longer needed. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Remove redundant data checks and logging in SendData and sendData methods Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Update agent/api/grpc/server_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update agent/api/grpc/endpoint_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor attestation handling: rename AttestationToken to AzureAttestationToken in server and test files Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor attestation command output messages for clarity and consistency Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Rename AttestationToken to AzureAttestationToken in TestAttestationToken for consistency Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Refactor TestChangeAttestationConfiguration to use vtpm.ConvertPolicyToJSON for JSON conversion Signed-off-by: Sammy Oina <sammyoina@gmail.com> * Fix: reset temporary file pointer after zipping directory Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: Sammy Oina <sammyoina@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package cli
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/absmach/supermq/pkg/errors"
|
|
"github.com/fatih/color"
|
|
"github.com/spf13/cobra"
|
|
"github.com/ultravioletrs/cocos/agent"
|
|
"github.com/ultravioletrs/cocos/internal"
|
|
"google.golang.org/grpc/metadata"
|
|
)
|
|
|
|
var decompressDataset bool
|
|
|
|
func (cli *CLI) NewDatasetsCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "data",
|
|
Short: "Upload a dataset",
|
|
Example: "data <dataset_path> <private_key_file_path>",
|
|
Args: cobra.ExactArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if cli.connectErr != nil {
|
|
printError(cmd, "Failed to connect to agent: %v ❌ ", cli.connectErr)
|
|
return
|
|
}
|
|
|
|
datasetPath := args[0]
|
|
|
|
cmd.Println("Uploading dataset:", datasetPath)
|
|
|
|
f, err := os.Stat(datasetPath)
|
|
if err != nil {
|
|
printError(cmd, "Error reading dataset file: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
var dataset *os.File
|
|
|
|
if f.IsDir() {
|
|
cmd.Println("Detected directory, zipping dataset...")
|
|
dataset, err = internal.ZipDirectoryToTempFile(datasetPath)
|
|
if err != nil {
|
|
printError(cmd, "Error zipping dataset directory: %v ❌ ", err)
|
|
return
|
|
}
|
|
defer dataset.Close()
|
|
defer os.Remove(dataset.Name())
|
|
} else {
|
|
dataset, err = os.Open(datasetPath)
|
|
if err != nil {
|
|
printError(cmd, "Error reading dataset file: %v ❌ ", err)
|
|
return
|
|
}
|
|
defer dataset.Close()
|
|
}
|
|
|
|
privKeyFile, err := os.ReadFile(args[1])
|
|
if err != nil {
|
|
printError(cmd, "Error reading private key file: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
pemBlock, _ := pem.Decode(privKeyFile)
|
|
|
|
privKey, err := decodeKey(pemBlock)
|
|
if err != nil {
|
|
printError(cmd, "Error decoding private key: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
ctx := metadata.NewOutgoingContext(cmd.Context(), metadata.New(make(map[string]string)))
|
|
if err := cli.agentSDK.Data(addDatasetMetadata(ctx), dataset, path.Base(datasetPath), privKey); err != nil {
|
|
printError(cmd, "Failed to upload dataset due to error: %v ❌ ", err)
|
|
return
|
|
}
|
|
|
|
cmd.Println(color.New(color.FgGreen).Sprint("Successfully uploaded dataset! ✔ "))
|
|
},
|
|
}
|
|
|
|
cmd.Flags().BoolVarP(&decompressDataset, "decompress", "d", false, "Decompress the dataset on agent")
|
|
return cmd
|
|
}
|
|
|
|
func decodeKey(b *pem.Block) (interface{}, error) {
|
|
if b == nil {
|
|
return nil, errors.New("error decoding key")
|
|
}
|
|
switch b.Type {
|
|
case rsaKeyType:
|
|
privKey, err := x509.ParsePKCS8PrivateKey(b.Bytes)
|
|
if err != nil {
|
|
privKey, err = x509.ParsePKCS1PrivateKey(b.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return privKey, nil
|
|
case ecdsaKeyType:
|
|
privKey, err := x509.ParseECPrivateKey(b.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return privKey, nil
|
|
default:
|
|
return nil, errors.New("error decoding key")
|
|
}
|
|
}
|
|
|
|
func addDatasetMetadata(ctx context.Context) context.Context {
|
|
return agent.DecompressToContext(ctx, decompressDataset)
|
|
}
|