working commit
This commit is contained in:
@@ -6,6 +6,20 @@ import (
|
||||
)
|
||||
|
||||
const sha256prefix = "sha256:"
|
||||
const sha512prefix = "sha512:"
|
||||
|
||||
func normalizeSHADigest(digest string) string {
|
||||
if stringLikeSHA256Digest(digest) && !strings.HasPrefix(digest, sha256prefix) {
|
||||
digest = sha256prefix + digest
|
||||
} else if stringLikeSHA512Digest(digest) && !strings.HasPrefix(digest, sha512prefix) {
|
||||
digest = sha512prefix + digest
|
||||
}
|
||||
return digest
|
||||
}
|
||||
|
||||
func stringLikeSHADigest(some string) bool {
|
||||
return stringLikeSHA256Digest(some) || stringLikeSHA512Digest(some)
|
||||
}
|
||||
|
||||
func stringLikeSHA256Digest(some string) bool {
|
||||
if strings.HasPrefix(some, sha256prefix) {
|
||||
@@ -20,3 +34,17 @@ func stringLikeSHA256Digest(some string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func stringLikeSHA512Digest(some string) bool {
|
||||
if strings.HasPrefix(some, sha512prefix) {
|
||||
some = strings.TrimPrefix(some, sha512prefix)
|
||||
}
|
||||
_, err := hex.DecodeString(some)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if len(some) == 128 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import (
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/pkg/auxoci"
|
||||
|
||||
ocidigest "github.com/opencontainers/go-digest"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -69,7 +67,7 @@ func (oper *Operator) ManifestExists(ctx context.Context, params *ManifestExists
|
||||
}
|
||||
}
|
||||
|
||||
digest := ocidigest.SHA256.FromString(manifest.Payload)
|
||||
digest := auxoci.SHA256DigestFromString(manifest.Payload)
|
||||
payloadSize := len(manifest.Payload)
|
||||
res.ContentLength = strconv.FormatInt(int64(payloadSize), 10)
|
||||
res.ContentType = manifest.ContentType
|
||||
@@ -227,3 +225,52 @@ func (oper *Operator) PutManifest(ctx context.Context, params *PutManifestParams
|
||||
return res, http.StatusCreated, err
|
||||
|
||||
}
|
||||
|
||||
type GetManifestParams struct {
|
||||
Name string
|
||||
Reference string
|
||||
}
|
||||
type GetManifestResult struct {
|
||||
ContentLength string
|
||||
ContentType string
|
||||
DockerContentDigest string
|
||||
Payload string
|
||||
}
|
||||
|
||||
func (oper *Operator) GetManifest(ctx context.Context, params *GetManifestParams) (*GetManifestResult, int, error) {
|
||||
var err error
|
||||
res := &GetManifestResult{}
|
||||
oper.logg.Debugf("Get manifest %s:%s", params.Name, params.Reference)
|
||||
|
||||
manifestDescr := descr.Manifest{}
|
||||
var exists bool
|
||||
if stringLikeSHADigest(params.Reference) {
|
||||
digest := normalizeSHADigest(params.Reference)
|
||||
oper.logg.Debugf("Get manifest %s with digest %s", params.Name, params.Reference)
|
||||
exists, manifestDescr, 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("Get manifest %s with tag %s", params.Name, params.Reference)
|
||||
exists, manifestDescr, err = oper.mdb.GetManifestByReference(ctx, params.Name, params.Reference)
|
||||
if err != nil {
|
||||
return res, http.StatusInternalServerError, err
|
||||
}
|
||||
if !exists {
|
||||
return res, http.StatusNotFound, err
|
||||
}
|
||||
}
|
||||
|
||||
manifestDigest := auxoci.SHA256DigestFromString(manifestDescr.Payload)
|
||||
res.DockerContentDigest = manifestDigest.String()
|
||||
|
||||
res.ContentLength = strconv.FormatInt(int64(len(manifestDescr.Payload)), 10)
|
||||
res.ContentType = manifestDescr.ContentType
|
||||
res.Payload = manifestDescr.Payload
|
||||
|
||||
return res, http.StatusOK, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user