2019-06-20 18:36:40 -05:00
|
|
|
// @title Open Container Initiative Distribution Specification
|
|
|
|
// @version v0.1.0-dev
|
|
|
|
// @description APIs for Open Container Initiative Distribution Specification
|
|
|
|
|
|
|
|
// @license.name Apache 2.0
|
|
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-13 14:23:31 -05:00
|
|
|
"errors"
|
2019-06-20 18:36:40 -05:00
|
|
|
"fmt"
|
2019-07-10 00:23:59 -05:00
|
|
|
"io"
|
2019-06-20 18:36:40 -05:00
|
|
|
"net/http"
|
2022-06-02 15:54:42 -05:00
|
|
|
"net/url"
|
2019-06-20 18:36:40 -05:00
|
|
|
"path"
|
2022-08-30 17:12:10 -05:00
|
|
|
"regexp"
|
2020-01-09 19:31:34 -05:00
|
|
|
"sort"
|
2019-06-20 18:36:40 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
2022-08-20 20:18:50 -05:00
|
|
|
notreg "github.com/notaryproject/notation-go/registry"
|
2022-02-24 15:31:36 -05:00
|
|
|
"github.com/opencontainers/distribution-spec/specs-go/v1/extensions"
|
2019-06-20 18:36:40 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-01-31 16:33:07 -05:00
|
|
|
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2022-03-24 07:25:14 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2022-10-05 14:56:41 -05:00
|
|
|
gqlPlayground "zotregistry.io/zot/pkg/debug/gqlplayground"
|
2022-10-03 11:53:44 -05:00
|
|
|
debug "zotregistry.io/zot/pkg/debug/swagger"
|
2021-12-03 22:50:58 -05:00
|
|
|
ext "zotregistry.io/zot/pkg/extensions"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
2022-08-16 03:57:09 -05:00
|
|
|
localCtx "zotregistry.io/zot/pkg/requestcontext"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-10-05 05:21:14 -05:00
|
|
|
"zotregistry.io/zot/pkg/test" //nolint:goimports
|
2019-06-20 18:36:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type RouteHandler struct {
|
2020-02-17 16:57:15 -05:00
|
|
|
c *Controller
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouteHandler(c *Controller) *RouteHandler {
|
2020-02-17 16:57:15 -05:00
|
|
|
rh := &RouteHandler{c: c}
|
2019-06-20 18:36:40 -05:00
|
|
|
rh.SetupRoutes()
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return rh
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:15:13 -05:00
|
|
|
func allowedMethods(method string) []string {
|
|
|
|
return []string{http.MethodOptions, method}
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
func (rh *RouteHandler) SetupRoutes() {
|
2020-01-24 16:32:38 -05:00
|
|
|
rh.c.Router.Use(AuthHandler(rh.c))
|
2022-03-10 07:25:15 -05:00
|
|
|
// authz is being enabled if AccessControl is specified
|
|
|
|
// if Authn is not present AccessControl will have only default policies
|
|
|
|
if rh.c.Config.AccessControl != nil && !isBearerAuthEnabled(rh.c.Config) {
|
|
|
|
if isAuthnEnabled(rh.c.Config) {
|
|
|
|
rh.c.Log.Info().Msg("access control is being enabled")
|
|
|
|
} else {
|
|
|
|
rh.c.Log.Info().Msg("default policy only access control is being enabled")
|
|
|
|
}
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
rh.c.Router.Use(AuthzHandler(rh.c))
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#endpoints
|
2022-03-24 07:25:14 -05:00
|
|
|
prefixedRouter := rh.c.Router.PathPrefix(constants.RoutePrefix).Subrouter()
|
2019-06-20 18:36:40 -05:00
|
|
|
{
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/tags/list", NameRegexp.String()),
|
2022-02-15 20:15:13 -05:00
|
|
|
rh.ListTags).Methods(allowedMethods("GET")...)
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/manifests/{reference}", NameRegexp.String()),
|
2022-02-15 20:15:13 -05:00
|
|
|
rh.CheckManifest).Methods(allowedMethods("HEAD")...)
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/manifests/{reference}", NameRegexp.String()),
|
2022-02-15 20:15:13 -05:00
|
|
|
rh.GetManifest).Methods(allowedMethods("GET")...)
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/manifests/{reference}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.UpdateManifest).Methods("PUT")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/manifests/{reference}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.DeleteManifest).Methods("DELETE")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/{digest}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.CheckBlob).Methods("HEAD")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/{digest}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.GetBlob).Methods("GET")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/{digest}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.DeleteBlob).Methods("DELETE")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/uploads/", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.CreateBlobUpload).Methods("POST")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/uploads/{session_id}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.GetBlobUpload).Methods("GET")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/uploads/{session_id}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.PatchBlobUpload).Methods("PATCH")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/uploads/{session_id}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.UpdateBlobUpload).Methods("PUT")
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc(fmt.Sprintf("/{name:%s}/blobs/uploads/{session_id}", NameRegexp.String()),
|
2020-09-30 19:16:34 -05:00
|
|
|
rh.DeleteBlobUpload).Methods("DELETE")
|
2022-02-24 15:31:36 -05:00
|
|
|
prefixedRouter.HandleFunc(constants.ExtCatalogPrefix,
|
2022-02-15 20:15:13 -05:00
|
|
|
rh.ListRepositories).Methods(allowedMethods("GET")...)
|
2022-02-24 15:31:36 -05:00
|
|
|
prefixedRouter.HandleFunc(constants.ExtOciDiscoverPrefix,
|
|
|
|
rh.ListExtensions).Methods(allowedMethods("GET")...)
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc("/",
|
2022-02-15 20:15:13 -05:00
|
|
|
rh.CheckVersionSupport).Methods(allowedMethods("GET")...)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2021-10-29 21:10:55 -05:00
|
|
|
|
|
|
|
// support for oras artifact reference types (alpha 1) - image signature use case
|
2022-03-24 07:25:14 -05:00
|
|
|
rh.c.Router.HandleFunc(fmt.Sprintf("%s/{name:%s}/manifests/{digest}/referrers",
|
|
|
|
constants.ArtifactSpecRoutePrefix, NameRegexp.String()), rh.GetReferrers).Methods("GET")
|
2021-10-29 21:10:55 -05:00
|
|
|
|
2022-10-03 11:53:44 -05:00
|
|
|
// swagger
|
|
|
|
debug.SetupSwaggerRoutes(rh.c.Config, rh.c.Router, rh.c.Log)
|
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
// Setup Extensions Routes
|
2021-10-15 10:05:00 -05:00
|
|
|
if rh.c.Config != nil {
|
|
|
|
if rh.c.Config.Extensions == nil {
|
|
|
|
// minimal build
|
2021-12-13 14:23:31 -05:00
|
|
|
prefixedRouter.HandleFunc("/metrics", rh.GetMetrics).Methods("GET")
|
2021-10-15 10:05:00 -05:00
|
|
|
} else {
|
|
|
|
// extended build
|
2022-04-27 01:00:20 -05:00
|
|
|
ext.SetupMetricsRoutes(rh.c.Config, rh.c.Router, rh.c.StoreController, rh.c.Log)
|
|
|
|
ext.SetupSearchRoutes(rh.c.Config, rh.c.Router, rh.c.StoreController, rh.c.Log)
|
2022-10-05 14:56:41 -05:00
|
|
|
gqlPlayground.SetupGQLPlaygroundRoutes(rh.c.Config, rh.c.Router, rh.c.StoreController, rh.c.Log)
|
2021-10-15 10:05:00 -05:00
|
|
|
}
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Method handlers
|
|
|
|
|
|
|
|
// CheckVersionSupport godoc
|
|
|
|
// @Summary Check API support
|
|
|
|
// @Description Check if this API version is supported
|
|
|
|
// @Router /v2/ [get]
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Success 200 {string} string "ok".
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) CheckVersionSupport(response http.ResponseWriter, request *http.Request) {
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.DistAPIVersion, "registry/2.0")
|
2020-05-19 15:17:15 -05:00
|
|
|
// NOTE: compatibility workaround - return this header in "allowed-read" mode to allow for clients to
|
|
|
|
// work correctly
|
2022-07-14 10:13:46 -05:00
|
|
|
if rh.c.Config.HTTP.Auth != nil {
|
|
|
|
if rh.c.Config.HTTP.Auth.Bearer != nil {
|
|
|
|
response.Header().Set("WWW-Authenticate", fmt.Sprintf("bearer realm=%s", rh.c.Config.HTTP.Auth.Bearer.Realm))
|
|
|
|
} else {
|
|
|
|
response.Header().Set("WWW-Authenticate", fmt.Sprintf("basic realm=%s", rh.c.Config.HTTP.Realm))
|
2020-05-19 15:17:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteData(response, http.StatusOK, "application/json", []byte{})
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImageTags struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListTags godoc
|
|
|
|
// @Summary List image tags
|
|
|
|
// @Description List all image tags in a repository
|
|
|
|
// @Router /v2/{name}/tags/list [get]
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "test"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Param n query integer true "limit entries for pagination"
|
|
|
|
// @Param last query string true "last tag value for pagination"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Success 200 {object} api.ImageTags
|
|
|
|
// @Failure 404 {string} string "not found"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Failure 400 {string} string "bad request".
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) ListTags(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2022-01-20 23:11:44 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
paginate := false
|
2021-12-13 14:23:31 -05:00
|
|
|
numTags := -1
|
2020-01-09 19:31:34 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nQuery, ok := request.URL.Query()["n"]
|
2020-01-09 19:31:34 -05:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
if len(nQuery) != 1 {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
var nQuery1 int64
|
2020-01-09 19:31:34 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
var err error
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
if nQuery1, err = strconv.ParseInt(nQuery[0], 10, 0); err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
numTags = int(nQuery1)
|
2020-01-09 19:31:34 -05:00
|
|
|
paginate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
last := ""
|
2021-12-13 14:23:31 -05:00
|
|
|
lastQuery, ok := request.URL.Query()["last"]
|
2020-01-09 19:31:34 -05:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
if len(lastQuery) != 1 {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
last = lastQuery[0]
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
tags, err := imgStore.GetImageTags(name)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusNotFound, NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if paginate && (numTags < len(tags)) {
|
2020-01-09 19:31:34 -05:00
|
|
|
sort.Strings(tags)
|
|
|
|
|
|
|
|
pTags := ImageTags{Name: name}
|
|
|
|
|
|
|
|
if last == "" {
|
|
|
|
// first
|
2021-12-13 14:23:31 -05:00
|
|
|
pTags.Tags = tags[:numTags]
|
2020-01-09 19:31:34 -05:00
|
|
|
} else {
|
|
|
|
// next
|
2021-12-13 14:23:31 -05:00
|
|
|
var i int
|
2020-01-09 19:31:34 -05:00
|
|
|
found := false
|
2021-12-13 14:23:31 -05:00
|
|
|
for idx, tag := range tags {
|
2020-01-09 19:31:34 -05:00
|
|
|
if tag == last {
|
|
|
|
found = true
|
2021-12-13 14:23:31 -05:00
|
|
|
i = idx
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
if !found {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
if numTags >= len(tags)-i {
|
2020-01-09 19:31:34 -05:00
|
|
|
pTags.Tags = tags[i+1:]
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusOK, pTags)
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
pTags.Tags = tags[i+1 : i+1+numTags]
|
2020-01-09 19:31:34 -05:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:17:24 -05:00
|
|
|
if len(pTags.Tags) == 0 {
|
|
|
|
last = ""
|
|
|
|
} else {
|
|
|
|
last = pTags.Tags[len(pTags.Tags)-1]
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Link", fmt.Sprintf("/v2/%s/tags/list?n=%d&last=%s; rel=\"next\"", name, numTags, last))
|
|
|
|
WriteJSON(response, http.StatusOK, pTags)
|
2020-01-09 19:31:34 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusOK, ImageTags{Name: name, Tags: tags})
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckManifest godoc
|
|
|
|
// @Summary Check image manifest
|
|
|
|
// @Description Check an image's manifest given a reference or a digest
|
|
|
|
// @Router /v2/{name}/manifests/{reference} [head]
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param reference path string true "image reference or digest"
|
|
|
|
// @Success 200 {string} string "ok"
|
2022-03-24 07:25:14 -05:00
|
|
|
// @Header 200 {object} cosntants.DistContentDigestKey
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Failure 404 {string} string "not found"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Failure 500 {string} string "internal server error".
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) CheckManifest(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2022-01-20 23:11:44 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
reference, ok := vars["reference"]
|
2022-01-20 23:11:44 -05:00
|
|
|
if !ok || reference == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(MANIFEST_INVALID, map[string]string{"reference": reference})))
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
content, digest, mediaType, err := getImageManifest(rh, imgStore, name, reference) //nolint:contextcheck
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-02-13 14:00:11 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(MANIFEST_UNKNOWN, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusInternalServerError,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(MANIFEST_INVALID, map[string]string{"reference": reference})))
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.DistContentDigestKey, digest)
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Content-Length", fmt.Sprintf("%d", len(content)))
|
|
|
|
response.Header().Set("Content-Type", mediaType)
|
|
|
|
response.WriteHeader(http.StatusOK)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:13:24 -05:00
|
|
|
// NOTE: https://github.com/swaggo/swag/issues/387.
|
2019-06-20 18:36:40 -05:00
|
|
|
type ImageManifest struct {
|
|
|
|
ispec.Manifest
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
type ExtensionList struct {
|
|
|
|
extensions.ExtensionList
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
// GetManifest godoc
|
|
|
|
// @Summary Get image manifest
|
|
|
|
// @Description Get an image's manifest given a reference or a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce application/vnd.oci.image.manifest.v1+json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param reference path string true "image reference or digest"
|
|
|
|
// @Success 200 {object} api.ImageManifest
|
2022-03-24 07:25:14 -05:00
|
|
|
// @Header 200 {object} constants.DistContentDigestKey
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/manifests/{reference} [get].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) GetManifest(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
reference, ok := vars["reference"]
|
|
|
|
if !ok || reference == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(MANIFEST_UNKNOWN, map[string]string{"reference": reference})))
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
content, digest, mediaType, err := getImageManifest(rh, imgStore, name, reference) //nolint: contextcheck
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrRepoBadVersion) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(MANIFEST_UNKNOWN, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.DistContentDigestKey, digest)
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteData(response, http.StatusOK, mediaType, content)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateManifest godoc
|
|
|
|
// @Summary Update image manifest
|
|
|
|
// @Description Update an image's manifest given a reference or a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param reference path string true "image reference or digest"
|
2022-03-24 07:25:14 -05:00
|
|
|
// @Header 201 {object} constants.DistContentDigestKey
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Success 201 {string} string "created"
|
|
|
|
// @Failure 400 {string} string "bad request"
|
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/manifests/{reference} [put].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) UpdateManifest(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
reference, ok := vars["reference"]
|
|
|
|
if !ok || reference == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(MANIFEST_INVALID, map[string]string{"reference": reference})))
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
mediaType := request.Header.Get("Content-Type")
|
2021-10-29 21:10:55 -05:00
|
|
|
if !storage.IsSupportedMediaType(mediaType) {
|
2022-06-28 05:38:00 -05:00
|
|
|
// response.WriteHeader(http.StatusUnsupportedMediaType)
|
|
|
|
WriteJSON(response, http.StatusUnsupportedMediaType,
|
|
|
|
NewErrorList(NewError(MANIFEST_INVALID, map[string]string{"mediaType": mediaType})))
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
body, err := io.ReadAll(request.Body)
|
2022-01-23 15:26:02 -05:00
|
|
|
// hard to reach test case, injected error (simulates an interrupted image manifest upload)
|
|
|
|
// err could be io.ErrUnexpectedEOF
|
|
|
|
if err := test.Error(err); err != nil {
|
2020-01-06 19:21:46 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2020-01-06 19:21:46 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
digest, err := imgStore.PutImageManifest(name, reference, mediaType, body)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(MANIFEST_UNKNOWN, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrBadManifest) {
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(MANIFEST_INVALID, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrBlobNotFound) {
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UNKNOWN, map[string]string{"blob": digest})))
|
2022-01-23 15:26:02 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrRepoBadVersion) {
|
|
|
|
WriteJSON(response, http.StatusInternalServerError,
|
|
|
|
NewErrorList(NewError(INVALID_INDEX, map[string]string{"name": name})))
|
2022-06-24 08:08:47 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrImageLintAnnotations) {
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
|
|
|
NewErrorList(NewError(MANIFEST_INVALID, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2022-01-23 15:26:02 -05:00
|
|
|
// could be syscall.EMFILE (Err:0x18 too many opened files), etc
|
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error: performing cleanup")
|
|
|
|
|
|
|
|
if err = imgStore.DeleteImageManifest(name, reference); err != nil {
|
|
|
|
// deletion of image manifest is important, but not critical for image repo consistancy
|
|
|
|
// in the worst scenario a partial manifest file written to disk will not affect the repo because
|
|
|
|
// the new manifest was not added to "index.json" file (it is possible that GC will take care of it)
|
|
|
|
rh.c.Log.Error().Err(err).Msgf("couldn't remove image manifest %s in repo %s", reference, name)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Location", fmt.Sprintf("/v2/%s/manifests/%s", name, digest))
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.DistContentDigestKey, digest)
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusCreated)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteManifest godoc
|
|
|
|
// @Summary Delete image manifest
|
|
|
|
// @Description Delete an image's manifest given a reference or a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param reference path string true "image reference or digest"
|
|
|
|
// @Success 200 {string} string "ok"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/manifests/{reference} [delete].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) DeleteManifest(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
reference, ok := vars["reference"]
|
|
|
|
if !ok || reference == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := imgStore.DeleteImageManifest(name, reference)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(MANIFEST_UNKNOWN, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrBadManifest) {
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-03-25 13:22:52 -05:00
|
|
|
NewErrorList(NewError(UNSUPPORTED, map[string]string{"reference": reference})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusAccepted)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckBlob godoc
|
|
|
|
// @Summary Check image blob/layer
|
|
|
|
// @Description Check an image's blob/layer given a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param digest path string true "blob/layer digest"
|
|
|
|
// @Success 200 {object} api.ImageManifest
|
2022-03-24 07:25:14 -05:00
|
|
|
// @Header 200 {object} constants.DistContentDigestKey
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/{digest} [head].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) CheckBlob(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
digest, ok := vars["digest"]
|
|
|
|
if !ok || digest == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
ok, blen, err := imgStore.CheckBlob(name, digest)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrBadBlobDigest) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
NewErrorList(NewError(DIGEST_INVALID, map[string]string{"digest": digest})))
|
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound, NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
|
|
|
} else if errors.Is(err, zerr.ErrBlobNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound, NewErrorList(NewError(BLOB_UNKNOWN, map[string]string{"digest": digest})))
|
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusNotFound, NewErrorList(NewError(BLOB_UNKNOWN, map[string]string{"digest": digest})))
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Content-Length", fmt.Sprintf("%d", blen))
|
2022-08-30 17:12:10 -05:00
|
|
|
response.Header().Set("Accept-Ranges", "bytes")
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.DistContentDigestKey, digest)
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusOK)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2022-08-30 17:12:10 -05:00
|
|
|
/* parseRangeHeader validates the "Range" HTTP header and returns the range. */
|
|
|
|
func parseRangeHeader(contentRange string) (int64, int64, error) {
|
|
|
|
/* bytes=<start>- and bytes=<start>-<end> formats are supported */
|
|
|
|
pattern := `bytes=(?P<rangeFrom>\d+)-(?P<rangeTo>\d*$)`
|
|
|
|
|
|
|
|
regex, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, zerr.ErrParsingHTTPHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
match := regex.FindStringSubmatch(contentRange)
|
|
|
|
|
|
|
|
paramsMap := make(map[string]string)
|
|
|
|
|
|
|
|
for i, name := range regex.SubexpNames() {
|
|
|
|
if i > 0 && i <= len(match) {
|
|
|
|
paramsMap[name] = match[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var from int64
|
|
|
|
to := int64(-1)
|
|
|
|
|
|
|
|
rangeFrom := paramsMap["rangeFrom"]
|
|
|
|
if rangeFrom == "" {
|
|
|
|
return -1, -1, zerr.ErrParsingHTTPHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
if from, err = strconv.ParseInt(rangeFrom, 10, 64); err != nil {
|
|
|
|
return -1, -1, zerr.ErrParsingHTTPHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
rangeTo := paramsMap["rangeTo"]
|
|
|
|
if rangeTo != "" {
|
|
|
|
if to, err = strconv.ParseInt(rangeTo, 10, 64); err != nil {
|
|
|
|
return -1, -1, zerr.ErrParsingHTTPHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
if to < from {
|
|
|
|
return -1, -1, zerr.ErrParsingHTTPHeader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return from, to, nil
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
// GetBlob godoc
|
|
|
|
// @Summary Get image blob/layer
|
|
|
|
// @Description Get an image's blob/layer given a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce application/vnd.oci.image.layer.v1.tar+gzip
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param digest path string true "blob/layer digest"
|
2022-03-24 07:25:14 -05:00
|
|
|
// @Header 200 {object} constants.DistContentDigestKey
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Success 200 {object} api.ImageManifest
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/{digest} [get].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) GetBlob(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
digest, ok := vars["digest"]
|
|
|
|
if !ok || digest == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
mediaType := request.Header.Get("Accept")
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2022-08-30 17:12:10 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
/* content range is supported for resumbale pulls */
|
|
|
|
partial := false
|
|
|
|
|
|
|
|
var from, to int64
|
|
|
|
|
|
|
|
contentRange := request.Header.Get("Range")
|
|
|
|
|
|
|
|
_, ok = request.Header["Range"]
|
|
|
|
if ok && contentRange == "" {
|
|
|
|
response.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentRange != "" {
|
|
|
|
from, to, err = parseRangeHeader(contentRange)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
partial = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var repo io.ReadCloser
|
|
|
|
|
|
|
|
var blen, bsize int64
|
|
|
|
|
|
|
|
if partial {
|
|
|
|
repo, blen, bsize, err = imgStore.GetBlobPartial(name, digest, mediaType, from, to)
|
|
|
|
} else {
|
|
|
|
repo, blen, err = imgStore.GetBlob(name, digest, mediaType)
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrBadBlobDigest) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
NewErrorList(NewError(DIGEST_INVALID, map[string]string{"digest": digest})))
|
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
|
|
|
} else if errors.Is(err, zerr.ErrBlobNotFound) {
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(BLOB_UNKNOWN, map[string]string{"digest": digest})))
|
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
2022-08-19 05:38:59 -05:00
|
|
|
defer repo.Close()
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Content-Length", fmt.Sprintf("%d", blen))
|
2022-08-30 17:12:10 -05:00
|
|
|
|
|
|
|
status := http.StatusOK
|
|
|
|
|
|
|
|
if partial {
|
|
|
|
status = http.StatusPartialContent
|
|
|
|
|
|
|
|
response.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", from, from+blen-1, bsize))
|
|
|
|
} else {
|
|
|
|
response.Header().Set(constants.DistContentDigestKey, digest)
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
// return the blob data
|
2022-08-30 17:12:10 -05:00
|
|
|
WriteDataFromReader(response, status, blen, mediaType, repo, rh.c.Log)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlob godoc
|
|
|
|
// @Summary Delete image blob/layer
|
|
|
|
// @Description Delete an image's blob/layer given a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param digest path string true "blob/layer digest"
|
|
|
|
// @Success 202 {string} string "accepted"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/{digest} [delete].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) DeleteBlob(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
digest, ok := vars["digest"]
|
|
|
|
if !ok || digest == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err := imgStore.DeleteBlob(name, digest)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrBadBlobDigest) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
NewErrorList(NewError(DIGEST_INVALID, map[string]string{"digest": digest})))
|
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
|
|
|
} else if errors.Is(err, zerr.ErrBlobNotFound) {
|
|
|
|
WriteJSON(response,
|
|
|
|
http.StatusNotFound,
|
|
|
|
NewErrorList(NewError(BLOB_UNKNOWN, map[string]string{"digest": digest})))
|
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusAccepted)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBlobUpload godoc
|
|
|
|
// @Summary Create image blob/layer upload
|
|
|
|
// @Description Create a new image blob/layer upload
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Success 202 {string} string "accepted"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Header 202 {string} Location "/v2/{name}/blobs/uploads/{session_id}"
|
2022-06-29 03:12:20 -05:00
|
|
|
// @Header 202 {string} Range "0-0"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/uploads [post].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) CreateBlobUpload(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
// currently zot does not support cross-repository mounting, following dist-spec and returning 202
|
2021-12-13 14:23:31 -05:00
|
|
|
if mountDigests, ok := request.URL.Query()["mount"]; ok {
|
2021-01-26 17:26:18 -05:00
|
|
|
if len(mountDigests) != 1 {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2021-01-26 17:26:18 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:45:01 -05:00
|
|
|
// zot does not support cross mounting directly and do a workaround creating using hard link.
|
|
|
|
// check blob looks for actual path (name+mountDigests[0]) first then look for cache and
|
|
|
|
// if found in cache, will do hard link and if fails we will start new upload.
|
2021-12-13 14:23:31 -05:00
|
|
|
_, _, err := imgStore.CheckBlob(name, mountDigests[0])
|
2021-01-26 17:26:18 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload(name)
|
2021-04-23 17:51:24 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound, NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
|
|
|
} else {
|
2021-04-23 17:51:24 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2021-04-23 17:51:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadSessionLocation(request.URL, upload))
|
2022-06-29 03:12:20 -05:00
|
|
|
response.Header().Set("Range", "0-0")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusAccepted)
|
2021-04-23 17:51:24 -05:00
|
|
|
|
2021-01-26 17:26:18 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadLocation(request.URL, name, mountDigests[0]))
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusCreated)
|
2021-01-26 17:26:18 -05:00
|
|
|
|
2020-01-31 02:59:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if _, ok := request.URL.Query()["from"]; ok {
|
|
|
|
response.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
2020-01-31 02:59:36 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-31 02:58:08 -05:00
|
|
|
// a full blob upload if "digest" is present
|
2021-12-13 14:23:31 -05:00
|
|
|
digests, ok := request.URL.Query()["digest"]
|
2020-01-31 02:58:08 -05:00
|
|
|
if ok {
|
|
|
|
if len(digests) != 1 {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2020-01-31 02:58:08 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
digest := digests[0]
|
|
|
|
|
2022-03-24 07:25:14 -05:00
|
|
|
if contentType := request.Header.Get("Content-Type"); contentType != constants.BinaryMediaType {
|
|
|
|
rh.c.Log.Warn().Str("actual", contentType).Str("expected", constants.BinaryMediaType).Msg("invalid media type")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusUnsupportedMediaType)
|
2020-01-31 02:58:08 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
rh.c.Log.Info().Int64("r.ContentLength", request.ContentLength).Msg("DEBUG")
|
2020-01-31 02:58:08 -05:00
|
|
|
|
|
|
|
var contentLength int64
|
2020-01-31 02:59:36 -05:00
|
|
|
|
2020-01-31 02:58:08 -05:00
|
|
|
var err error
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
contentLength, err = strconv.ParseInt(request.Header.Get("Content-Length"), 10, 64)
|
|
|
|
if err != nil || contentLength <= 0 {
|
|
|
|
rh.c.Log.Warn().Str("actual", request.Header.Get("Content-Length")).Msg("invalid content length")
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_INVALID, map[string]string{"digest": digest})))
|
2020-01-31 02:58:08 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
sessionID, size, err := imgStore.FullBlobUpload(name, request.Body, digest)
|
2020-01-31 02:58:08 -05:00
|
|
|
if err != nil {
|
|
|
|
rh.c.Log.Error().Err(err).Int64("actual", size).Int64("expected", contentLength).Msg("failed full upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2020-01-31 02:58:08 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if size != contentLength {
|
|
|
|
rh.c.Log.Warn().Int64("actual", size).Int64("expected", contentLength).Msg("invalid content length")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2020-01-31 02:58:08 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadLocation(request.URL, name, digest))
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.BlobUploadUUID, sessionID)
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusCreated)
|
2020-01-31 02:58:08 -05:00
|
|
|
|
2019-12-20 13:37:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
upload, err := imgStore.NewBlobUpload(name)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound, NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadSessionLocation(request.URL, upload))
|
2022-06-29 03:12:20 -05:00
|
|
|
response.Header().Set("Range", "0-0")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusAccepted)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlobUpload godoc
|
|
|
|
// @Summary Get image blob/layer upload
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Description Get an image's blob/layer upload given a session_id
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Param session_id path string true "upload session_id"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Success 204 {string} string "no content"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Header 202 {string} Location "/v2/{name}/blobs/uploads/{session_id}"
|
2022-06-29 03:12:20 -05:00
|
|
|
// @Header 202 {string} Range "0-128"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/uploads/{session_id} [get].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) GetBlobUpload(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
sessionID, ok := vars["session_id"]
|
|
|
|
if !ok || sessionID == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
size, err := imgStore.GetBlobUpload(name, sessionID)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrBadUploadRange) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_INVALID, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrBadBlobDigest) {
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_INVALID, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrUploadNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_UNKNOWN, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadSessionLocation(request.URL, sessionID))
|
2022-06-29 03:12:20 -05:00
|
|
|
response.Header().Set("Range", fmt.Sprintf("0-%d", size-1))
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNoContent)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchBlobUpload godoc
|
|
|
|
// @Summary Resume image blob/layer upload
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Description Resume an image's blob/layer upload given an session_id
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Param session_id path string true "upload session_id"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Success 202 {string} string "accepted"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Header 202 {string} Location "/v2/{name}/blobs/uploads/{session_id}"
|
2022-06-29 03:12:20 -05:00
|
|
|
// @Header 202 {string} Range "0-128"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Header 200 {object} api.BlobUploadUUID
|
|
|
|
// @Failure 400 {string} string "bad request"
|
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 416 {string} string "range not satisfiable"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/uploads/{session_id} [patch].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) PatchBlobUpload(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
sessionID, ok := vars["session_id"]
|
|
|
|
if !ok || sessionID == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
var clen int64
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
var err error
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if request.Header.Get("Content-Length") == "" || request.Header.Get("Content-Range") == "" {
|
2020-01-09 19:31:34 -05:00
|
|
|
// streamed blob upload
|
2021-12-13 14:23:31 -05:00
|
|
|
clen, err = imgStore.PutBlobChunkStreamed(name, sessionID, request.Body)
|
2020-01-09 19:31:34 -05:00
|
|
|
} else {
|
|
|
|
// chunked blob upload
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
var contentLength int64
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if contentLength, err = strconv.ParseInt(request.Header.Get("Content-Length"), 10, 64); err != nil {
|
|
|
|
rh.c.Log.Warn().Str("actual", request.Header.Get("Content-Length")).Msg("invalid content length")
|
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
var from, to int64
|
2021-12-13 14:23:31 -05:00
|
|
|
if from, to, err = getContentRange(request); err != nil || (to-from)+1 != contentLength {
|
|
|
|
response.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
clen, err = imgStore.PutBlobChunk(name, sessionID, from, to, request.Body)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-01-23 15:26:02 -05:00
|
|
|
if errors.Is(err, zerr.ErrBadUploadRange) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusRequestedRangeNotSatisfiable,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_INVALID, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrUploadNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_UNKNOWN, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2022-01-23 15:26:02 -05:00
|
|
|
// could be io.ErrUnexpectedEOF, syscall.EMFILE (Err:0x18 too many opened files), etc
|
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error: removing .uploads/ files")
|
|
|
|
|
|
|
|
if err = imgStore.DeleteBlobUpload(name, sessionID); err != nil {
|
|
|
|
rh.c.Log.Error().Err(err).Msgf("couldn't remove blobUpload %s in repo %s", sessionID, name)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadSessionLocation(request.URL, sessionID))
|
2022-06-29 03:12:20 -05:00
|
|
|
response.Header().Set("Range", fmt.Sprintf("0-%d", clen-1))
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Content-Length", "0")
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.BlobUploadUUID, sessionID)
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusAccepted)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateBlobUpload godoc
|
|
|
|
// @Summary Update image blob/layer upload
|
|
|
|
// @Description Update and finish an image's blob/layer upload given a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Param session_id path string true "upload session_id"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Param digest query string true "blob/layer digest"
|
|
|
|
// @Success 201 {string} string "created"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Header 202 {string} Location "/v2/{name}/blobs/uploads/{digest}"
|
2022-03-24 07:25:14 -05:00
|
|
|
// @Header 200 {object} constants.DistContentDigestKey
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/uploads/{session_id} [put].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) UpdateBlobUpload(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
sessionID, ok := vars["session_id"]
|
|
|
|
if !ok || sessionID == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
digests, ok := request.URL.Query()["digest"]
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || len(digests) != 1 {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
digest := digests[0]
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
rh.c.Log.Info().Int64("r.ContentLength", request.ContentLength).Msg("DEBUG")
|
2020-01-09 19:31:34 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
contentPresent := true
|
2021-01-27 20:36:33 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
contentLen, err := strconv.ParseInt(request.Header.Get("Content-Length"), 10, 64)
|
2020-01-09 19:31:34 -05:00
|
|
|
if err != nil {
|
2019-06-20 18:36:40 -05:00
|
|
|
contentPresent = false
|
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
contentRangePresent := true
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if request.Header.Get("Content-Range") == "" {
|
2019-06-20 18:36:40 -05:00
|
|
|
contentRangePresent = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// we expect at least one of "Content-Length" or "Content-Range" to be
|
|
|
|
// present
|
|
|
|
if !contentPresent && !contentRangePresent {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var from, to int64
|
|
|
|
|
|
|
|
if contentPresent {
|
2021-12-13 14:23:31 -05:00
|
|
|
contentRange := request.Header.Get("Content-Range")
|
2019-06-20 18:36:40 -05:00
|
|
|
if contentRange == "" { // monolithic upload
|
|
|
|
from = 0
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
if contentLen == 0 {
|
2021-12-13 14:23:31 -05:00
|
|
|
goto finish
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
to = contentLen
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if from, to, err = getContentRange(request); err != nil { // finish chunked upload
|
|
|
|
response.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err = imgStore.PutBlobChunk(name, sessionID, from, to, request.Body)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2022-01-23 15:26:02 -05:00
|
|
|
if errors.Is(err, zerr.ErrBadUploadRange) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_INVALID, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrUploadNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_UNKNOWN, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2022-01-23 15:26:02 -05:00
|
|
|
// could be io.ErrUnexpectedEOF, syscall.EMFILE (Err:0x18 too many opened files), etc
|
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error: removing .uploads/ files")
|
|
|
|
|
|
|
|
if err = imgStore.DeleteBlobUpload(name, sessionID); err != nil {
|
|
|
|
rh.c.Log.Error().Err(err).Msgf("couldn't remove blobUpload %s in repo %s", sessionID, name)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
finish:
|
2019-06-20 18:36:40 -05:00
|
|
|
// blob chunks already transferred, just finish
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := imgStore.FinishBlobUpload(name, sessionID, request.Body, digest); err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrBadBlobDigest) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(DIGEST_INVALID, map[string]string{"digest": digest})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrBadUploadRange) {
|
|
|
|
WriteJSON(response, http.StatusBadRequest,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_INVALID, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrRepoNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrUploadNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_UNKNOWN, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2022-01-23 15:26:02 -05:00
|
|
|
// could be io.ErrUnexpectedEOF, syscall.EMFILE (Err:0x18 too many opened files), etc
|
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error: removing .uploads/ files")
|
|
|
|
|
|
|
|
if err = imgStore.DeleteBlobUpload(name, sessionID); err != nil {
|
|
|
|
rh.c.Log.Error().Err(err).Msgf("couldn't remove blobUpload %s in repo %s", sessionID, name)
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:54:42 -05:00
|
|
|
response.Header().Set("Location", getBlobUploadLocation(request.URL, name, digest))
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Content-Length", "0")
|
2022-03-24 07:25:14 -05:00
|
|
|
response.Header().Set(constants.DistContentDigestKey, digest)
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusCreated)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlobUpload godoc
|
|
|
|
// @Summary Delete image blob/layer
|
|
|
|
// @Description Delete an image's blob/layer given a digest
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
2020-01-09 19:31:34 -05:00
|
|
|
// @Param session_id path string true "upload session_id"
|
2019-06-20 18:36:40 -05:00
|
|
|
// @Success 200 {string} string "ok"
|
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/{name}/blobs/uploads/{session_id} [delete].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) DeleteBlobUpload(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2019-07-10 00:23:59 -05:00
|
|
|
name, ok := vars["name"]
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2020-01-09 19:31:34 -05:00
|
|
|
sessionID, ok := vars["session_id"]
|
|
|
|
if !ok || sessionID == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := imgStore.DeleteBlobUpload(name, sessionID); err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) { //nolint:gocritic // errorslint conflicts with gocritic:IfElseChain
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(NAME_UNKNOWN, map[string]string{"name": name})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else if errors.Is(err, zerr.ErrUploadNotFound) {
|
|
|
|
WriteJSON(response, http.StatusNotFound,
|
2020-01-31 02:59:36 -05:00
|
|
|
NewErrorList(NewError(BLOB_UPLOAD_UNKNOWN, map[string]string{"session_id": sessionID})))
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2019-11-25 17:33:58 -05:00
|
|
|
rh.c.Log.Error().Err(err).Msg("unexpected error")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNoContent)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type RepositoryList struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepositories godoc
|
|
|
|
// @Summary List image repositories
|
|
|
|
// @Description List all image repositories
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} api.RepositoryList
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
2020-05-11 17:13:24 -05:00
|
|
|
// @Router /v2/_catalog [get].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) ListRepositories(response http.ResponseWriter, request *http.Request) {
|
2021-04-05 19:40:33 -05:00
|
|
|
combineRepoList := make([]string, 0)
|
|
|
|
|
|
|
|
subStore := rh.c.StoreController.SubStore
|
|
|
|
|
|
|
|
for _, imgStore := range subStore {
|
|
|
|
repos, err := imgStore.GetRepositories()
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
combineRepoList = append(combineRepoList, repos...)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
singleStore := rh.c.StoreController.DefaultStore
|
|
|
|
if singleStore != nil {
|
|
|
|
repos, err := singleStore.GetRepositories()
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
combineRepoList = append(combineRepoList, repos...)
|
|
|
|
}
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
var repos []string
|
2022-08-16 03:57:09 -05:00
|
|
|
authzCtxKey := localCtx.GetContextKey()
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
// get passed context from authzHandler and filter out repos based on permissions
|
2021-12-13 14:23:31 -05:00
|
|
|
if authCtx := request.Context().Value(authzCtxKey); authCtx != nil {
|
2022-08-16 03:57:09 -05:00
|
|
|
acCtx, ok := authCtx.(localCtx.AccessControlContext)
|
2021-12-13 14:23:31 -05:00
|
|
|
if !ok {
|
|
|
|
response.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
for _, r := range combineRepoList {
|
2022-08-16 03:57:09 -05:00
|
|
|
if acCtx.IsAdmin || matchesRepo(acCtx.GlobPatterns, r) {
|
2021-05-13 13:59:12 -05:00
|
|
|
repos = append(repos, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
repos = combineRepoList
|
|
|
|
}
|
|
|
|
|
|
|
|
is := RepositoryList{Repositories: repos}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusOK, is)
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
// ListExtensions godoc
|
|
|
|
// @Summary List Registry level extensions
|
|
|
|
// @Description List all extensions present on registry
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} api.ExtensionList
|
|
|
|
// @Router /v2/_oci/ext/discover [get].
|
|
|
|
func (rh *RouteHandler) ListExtensions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
extensionList := ext.GetExtensions(rh.c.Config)
|
|
|
|
|
|
|
|
WriteJSON(w, http.StatusOK, extensionList)
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:05:00 -05:00
|
|
|
func (rh *RouteHandler) GetMetrics(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := rh.c.Metrics.ReceiveMetrics()
|
|
|
|
WriteJSON(w, http.StatusOK, m)
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
// helper routines
|
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
func getContentRange(r *http.Request) (int64 /* from */, int64 /* to */, error) {
|
|
|
|
contentRange := r.Header.Get("Content-Range")
|
2019-06-20 18:36:40 -05:00
|
|
|
tokens := strings.Split(contentRange, "-")
|
2021-01-27 20:36:33 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
rangeStart, err := strconv.ParseInt(tokens[0], 10, 64)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return -1, -1, zerr.ErrBadUploadRange
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
rangeEnd, err := strconv.ParseInt(tokens[1], 10, 64)
|
2019-06-20 18:36:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return -1, -1, zerr.ErrBadUploadRange
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if rangeStart > rangeEnd {
|
|
|
|
return -1, -1, zerr.ErrBadUploadRange
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return rangeStart, rangeEnd, nil
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
2019-07-10 00:23:59 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func WriteJSON(response http.ResponseWriter, status int, data interface{}) {
|
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2021-01-26 17:26:18 -05:00
|
|
|
body, err := json.Marshal(data)
|
2019-07-10 00:23:59 -05:00
|
|
|
if err != nil {
|
2020-01-06 19:21:46 -05:00
|
|
|
panic(err)
|
2019-07-10 00:23:59 -05:00
|
|
|
}
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2022-03-24 07:25:14 -05:00
|
|
|
WriteData(response, status, constants.DefaultMediaType, body)
|
2019-07-10 00:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteData(w http.ResponseWriter, status int, mediaType string, data []byte) {
|
|
|
|
w.Header().Set("Content-Type", mediaType)
|
|
|
|
w.WriteHeader(status)
|
|
|
|
_, _ = w.Write(data)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func WriteDataFromReader(response http.ResponseWriter, status int, length int64, mediaType string,
|
2022-03-21 12:37:23 -05:00
|
|
|
reader io.Reader, logger log.Logger,
|
|
|
|
) {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.Header().Set("Content-Type", mediaType)
|
|
|
|
response.Header().Set("Content-Length", strconv.FormatInt(length, 10))
|
|
|
|
response.WriteHeader(status)
|
2019-07-10 00:23:59 -05:00
|
|
|
|
|
|
|
const maxSize = 10 * 1024 * 1024
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
for {
|
2021-12-13 14:23:31 -05:00
|
|
|
_, err := io.CopyN(response, reader, maxSize)
|
|
|
|
if errors.Is(err, io.EOF) {
|
2019-07-10 00:23:59 -05:00
|
|
|
break
|
2020-01-04 21:20:06 -05:00
|
|
|
} else if err != nil {
|
|
|
|
// other kinds of intermittent errors can occur, e.g, io.ErrShortWrite
|
2020-01-06 19:21:46 -05:00
|
|
|
logger.Error().Err(err).Msg("copying data into http response")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-01-06 19:21:46 -05:00
|
|
|
return
|
2019-07-10 00:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
// will return image storage corresponding to subpath provided in config.
|
2021-09-30 08:27:13 -05:00
|
|
|
func (rh *RouteHandler) getImageStore(name string) storage.ImageStore {
|
2021-04-05 19:40:33 -05:00
|
|
|
return rh.c.StoreController.GetImageStore(name)
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
// will sync on demand if an image is not found, in case sync extensions is enabled.
|
2021-12-13 14:23:31 -05:00
|
|
|
func getImageManifest(routeHandler *RouteHandler, imgStore storage.ImageStore, name,
|
2022-03-21 12:37:23 -05:00
|
|
|
reference string,
|
|
|
|
) ([]byte, string, string, error) {
|
2021-12-13 14:23:31 -05:00
|
|
|
content, digest, mediaType, err := imgStore.GetImageManifest(name, reference)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
2022-01-10 11:06:12 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) || errors.Is(err, zerr.ErrManifestNotFound) {
|
2021-12-28 08:29:30 -05:00
|
|
|
if routeHandler.c.Config.Extensions != nil &&
|
|
|
|
routeHandler.c.Config.Extensions.Sync != nil &&
|
|
|
|
*routeHandler.c.Config.Extensions.Sync.Enable {
|
2021-12-13 14:23:31 -05:00
|
|
|
routeHandler.c.Log.Info().Msgf("image not found, trying to get image %s:%s by syncing on demand",
|
|
|
|
name, reference)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
errSync := ext.SyncOneImage(routeHandler.c.Config, routeHandler.c.StoreController,
|
2022-01-10 11:06:12 -05:00
|
|
|
name, reference, false, routeHandler.c.Log)
|
2021-10-28 04:10:01 -05:00
|
|
|
if errSync != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
routeHandler.c.Log.Err(errSync).Msgf("error encounter while syncing image %s:%s",
|
|
|
|
name, reference)
|
2021-10-28 04:10:01 -05:00
|
|
|
} else {
|
2021-12-13 14:23:31 -05:00
|
|
|
content, digest, mediaType, err = imgStore.GetImageManifest(name, reference)
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
} else {
|
2021-06-08 15:11:18 -05:00
|
|
|
return []byte{}, "", "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, digest, mediaType, err
|
|
|
|
}
|
2021-10-29 21:10:55 -05:00
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
// will sync referrers on demand if they are not found, in case sync extensions is enabled.
|
|
|
|
func getReferrers(routeHandler *RouteHandler, imgStore storage.ImageStore, name, digest,
|
2022-03-21 12:37:23 -05:00
|
|
|
artifactType string,
|
|
|
|
) ([]artifactspec.Descriptor, error) {
|
2022-01-10 11:06:12 -05:00
|
|
|
refs, err := imgStore.GetReferrers(name, digest, artifactType)
|
|
|
|
if err != nil {
|
2021-12-28 08:29:30 -05:00
|
|
|
if routeHandler.c.Config.Extensions != nil &&
|
|
|
|
routeHandler.c.Config.Extensions.Sync != nil &&
|
|
|
|
*routeHandler.c.Config.Extensions.Sync.Enable {
|
2022-01-10 11:06:12 -05:00
|
|
|
routeHandler.c.Log.Info().Msgf("signature not found, trying to get signature %s:%s by syncing on demand",
|
|
|
|
name, digest)
|
|
|
|
|
|
|
|
errSync := ext.SyncOneImage(routeHandler.c.Config, routeHandler.c.StoreController,
|
|
|
|
name, digest, true, routeHandler.c.Log)
|
|
|
|
if errSync != nil {
|
|
|
|
routeHandler.c.Log.Error().Err(err).Str("name", name).Str("digest", digest).Msg("unable to get references")
|
|
|
|
|
|
|
|
return []artifactspec.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs, err = imgStore.GetReferrers(name, digest, artifactType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs, err
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:10:55 -05:00
|
|
|
type ReferenceList struct {
|
2022-01-31 16:33:07 -05:00
|
|
|
References []artifactspec.Descriptor `json:"references"`
|
2021-10-29 21:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReferrers godoc
|
|
|
|
// @Summary Get references for an image
|
|
|
|
// @Description Get references for an image given a digest and artifact type
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param name path string true "repository name"
|
|
|
|
// @Param digest path string true "image digest"
|
|
|
|
// @Param artifactType query string true "artifact type"
|
|
|
|
// @Success 200 {string} string "ok"
|
|
|
|
// @Failure 404 {string} string "not found"
|
|
|
|
// @Failure 500 {string} string "internal server error"
|
|
|
|
// @Router /oras/artifacts/v1/{name:%s}/manifests/{digest}/referrers [get].
|
2021-12-13 14:23:31 -05:00
|
|
|
func (rh *RouteHandler) GetReferrers(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2021-10-29 21:10:55 -05:00
|
|
|
name, ok := vars["name"]
|
|
|
|
|
|
|
|
if !ok || name == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2021-10-29 21:10:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
digest, ok := vars["digest"]
|
|
|
|
if !ok || digest == "" {
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2021-10-29 21:10:55 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
artifactTypes, ok := request.URL.Query()["artifactType"]
|
2021-10-29 21:10:55 -05:00
|
|
|
if !ok || len(artifactTypes) != 1 {
|
|
|
|
rh.c.Log.Error().Msg("invalid artifact types")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
2021-10-29 21:10:55 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactType := artifactTypes[0]
|
|
|
|
|
|
|
|
if artifactType != notreg.ArtifactTypeNotation {
|
|
|
|
rh.c.Log.Error().Str("artifactType", artifactType).Msg("invalid artifact type")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
2021-10-29 21:10:55 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := rh.getImageStore(name)
|
2021-10-29 21:10:55 -05:00
|
|
|
|
|
|
|
rh.c.Log.Info().Str("digest", digest).Str("artifactType", artifactType).Msg("getting manifest")
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
refs, err := getReferrers(rh, imgStore, name, digest, artifactType) //nolint:contextcheck
|
2021-10-29 21:10:55 -05:00
|
|
|
if err != nil {
|
|
|
|
rh.c.Log.Error().Err(err).Str("name", name).Str("digest", digest).Msg("unable to get references")
|
2021-12-13 14:23:31 -05:00
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
2021-10-29 21:10:55 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := ReferenceList{References: refs}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
WriteJSON(response, http.StatusOK, rs)
|
2021-10-29 21:10:55 -05:00
|
|
|
}
|
2022-06-02 15:54:42 -05:00
|
|
|
|
|
|
|
// GetBlobUploadSessionLocation returns actual blob location to start/resume uploading blobs.
|
|
|
|
// e.g. /v2/<name>/blobs/uploads/<session-id>.
|
|
|
|
func getBlobUploadSessionLocation(url *url.URL, sessionID string) string {
|
|
|
|
url.RawQuery = ""
|
|
|
|
|
|
|
|
if !strings.Contains(url.Path, sessionID) {
|
|
|
|
url.Path = path.Join(url.Path, sessionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlobUploadLocation returns actual blob location on registry
|
|
|
|
// e.g /v2/<name>/blobs/<digest>.
|
|
|
|
func getBlobUploadLocation(url *url.URL, name, digest string) string {
|
|
|
|
url.RawQuery = ""
|
|
|
|
|
|
|
|
// we are relying on request URL to set location and
|
|
|
|
// if request URL contains uploads either we are resuming blob upload or starting a new blob upload.
|
|
|
|
// getBlobUploadLocation will be called only when blob upload is completed and
|
|
|
|
// location should be set as blob url <v2/<name>/blobs/<digest>>.
|
|
|
|
if strings.Contains(url.Path, "uploads") {
|
|
|
|
url.Path = path.Join(constants.RoutePrefix, name, constants.Blobs, digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.String()
|
|
|
|
}
|