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.
126 lines
5.7 KiB
Go
126 lines
5.7 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/peermigration"
|
|
)
|
|
|
|
const peerMigrationMaximumLifetime = 7 * 24 * time.Hour
|
|
|
|
func ValidatePreparePeerMigrationRequest(request PreparePeerMigrationRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.MigrationID) ||
|
|
request.ExpiresAt <= time.Now().Unix() ||
|
|
request.ExpiresAt > time.Now().Add(peerMigrationMaximumLifetime).Unix() ||
|
|
len(request.Artifacts) < 1 || len(request.Artifacts) > peermigration.MaxArtifacts {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_prepare", "peer migration preparation identity is invalid", nil)
|
|
}
|
|
if _, err := peermigration.TicketDigest(request.Ticket); err != nil {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_ticket", "peer migration ticket is invalid", err)
|
|
}
|
|
seen := make(map[string]struct{}, len(request.Artifacts))
|
|
rootCount := 0
|
|
for _, artifact := range request.Artifacts {
|
|
if !catalogIDPattern.MatchString(artifact.ID) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_artifact", "peer migration artifact identity is invalid", nil)
|
|
}
|
|
if _, duplicate := seen[artifact.ID]; duplicate {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_artifact", "peer migration artifact identities must be unique", nil)
|
|
}
|
|
seen[artifact.ID] = struct{}{}
|
|
switch artifact.Role {
|
|
case "root":
|
|
rootCount++
|
|
if artifact.SnapshotKey != "" {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_artifact", "peer migration root artifact is invalid", nil)
|
|
}
|
|
case "snapshot":
|
|
if !catalogIDPattern.MatchString(artifact.SnapshotKey) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_artifact", "peer migration snapshot identity is invalid", nil)
|
|
}
|
|
default:
|
|
return NewError(ErrInvalid, "invalid_peer_migration_artifact", "peer migration artifact role is invalid", nil)
|
|
}
|
|
}
|
|
if rootCount != 1 {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_artifact", "peer migration requires exactly one root artifact", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateReceivePeerMigrationRequest(request ReceivePeerMigrationRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.MigrationID) ||
|
|
!uuidPattern.MatchString(strings.ToLower(request.SourceUUID)) ||
|
|
!sha256Pattern.MatchString(strings.ToLower(request.ManifestDigest)) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_receive", "peer migration receive identity is invalid", nil)
|
|
}
|
|
if _, err := peermigration.TicketDigest(request.Ticket); err != nil {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_ticket", "peer migration ticket is invalid", err)
|
|
}
|
|
if err := peermigration.ValidateEndpoints(request.SourceEndpoints); err != nil ||
|
|
!sha256Pattern.MatchString(strings.ToLower(request.SourceIdentitySHA256)) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_endpoint", "peer migration source endpoint is invalid", err)
|
|
}
|
|
if request.Source != "" || request.TemplateID != "" || request.TemplateRevision != 0 ||
|
|
request.TemplateGenerationToken != "" || request.GuestSSHPublicKey != "" ||
|
|
request.RootPassword != "" || request.StartupScript != "" || request.Hostname != "" {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_receive", "peer migration cannot inject an image or guest identity", nil)
|
|
}
|
|
candidate := request.CreateVMRequest
|
|
candidate.Source = "cloud"
|
|
candidate.TemplateID = "peer-" + request.MigrationID
|
|
candidate.TemplateRevision = 1
|
|
candidate.TemplateGenerationToken = "peer_migration_v3"
|
|
candidate.RootPassword = "Peer1!Validation"
|
|
if err := ValidateCreateVM(candidate); err != nil {
|
|
return err
|
|
}
|
|
if request.VPCPort != nil {
|
|
if err := validateVPCPortMetadata(*request.VPCPort); err != nil {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_vpc_port", "peer migration VPC port is invalid", err)
|
|
}
|
|
if strings.EqualFold(request.VPCPort.MAC, vmMACAddress(request.UUID)) {
|
|
return NewError(ErrConflict, "vpc_mac_conflicts_with_public", "peer migration VPC MAC conflicts with the managed public interface", nil)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateRollbackPeerMigrationRequest(request RollbackPeerMigrationRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.MigrationID) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_rollback", "peer migration rollback identity is invalid", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateActivatePeerMigrationRequest(request ActivatePeerMigrationRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.MigrationID) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_activation", "peer migration activation identity is invalid", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateCleanupPeerMigrationRequest(request CleanupPeerMigrationRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.MigrationID) || !request.RemoveManagedDisks {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_cleanup", "peer migration cleanup identity is invalid", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateCleanupPeerMigrationTargetRequest(request CleanupPeerMigrationTargetRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!operationIdentifierPattern.MatchString(request.PriorOperationID) ||
|
|
request.OperationID == request.PriorOperationID ||
|
|
!catalogIDPattern.MatchString(request.MigrationID) ||
|
|
!sha256Pattern.MatchString(strings.ToLower(request.CleanupPlanID)) {
|
|
return NewError(ErrInvalid, "invalid_peer_migration_target_cleanup", "peer migration target cleanup identity is invalid", nil)
|
|
}
|
|
return nil
|
|
}
|