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>
161 lines
2.8 KiB
Go
161 lines
2.8 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package internal
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func ZipDirectoryToMemory(sourceDir string) ([]byte, error) {
|
|
buf := new(bytes.Buffer)
|
|
zipWriter := zip.NewWriter(buf)
|
|
|
|
err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
relPath, err := filepath.Rel(sourceDir, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
zipHeader, err := zip.FileInfoHeader(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
zipHeader.Name = relPath
|
|
|
|
zipWriterEntry, err := zipWriter.CreateHeader(zipHeader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileToZip, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fileToZip.Close()
|
|
|
|
_, err = io.Copy(zipWriterEntry, fileToZip)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
zipWriter.Close()
|
|
return nil, err
|
|
}
|
|
|
|
if err := zipWriter.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func ZipDirectoryToTempFile(sourceDir string) (*os.File, error) {
|
|
tmpFile, err := os.CreateTemp("", "dataset*.zip")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
zipWriter := zip.NewWriter(tmpFile)
|
|
|
|
err = filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
relPath, err := filepath.Rel(sourceDir, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
zipHeader, err := zip.FileInfoHeader(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
zipHeader.Name = relPath
|
|
|
|
zipWriterEntry, err := zipWriter.CreateHeader(zipHeader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileToZip, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fileToZip.Close()
|
|
|
|
_, err = io.Copy(zipWriterEntry, fileToZip)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
zipWriter.Close()
|
|
return nil, err
|
|
}
|
|
|
|
if err := zipWriter.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, err := tmpFile.Seek(0, 0); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tmpFile, nil
|
|
}
|
|
|
|
func UnzipFromMemory(zipData []byte, targetDir string) error {
|
|
reader := bytes.NewReader(zipData)
|
|
zipReader, err := zip.NewReader(reader, int64(len(zipData)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, file := range zipReader.File {
|
|
filePath := filepath.Join(targetDir, file.Name)
|
|
|
|
if file.FileInfo().IsDir() {
|
|
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
|
|
return err
|
|
}
|
|
|
|
srcFile, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer srcFile.Close()
|
|
|
|
dstFile, err := os.Create(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dstFile.Close()
|
|
|
|
if _, err := io.Copy(dstFile, srcFile); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|