Fix typos in attestation function names and update JSON handling in commands (#497)
CI / lint (push) Has been cancelled
CI / test (agent) (push) Has been cancelled
CI / test (cli) (push) Has been cancelled
CI / test (cmd) (push) Has been cancelled
CI / test (internal) (push) Has been cancelled
CI / test (manager, true) (push) Has been cancelled
CI / test (pkg) (push) Has been cancelled
CI / upload-coverage (push) Has been cancelled

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
This commit is contained in:
Sammy Kerata Oina
2025-08-11 17:18:54 +03:00
committed by GitHub
parent ccab296b62
commit 3e02cde7a2
4 changed files with 38 additions and 16 deletions
+3 -3
View File
@@ -246,7 +246,7 @@ func (cli *CLI) NewGetAttestationCmd() *cobra.Command {
switch attestationType {
case SNP:
result, err = attesationToJSON(result)
result, err = attestationToJSON(result)
if err != nil {
printError(cmd, "Error converting SNP attestation to JSON: %v ❌", err)
return
@@ -292,7 +292,7 @@ func (cli *CLI) NewGetAttestationCmd() *cobra.Command {
return cmd
}
func attesationToJSON(report []byte) ([]byte, error) {
func attestationToJSON(report []byte) ([]byte, error) {
if len(report) < abi.ReportSize {
return nil, errors.Wrap(errReportSize, fmt.Errorf("attestation contents too small (0x%x bytes). Want at least 0x%x bytes", len(report), abi.ReportSize))
}
@@ -304,7 +304,7 @@ func attesationToJSON(report []byte) ([]byte, error) {
return json.MarshalIndent(attestationPB, "", " ")
}
func attesationFromJSON(reportFile []byte) ([]byte, error) {
func attestationFromJSON(reportFile []byte) ([]byte, error) {
var attestationPB sevsnp.Attestation
if err := json.Unmarshal(reportFile, &attestationPB); err != nil {
return nil, err
+30 -8
View File
@@ -22,6 +22,7 @@ import (
"github.com/ultravioletrs/cocos/pkg/attestation/azure"
"github.com/ultravioletrs/cocos/pkg/attestation/gcp"
"github.com/ultravioletrs/cocos/pkg/attestation/vtpm"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
@@ -51,6 +52,7 @@ var (
errReadingManifestFile = errors.New("error while reading manifest file")
errDecodeHex = errors.New("error decoding hex string")
policy uint64 = 196639
isJsonAttestation bool
)
func (cli *CLI) NewAttestationPolicyCmd() *cobra.Command {
@@ -114,7 +116,7 @@ func (cli *CLI) NewAddHostDataCmd() *cobra.Command {
}
func (cli *CLI) NewGCPAttestationPolicy() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "gcp",
Short: "Get attestation policy for GCP CVM",
Example: `gcp <bin_vtmp_attestation_report_file> <vcpu_count>`,
@@ -134,9 +136,16 @@ func (cli *CLI) NewGCPAttestationPolicy() *cobra.Command {
attestation := &attest.Attestation{}
if err := proto.Unmarshal(attestationBin, attestation); err != nil {
printError(cmd, "Error unmarshaling attestation report: %v ❌ ", err)
return
if isJsonAttestation {
if err := protojson.Unmarshal(attestationBin, attestation); err != nil {
printError(cmd, "Error converting JSON attestation to binary: %v ❌", err)
return
}
} else {
if err := proto.Unmarshal(attestationBin, attestation); err != nil {
printError(cmd, "Error unmarshaling attestation report: %v ❌ ", err)
return
}
}
attestationPB := attestation.GetSevSnpAttestation()
@@ -173,10 +182,13 @@ func (cli *CLI) NewGCPAttestationPolicy() *cobra.Command {
cmd.Println("Attestation policy file generated successfully ✅")
},
}
cmd.Flags().BoolVarP(&isJsonAttestation, "json", "j", false, "Use JSON attestation report instead of binary")
return cmd
}
func (cli *CLI) NewDownloadGCPOvmfFile() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "download",
Short: "Download GCP OVMF file",
Example: `download <bin_vtmp_attestation_report_file>`,
@@ -190,9 +202,16 @@ func (cli *CLI) NewDownloadGCPOvmfFile() *cobra.Command {
attestation := &attest.Attestation{}
if err := proto.Unmarshal(attestationBin, attestation); err != nil {
printError(cmd, "Error unmarshaling attestation report: %v ❌ ", err)
return
if isJsonAttestation {
if err := protojson.Unmarshal(attestationBin, attestation); err != nil {
printError(cmd, "Error converting JSON attestation to binary: %v ❌", err)
return
}
} else {
if err := proto.Unmarshal(attestationBin, attestation); err != nil {
printError(cmd, "Error unmarshaling attestation report: %v ❌ ", err)
return
}
}
attestationPB := attestation.GetSevSnpAttestation()
@@ -231,6 +250,9 @@ func (cli *CLI) NewDownloadGCPOvmfFile() *cobra.Command {
cmd.Println("OVMF file downloaded successfully ✅")
},
}
cmd.Flags().BoolVarP(&isJsonAttestation, "json", "j", false, "Use JSON attestation report instead of binary")
return cmd
}
func (cli *CLI) NewAzureAttestationPolicy() *cobra.Command {
+1 -1
View File
@@ -473,7 +473,7 @@ func parseAttestationFile() error {
}
attestationRaw = file
if isFileJSON(attestationFile) {
attestationRaw, err = attesationFromJSON(attestationRaw)
attestationRaw, err = attestationFromJSON(attestationRaw)
if err != nil {
return err
}
+4 -4
View File
@@ -517,7 +517,7 @@ func TestAttestationToJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := attesationToJSON(tt.input)
got, err := attestationToJSON(tt.input)
assert.True(t, errors.Contains(err, tt.err))
if tt.err != nil {
assert.Nil(t, got)
@@ -588,7 +588,7 @@ func TestAttestationFromJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := attesationFromJSON(tt.input)
got, err := attestationFromJSON(tt.input)
assert.True(t, errors.Contains(err, tt.err))
tt.validate(t, got)
})
@@ -644,11 +644,11 @@ func TestIsFileJSON(t *testing.T) {
func TestRoundTrip(t *testing.T) {
originalReport, err := os.ReadFile("../attestation.bin")
require.NoError(t, err)
jsonData, err := attesationToJSON(originalReport)
jsonData, err := attestationToJSON(originalReport)
require.NoError(t, err)
require.NotNil(t, jsonData)
roundTripReport, err := attesationFromJSON(jsonData)
roundTripReport, err := attestationFromJSON(jsonData)
require.NoError(t, err)
require.NotNil(t, roundTripReport)
}