working commit
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package operator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"mstore/pkg/auxuuid"
|
||||
)
|
||||
|
||||
type BlobExistsParams struct {
|
||||
Name string
|
||||
Digest string
|
||||
}
|
||||
type BlobExistsResult struct {
|
||||
DockerContentDigest string
|
||||
ContentLength string
|
||||
Exists bool
|
||||
}
|
||||
|
||||
func (oper *Operator) BlobExists(ctx context.Context, params *BlobExistsParams) (*BlobExistsResult, int, error) {
|
||||
var err error
|
||||
res := &BlobExistsResult{}
|
||||
oper.logg.Debugf("Call BlobExists")
|
||||
if params.Digest == "" {
|
||||
err = fmt.Errorf("Empty reference")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
if params.Name == "" {
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
exists, blobDescr, err := oper.mdb.GetBlobByDigest(ctx, params.Digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !exists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
res.ContentLength = strconv.FormatInt(blobDescr.Size, 10)
|
||||
res.DockerContentDigest = params.Digest
|
||||
res.Exists = exists
|
||||
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
// https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md
|
||||
//
|
||||
// PostUpload
|
||||
//
|
||||
// POST then PUT
|
||||
//
|
||||
// To push a blob monolithically by using a POST request followed by a PUT request, there are two steps:
|
||||
//
|
||||
// - Obtain a session id (upload URL)
|
||||
// - Upload the blob to said URL
|
||||
//
|
||||
// A chunked blob upload is accomplished in three phases:
|
||||
//
|
||||
// - Obtain a session ID (upload URL) (POST)
|
||||
// - Upload the chunks (PATCH)
|
||||
// Close the session (PUT)
|
||||
|
||||
type PostUploadParams struct {
|
||||
Name string
|
||||
Digest string
|
||||
Mount string
|
||||
From string
|
||||
}
|
||||
type PostUploadResult struct {
|
||||
DockerUploadUUID string
|
||||
Location string
|
||||
ContentLength string
|
||||
}
|
||||
|
||||
func (oper *Operator) PostUpload(ctx context.Context, params *PostUploadParams) (*PostUploadResult, int, error) {
|
||||
var err error
|
||||
res := &PostUploadResult{}
|
||||
oper.logg.Debugf("PostUpload")
|
||||
|
||||
if params.Digest == "" {
|
||||
uuid := auxuuid.NewUUID()
|
||||
location := fmt.Sprintf("/v2/%s/blobs/uploads/%s", params.Name, uuid)
|
||||
res.DockerUploadUUID = uuid
|
||||
res.Location = location
|
||||
res.ContentLength = strconv.FormatInt(0, 10)
|
||||
return res, http.StatusAccepted, err
|
||||
} else {
|
||||
err = fmt.Errorf("PostUpload: Not empty digest header")
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
type PatchUploadParams struct {
|
||||
ContentType string
|
||||
ContentLength string
|
||||
Name string
|
||||
Reference string
|
||||
Reader io.Reader
|
||||
}
|
||||
type PatchUploadResult struct {
|
||||
Location string
|
||||
}
|
||||
|
||||
// The response for each successful chunk upload MUST be 202 Accepted, and MUST have the following headers:
|
||||
//
|
||||
// Location: <location>
|
||||
// Range: 0-<end-of-range>
|
||||
|
||||
func (oper *Operator) PatchUpload(ctx context.Context, params *PatchUploadParams) (*PatchUploadResult, int, error) {
|
||||
var err error
|
||||
res := &PatchUploadResult{}
|
||||
oper.logg.Debugf("Call PatchUpload")
|
||||
|
||||
if params.Reference == "" {
|
||||
err = fmt.Errorf("Empty reference")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
if params.Name == "" {
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
if params.ContentType != "application/octet-stream" {
|
||||
err = fmt.Errorf("Wrong Conten-Type header: %s", params.ContentType)
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
if params.ContentLength == "" {
|
||||
err = fmt.Errorf("Empty Content-length header")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
contentLength, err := strconv.ParseInt(params.ContentLength, 10, 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Wrong Content-length header")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
recsize, err := oper.store.WriteUpload(params.Reference, params.Reader)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if recsize != contentLength {
|
||||
oper.store.RemoveUpload(params.Reference)
|
||||
err = fmt.Errorf("Mismatch upload recorded size and content length")
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
res.Location = fmt.Sprintf("/v2/%s/uploads/%s", params.Name, params.Reference)
|
||||
|
||||
return res, http.StatusAccepted, err // http.StatusCreated
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package operator
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const sha256prefix = "sha256:"
|
||||
|
||||
func stringLikeSHA256Digest(some string) bool {
|
||||
if strings.HasPrefix(some, sha256prefix) {
|
||||
some = strings.TrimPrefix(some, sha256prefix)
|
||||
}
|
||||
_, err := hex.DecodeString(some)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if len(some) == 64 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package operator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"mstore/app/descr"
|
||||
|
||||
ocidigest "github.com/opencontainers/go-digest"
|
||||
)
|
||||
|
||||
const (
|
||||
ddmMimeType = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
oimMimeType = "application/vnd.oci.image.manifest.v1+json"
|
||||
)
|
||||
|
||||
type ManifestExistsParams struct {
|
||||
Name string
|
||||
Reference string
|
||||
}
|
||||
type ManifestExistsResult struct {
|
||||
ContentLength string
|
||||
ContentType string
|
||||
DockerContentDigest string
|
||||
Exists bool
|
||||
}
|
||||
|
||||
func (oper *Operator) ManifestExists(ctx context.Context, params *ManifestExistsParams) (*ManifestExistsResult, int, error) {
|
||||
var err error
|
||||
res := &ManifestExistsResult{}
|
||||
|
||||
if params.Name == "" {
|
||||
err = fmt.Errorf("Empty name")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
if params.Reference == "" {
|
||||
err = fmt.Errorf("Empty reference")
|
||||
return res, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
oper.logg.Debugf("Head manifest [%s:%s]", params.Name, params.Reference)
|
||||
|
||||
var manifest descr.Manifest
|
||||
var exists bool
|
||||
if stringLikeSHA256Digest(params.Reference) {
|
||||
digest := fmt.Sprintf("%s:%s", sha256prefix, params.Reference)
|
||||
oper.logg.Debugf("Find manifest %s by digest %s", params.Name, params.Reference)
|
||||
exists, manifest, err = oper.mdb.GetManifestByDigest(ctx, params.Name, digest)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !exists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
} else {
|
||||
oper.logg.Debugf("Find manifest %s by reference %s", params.Name, params.Reference)
|
||||
exists, manifest, err = oper.mdb.GetManifestByReference(ctx, params.Name, params.Reference)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !exists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
}
|
||||
|
||||
digest := ocidigest.SHA256.FromString(manifest.Payload)
|
||||
payloadSize := len(manifest.Payload)
|
||||
res.ContentLength = strconv.FormatInt(int64(payloadSize), 10)
|
||||
res.ContentType = manifest.ContentType
|
||||
res.DockerContentDigest = digest.String()
|
||||
res.Exists = exists
|
||||
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
type PutManifestParams struct {
|
||||
ContentType string
|
||||
Name string
|
||||
Reference string
|
||||
Reader io.Reader
|
||||
}
|
||||
type PutManifestResult struct {
|
||||
Location string
|
||||
}
|
||||
|
||||
// TODO: control size 413 Payload Too Large
|
||||
|
||||
func (oper *Operator) PutManifest(ctx context.Context, params *PutManifestParams) (*PutManifestResult, int, error) {
|
||||
var err error
|
||||
res := &PutManifestResult{}
|
||||
oper.logg.Debugf("Put manifest %s:%s", params.Name, params.Reference)
|
||||
|
||||
// Check Content-Type
|
||||
if params.ContentType != ddmMimeType && params.ContentType != oimMimeType {
|
||||
err = fmt.Errorf("Unknown or empty Content-Type: %s", params.ContentType)
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
// Copy manifest data
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
_, err = io.Copy(buffer, params.Reader)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
inBytes := buffer.Bytes()
|
||||
oper.logg.Debugf("Manifest data: [%s]", string(inBytes))
|
||||
|
||||
res.Location = fmt.Sprintf(`/v2/%s/manifests/%s`, params.Name, params.Reference)
|
||||
return res, http.StatusCreated, err
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user