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.
1038 lines
35 KiB
Go
1038 lines
35 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/netip"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/cloudinit"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
func validSSHKeyForTest() string {
|
|
keyBlob := make([]byte, 4+len("ssh-ed25519")+4+32)
|
|
binary.BigEndian.PutUint32(keyBlob[:4], uint32(len("ssh-ed25519")))
|
|
copy(keyBlob[4:], "ssh-ed25519")
|
|
offset := 4 + len("ssh-ed25519")
|
|
binary.BigEndian.PutUint32(keyBlob[offset:offset+4], 32)
|
|
for index := offset + 4; index < len(keyBlob); index++ {
|
|
keyBlob[index] = byte(index)
|
|
}
|
|
return "ssh-ed25519 " + base64.StdEncoding.EncodeToString(keyBlob) + " user@test"
|
|
}
|
|
|
|
func managedSeedForTest(t *testing.T) []byte {
|
|
t.Helper()
|
|
image, err := cloudinit.BuildISO(cloudinit.Request{
|
|
UUID: "123e4567-e89b-42d3-a456-426614174000", Name: "vm-test",
|
|
Hostname: "old.example.com", RootPassword: "StrongPassword!123",
|
|
StartupScript: "#!/bin/bash\necho once\n",
|
|
Network: cloudinit.Network{MAC: "52:54:00:12:34:56", Public: dualStackPublicNetwork(), InterfaceKey: "public0"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return image
|
|
}
|
|
|
|
func backupRestoreSeedForTest(t *testing.T) []byte {
|
|
t.Helper()
|
|
image, err := cloudinit.BuildISO(cloudinit.Request{
|
|
UUID: "123e4567-e89b-42d3-a456-426614174001", Name: "vm-restored",
|
|
Hostname: "vm-restored.example.com", PreserveGuestCredentials: true,
|
|
Network: cloudinit.Network{MAC: "52:54:00:12:34:57", Public: dualStackPublicNetwork(), InterfaceKey: "public0"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return image
|
|
}
|
|
|
|
func TestBackupRestoreSeedAllowsExplicitFutureSettings(t *testing.T) {
|
|
hostname := "after-restore.example.com"
|
|
password := "ExplicitReset!456"
|
|
sshKey := validSSHKeyForTest()
|
|
tests := []struct {
|
|
name string
|
|
request SettingsRequest
|
|
}{
|
|
{name: "hostname", request: SettingsRequest{Hostname: &hostname}},
|
|
{name: "root password", request: SettingsRequest{RootPassword: &password}},
|
|
{name: "SSH public key", request: SettingsRequest{SSHPublicKey: &sshKey}},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if _, _, err := mutateSeedSettings(
|
|
backupRestoreSeedForTest(t), test.request,
|
|
"operation-restore-settings",
|
|
); err != nil {
|
|
t.Fatalf("backup-restored instance setting failed: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMutateSeedSettingsPreservesStartupAndHashesNewPassword(t *testing.T) {
|
|
seed := managedSeedForTest(t)
|
|
originalFiles, err := readSeedISO(seed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var originalMetadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(originalFiles.MetaData), &originalMetadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hostname := "new.example.com"
|
|
password := "AnotherStrong!456"
|
|
updated, resultHostname, err := mutateSeedSettings(seed, SettingsRequest{Hostname: &hostname, RootPassword: &password}, "operation-settings")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resultHostname != hostname || bytes.Contains(updated, []byte(password)) {
|
|
t.Fatalf("hostname=%q plaintext=%v", resultHostname, bytes.Contains(updated, []byte(password)))
|
|
}
|
|
files, err := readSeedISO(updated)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Contains(files.UserData, []byte("echo once")) {
|
|
t.Fatal("settings update removed the immutable startup payload")
|
|
}
|
|
var metadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata["local-hostname"] != hostname {
|
|
t.Fatalf("local-hostname = %#v", metadata["local-hostname"])
|
|
}
|
|
instanceID, _ := metadata["instance-id"].(string)
|
|
if !strings.HasPrefix(instanceID, "virt-cloud-settings-") || instanceID == originalMetadata["instance-id"] {
|
|
t.Fatalf("settings instance-id was not rotated: old=%#v new=%#v", originalMetadata["instance-id"], instanceID)
|
|
}
|
|
if metadata["virt-cloud-network-policy"] != "boot-v1" {
|
|
t.Fatalf("network policy marker = %#v", metadata["virt-cloud-network-policy"])
|
|
}
|
|
if metadata["virt-cloud-settings-operation"] != "operation-settings" {
|
|
t.Fatalf("settings operation marker = %#v", metadata["virt-cloud-settings-operation"])
|
|
}
|
|
if !bytes.Equal(files.NetworkConfig, originalFiles.NetworkConfig) {
|
|
t.Fatal("settings update changed network-config")
|
|
}
|
|
}
|
|
|
|
func TestSanitizeSeedForDiskAuthoritativeRecoveryKeepsOnlyNetworkIntent(t *testing.T) {
|
|
original := managedSeedForTest(t)
|
|
originalFiles, err := readSeedISO(original)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sanitized, err := sanitizeSeedForDiskAuthoritativeRecovery(
|
|
original, "operation-recover-backup",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files, err := readSeedISO(sanitized)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(files.NetworkConfig, originalFiles.NetworkConfig) {
|
|
t.Fatal("backup recovery changed the current network-config")
|
|
}
|
|
var userData map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(
|
|
files.UserData, []byte("#cloud-config\n")), &userData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{
|
|
"users", "chpasswd", "ssh_pwauth", "disable_root",
|
|
"write_files", "runcmd",
|
|
} {
|
|
if _, exists := userData[forbidden]; exists {
|
|
t.Fatalf("disk-authoritative recovery seed contains %q: %#v", forbidden, userData)
|
|
}
|
|
}
|
|
if userData["preserve_hostname"] != true {
|
|
t.Fatalf("preserve_hostname = %#v", userData["preserve_hostname"])
|
|
}
|
|
if bytes.Contains(files.UserData, []byte("echo once")) ||
|
|
bytes.Contains(files.UserData, []byte("hashed_passwd")) {
|
|
t.Fatalf("recovery seed retained guest identity or startup payload: %s", files.UserData)
|
|
}
|
|
var originalMetadata, metadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(originalFiles.MetaData), &originalMetadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata["instance-id"] != originalMetadata["instance-id"] {
|
|
t.Fatalf("instance-id changed: old=%#v new=%#v",
|
|
originalMetadata["instance-id"], metadata["instance-id"])
|
|
}
|
|
if _, exists := metadata["local-hostname"]; exists {
|
|
t.Fatalf("recovery seed retained local-hostname: %#v", metadata)
|
|
}
|
|
if metadata["virt-cloud-backup-recovery-operation"] !=
|
|
"operation-recover-backup" {
|
|
t.Fatalf("backup recovery marker = %#v",
|
|
metadata["virt-cloud-backup-recovery-operation"])
|
|
}
|
|
}
|
|
|
|
func TestDiskAuthoritativeRecoveryPreservesCurrentVPCNetworkIntent(t *testing.T) {
|
|
port := validVPCPortMetadata()
|
|
attached, err := mutateSeedVPCPort(
|
|
managedSeedForTest(t), port, "operation-vpc-before-recovery", true,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recovered, err := sanitizeSeedForDiskAuthoritativeRecovery(
|
|
attached, "operation-recover-vpc-backup",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
private, err := seedVPCPort(recovered)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := seedVPCNetwork{
|
|
MAC: port.MAC, PrivateIP: port.PrivateIP,
|
|
CIDR: port.CIDR, Gateway: port.Gateway,
|
|
}
|
|
if private == nil || *private != want {
|
|
t.Fatalf("recovered VPC network = %#v, want %#v", private, want)
|
|
}
|
|
publicMAC, public, err := seedNetwork(recovered)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if publicMAC != "52:54:00:12:34:56" || !public.matches(dualStackPublicNetwork(), publicMAC) {
|
|
t.Fatalf("recovery changed public network: %q %#v", publicMAC, public)
|
|
}
|
|
}
|
|
|
|
func TestDiskAuthoritativeRecoverySeedAllowsExplicitFutureSettings(t *testing.T) {
|
|
sanitized, err := sanitizeSeedForDiskAuthoritativeRecovery(
|
|
managedSeedForTest(t), "operation-recover-backup",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
hostname := "after-recovery.example.com"
|
|
hostnameOnly, resultHostname, err := mutateSeedSettings(
|
|
sanitized, SettingsRequest{Hostname: &hostname}, "operation-hostname",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resultHostname != hostname {
|
|
t.Fatalf("hostname = %q", resultHostname)
|
|
}
|
|
hostnameFiles, err := readSeedISO(hostnameOnly)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var hostnameData map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(
|
|
hostnameFiles.UserData, []byte("#cloud-config\n")), &hostnameData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{"users", "chpasswd", "ssh_pwauth"} {
|
|
if _, exists := hostnameData[forbidden]; exists {
|
|
t.Fatalf("hostname-only update injected %q: %#v", forbidden, hostnameData)
|
|
}
|
|
}
|
|
|
|
password := "ExplicitReset!456"
|
|
passwordReset, _, err := mutateSeedSettings(
|
|
sanitized, SettingsRequest{RootPassword: &password}, "operation-password",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if bytes.Contains(passwordReset, []byte(password)) {
|
|
t.Fatal("explicit password reset stored the plaintext password")
|
|
}
|
|
passwordFiles, err := readSeedISO(passwordReset)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var passwordData map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(
|
|
passwordFiles.UserData, []byte("#cloud-config\n")), &passwordData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
users, ok := passwordData["users"].([]any)
|
|
if !ok || len(users) != 1 || passwordData["chpasswd"] == nil {
|
|
t.Fatalf("explicit password reset did not create managed credentials: %#v", passwordData)
|
|
}
|
|
|
|
passwordAfterHostname, _, err := mutateSeedSettings(
|
|
hostnameOnly, SettingsRequest{RootPassword: &password},
|
|
"operation-password-after-hostname",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
passwordAfterHostnameFiles, err := readSeedISO(passwordAfterHostname)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var passwordAfterHostnameData, passwordAfterHostnameMetadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(
|
|
passwordAfterHostnameFiles.UserData, []byte("#cloud-config\n")),
|
|
&passwordAfterHostnameData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(bytes.TrimSpace(
|
|
passwordAfterHostnameFiles.MetaData), &passwordAfterHostnameMetadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if passwordAfterHostnameData["preserve_hostname"] != false {
|
|
t.Fatalf("password-only update did not retain the explicit hostname: %#v",
|
|
passwordAfterHostnameData)
|
|
}
|
|
if passwordAfterHostnameMetadata["local-hostname"] != hostname {
|
|
t.Fatalf("password-only update lost the explicit hostname: %#v",
|
|
passwordAfterHostnameMetadata)
|
|
}
|
|
|
|
hostnameAfterPassword, _, err := mutateSeedSettings(
|
|
passwordReset, SettingsRequest{Hostname: &hostname},
|
|
"operation-hostname-after-password",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hostnameAfterPasswordFiles, err := readSeedISO(hostnameAfterPassword)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var hostnameAfterPasswordData map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(
|
|
hostnameAfterPasswordFiles.UserData, []byte("#cloud-config\n")),
|
|
&hostnameAfterPasswordData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
users, ok = hostnameAfterPasswordData["users"].([]any)
|
|
if !ok || len(users) != 1 || hostnameAfterPasswordData["chpasswd"] == nil ||
|
|
hostnameAfterPasswordData["ssh_pwauth"] != true {
|
|
t.Fatalf("hostname-only update lost the explicit password intent: %#v",
|
|
hostnameAfterPasswordData)
|
|
}
|
|
}
|
|
|
|
func TestDiskAuthoritativeRecoverySSHKeyDoesNotChangePasswordState(t *testing.T) {
|
|
sanitized, err := sanitizeSeedForDiskAuthoritativeRecovery(
|
|
managedSeedForTest(t), "operation-recover-backup",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := validSSHKeyForTest()
|
|
updated, _, err := mutateSeedSettings(
|
|
sanitized, SettingsRequest{SSHPublicKey: &key}, "operation-ssh-key",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files, err := readSeedISO(updated)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var userData map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(
|
|
files.UserData, []byte("#cloud-config\n")), &userData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{
|
|
"users", "chpasswd", "ssh_pwauth", "disable_root",
|
|
} {
|
|
if _, exists := userData[forbidden]; exists {
|
|
t.Fatalf("SSH-key-only update changed password state through %q: %#v",
|
|
forbidden, userData)
|
|
}
|
|
}
|
|
if bytes.Contains(files.UserData, []byte("lock_passwd")) ||
|
|
bytes.Contains(files.UserData, []byte("hashed_passwd")) {
|
|
t.Fatalf("SSH-key-only update included password state: %s", files.UserData)
|
|
}
|
|
writeFiles, ok := userData["write_files"].([]any)
|
|
if !ok || len(writeFiles) != 1 {
|
|
t.Fatalf("SSH-key-only update write_files = %#v", userData["write_files"])
|
|
}
|
|
entry, ok := writeFiles[0].(map[string]any)
|
|
if !ok || entry["path"] != diskSSHAuthorizedKeysPath ||
|
|
entry["content"] != key+"\n" || entry["permissions"] != "0600" {
|
|
t.Fatalf("SSH-key-only update file = %#v", writeFiles[0])
|
|
}
|
|
if !bytes.Equal(files.NetworkConfig,
|
|
mustReadSeedFiles(t, sanitized).NetworkConfig) {
|
|
t.Fatal("SSH-key-only update changed network-config")
|
|
}
|
|
|
|
hostname := "after-key.example.com"
|
|
hostnameOnly, _, err := mutateSeedSettings(
|
|
updated, SettingsRequest{Hostname: &hostname}, "operation-hostname-after-key",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hostnameFiles := mustReadSeedFiles(t, hostnameOnly)
|
|
if !bytes.Contains(hostnameFiles.UserData, []byte(diskSSHAuthorizedKeysPath)) ||
|
|
!bytes.Contains(hostnameFiles.UserData, []byte(key)) {
|
|
t.Fatalf("later hostname update lost the explicit SSH key intent: %s",
|
|
hostnameFiles.UserData)
|
|
}
|
|
}
|
|
|
|
func mustReadSeedFiles(t *testing.T, contents []byte) seedFiles {
|
|
t.Helper()
|
|
files, err := readSeedISO(contents)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return files
|
|
}
|
|
|
|
func TestMutateSeedNetworkChangesOnlyAddressIntent(t *testing.T) {
|
|
target := dualStackPublicNetwork()
|
|
target.IPv4.PoolCIDR = "9.9.9.0/24"
|
|
target.IPv4.AddressCIDR = "9.9.9.10/24"
|
|
target.IPv4.Gateway = "9.9.9.1"
|
|
target.IPv6.AddressCIDR = "2404:c140:156:3a::1001/64"
|
|
target.DNSServers = []string{"1.1.1.1", "2606:4700:4700::1111"}
|
|
updated, mac, err := mutateSeedNetwork(managedSeedForTest(t), target, "operation-network")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parsedMAC, public, err := seedNetwork(updated)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if mac != "52:54:00:12:34:56" || parsedMAC != mac || !public.matches(target, parsedMAC) {
|
|
t.Fatalf("network = mac=%q parsed=%q public=%#v", mac, parsedMAC, public)
|
|
}
|
|
files, err := readSeedISO(updated)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var metadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata["virt-cloud-network-operation"] != "operation-network" {
|
|
t.Fatalf("network operation marker = %#v", metadata["virt-cloud-network-operation"])
|
|
}
|
|
}
|
|
|
|
func TestSeedNetworkRejectsAutomaticOrUnfrozenLinkLocalIdentity(t *testing.T) {
|
|
files := mustReadSeedFiles(t, managedSeedForTest(t))
|
|
var network map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.NetworkConfig), &network); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ethernets, err := managedSeedEthernets(network)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
public, _ := ethernets[seedPublicInterfaceKey].(map[string]any)
|
|
delete(public, "link-local")
|
|
files.NetworkConfig, err = marshalJSONLine(network)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tampered, err := buildSeedISO(files)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := seedNetwork(tampered); err == nil {
|
|
t.Fatal("seed with automatic link-local generation was accepted")
|
|
}
|
|
}
|
|
|
|
func TestMutateSeedVPCPortPreservesPublicNetworkAndBindsMarker(t *testing.T) {
|
|
original := managedSeedForTest(t)
|
|
port := validVPCPortMetadata()
|
|
attached, err := mutateSeedVPCPort(original, port, "operation-vpc-port-attach", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
publicMAC, public, err := seedNetwork(attached)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if publicMAC != "52:54:00:12:34:56" || !public.matches(dualStackPublicNetwork(), publicMAC) {
|
|
t.Fatalf("public network changed: mac=%q public=%#v", publicMAC, public)
|
|
}
|
|
private, err := seedVPCPort(attached)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := seedVPCNetwork{
|
|
MAC: port.MAC, PrivateIP: port.PrivateIP,
|
|
CIDR: port.CIDR, Gateway: port.Gateway,
|
|
}
|
|
if private == nil || *private != want {
|
|
t.Fatalf("private network = %#v, want %#v", private, want)
|
|
}
|
|
files := mustReadSeedFiles(t, attached)
|
|
var metadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata[vpcPortOperationMarker] != "operation-vpc-port-attach" {
|
|
t.Fatalf("VPC operation marker = %#v", metadata[vpcPortOperationMarker])
|
|
}
|
|
|
|
preserved, err := preservedVPCCloudInitNetwork(
|
|
attached, func() *domainVPCPortMetadataXML {
|
|
value := domainVPCPortFromPublic(port)
|
|
return &value
|
|
}(),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if preserved == nil || preserved.MAC != port.MAC ||
|
|
preserved.PrivateIP != port.PrivateIP || preserved.CIDR != port.CIDR ||
|
|
preserved.Gateway != port.Gateway || preserved.InterfaceKey != seedVPCInterfaceKey {
|
|
t.Fatalf("preserved VPC network = %#v", preserved)
|
|
}
|
|
|
|
detached, err := mutateSeedVPCPort(attached, port, "operation-vpc-port-detach", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if private, err := seedVPCPort(detached); err != nil || private != nil {
|
|
t.Fatalf("detached VPC network = %#v err=%v", private, err)
|
|
}
|
|
}
|
|
|
|
func TestRepairSeedVPCPortConvergesOnlyTheSameManagedIdentity(t *testing.T) {
|
|
port := validVPCPortMetadata()
|
|
original := managedSeedForTest(t)
|
|
attached, err := repairSeedVPCPort(
|
|
original, port, "operation-vpc-repair-attach", true,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repaired, err := repairSeedVPCPort(
|
|
attached, port, "operation-vpc-repair-rebind", true,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
private, err := seedVPCPort(repaired)
|
|
want := seedVPCNetwork{
|
|
MAC: port.MAC, PrivateIP: port.PrivateIP,
|
|
CIDR: port.CIDR, Gateway: port.Gateway,
|
|
}
|
|
if err != nil || private == nil || *private != want {
|
|
t.Fatalf("repaired private network = %#v err=%v", private, err)
|
|
}
|
|
changed := port
|
|
changed.PrivateIP = "10.60.0.10"
|
|
if _, err := repairSeedVPCPort(
|
|
repaired, changed, "operation-vpc-repair-changed", true,
|
|
); err == nil {
|
|
t.Fatal("repair adopted a different VPC identity")
|
|
}
|
|
detached, err := repairSeedVPCPort(
|
|
repaired, port, "operation-vpc-repair-detach", false,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if detached, err = repairSeedVPCPort(
|
|
detached, port, "operation-vpc-repair-detach-again", false,
|
|
); err != nil {
|
|
t.Fatalf("repair could not converge an already absent seed port: %v", err)
|
|
}
|
|
if private, err := seedVPCPort(detached); err != nil || private != nil {
|
|
t.Fatalf("repair detach left private network = %#v err=%v", private, err)
|
|
}
|
|
}
|
|
|
|
func TestPreservedVPCCloudInitNetworkRejectsCrossLayerMismatch(t *testing.T) {
|
|
port := validVPCPortMetadata()
|
|
attached, err := mutateSeedVPCPort(
|
|
managedSeedForTest(t), port, "operation-vpc-port-mismatch", true,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := preservedVPCCloudInitNetwork(attached, nil); err == nil {
|
|
t.Fatal("seed-only VPC interface was accepted")
|
|
}
|
|
metadata := domainVPCPortFromPublic(port)
|
|
metadata.PrivateIP = "10.60.0.10"
|
|
if _, err := preservedVPCCloudInitNetwork(attached, &metadata); err == nil {
|
|
t.Fatal("mismatched seed and domain VPC identities were accepted")
|
|
}
|
|
}
|
|
|
|
func TestFirewallPolicyDigestAndXMLAreDeterministic(t *testing.T) {
|
|
policy := FirewallPolicy{Revision: 7, DefaultIn: "drop", DefaultOut: "accept", Rules: []FirewallRule{{Direction: "ingress", Action: "accept", Protocol: "tcp", CIDR: "8.8.8.0/24", PortFrom: 22, PortTo: 22}}}
|
|
validated, err := validateFirewallPolicy(policy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(validated.PolicyDigest) != 64 {
|
|
t.Fatalf("digest=%q", validated.PolicyDigest)
|
|
}
|
|
document, err := buildUserFirewallXML(`<filter name="virt-test" chain="root"><filterref filter="clean-traffic"/><rule action="accept" direction="inout" priority="900"><all/></rule></filter>`, validated)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, expected := range []string{`comment="virt-user:0"`, `comment="virt-default:in:ipv4"`, `comment="virt-default:in:ipv6"`, `comment="virt-default:out:ipv4"`} {
|
|
if !strings.Contains(document, expected) {
|
|
t.Fatalf("firewall XML missing %s: %s", expected, document)
|
|
}
|
|
}
|
|
validated.PolicyDigest = strings.Repeat("0", 64)
|
|
if _, err := validateFirewallPolicy(validated); err == nil {
|
|
t.Fatal("mismatched firewall digest was accepted")
|
|
}
|
|
}
|
|
|
|
func TestFirewallInboundDefaultsAreAddressFamilySpecific(t *testing.T) {
|
|
policy, err := validateFirewallPolicy(FirewallPolicy{
|
|
Revision: 1, DefaultIn: "drop", DefaultOut: "accept",
|
|
Rules: []FirewallRule{{
|
|
Direction: "ingress", Action: "accept", Protocol: "tcp",
|
|
CIDR: "203.0.113.0/24", PortFrom: 22, PortTo: 22,
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document, err := buildUserFirewallXML(`<filter name="virt-test" chain="root"></filter>`, policy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var filter libvirtxml.NWFilter
|
|
if err := filter.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, entry := range filter.Entries {
|
|
rule := entry.Rule
|
|
if rule == nil || rule.Direction != "in" ||
|
|
!strings.HasPrefix(nwfilterRuleComment(rule), "virt-default:in") {
|
|
continue
|
|
}
|
|
if rule.All != nil || rule.AllIPv6 != nil {
|
|
t.Fatalf("inbound default used a family-ambiguous all rule: %#v", rule)
|
|
}
|
|
switch nwfilterRuleComment(rule) {
|
|
case "virt-default:in:ipv4":
|
|
if rule.IP == nil || rule.IPv6 != nil {
|
|
t.Fatalf("IPv4 inbound default has the wrong protocol: %#v", rule)
|
|
}
|
|
seen["ipv4"] = true
|
|
case "virt-default:in:ipv6":
|
|
if rule.IPv6 == nil || rule.IP != nil {
|
|
t.Fatalf("IPv6 inbound default has the wrong protocol: %#v", rule)
|
|
}
|
|
seen["ipv6"] = true
|
|
default:
|
|
t.Fatalf("unexpected inbound default rule: %#v", rule)
|
|
}
|
|
}
|
|
if !reflect.DeepEqual(seen, map[string]bool{"ipv4": true, "ipv6": true}) {
|
|
t.Fatalf("inbound defaults = %#v, want IPv4 and IPv6", seen)
|
|
}
|
|
}
|
|
|
|
func TestFirewallDualStackDigestMatchesPanelContract(t *testing.T) {
|
|
policy := FirewallPolicy{
|
|
Revision: 1, DefaultIn: "drop", DefaultOut: "accept",
|
|
Rules: []FirewallRule{
|
|
{Direction: "ingress", Action: "accept", Protocol: "icmp", CIDR: "0.0.0.0/0"},
|
|
{Direction: "ingress", Action: "accept", Protocol: "tcp", CIDR: "203.0.113.55/32", PortFrom: 22, PortTo: 22},
|
|
{Direction: "ingress", Action: "accept", Protocol: "udp", CIDR: "::c000:201/128", PortFrom: 53, PortTo: 53},
|
|
},
|
|
}
|
|
validated, err := validateFirewallPolicy(policy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const expected = "ad6621eb15e30da61eeb4379a27cc7c9eda192f1a41b6cdc5ec00e1c2f8a0398"
|
|
if validated.PolicyDigest != expected {
|
|
t.Fatalf("dual-stack policy digest = %s, want %s", validated.PolicyDigest, expected)
|
|
}
|
|
base, err := BuildPlatformFilterXML("virt-test", dualStackPublicNetwork(), testPlatformPolicy())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document, err := buildUserFirewallXML(base, validated)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !managedFilterSemanticsValid(document, "virt-test", testPlatformPolicy()) {
|
|
t.Fatalf("dual-stack policy no longer satisfies the managed filter contract: %s", document)
|
|
}
|
|
var filter libvirtxml.NWFilter
|
|
if err := filter.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seen := map[int]string{}
|
|
for _, entry := range filter.Entries {
|
|
if index, ok := managedUserFirewallRuleIndex(entry.Rule); ok {
|
|
if entry.Rule.StateMatch != "" {
|
|
t.Fatalf("user rule %d disabled state tracking: %#v", index, entry.Rule)
|
|
}
|
|
switch {
|
|
case entry.Rule.ICMP != nil:
|
|
seen[index] = "icmp4"
|
|
case entry.Rule.TCP != nil:
|
|
seen[index] = "tcp4"
|
|
case entry.Rule.UDPIPv6 != nil:
|
|
seen[index] = "udp6"
|
|
}
|
|
}
|
|
}
|
|
if !reflect.DeepEqual(seen, map[int]string{0: "icmp4", 1: "tcp4", 2: "udp6"}) {
|
|
t.Fatalf("dual-stack user firewall protocols = %#v", seen)
|
|
}
|
|
}
|
|
|
|
func TestFirewallCIDRsMustUseCanonicalIPv4OrIPv6Text(t *testing.T) {
|
|
for _, cidr := range []string{
|
|
"0.0.0.0/0", "203.0.113.55/32", "::/0", "::c000:201/128",
|
|
"::ffff:192.0.2.0/120", "64:ff9b::/96",
|
|
"2001:db8:abcd:ef01:ff80::/73",
|
|
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128",
|
|
} {
|
|
policy := FirewallPolicy{Revision: 1, DefaultIn: "drop", DefaultOut: "accept", Rules: []FirewallRule{{
|
|
Direction: "ingress", Action: "accept", Protocol: "icmp", CIDR: cidr,
|
|
}}}
|
|
if _, err := validateFirewallPolicy(policy); err != nil {
|
|
t.Fatalf("canonical CIDR %q rejected: %v", cidr, err)
|
|
}
|
|
}
|
|
for _, cidr := range []string{
|
|
"203.0.113.55/24", "2001:db8::1/64", "2001:0DB8::/32",
|
|
"2001:db8:0:0::/32", "::192.0.2.1/128", "fe80::1%eth0/128",
|
|
} {
|
|
policy := FirewallPolicy{Revision: 1, DefaultIn: "drop", DefaultOut: "accept", Rules: []FirewallRule{{
|
|
Direction: "ingress", Action: "accept", Protocol: "icmp", CIDR: cidr,
|
|
}}}
|
|
if _, err := validateFirewallPolicy(policy); err == nil {
|
|
t.Fatalf("noncanonical CIDR %q was accepted", cidr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFirewallRuleXMLSelectsAddressFamilyAndKeepsUserRulesStateful(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cidr string
|
|
protocol string
|
|
ipv6 bool
|
|
}{
|
|
{name: "any v4", cidr: "198.51.100.0/24", protocol: "any"},
|
|
{name: "tcp v4", cidr: "198.51.100.1/32", protocol: "tcp"},
|
|
{name: "udp v4", cidr: "198.51.100.1/32", protocol: "udp"},
|
|
{name: "icmp v4", cidr: "0.0.0.0/0", protocol: "icmp"},
|
|
{name: "any v6", cidr: "2001:db8::/32", protocol: "any", ipv6: true},
|
|
{name: "tcp v6", cidr: "2001:db8::1/128", protocol: "tcp", ipv6: true},
|
|
{name: "udp v6", cidr: "::c000:201/128", protocol: "udp", ipv6: true},
|
|
{name: "icmp v6", cidr: "::/0", protocol: "icmp", ipv6: true},
|
|
}
|
|
for index, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
rule := FirewallRule{Direction: "ingress", Action: "accept", Protocol: test.protocol, CIDR: test.cidr}
|
|
if test.protocol == "tcp" || test.protocol == "udp" {
|
|
rule.PortFrom, rule.PortTo = 443, 443
|
|
}
|
|
entry := firewallRuleXML(rule, 100+index, "virt-user:"+strconv.Itoa(index))
|
|
if entry.StateMatch != "" {
|
|
t.Fatalf("user rule statematch = %q", entry.StateMatch)
|
|
}
|
|
v4 := entry.IP != nil || entry.TCP != nil || entry.UDP != nil || entry.ICMP != nil
|
|
v6 := entry.IPv6 != nil || entry.TCPIPv6 != nil || entry.UDPIPv6 != nil || entry.ICMPv6 != nil
|
|
if v4 == v6 || v6 != test.ipv6 {
|
|
t.Fatalf("wrong address-family XML: %#v", entry)
|
|
}
|
|
if parsed, ok := managedUserFirewallRuleIndex(entry); !ok || parsed != index {
|
|
t.Fatalf("managed user rule index = %d ok=%v", parsed, ok)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUserFirewallReplacesOnlyPlatformPassRulesAndKeepsAntiSpoof(t *testing.T) {
|
|
base, err := BuildPlatformFilterXML(
|
|
"virt-test", dualStackPublicNetwork(), testPlatformPolicy(),
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
policy, err := validateFirewallPolicy(FirewallPolicy{
|
|
Revision: 9, DefaultIn: "drop", DefaultOut: "accept",
|
|
Rules: []FirewallRule{{Direction: "ingress", Action: "accept", Protocol: "tcp", CIDR: "8.8.8.0/24", PortFrom: 22, PortTo: 22}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document, err := buildUserFirewallXML(base, policy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !managedFilterSemanticsValid(document, "virt-test", testPlatformPolicy()) {
|
|
t.Fatalf("valid user firewall no longer satisfies the managed security baseline: %s", document)
|
|
}
|
|
if strings.Contains(document, `comment="virt-platform-pass:in"`) ||
|
|
strings.Contains(document, `comment="virt-platform-pass:ipv4"`) ||
|
|
strings.Contains(document, `comment="virt-platform-pass:ipv6"`) {
|
|
t.Fatalf("broad platform pass rules survived user firewall replacement: %s", document)
|
|
}
|
|
for _, required := range []string{
|
|
`comment="block-spoofed-mac"`, `comment="block-spoofed-ipv4"`,
|
|
`comment="block-spoofed-ipv6"`, `comment="virt-platform-drop:out"`,
|
|
`comment="virt-platform-pass:in:arp"`, `comment="virt-platform-pass:in:rarp"`,
|
|
`comment="virt-user:0"`, `comment="virt-default:in:ipv4"`,
|
|
`comment="virt-default:in:ipv6"`, `comment="virt-default:out:ipv4"`,
|
|
} {
|
|
if !strings.Contains(document, required) {
|
|
t.Fatalf("rebuilt firewall is missing %s: %s", required, document)
|
|
}
|
|
}
|
|
var filter libvirtxml.NWFilter
|
|
if err := filter.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outboundDefaults := map[string]bool{}
|
|
inboundPlatform := map[string]int{}
|
|
inboundDefaultPriority := 0
|
|
for _, entry := range filter.Entries {
|
|
rule := entry.Rule
|
|
if rule != nil {
|
|
switch nwfilterRuleComment(rule) {
|
|
case "virt-platform-pass:in:arp":
|
|
inboundPlatform["arp"] = rule.Priority
|
|
case "virt-platform-pass:in:rarp":
|
|
inboundPlatform["rarp"] = rule.Priority
|
|
case "virt-default:in:ipv4", "virt-default:in:ipv6":
|
|
if inboundDefaultPriority == 0 || rule.Priority < inboundDefaultPriority {
|
|
inboundDefaultPriority = rule.Priority
|
|
}
|
|
}
|
|
}
|
|
if rule == nil || rule.Direction != "out" || rule.Action != "accept" ||
|
|
!strings.HasPrefix(nwfilterRuleComment(rule), "virt-default:out") {
|
|
continue
|
|
}
|
|
if rule.All != nil {
|
|
t.Fatalf("outbound default accept bypasses the L2 protocol allowlist: %#v", rule)
|
|
}
|
|
switch {
|
|
case rule.ARP != nil:
|
|
outboundDefaults["arp"] = true
|
|
case rule.RARP != nil:
|
|
outboundDefaults["rarp"] = true
|
|
case rule.IP != nil:
|
|
outboundDefaults["ipv4"] = true
|
|
case rule.IPv6 != nil:
|
|
outboundDefaults["ipv6"] = true
|
|
}
|
|
}
|
|
for _, protocol := range []string{"arp", "rarp", "ipv4", "ipv6"} {
|
|
if !outboundDefaults[protocol] {
|
|
t.Fatalf("outbound %s default did not preserve the L2 allowlist: %s", protocol, document)
|
|
}
|
|
}
|
|
for _, protocol := range []string{"arp", "rarp"} {
|
|
priority, ok := inboundPlatform[protocol]
|
|
if !ok || inboundDefaultPriority == 0 || priority >= inboundDefaultPriority {
|
|
t.Fatalf("inbound %s platform pass priority=%d default=%d", protocol, priority, inboundDefaultPriority)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFirewallLegacyIngressDefaultIsRejectedAndRebuilt(t *testing.T) {
|
|
const name = "virt-test"
|
|
platformPolicy := testPlatformPolicy()
|
|
base, err := BuildPlatformFilterXML(name, dualStackPublicNetwork(), platformPolicy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
policy, err := validateFirewallPolicy(FirewallPolicy{
|
|
Revision: 1, DefaultIn: "drop", DefaultOut: "accept",
|
|
Rules: []FirewallRule{{
|
|
Direction: "ingress", Action: "accept", Protocol: "tcp",
|
|
CIDR: "203.0.113.0/24", PortFrom: 22, PortTo: 22,
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document, err := buildUserFirewallXML(base, policy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !managedFilterSemanticsValid(document, name, platformPolicy) {
|
|
t.Fatal("valid split-default firewall was rejected")
|
|
}
|
|
|
|
var mixed libvirtxml.NWFilter
|
|
if err := mixed.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range mixed.Entries {
|
|
if nwfilterRuleComment(entry.Rule) == "virt-default:in:ipv6" {
|
|
entry.Rule.Action = "accept"
|
|
}
|
|
}
|
|
mixedDocument, err := mixed.Marshal()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if managedFilterSemanticsValid(mixedDocument, name, platformPolicy) {
|
|
t.Fatal("mixed IPv4/IPv6 ingress defaults were accepted")
|
|
}
|
|
|
|
var legacy libvirtxml.NWFilter
|
|
if err := legacy.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries := make([]libvirtxml.NWFilterEntry, 0, len(legacy.Entries)-1)
|
|
for _, entry := range legacy.Entries {
|
|
comment := nwfilterRuleComment(entry.Rule)
|
|
if comment == "virt-default:in:ipv4" || comment == "virt-default:in:ipv6" {
|
|
continue
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
entries = append(entries, libvirtxml.NWFilterEntry{Rule: &libvirtxml.NWFilterRule{
|
|
Action: "drop", Direction: "in", Priority: 800,
|
|
All: &libvirtxml.NWFilterRuleAll{Comment: "virt-default:in"},
|
|
}})
|
|
legacy.Entries = entries
|
|
legacyDocument, err := legacy.Marshal()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if managedFilterSemanticsValid(legacyDocument, name, platformPolicy) {
|
|
t.Fatal("legacy family-ambiguous ingress default was accepted")
|
|
}
|
|
rebuilt, err := buildUserFirewallXML(legacyDocument, policy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rebuilt == legacyDocument || !managedFilterSemanticsValid(rebuilt, name, platformPolicy) {
|
|
t.Fatal("reapplying the stored policy did not rebuild the legacy firewall")
|
|
}
|
|
}
|
|
|
|
func TestRemoteImportAddressPolicyBlocksLocalNetworks(t *testing.T) {
|
|
for _, raw := range []string{
|
|
"127.0.0.1", "10.0.0.1", "169.254.1.1", "100.64.0.1",
|
|
"192.0.2.1", "198.18.0.1", "198.51.100.1", "203.0.113.1",
|
|
"240.0.0.1", "::1", "fc00::1", "64:ff9b::1", "100::1",
|
|
"2001:2::1", "2001:db8::1", "2002::1",
|
|
} {
|
|
if remoteAddressAllowed(netip.MustParseAddr(raw)) {
|
|
t.Fatalf("private address %s was allowed", raw)
|
|
}
|
|
}
|
|
if !remoteAddressAllowed(netip.MustParseAddr("8.8.8.8")) {
|
|
t.Fatal("public address was rejected")
|
|
}
|
|
client := remoteHTTPClient(true)
|
|
transport, ok := client.Transport.(*http.Transport)
|
|
if !ok || !transport.DisableCompression || transport.TLSHandshakeTimeout != 15*time.Second ||
|
|
transport.ResponseHeaderTimeout != 30*time.Second || transport.ExpectContinueTimeout != time.Second {
|
|
t.Fatal("remote import client transport bounds are incomplete")
|
|
}
|
|
client.CloseIdleConnections()
|
|
if _, err := validateRemoteSource("http://example.com/image.iso"); err == nil {
|
|
t.Fatal("plain HTTP source URL was accepted")
|
|
}
|
|
}
|
|
|
|
func TestValidateQCOW2HeaderAcceptsBoundedStandaloneV3Image(t *testing.T) {
|
|
header := make([]byte, qcow2HeaderBytes)
|
|
copy(header[:4], []byte{'Q', 'F', 'I', 0xfb})
|
|
binary.BigEndian.PutUint32(header[4:8], 3)
|
|
binary.BigEndian.PutUint32(header[20:24], 16)
|
|
binary.BigEndian.PutUint64(header[24:32], 40<<30)
|
|
binary.BigEndian.PutUint32(header[36:40], 5)
|
|
binary.BigEndian.PutUint64(header[40:48], 1<<16)
|
|
binary.BigEndian.PutUint64(header[48:56], 2<<16)
|
|
binary.BigEndian.PutUint32(header[56:60], 1)
|
|
binary.BigEndian.PutUint32(header[96:100], 4)
|
|
binary.BigEndian.PutUint32(header[100:104], qcow2HeaderBytes)
|
|
virtualSize, err := validateQCOW2Header(header)
|
|
if err != nil || virtualSize != 40<<30 {
|
|
t.Fatalf("valid QCOW2 header = size %d error %v", virtualSize, err)
|
|
}
|
|
|
|
withBacking := append([]byte(nil), header...)
|
|
binary.BigEndian.PutUint64(withBacking[8:16], 1<<16)
|
|
binary.BigEndian.PutUint32(withBacking[16:20], 12)
|
|
if _, err := validateQCOW2Header(withBacking); err == nil {
|
|
t.Fatal("QCOW2 backing file was accepted")
|
|
}
|
|
|
|
badMagic := append([]byte(nil), header...)
|
|
copy(badMagic[:4], "NOPE")
|
|
if _, err := validateQCOW2Header(badMagic); err == nil {
|
|
t.Fatal("non-QCOW2 image was accepted")
|
|
}
|
|
|
|
unsupported := append([]byte(nil), header...)
|
|
binary.BigEndian.PutUint64(unsupported[72:80], 1)
|
|
if _, err := validateQCOW2Header(unsupported); err == nil {
|
|
t.Fatal("QCOW2 incompatible feature was accepted")
|
|
}
|
|
}
|
|
|
|
func TestWireResultsExcludeAgentLocalPathsAndConsoleTargets(t *testing.T) {
|
|
localRoot := "/var/lib/libvirt/images/private-root.qcow2"
|
|
localLoader := "/usr/share/edk2/ovmf/OVMF_CODE.fd"
|
|
localNVRAM := "/var/lib/libvirt/qemu/nvram/private_VARS.fd"
|
|
payload := struct {
|
|
VM VMMetadata `json:"vm"`
|
|
Firmware Firmware `json:"firmware_definition"`
|
|
Console ConsoleSession `json:"console"`
|
|
Snapshot SnapshotMutationResult `json:"snapshot"`
|
|
}{
|
|
VM: VMMetadata{
|
|
UUID: "123e4567-e89b-42d3-a456-426614174000", Name: "vm-test",
|
|
Machine: "q35", Firmware: "uefi", Disks: []Disk{{Path: localRoot, Device: "disk", Format: "qcow2"}},
|
|
},
|
|
Firmware: Firmware{Mode: "uefi", Loader: localLoader, NVRAMTemplate: localLoader + ".vars", NVRAMPath: localNVRAM},
|
|
Console: ConsoleSession{Protocol: "vnc", Address: "127.0.0.1:5900"},
|
|
Snapshot: SnapshotMutationResult{SnapshotKey: "0123456789abcdef0123456789abcdef", Restored: true, ActiveVolumeName: "vrestore-private.qcow2"},
|
|
}
|
|
encoded, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := string(encoded)
|
|
for _, forbidden := range []string{localRoot, localLoader, localNVRAM, "127.0.0.1:5900", "vrestore-private.qcow2", `"path"`, `"loader"`, `"nvram_template"`, `"nvram_path"`, `"address"`, `"active_volume_name"`} {
|
|
if strings.Contains(wire, forbidden) {
|
|
t.Fatalf("wire result leaked %q: %s", forbidden, wire)
|
|
}
|
|
}
|
|
if !strings.Contains(wire, `"device":"disk"`) || !strings.Contains(wire, `"protocol":"vnc"`) {
|
|
t.Fatalf("safe public fields were omitted: %s", wire)
|
|
}
|
|
}
|