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.
270 lines
8.4 KiB
Go
270 lines
8.4 KiB
Go
package peermigration
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
http *http.Client
|
|
store *Store
|
|
chunkSize int64
|
|
}
|
|
|
|
func NewClient(store *Store, sourceIdentitySHA256 string) (*Client, error) {
|
|
if store == nil {
|
|
return nil, errors.New("peer migration store is required")
|
|
}
|
|
tlsConfig, err := PinnedClientTLSConfig(sourceIdentitySHA256)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
transport := &http.Transport{
|
|
Proxy: nil,
|
|
DialContext: (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
|
|
TLSClientConfig: tlsConfig.Clone(),
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: 4,
|
|
MaxIdleConnsPerHost: 4,
|
|
IdleConnTimeout: 30 * time.Second,
|
|
ResponseHeaderTimeout: 30 * time.Second,
|
|
}
|
|
return &Client{
|
|
http: &http.Client{
|
|
Transport: transport,
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
return errors.New("peer migration redirects are disabled")
|
|
},
|
|
},
|
|
store: store, chunkSize: DefaultChunkSize,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) CloseIdleConnections() { c.http.CloseIdleConnections() }
|
|
|
|
func (c *Client) FetchManifest(ctx context.Context, endpoints []string, migrationID, ticket,
|
|
expectedDigest string) (Manifest, string, error) {
|
|
if err := ValidateEndpoints(endpoints); err != nil || !idPattern.MatchString(migrationID) ||
|
|
!sha256Pattern.MatchString(expectedDigest) {
|
|
return Manifest{}, "", errors.New("peer migration source identity is invalid")
|
|
}
|
|
if _, err := TicketDigest(ticket); err != nil {
|
|
return Manifest{}, "", err
|
|
}
|
|
var lastErr error
|
|
for _, endpoint := range orderEndpoints(endpoints) {
|
|
manifest, err := c.fetchManifest(ctx, endpoint, migrationID, ticket, expectedDigest)
|
|
if err == nil {
|
|
return manifest, endpoint, nil
|
|
}
|
|
lastErr = err
|
|
}
|
|
return Manifest{}, "", fmt.Errorf("fetch peer migration manifest from every source endpoint: %w", lastErr)
|
|
}
|
|
|
|
func (c *Client) fetchManifest(ctx context.Context, endpoint, migrationID, ticket,
|
|
expectedDigest string) (Manifest, error) {
|
|
capability := strings.TrimSuffix(endpoint, "/") + "/v3/peer-migrations/" + migrationID +
|
|
"/manifest"
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, capability, nil)
|
|
if err != nil {
|
|
return Manifest{}, errors.New("build peer migration manifest request")
|
|
}
|
|
request.Header.Set("Authorization", "Bearer "+ticket)
|
|
response, err := c.http.Do(request)
|
|
if err != nil {
|
|
return Manifest{}, fmt.Errorf("fetch peer migration manifest: %w", err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusOK || response.Header.Get("Content-Encoding") != "" {
|
|
return Manifest{}, fmt.Errorf("peer migration manifest returned HTTP %d", response.StatusCode)
|
|
}
|
|
decoder := json.NewDecoder(io.LimitReader(response.Body, maxManifestBytes+1))
|
|
decoder.DisallowUnknownFields()
|
|
var envelope struct {
|
|
Manifest
|
|
Digest string `json:"digest"`
|
|
}
|
|
if err := decoder.Decode(&envelope); err != nil {
|
|
return Manifest{}, errors.New("decode peer migration manifest")
|
|
}
|
|
var trailing any
|
|
if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) {
|
|
return Manifest{}, errors.New("peer migration manifest contains trailing data")
|
|
}
|
|
if err := ValidateManifest(envelope.Manifest, time.Now()); err != nil ||
|
|
envelope.MigrationID != migrationID || !hashEqual(envelope.Digest, expectedDigest) {
|
|
return Manifest{}, errors.New("peer migration manifest does not match the requested session")
|
|
}
|
|
digest, err := envelope.Manifest.Digest()
|
|
if err != nil || !hashEqual(digest, expectedDigest) {
|
|
return Manifest{}, errors.New("peer migration manifest digest mismatch")
|
|
}
|
|
return envelope.Manifest, nil
|
|
}
|
|
|
|
func (c *Client) Download(ctx context.Context, endpoint, ticket string,
|
|
manifest Manifest) (map[string]string, error) {
|
|
if err := ValidateEndpoint(endpoint); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := TicketDigest(ticket); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ValidateManifest(manifest, time.Now()); err != nil {
|
|
return nil, err
|
|
}
|
|
directory, err := c.store.TargetSessionDir(manifest.MigrationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
paths := make(map[string]string, len(manifest.Artifacts))
|
|
for _, artifact := range manifest.Artifacts {
|
|
path, err := c.downloadArtifact(ctx, endpoint, ticket, manifest.MigrationID,
|
|
artifact, directory)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
paths[artifact.ID] = path
|
|
}
|
|
return paths, nil
|
|
}
|
|
|
|
func (c *Client) downloadArtifact(ctx context.Context, endpoint, ticket, migrationID string,
|
|
artifact Artifact, directory string) (string, error) {
|
|
readyPath := filepath.Join(directory, artifact.ID+".ready")
|
|
if matches, _ := fileMatches(readyPath, artifact); matches {
|
|
return readyPath, nil
|
|
}
|
|
partialPath := filepath.Join(directory, artifact.ID+".partial")
|
|
file, err := os.OpenFile(partialPath, os.O_CREATE|os.O_RDWR, 0600)
|
|
if err != nil {
|
|
return "", fmt.Errorf("open peer migration partial artifact: %w", err)
|
|
}
|
|
defer file.Close()
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
offset := info.Size()
|
|
if offset < 0 || offset > artifact.SizeBytes {
|
|
if err := file.Truncate(0); err != nil {
|
|
return "", err
|
|
}
|
|
offset = 0
|
|
}
|
|
capability := strings.TrimSuffix(endpoint, "/") + "/v3/peer-migrations/" + migrationID +
|
|
"/artifacts/" + artifact.ID
|
|
for offset < artifact.SizeBytes {
|
|
end := offset + c.chunkSize - 1
|
|
if end >= artifact.SizeBytes {
|
|
end = artifact.SizeBytes - 1
|
|
}
|
|
var lastErr error
|
|
for attempt := 0; attempt < 5; attempt++ {
|
|
if attempt > 0 {
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", ctx.Err()
|
|
case <-time.After(time.Duration(attempt) * time.Second):
|
|
}
|
|
}
|
|
lastErr = c.fetchRange(ctx, capability, ticket, file, offset, end, artifact.SizeBytes)
|
|
if lastErr == nil {
|
|
break
|
|
}
|
|
}
|
|
if lastErr != nil {
|
|
return "", lastErr
|
|
}
|
|
offset = end + 1
|
|
if err := file.Sync(); err != nil {
|
|
return "", fmt.Errorf("sync peer migration partial artifact: %w", err)
|
|
}
|
|
}
|
|
if err := file.Close(); err != nil {
|
|
return "", err
|
|
}
|
|
matches, err := fileMatches(partialPath, artifact)
|
|
if err != nil || !matches {
|
|
return "", errors.New("peer migration artifact size or SHA-256 mismatch")
|
|
}
|
|
if err := os.Rename(partialPath, readyPath); err != nil {
|
|
return "", fmt.Errorf("publish peer migration artifact: %w", err)
|
|
}
|
|
return readyPath, nil
|
|
}
|
|
|
|
func (c *Client) fetchRange(ctx context.Context, capability, ticket string, file *os.File,
|
|
start, end, total int64) error {
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, capability, nil)
|
|
if err != nil {
|
|
return errors.New("build peer migration artifact request")
|
|
}
|
|
request.Header.Set("Authorization", "Bearer "+ticket)
|
|
request.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
|
|
response, err := c.http.Do(request)
|
|
if err != nil {
|
|
return fmt.Errorf("download peer migration artifact: %w", err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusPartialContent || response.Header.Get("Content-Encoding") != "" ||
|
|
!validContentRange(response.Header.Get("Content-Range"), start, end, total) ||
|
|
response.ContentLength != end-start+1 {
|
|
return fmt.Errorf("peer migration range response is invalid: HTTP %d", response.StatusCode)
|
|
}
|
|
if _, err := file.Seek(start, io.SeekStart); err != nil {
|
|
return err
|
|
}
|
|
written, err := io.CopyN(file, response.Body, end-start+1)
|
|
if err != nil || written != end-start+1 {
|
|
return errors.New("peer migration range response ended early")
|
|
}
|
|
var extra [1]byte
|
|
if count, _ := response.Body.Read(extra[:]); count != 0 {
|
|
return errors.New("peer migration range response exceeded its bound")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validContentRange(value string, start, end, total int64) bool {
|
|
return value == "bytes "+strconv.FormatInt(start, 10)+"-"+strconv.FormatInt(end, 10)+
|
|
"/"+strconv.FormatInt(total, 10)
|
|
}
|
|
|
|
func fileMatches(path string, artifact Artifact) (bool, error) {
|
|
file, err := os.Open(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer file.Close()
|
|
info, err := file.Stat()
|
|
if err != nil || !info.Mode().IsRegular() || info.Size() != artifact.SizeBytes {
|
|
return false, err
|
|
}
|
|
hash := sha256.New()
|
|
if _, err := io.Copy(hash, file); err != nil {
|
|
return false, err
|
|
}
|
|
return hashEqual(hex.EncodeToString(hash.Sum(nil)), artifact.SHA256), nil
|
|
}
|
|
|
|
func hashEqual(left, right string) bool {
|
|
return len(left) == len(right) && strings.EqualFold(left, right)
|
|
}
|