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.
98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package peermigration
|
|
|
|
import (
|
|
"crypto/subtle"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Handler struct {
|
|
store *Store
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewHandler(store *Store) (*Handler, error) {
|
|
if store == nil {
|
|
return nil, errors.New("peer migration store is required")
|
|
}
|
|
return &Handler{store: store, now: time.Now}, nil
|
|
}
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
if r.Method != http.MethodGet {
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
|
if len(segments) < 4 || segments[0] != "v3" || segments[1] != "peer-migrations" ||
|
|
!idPattern.MatchString(segments[2]) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
manifest, err := h.store.LoadSource(segments[2])
|
|
if err != nil || manifest.ExpiresAt <= h.now().Unix() || !h.authorized(r, manifest) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if len(segments) == 4 && segments[3] == "manifest" {
|
|
public := manifest.Public()
|
|
digest, err := public.Digest()
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(struct {
|
|
Manifest
|
|
Digest string `json:"digest"`
|
|
}{Manifest: public, Digest: digest})
|
|
return
|
|
}
|
|
if len(segments) != 5 || segments[3] != "artifacts" || !idPattern.MatchString(segments[4]) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
for _, artifact := range manifest.Artifacts {
|
|
if artifact.ID != segments[4] {
|
|
continue
|
|
}
|
|
file, err := os.Open(artifact.Path)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
info, err := file.Stat()
|
|
if err != nil || !info.Mode().IsRegular() || info.Size() != artifact.SizeBytes {
|
|
w.WriteHeader(http.StatusConflict)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
http.ServeContent(w, r, artifact.ID+".qcow2", time.Time{}, file)
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
}
|
|
|
|
func (h *Handler) authorized(r *http.Request, manifest StoredManifest) bool {
|
|
if r.URL.RawQuery != "" {
|
|
return false
|
|
}
|
|
authorization := r.Header.Get("Authorization")
|
|
if !strings.HasPrefix(authorization, "Bearer ") || strings.Count(authorization, " ") != 1 {
|
|
return false
|
|
}
|
|
ticket := strings.TrimPrefix(authorization, "Bearer ")
|
|
digest, err := TicketDigest(ticket)
|
|
if err != nil || len(digest) != len(manifest.TicketSHA256) {
|
|
return false
|
|
}
|
|
return subtle.ConstantTimeCompare([]byte(digest), []byte(manifest.TicketSHA256)) == 1
|
|
}
|