You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
422 lines
15 KiB
Go
422 lines
15 KiB
Go
package domaincreateattach
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"g.luoli.live/root/virt/agent/internal/domaincreateplan"
|
|
"g.luoli.live/root/virt/agent/internal/domainpreflight"
|
|
"g.luoli.live/root/virt/agent/internal/hypervisor"
|
|
"g.luoli.live/root/virt/agent/internal/startup"
|
|
)
|
|
|
|
const (
|
|
attachDomainUUID = "00000000-0000-4000-8000-000000001101"
|
|
attachSeedUUID = "00000000-0000-4000-8000-000000001102"
|
|
)
|
|
|
|
func attachDomainRequest() domainpreflight.Request {
|
|
readonly := true
|
|
secureBoot := false
|
|
return domainpreflight.Request{
|
|
UUID: attachDomainUUID,
|
|
Firmware: domainpreflight.FirmwareEFI,
|
|
Architecture: domainpreflight.ArchitectureX86,
|
|
Machine: domainpreflight.MachineQ35,
|
|
OSType: domainpreflight.OSTypeHVM,
|
|
Loader: domainpreflight.LoaderPolicy{
|
|
Type: domainpreflight.LoaderPFlash,
|
|
Readonly: &readonly,
|
|
SecureBoot: &secureBoot,
|
|
},
|
|
}
|
|
}
|
|
|
|
type attachFixture struct {
|
|
request Request
|
|
artifact string
|
|
loader string
|
|
diskRoot string
|
|
diskPath string
|
|
seedFiles startup.SeedFiles
|
|
}
|
|
|
|
type fixtureLoaderProvider struct {
|
|
path string
|
|
err error
|
|
}
|
|
|
|
func (p fixtureLoaderProvider) PreferredNonSecureLoaderPath(context.Context) (string, error) {
|
|
return p.path, p.err
|
|
}
|
|
|
|
func newAttachFixture(t *testing.T) attachFixture {
|
|
t.Helper()
|
|
artifactRoot := canonicalAttachDir(t)
|
|
diskRoot := canonicalAttachDir(t)
|
|
loader := filepath.Join(canonicalAttachDir(t), "OVMF_CODE_4M.fd")
|
|
if err := os.WriteFile(loader, []byte("firmware"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
diskPath := filepath.Join(diskRoot, "volume-1.qcow2")
|
|
if err := os.WriteFile(diskPath, []byte("disk contents"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedFiles, err := startup.BuildCreateSeed(
|
|
"instance-attach-1",
|
|
"vm-attach-1",
|
|
`{"version":2,"ethernets":{}}`,
|
|
"#!/bin/sh\necho first boot\n",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
artifactPath := filepath.Join(artifactRoot, attachSeedUUID)
|
|
artifact, err := startup.WriteSeedDirectory(artifactPath, seedFiles)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest := domaincreateplan.SeedManifest{
|
|
Version: artifact.Manifest.Version,
|
|
User: domaincreateplan.FileDigest{
|
|
Bytes: artifact.Manifest.Files[startup.UserDataFilename].Bytes,
|
|
SHA256: artifact.Manifest.Files[startup.UserDataFilename].SHA256,
|
|
},
|
|
Meta: domaincreateplan.FileDigest{
|
|
Bytes: artifact.Manifest.Files[startup.MetaDataFilename].Bytes,
|
|
SHA256: artifact.Manifest.Files[startup.MetaDataFilename].SHA256,
|
|
},
|
|
Network: domaincreateplan.FileDigest{
|
|
Bytes: artifact.Manifest.Files[startup.NetworkConfigFilename].Bytes,
|
|
SHA256: artifact.Manifest.Files[startup.NetworkConfigFilename].SHA256,
|
|
},
|
|
}
|
|
return attachFixture{
|
|
request: Request{
|
|
Domain: attachDomainRequest(),
|
|
LoaderPath: loader,
|
|
ArtifactRoot: artifactRoot,
|
|
StorageRoots: []string{diskRoot},
|
|
SeedArtifactID: attachSeedUUID,
|
|
SeedManifest: manifest,
|
|
StartupScriptPresent: true,
|
|
StartupScriptSHA256: attachScriptDigest(),
|
|
Disks: []DiskAttachment{{
|
|
VolumeID: "volume-attach-1",
|
|
Slot: 0,
|
|
Path: diskPath,
|
|
}},
|
|
},
|
|
artifact: artifactPath,
|
|
loader: loader,
|
|
diskRoot: diskRoot,
|
|
diskPath: diskPath,
|
|
seedFiles: seedFiles,
|
|
}
|
|
}
|
|
|
|
func attachScriptDigest() string {
|
|
sum := sha256.Sum256([]byte("#!/bin/sh\necho first boot\n"))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func canonicalAttachDir(t *testing.T) string {
|
|
t.Helper()
|
|
path := t.TempDir()
|
|
resolved, err := filepath.EvalSymlinks(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return resolved
|
|
}
|
|
|
|
func TestBuildChecksSeedAndStorageWithoutMutation(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
plan, err := Build(context.Background(), fixture.request)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if plan.CommitBoundary != CommitBoundaryChecked || plan.LoaderPending {
|
|
t.Fatalf("result boundary/loader state = %q/%v", plan.CommitBoundary, plan.LoaderPending)
|
|
}
|
|
if plan.DomainUUID != attachDomainUUID || plan.SeedArtifactID != attachSeedUUID || plan.DiskCount != 1 {
|
|
t.Fatalf("result identity = %+v", plan.Result)
|
|
}
|
|
if len(plan.DomainXMLHash) != 64 || len(plan.DiskPlanHash) != 64 || len(plan.PlanHash) != 64 {
|
|
t.Fatalf("result hashes = %+v", plan.Result)
|
|
}
|
|
xml := string(plan.HostXML())
|
|
if !strings.Contains(xml, `loader`) || !strings.Contains(xml, fixture.loader) {
|
|
t.Fatalf("host XML did not pin selected loader: %s", xml)
|
|
}
|
|
for _, forbidden := range []string{"<disk", "<interface", fixture.diskPath, fixture.artifact} {
|
|
if strings.Contains(xml, forbidden) {
|
|
t.Fatalf("host base XML contains forbidden %q: %s", forbidden, xml)
|
|
}
|
|
}
|
|
if got := plan.SeedDirectory(); got != fixture.artifact {
|
|
t.Fatalf("seed directory = %q, want %q", got, fixture.artifact)
|
|
}
|
|
disks := plan.DiskAttachments()
|
|
if len(disks) != 1 || disks[0].Path != fixture.diskPath {
|
|
t.Fatalf("disk plan = %+v", disks)
|
|
}
|
|
// Accessors must not expose mutable backing storage.
|
|
xmlBytes := plan.HostXML()
|
|
xmlBytes[0] = 'X'
|
|
if plan.HostXML()[0] == 'X' {
|
|
t.Fatal("HostXML returned an aliased byte slice")
|
|
}
|
|
disks[0].Path = "changed"
|
|
if plan.DiskAttachments()[0].Path != fixture.diskPath {
|
|
t.Fatal("DiskAttachments returned an aliased slice")
|
|
}
|
|
encoded, err := json.Marshal(plan)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{fixture.loader, fixture.diskPath, fixture.artifact, string(fixture.seedFiles.UserData)} {
|
|
if strings.Contains(string(encoded), forbidden) {
|
|
t.Fatalf("safe plan JSON leaked %q: %s", forbidden, encoded)
|
|
}
|
|
}
|
|
if !strings.Contains(string(encoded), `"disk_count":1`) {
|
|
t.Fatalf("safe plan JSON omitted disk count: %s", encoded)
|
|
}
|
|
if _, err := os.Stat(fixture.diskPath); err != nil {
|
|
t.Fatalf("disk was unexpectedly removed: %v", err)
|
|
}
|
|
entries, err := os.ReadDir(fixture.artifact)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 4 {
|
|
t.Fatalf("seed entry count changed = %d, want 4", len(entries))
|
|
}
|
|
}
|
|
|
|
func TestBuildWithLoaderProviderUsesFreshHostSelection(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
fixture.request.LoaderPath = filepath.Join(fixture.diskRoot, "panel-supplied-loader.fd")
|
|
plan, err := BuildWithLoaderProvider(context.Background(), fixtureLoaderProvider{path: fixture.loader}, fixture.request)
|
|
if err != nil {
|
|
t.Fatalf("BuildWithLoaderProvider() error = %v", err)
|
|
}
|
|
if !strings.Contains(string(plan.HostXML()), fixture.loader) {
|
|
t.Fatalf("host XML did not use provider-selected loader: %s", plan.HostXML())
|
|
}
|
|
if strings.Contains(string(plan.HostXML()), "panel-supplied-loader") {
|
|
t.Fatal("host XML used the request's loader path instead of fresh provider output")
|
|
}
|
|
}
|
|
|
|
func TestBuildWithLoaderProviderClassifiesObservationFailure(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
_, err := BuildWithLoaderProvider(context.Background(), fixtureLoaderProvider{err: errors.New("capability transport unavailable")}, fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"loader_unavailable" || IsDefinitive(err) || !errors.Is(err, ErrHostUnavailable) {
|
|
t.Fatalf("BuildWithLoaderProvider() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildWithLoaderProviderClassifiesHostPolicyRejection(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
_, err := BuildWithLoaderProvider(context.Background(), fixtureLoaderProvider{err: fmt.Errorf("%w: %w", hypervisor.ErrCapabilityPolicy, hypervisor.ErrNoNonSecureLoader)}, fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"loader_rejected" || !IsDefinitive(err) || !errors.Is(err, hypervisor.ErrCapabilityPolicy) {
|
|
t.Fatalf("BuildWithLoaderProvider() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsSeedBeforeStorageMutationOrInspection(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
fixture.request.SeedManifest.User.SHA256 = strings.Repeat("a", 64)
|
|
fixture.request.Disks[0].Path = filepath.Join(fixture.diskRoot, "missing.qcow2")
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"seed_artifact_rejected" || !IsDefinitive(err) || !errors.Is(err, ErrSeedRejected) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsMissingSeedAsDefinitive(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
fixture.request.SeedArtifactID = "00000000-0000-4000-8000-000000001199"
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"seed_artifact_rejected" || !IsDefinitive(err) || !errors.Is(err, ErrSeedRejected) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildClassifiesStorageBoundary(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mutate func(*attachFixture)
|
|
code string
|
|
definitive bool
|
|
cause error
|
|
}{
|
|
{
|
|
name: "outside root",
|
|
mutate: func(f *attachFixture) {
|
|
f.request.Disks[0].Path = filepath.Join(canonicalAttachDir(t), "outside.qcow2")
|
|
if err := os.WriteFile(f.request.Disks[0].Path, []byte("outside"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
},
|
|
code: codePrefix + "storage_rejected",
|
|
definitive: true,
|
|
cause: ErrStorageRejected,
|
|
},
|
|
{
|
|
name: "root unavailable",
|
|
mutate: func(f *attachFixture) {
|
|
f.request.StorageRoots = []string{filepath.Join(canonicalAttachDir(t), "not-mounted")}
|
|
},
|
|
code: codePrefix + "storage_unavailable",
|
|
definitive: false,
|
|
cause: ErrHostUnavailable,
|
|
},
|
|
{
|
|
name: "duplicate resolved path",
|
|
mutate: func(f *attachFixture) {
|
|
f.request.Disks = append(f.request.Disks, DiskAttachment{VolumeID: "volume-attach-2", Slot: 1, Path: f.request.Disks[0].Path})
|
|
},
|
|
code: codePrefix + "storage_rejected",
|
|
definitive: true,
|
|
cause: ErrStorageRejected,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
tt.mutate(&fixture)
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != tt.code || IsDefinitive(err) != tt.definitive || !errors.Is(err, tt.cause) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsUnsafeLoaderAndDiskIntents(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mutate func(*Request)
|
|
code string
|
|
}{
|
|
{name: "secure boot loader name", mutate: func(r *Request) { r.LoaderPath = filepath.Join(filepath.Dir(r.LoaderPath), "OVMF_SECBOOT.fd") }, code: codePrefix + "loader_rejected"},
|
|
{name: "duplicate slot", mutate: func(r *Request) {
|
|
r.Disks = append(r.Disks, DiskAttachment{VolumeID: "volume-attach-2", Slot: 0, Path: r.Disks[0].Path})
|
|
}, code: codePrefix + "storage_rejected"},
|
|
{name: "invalid volume id", mutate: func(r *Request) { r.Disks[0].VolumeID = "../escape" }, code: codePrefix + "storage_rejected"},
|
|
{name: "unresolved path", mutate: func(r *Request) { r.Disks[0].Path = "" }, code: codePrefix + "storage_rejected"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
if strings.Contains(tt.name, "secure") {
|
|
if err := os.WriteFile(fixture.request.LoaderPath, []byte("firmware"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixture.request.LoaderPath = filepath.Join(filepath.Dir(fixture.request.LoaderPath), "OVMF_SECBOOT.fd")
|
|
if err := os.WriteFile(fixture.request.LoaderPath, []byte("firmware"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
tt.mutate(&fixture.request)
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != tt.code || !IsDefinitive(err) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildClassifiesMissingLoaderAsUncertain(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
fixture.request.LoaderPath = filepath.Join(filepath.Dir(fixture.loader), "missing-loader.fd")
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"loader_unavailable" || IsDefinitive(err) || !errors.Is(err, ErrHostUnavailable) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsLoaderSymlink(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
link := filepath.Join(filepath.Dir(fixture.loader), "OVMF-link.fd")
|
|
if err := os.Symlink(fixture.loader, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixture.request.LoaderPath = link
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"loader_rejected" || !IsDefinitive(err) || !errors.Is(err, ErrInvalidRequest) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildRejectsWritableLoaderAncestor(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
writable := filepath.Join(canonicalAttachDir(t), "firmware")
|
|
if err := os.Mkdir(writable, 0o770); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(writable, 0o770); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loader := filepath.Join(writable, "OVMF_CODE_4M.fd")
|
|
if err := os.WriteFile(loader, []byte("firmware"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixture.request.LoaderPath = loader
|
|
_, err := Build(context.Background(), fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"loader_rejected" || !IsDefinitive(err) {
|
|
t.Fatalf("writable loader ancestor was accepted: err=%v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestBuildHonorsCancellationBeforeFilesystemWork(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, err := Build(ctx, fixture.request)
|
|
if err == nil || Code(err) != codePrefix+"canceled" || IsDefinitive(err) || !errors.Is(err, ErrCanceled) {
|
|
t.Fatalf("Build() error = %v code=%q definitive=%v", err, Code(err), IsDefinitive(err))
|
|
}
|
|
}
|
|
|
|
func TestRequestJSONDoesNotCarryHostResolutionFields(t *testing.T) {
|
|
fixture := newAttachFixture(t)
|
|
encoded, err := json.Marshal(fixture.request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{fixture.loader, fixture.request.ArtifactRoot, fixture.diskPath, "LoaderPath", "StorageRoots"} {
|
|
if strings.Contains(string(encoded), forbidden) {
|
|
t.Fatalf("request JSON leaked host field %q: %s", forbidden, encoded)
|
|
}
|
|
}
|
|
if !strings.Contains(string(encoded), `"volume_id":"volume-attach-1"`) {
|
|
t.Fatalf("request JSON lost opaque volume identity: %s", encoded)
|
|
}
|
|
}
|
|
|
|
func TestDiskPlanHashIsCanonicalBySlot(t *testing.T) {
|
|
left := []DiskAttachment{
|
|
{VolumeID: "volume-b", Slot: 1, Readonly: true},
|
|
{VolumeID: "volume-a", Slot: 0},
|
|
}
|
|
right := []DiskAttachment{
|
|
{VolumeID: "volume-a", Slot: 0},
|
|
{VolumeID: "volume-b", Slot: 1, Readonly: true},
|
|
}
|
|
if digestDiskPlan(left) != digestDiskPlan(right) {
|
|
t.Fatal("disk plan hash depends on input ordering instead of slot identity")
|
|
}
|
|
}
|