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.
325 lines
12 KiB
Go
325 lines
12 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const testSecret = "0123456789abcdef0123456789abcdef"
|
|
|
|
func validConfigJSON(extra string) string {
|
|
return `{"panel_url":"https://panel.example.com","agent_id":"42","agent_secret":"` + testSecret +
|
|
`","journal_path":"/tmp/virt-agent-operations.db",` +
|
|
`"storage_path_roots":["/tmp"],` +
|
|
`"firmware_loader":"/usr/share/OVMF/OVMF_CODE.fd","firmware_nvram_template":"/usr/share/OVMF/OVMF_VARS.fd",` +
|
|
`"nvram_directory":"/var/lib/libvirt/qemu/nvram"` +
|
|
extra + `}`
|
|
}
|
|
|
|
func TestLoadAcceptsReadableConfigRegardlessOfMode(t *testing.T) {
|
|
for _, mode := range []os.FileMode{0o600, 0o644, 0o666} {
|
|
t.Run(fmt.Sprintf("%#o", mode), func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
body := validConfigJSON("")
|
|
if err := os.WriteFile(path, []byte(body), mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(path, mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("readable mode %o was rejected: %v", mode, err)
|
|
}
|
|
if cfg.LibvirtURI != "qemu:///system" {
|
|
t.Fatalf("unexpected default URI: %q", cfg.LibvirtURI)
|
|
}
|
|
if cfg.OperationTimeoutSec != 840 {
|
|
t.Fatalf("unexpected default operation timeout: %d", cfg.OperationTimeoutSec)
|
|
}
|
|
if cfg.GuestAgentTimeoutSec != 15 {
|
|
t.Fatalf("unexpected default guest agent timeout: %d", cfg.GuestAgentTimeoutSec)
|
|
}
|
|
if cfg.HeavyIOConcurrency != 4 {
|
|
t.Fatalf("unexpected default heavy I/O concurrency: %d", cfg.HeavyIOConcurrency)
|
|
}
|
|
if cfg.PeerMigrationPort != 9443 {
|
|
t.Fatalf("unexpected default peer migration port: %d", cfg.PeerMigrationPort)
|
|
}
|
|
if cfg.PeerMigrationListenAddress() != ":9443" ||
|
|
cfg.PeerMigrationStagingPath() != "/tmp/.peer-migration-staging" {
|
|
t.Fatalf("unexpected automatic peer migration paths: listen=%q staging=%q",
|
|
cfg.PeerMigrationListenAddress(), cfg.PeerMigrationStagingPath())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatesContentNotPermissions(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(`{"panel_url":"http://panel.example.com/path","agent_id":"bad id","agent_secret":"short","journal_path":""}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("invalid content was accepted")
|
|
}
|
|
for _, want := range []string{"panel_url", "agent_id", "agent_secret", "journal_path"} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Fatalf("error %q does not mention %q", err, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadRejectsUnknownAndTrailingJSON(t *testing.T) {
|
|
for name, body := range map[string]string{
|
|
"unknown": validConfigJSON(`,"permission_mode":"0600"`),
|
|
"retired network selectors": validConfigJSON(`,"network_selectors":{"public":{"type":"bridge","source":"br0"}}`),
|
|
"retired managed pool": validConfigJSON(`,"managed_storage_pool":"retired"`),
|
|
"retired template pool": validConfigJSON(`,"template_storage_pool":"retired"`),
|
|
"retired seed pool": validConfigJSON(`,"seed_storage_pool":"retired"`),
|
|
"unused user ISO pool": validConfigJSON(`,"user_iso_storage_pool":"ignored"`),
|
|
"unused rescue pool": validConfigJSON(`,"rescue_storage_pool":"ignored"`),
|
|
"retired inbound listener": validConfigJSON(`,"listen_address":"0.0.0.0:9443"`),
|
|
"retired TLS key": validConfigJSON(`,"tls_key_file":"/etc/virt-agent/agent.key"`),
|
|
"retired peer listener": validConfigJSON(`,"peer_migration_listen":"0.0.0.0:9443"`),
|
|
"retired peer endpoint": validConfigJSON(`,"peer_migration_endpoint":"https://host.example.com:9443"`),
|
|
"retired peer certificate": validConfigJSON(`,"peer_migration_tls_cert":"/etc/virt-agent/peer.crt"`),
|
|
"retired peer key": validConfigJSON(`,"peer_migration_tls_key":"/etc/virt-agent/peer.key"`),
|
|
"retired peer CA": validConfigJSON(`,"peer_migration_ca":"/etc/virt-agent/peer-ca.crt"`),
|
|
"retired peer staging": validConfigJSON(`,"peer_migration_staging_path":"/tmp/staging"`),
|
|
"trailing": validConfigJSON("") + ` {}`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil {
|
|
t.Fatal("invalid JSON was accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresHTTPSPanelOriginAndAgentIdentity(t *testing.T) {
|
|
for name, body := range map[string]string{
|
|
"plain HTTP": strings.ReplaceAll(validConfigJSON(""),
|
|
`https://panel.example.com`, `http://panel.example.com`),
|
|
"path": strings.ReplaceAll(validConfigJSON(""),
|
|
`https://panel.example.com`, `https://panel.example.com/cloud`),
|
|
"credentials": strings.ReplaceAll(validConfigJSON(""),
|
|
`https://panel.example.com`, `https://user@panel.example.com`),
|
|
"zero port": strings.ReplaceAll(validConfigJSON(""),
|
|
`https://panel.example.com`, `https://panel.example.com:0`),
|
|
"empty port": strings.ReplaceAll(validConfigJSON(""),
|
|
`https://panel.example.com`, `https://panel.example.com:`),
|
|
"oversized port": strings.ReplaceAll(validConfigJSON(""),
|
|
`https://panel.example.com`, `https://panel.example.com:99999`),
|
|
"non-decimal ID": strings.ReplaceAll(validConfigJSON(""), `"agent_id":"42"`, `"agent_id":"host-hk-01"`),
|
|
"zero ID": strings.ReplaceAll(validConfigJSON(""), `"agent_id":"42"`, `"agent_id":"0"`),
|
|
"leading zero ID": strings.ReplaceAll(validConfigJSON(""), `"agent_id":"42"`, `"agent_id":"042"`),
|
|
"20 digit ID": strings.ReplaceAll(validConfigJSON(""), `"agent_id":"42"`,
|
|
`"agent_id":"12345678901234567890"`),
|
|
"short secret": strings.ReplaceAll(validConfigJSON(""), testSecret, `short`),
|
|
"oversized body": validConfigJSON(`,"max_body_bytes":1048577`),
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil {
|
|
t.Fatal("invalid outbound Agent configuration was accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadAcceptsMaximumLengthDecimalAgentID(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
body := strings.ReplaceAll(validConfigJSON(""), `"agent_id":"42"`, `"agent_id":"1234567890123456789"`)
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err != nil {
|
|
t.Fatalf("valid 19 digit Agent ID was rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresCleanAbsoluteJournalPath(t *testing.T) {
|
|
for name, value := range map[string]string{
|
|
"relative": "var/lib/virt-agent/operations.db",
|
|
"unclean": "/var/lib/virt-agent/../virt-agent/operations.db",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
body := strings.ReplaceAll(
|
|
validConfigJSON(""), "/tmp/virt-agent-operations.db", value,
|
|
)
|
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "clean absolute path") {
|
|
t.Fatalf("journal path %q error = %v", value, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatesStoragePathRoots(t *testing.T) {
|
|
for name, roots := range map[string]string{
|
|
"root": `"/"`,
|
|
"relative": `"srv/virt"`,
|
|
"unclean": `"/srv/../srv/virt"`,
|
|
"duplicates": `"/srv/virt","/srv/virt"`,
|
|
"empty": ``,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
body := strings.Replace(validConfigJSON(""), `"storage_path_roots":["/tmp"]`, `"storage_path_roots":[`+roots+`]`, 1)
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "storage_path_roots") {
|
|
t.Fatalf("invalid storage roots error = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatesSinglePeerMigrationPort(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(`,"peer_migration_port":10443`)), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil || cfg.PeerMigrationPort != 10443 ||
|
|
cfg.PeerMigrationListenAddress() != ":10443" {
|
|
t.Fatalf("peer migration port rejected: cfg=%+v err=%v", cfg, err)
|
|
}
|
|
for _, value := range []int{-1, 65536} {
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(
|
|
fmt.Sprintf(`,"peer_migration_port":%d`, value))), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil ||
|
|
!strings.Contains(err.Error(), "peer_migration_port must be between 1 and 65535") {
|
|
t.Fatalf("invalid peer migration port %d error = %v", value, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresNonceTTLToCoverFullClockSkewWindow(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(`,"clock_skew_seconds":300,"nonce_ttl_seconds":599`)), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "twice clock_skew_seconds") {
|
|
t.Fatalf("short nonce TTL error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadCapsOperationTimeoutBelowGatewayDeadline(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(`,"operation_timeout_seconds":840`)), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err != nil {
|
|
t.Fatalf("maximum layered operation timeout was rejected: %v", err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(`,"operation_timeout_seconds":841`)), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "between 5 and 840") {
|
|
t.Fatalf("operation timeout above Gateway margin error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatesGuestAgentTimeout(t *testing.T) {
|
|
for name, value := range map[string]int{
|
|
"minimum": 5,
|
|
"maximum": 120,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(fmt.Sprintf(`,"guest_agent_timeout_seconds":%d`, value))), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("guest agent timeout %d was rejected: %v", value, err)
|
|
}
|
|
if cfg.GuestAgentTimeoutSec != int64(value) {
|
|
t.Fatalf("guest agent timeout = %d, want %d", cfg.GuestAgentTimeoutSec, value)
|
|
}
|
|
})
|
|
}
|
|
|
|
for name, value := range map[string]int{
|
|
"below minimum": 4,
|
|
"above maximum": 121,
|
|
"negative": -1,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(fmt.Sprintf(`,"guest_agent_timeout_seconds":%d`, value))), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "guest_agent_timeout_seconds must be between 5 and 120") {
|
|
t.Fatalf("invalid guest agent timeout %d error = %v", value, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatesHeavyIOConcurrency(t *testing.T) {
|
|
for name, value := range map[string]int{
|
|
"minimum": 2,
|
|
"maximum": 4,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(fmt.Sprintf(`,"heavy_io_concurrency":%d`, value))), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("heavy I/O concurrency %d was rejected: %v", value, err)
|
|
}
|
|
if cfg.HeavyIOConcurrency != value {
|
|
t.Fatalf("heavy I/O concurrency = %d, want %d", cfg.HeavyIOConcurrency, value)
|
|
}
|
|
})
|
|
}
|
|
|
|
for name, value := range map[string]int{
|
|
"below minimum": 1,
|
|
"above maximum": 5,
|
|
"negative": -1,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(fmt.Sprintf(`,"heavy_io_concurrency":%d`, value))), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "heavy_io_concurrency must be between 2 and 4") {
|
|
t.Fatalf("invalid heavy I/O concurrency %d error = %v", value, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadRejectsRemovedRecoverySettings(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "agent.json")
|
|
if err := os.WriteFile(path, []byte(validConfigJSON(`,"recovery_max_attempts":7,"recovery_max_age_seconds":3600`)), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Load(path); err == nil || !strings.Contains(err.Error(), "unknown field") {
|
|
t.Fatalf("removed recovery settings error = %v", err)
|
|
}
|
|
}
|