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.

278 lines
10 KiB
Go

package peermigration
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
const (
ProtocolVersion = uint64(3)
MaxArtifacts = 3
DefaultChunkSize = int64(8 << 20)
maxManifestBytes = int64(64 << 10)
)
var (
idPattern = regexp.MustCompile(`^[a-f0-9]{32}$`)
uuidPattern = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
sha256Pattern = regexp.MustCompile(`^[a-f0-9]{64}$`)
)
// Artifact is one immutable qcow2 object in a PeerMigration V3 session. Path
// is persisted only in the source Agent's private state and is never encoded
// into the public manifest returned to a peer.
type Artifact struct {
ID string `json:"id"`
Role string `json:"role"`
SnapshotKey string `json:"snapshot_key,omitempty"`
Name string `json:"name,omitempty"`
Consistency string `json:"consistency"`
SizeBytes int64 `json:"size_bytes"`
VirtualSizeBytes uint64 `json:"virtual_size_bytes"`
SHA256 string `json:"sha256"`
CreatedAt int64 `json:"created_at"`
Path string `json:"path,omitempty"`
}
type Manifest struct {
Version uint64 `json:"version"`
MigrationID string `json:"migration_id"`
SourceUUID string `json:"source_uuid"`
ExpiresAt int64 `json:"expires_at"`
Artifacts []Artifact `json:"artifacts"`
}
type StoredManifest struct {
Manifest
TicketSHA256 string `json:"ticket_sha256"`
SourceWasRunning bool `json:"source_was_running"`
SourceAutostartEnabled bool `json:"source_autostart_enabled"`
}
// SourceIntent is fsync'd before the source power/autostart mutation begins.
// It is deliberately separate from the ready manifest so a process crash can
// always restore the original source state without publishing unhashed data.
type SourceIntent struct {
Version uint64 `json:"version"`
MigrationID string `json:"migration_id"`
SourceUUID string `json:"source_uuid"`
ExpiresAt int64 `json:"expires_at"`
TicketSHA256 string `json:"ticket_sha256"`
SourceWasRunning bool `json:"source_was_running"`
SourceAutostartEnabled bool `json:"source_autostart_enabled"`
Artifacts []SourceIntentArtifact `json:"artifacts"`
}
type SourceIntentArtifact struct {
ID string `json:"id"`
Role string `json:"role"`
SnapshotKey string `json:"snapshot_key,omitempty"`
Path string `json:"path"`
}
// TargetReceipt seals the path-free identity needed to reconcile a receive
// operation. It is persisted before the first target-host mutation; the
// receipt describes the required final state but is never sufficient proof by
// itself. Reconciliation must also inspect the complete powered-off domain,
// managed volumes, seed, network filter, and snapshot catalog.
type TargetReceipt struct {
Version uint64 `json:"version"`
MigrationID string `json:"migration_id"`
ManifestDigest string `json:"manifest_digest"`
SourceUUID string `json:"source_uuid"`
TargetUUID string `json:"target_uuid"`
ExpiresAt int64 `json:"expires_at"`
Artifacts []Artifact `json:"artifacts"`
StagedPoweredOff bool `json:"staged_powered_off"`
CreatedAt int64 `json:"created_at"`
}
func (m StoredManifest) Public() Manifest {
public := m.Manifest
public.Artifacts = make([]Artifact, len(m.Artifacts))
for index, artifact := range m.Artifacts {
artifact.Path = ""
public.Artifacts[index] = artifact
}
return public
}
func (m Manifest) Digest() (string, error) {
if err := ValidateManifest(m, time.Now()); err != nil {
return "", err
}
encoded, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("encode peer migration manifest: %w", err)
}
digest := sha256.Sum256(encoded)
return hex.EncodeToString(digest[:]), nil
}
func ValidateManifest(manifest Manifest, now time.Time) error {
if manifest.Version != ProtocolVersion || !idPattern.MatchString(manifest.MigrationID) ||
!uuidPattern.MatchString(strings.ToLower(manifest.SourceUUID)) ||
manifest.ExpiresAt <= now.Unix() || len(manifest.Artifacts) < 1 ||
len(manifest.Artifacts) > MaxArtifacts {
return errors.New("peer migration manifest identity is invalid")
}
seen := make(map[string]struct{}, len(manifest.Artifacts))
rootCount := 0
for _, artifact := range manifest.Artifacts {
if !idPattern.MatchString(artifact.ID) || artifact.SizeBytes < 1 ||
artifact.VirtualSizeBytes < 1 || !sha256Pattern.MatchString(artifact.SHA256) ||
artifact.CreatedAt < 1 || (artifact.Consistency != "filesystem" && artifact.Consistency != "crash") {
return errors.New("peer migration artifact is invalid")
}
if _, duplicate := seen[artifact.ID]; duplicate {
return errors.New("peer migration artifact identities must be unique")
}
seen[artifact.ID] = struct{}{}
switch artifact.Role {
case "root":
rootCount++
if artifact.SnapshotKey != "" || artifact.Name != "" {
return errors.New("peer migration root metadata is invalid")
}
case "snapshot":
if !idPattern.MatchString(artifact.SnapshotKey) || artifact.Name == "" ||
len(artifact.Name) > 64 || strings.ContainsRune(artifact.Name, 0) {
return errors.New("peer migration snapshot metadata is invalid")
}
default:
return errors.New("peer migration artifact role is invalid")
}
}
if rootCount != 1 {
return errors.New("peer migration manifest must contain exactly one root")
}
return nil
}
func ValidateStoredManifest(manifest StoredManifest, roots []string, now time.Time) error {
if err := ValidateManifest(manifest.Manifest, now); err != nil {
return err
}
if !sha256Pattern.MatchString(manifest.TicketSHA256) || len(roots) == 0 {
return errors.New("peer migration private manifest is invalid")
}
for _, artifact := range manifest.Artifacts {
if !cleanPathWithinRoots(artifact.Path, roots) {
return errors.New("peer migration artifact path is outside configured storage roots")
}
}
return nil
}
func ValidateSourceIntent(intent SourceIntent, roots []string) error {
if intent.Version != ProtocolVersion || !idPattern.MatchString(intent.MigrationID) ||
!uuidPattern.MatchString(strings.ToLower(intent.SourceUUID)) || intent.ExpiresAt < 1 ||
!sha256Pattern.MatchString(intent.TicketSHA256) || len(intent.Artifacts) < 1 ||
len(intent.Artifacts) > MaxArtifacts || len(roots) == 0 {
return errors.New("peer migration source intent identity is invalid")
}
seen := make(map[string]struct{}, len(intent.Artifacts))
rootCount := 0
for _, artifact := range intent.Artifacts {
if !idPattern.MatchString(artifact.ID) || !cleanPathWithinRoots(artifact.Path, roots) {
return errors.New("peer migration source intent artifact is invalid")
}
if _, duplicate := seen[artifact.ID]; duplicate {
return errors.New("peer migration source intent artifacts must be unique")
}
seen[artifact.ID] = struct{}{}
switch artifact.Role {
case "root":
rootCount++
if artifact.SnapshotKey != "" {
return errors.New("peer migration root intent is invalid")
}
case "snapshot":
if !idPattern.MatchString(artifact.SnapshotKey) {
return errors.New("peer migration snapshot intent is invalid")
}
default:
return errors.New("peer migration source intent role is invalid")
}
}
if rootCount != 1 {
return errors.New("peer migration source intent must contain exactly one root")
}
return nil
}
func ValidateTargetReceipt(receipt TargetReceipt) error {
validationTime := time.Unix(receipt.ExpiresAt-1, 0)
manifest := Manifest{
Version: ProtocolVersion, MigrationID: receipt.MigrationID,
SourceUUID: receipt.SourceUUID, ExpiresAt: receipt.ExpiresAt,
Artifacts: receipt.Artifacts,
}
if receipt.Version != ProtocolVersion || !sha256Pattern.MatchString(receipt.ManifestDigest) ||
!uuidPattern.MatchString(strings.ToLower(receipt.TargetUUID)) || !receipt.StagedPoweredOff ||
receipt.CreatedAt < 1 || receipt.ExpiresAt < 2 ||
ValidateManifest(manifest, validationTime) != nil {
return errors.New("peer migration target receipt is invalid")
}
for _, artifact := range receipt.Artifacts {
if artifact.Path != "" {
return errors.New("peer migration target receipt contains a host path")
}
}
return nil
}
func ValidateEndpoint(value string) error {
if value == "" || len(value) > 2048 || strings.TrimSpace(value) != value ||
strings.ContainsAny(value, "\x00\r\n\\") {
return errors.New("peer migration endpoint must be an HTTPS origin")
}
parsed, err := url.Parse(value)
if err != nil || parsed.Scheme != "https" || parsed.Host == "" || parsed.Hostname() == "" ||
parsed.User != nil || parsed.Opaque != "" || parsed.RawQuery != "" || parsed.ForceQuery ||
parsed.Fragment != "" || parsed.RawPath != "" || (parsed.Path != "" && parsed.Path != "/") ||
strings.HasSuffix(parsed.Host, ":") || net.ParseIP(parsed.Hostname()) == nil || parsed.Port() == "" {
return errors.New("peer migration endpoint must be an HTTPS origin")
}
port, err := strconv.Atoi(parsed.Port())
if err != nil || port < 1 || port > 65535 {
return errors.New("peer migration endpoint port is invalid")
}
return nil
}
func TicketDigest(ticket string) (string, error) {
if len(ticket) != 64 || !sha256Pattern.MatchString(strings.ToLower(ticket)) {
return "", errors.New("peer migration ticket must be 32 random bytes in hex")
}
digest := sha256.Sum256([]byte(ticket))
return hex.EncodeToString(digest[:]), nil
}
func cleanPathWithinRoots(path string, roots []string) bool {
if path == "" || !filepath.IsAbs(path) || filepath.Clean(path) != path || strings.ContainsRune(path, 0) {
return false
}
for _, root := range roots {
if root == "" || !filepath.IsAbs(root) || filepath.Clean(root) != root {
continue
}
relative, err := filepath.Rel(root, path)
if err == nil && relative != "." && relative != ".." &&
!strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
return true
}
}
return false
}