|
|
|
|
@ -11,6 +11,7 @@ import (
|
|
|
|
|
"net/netip"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
|
|
@ -183,6 +184,7 @@ const domainMetadataNamespace = "https://virt.example/schema/domain/1"
|
|
|
|
|
type domainMetadataXML struct {
|
|
|
|
|
XMLName xml.Name `xml:"virt-cloud"`
|
|
|
|
|
XMLNS string `xml:"xmlns,attr"`
|
|
|
|
|
DisplayName string `xml:"display-name,omitempty"`
|
|
|
|
|
DiskGiB uint64 `xml:"disk-gib"`
|
|
|
|
|
BandwidthMbps uint64 `xml:"bandwidth-mbps"`
|
|
|
|
|
Hostname string `xml:"hostname,omitempty"`
|
|
|
|
|
@ -241,7 +243,7 @@ func marshalDomainMetadata(definition DomainDefinition) (string, error) {
|
|
|
|
|
return "", fmt.Errorf("marshal platform network policy metadata: %w", err)
|
|
|
|
|
}
|
|
|
|
|
encoded, err := xml.Marshal(domainMetadataXML{
|
|
|
|
|
XMLNS: domainMetadataNamespace, DiskGiB: definition.DiskGiB,
|
|
|
|
|
XMLNS: domainMetadataNamespace, DisplayName: definition.DisplayName, DiskGiB: definition.DiskGiB,
|
|
|
|
|
BandwidthMbps: definition.BandwidthMbps, Hostname: definition.Hostname,
|
|
|
|
|
PublicIPCIDR: definition.PublicIPCIDR, PublicPoolKey: definition.PublicPoolKey,
|
|
|
|
|
PublicPoolCIDR: definition.PublicPoolCIDR, Gateway: definition.Gateway,
|
|
|
|
|
@ -259,6 +261,9 @@ func ValidateCreateVM(request CreateVMRequest) error {
|
|
|
|
|
if request.UUID != "" && !uuidPattern.MatchString(request.UUID) {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_uuid", "uuid must be a canonical RFC 4122 value", nil)
|
|
|
|
|
}
|
|
|
|
|
if err := validateLibvirtDomainName(request.DomainName, request.UUID); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if request.Source != "cloud" {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_source", "source must be cloud", nil)
|
|
|
|
|
}
|
|
|
|
|
@ -394,8 +399,11 @@ func ValidateDomainDefinition(definition DomainDefinition) error {
|
|
|
|
|
if !uuidPattern.MatchString(definition.UUID) {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_uuid", "uuid must be a canonical RFC 4122 value", nil)
|
|
|
|
|
}
|
|
|
|
|
if !namePattern.MatchString(definition.Name) {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_name", "name must contain 1-80 safe characters", nil)
|
|
|
|
|
if err := validateLibvirtDomainName(definition.Name, definition.UUID); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !namePattern.MatchString(definition.DisplayName) {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_name", "display name must contain 1-80 safe characters", nil)
|
|
|
|
|
}
|
|
|
|
|
if definition.VCPUs == 0 || definition.VCPUs > 512 || definition.MemoryMiB < 128 || definition.MemoryMiB > 16*1024*1024 {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_resources", "domain CPU or memory is invalid", nil)
|
|
|
|
|
@ -452,6 +460,32 @@ func ValidateDomainDefinition(definition DomainDefinition) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LibvirtDomainName derives the host-local libvirt identity from authenticated
|
|
|
|
|
// ownership and the canonical VM UUID. It is never accepted from request JSON.
|
|
|
|
|
func LibvirtDomainName(userID int64, uuid string) (string, error) {
|
|
|
|
|
if userID <= 0 {
|
|
|
|
|
return "", NewError(ErrInvalid, "invalid_owner_id", "authenticated owner id must be positive", nil)
|
|
|
|
|
}
|
|
|
|
|
if !uuidPattern.MatchString(uuid) {
|
|
|
|
|
return "", NewError(ErrInvalid, "invalid_uuid", "uuid must be a canonical RFC 4122 value", nil)
|
|
|
|
|
}
|
|
|
|
|
compactUUID := strings.ReplaceAll(strings.ToLower(uuid), "-", "")
|
|
|
|
|
return strconv.FormatInt(userID, 10) + ":vm-" + compactUUID[:12], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateLibvirtDomainName(name, uuid string) error {
|
|
|
|
|
if !uuidPattern.MatchString(uuid) {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_uuid", "uuid must be a canonical RFC 4122 value", nil)
|
|
|
|
|
}
|
|
|
|
|
owner, suffix, found := strings.Cut(name, ":")
|
|
|
|
|
ownerID, err := strconv.ParseInt(owner, 10, 64)
|
|
|
|
|
compactUUID := strings.ReplaceAll(strings.ToLower(uuid), "-", "")
|
|
|
|
|
if !found || err != nil || ownerID <= 0 || owner != strconv.FormatInt(ownerID, 10) || suffix != "vm-"+compactUUID[:12] {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_domain_name", "libvirt domain name must match the authenticated owner and VM UUID", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ValidatePlatformNetworkPolicy(policy PlatformNetworkPolicySnapshot) error {
|
|
|
|
|
if policy.SchemaVersion != 1 || policy.BasePolicyVersion == 0 || len(policy.Rules) == 0 || len(policy.Rules) > 70 || !digestPattern.MatchString(policy.Digest) {
|
|
|
|
|
return NewError(ErrInvalid, "invalid_platform_network_policy", "platform network policy snapshot is invalid", nil)
|
|
|
|
|
@ -601,6 +635,9 @@ func metadataFromXML(document string) (VMMetadata, error) {
|
|
|
|
|
if err := xml.Unmarshal([]byte(domain.Metadata.XML), &stored); err != nil {
|
|
|
|
|
return VMMetadata{}, fmt.Errorf("parse virt-cloud domain metadata: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if stored.DisplayName != "" {
|
|
|
|
|
metadata.Name = stored.DisplayName
|
|
|
|
|
}
|
|
|
|
|
metadata.DiskGiB = stored.DiskGiB
|
|
|
|
|
metadata.BandwidthMbps = stored.BandwidthMbps
|
|
|
|
|
metadata.Hostname = stored.Hostname
|
|
|
|
|
|