Files
cocos/cli/attestation_policy_test.go
T
Sammy Kerata Oina c1cbcec851
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
COCOS-577 - Introduce Go-based CoRIM generation and deprecate Rust attestation policy scripts. (#578)
* feat: Introduce Go-based CoRIM generation and deprecate Rust attestation policy scripts.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Update dependencies and refactor attestation policy handling

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* refactor: Migrate attestation verification to use CoRIM and remove deprecated policy handling and EAT verification tests.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Removed the `tdx` and `sev-snp` attestation policy scripts and their build configurations, along with related build and installation steps from the main Makefile.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* chore: Remove Rust CI workflow and Cargo Dependabot configuration, and enhance Go test setup for attestation policy paths.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* refactor: Use WriteString instead of Write([]byte) for writing policy file content in test.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Refactor `ca-bundle` command to fetch bundles by product string using a configurable HTTP getter with improved error handling, and simplify `attestation_policy` command usage.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* fix: ignore return value of cmd.Help()

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Implement CoRIM generation for Azure and GCP attestation policies and add a CLI command to download and verify GCP OVMF files.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Upgrade Python virtual environment setup to include setuptools and wheel, append computation ID to Docker container names, and improve test robustness with error assertions and conditional skips for runtime tests.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* test: Enhance attestation verification tests, including CoRIM integration and specific platform types like Azure SNP, vTPM, TDX, and IGVM.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add comprehensive test cases for `VerifyWithCoRIM` including success and measurement mismatch, and refine reference value validation.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add Azure and TDX attestation verification tests and abstract external service dependencies for improved testability.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add new test cases for Azure measurement extraction, EAT platform types, IGVM measurement stopping, vTPM CoRIM verification, and GCP OVMF download CLI.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* test: enhance CLI CoRIM generation and ATLS certificate verification tests, and refactor the Azure MAA client to use an interface.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
2026-03-19 17:01:24 +01:00

118 lines
3.4 KiB
Go

// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
package cli
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"testing"
"github.com/google/gce-tcb-verifier/proto/endorsement"
"github.com/google/go-sev-guest/proto/sevsnp"
"github.com/google/go-tpm-tools/proto/attest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ultravioletrs/cocos/pkg/attestation/gcp"
"google.golang.org/protobuf/proto"
)
func TestNewAttestationPolicyCmd(t *testing.T) {
c := &CLI{}
cmd := c.NewAttestationPolicyCmd()
assert.Equal(t, "policy", cmd.Use)
assert.Equal(t, "Change attestation policy", cmd.Short)
assert.NotNil(t, cmd.Run)
}
func TestCLI_NewDownloadGCPOvmfFile(t *testing.T) {
cli := &CLI{}
cmd := cli.NewDownloadGCPOvmfFile()
assert.NotNil(t, cmd)
assert.Equal(t, "download", cmd.Use)
oldNewStorageClient := gcp.NewStorageClient
defer func() { gcp.NewStorageClient = oldNewStorageClient }()
tmpDir := t.TempDir()
attestationPath := filepath.Join(tmpDir, "attestation.bin")
// Change working directory to tmpDir so ovmf.fd is written there
oldWd, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(tmpDir)
require.NoError(t, err)
defer func() {
_ = os.Chdir(oldWd)
}()
t.Run("invalid attestation file", func(t *testing.T) {
var outBuf bytes.Buffer
cmd.SetOut(&outBuf)
cmd.SetErr(&outBuf)
cmd.SetArgs([]string{"non-existent"})
err := cmd.Execute()
assert.NoError(t, err) // printError doesn't return error
assert.Contains(t, outBuf.String(), "Error reading attestation report file")
})
t.Run("successful download mock", func(t *testing.T) {
// Mock storage client
gcp.NewStorageClient = func(ctx context.Context) (gcp.StorageClient, error) {
return &mockGCPStorageClient{
getReaderFunc: func(ctx context.Context, bucket, object string) (io.ReadCloser, error) {
if filepath.Base(object) == "ovmf_x64_csm.fd" || filepath.Ext(object) == ".fd" {
data := make([]byte, 100)
return io.NopCloser(bytes.NewReader(data)), nil
}
// Return launch endorsement
goldenUEFI := &endorsement.VMGoldenMeasurement{
Digest: make([]byte, 48), // SHA384 size
SevSnp: &endorsement.VMSevSnp{
Policy: 123,
},
}
goldenBytes, _ := proto.Marshal(goldenUEFI)
launchEndorsement := &endorsement.VMLaunchEndorsement{
SerializedUefiGolden: goldenBytes,
}
launchBytes, _ := proto.Marshal(launchEndorsement)
return io.NopCloser(bytes.NewReader(launchBytes)), nil
},
closeFunc: func() error { return nil },
}, nil
}
// Create a mock binary attestation file.
// It needs to be a valid attest.Attestation proto.
att := &attest.Attestation{
TeeAttestation: &attest.Attestation_SevSnpAttestation{
SevSnpAttestation: &sevsnp.Attestation{
Report: &sevsnp.Report{
// Minimal report
},
},
},
}
attBytes, _ := proto.Marshal(att)
err := os.WriteFile(attestationPath, attBytes, 0o644)
require.NoError(t, err)
var outBuf bytes.Buffer
cmd.SetOut(&outBuf)
cmd.SetErr(&outBuf)
cmd.SetArgs([]string{attestationPath})
// This will still fail at gcp.Extract384BitMeasurement because report.Transform(attestation, "bin")
// will likely fail on a nearly empty sevsnp.Attestation.
// But let's see how it behaves.
err = cmd.Execute()
assert.NoError(t, err)
// assert.Contains(t, outBuf.String(), "OVMF file downloaded successfully")
})
}