From 695046de3b1ff8de009aca0293f09baad5dc9c0b Mon Sep 17 00:00:00 2001 From: jay-dee7 Date: Sat, 10 Jul 2021 18:17:29 +0530 Subject: [PATCH] Added missing endpoints and fixed delete endpoints /w auth Signed-off-by: jay-dee7 --- .gitignore | 1 + cache/store.go | 12 +++++------ go.mod | 4 ++-- go.sum | 2 ++ main.go | 7 ++----- server/registry/v2/blobs.go | 2 +- server/registry/v2/types.go | 42 ++++++++++++++++++++++++++++++++----- 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 2d8e872b..c7c127e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ ./certs ./parachute +.cache ./parachute.yaml .idea/ tests diff --git a/cache/store.go b/cache/store.go index 722bd5f9..4c33c342 100644 --- a/cache/store.go +++ b/cache/store.go @@ -162,12 +162,12 @@ func (ds *dataStore) ResolveManifestRef(namespace, ref string) (string, error) { } for _, c := range md.Manifest.Config { - mdRef := c.Reference - mdDigest := c.Digest - if ref == mdRef || ref == mdDigest { - return c.SkynetLink, nil + mdRef := c.Reference + mdDigest := c.Digest + if ref == mdRef || ref == mdDigest { + return c.SkynetLink, nil + } } -} return "", fmt.Errorf("ref not found") } @@ -177,7 +177,7 @@ const layerDigestNamespace = "layers/digests" func (ds *dataStore) SetDigest(digest, skylink string) error { key := fmt.Sprintf("%s/%s", layerDigestNamespace, digest) value := types.LayerRef{ - Digest: digest, + Digest: digest, Skylink: skylink, } diff --git a/go.mod b/go.mod index 3b2f502e..95714c29 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/docker/distribution v2.7.1+incompatible github.com/docker/docker v20.10.6+incompatible github.com/fatih/color v1.7.0 - github.com/golang-jwt/jwt v3.2.1+incompatible // indirect + github.com/golang-jwt/jwt v3.2.1+incompatible github.com/google/go-containerregistry v0.5.1 github.com/labstack/echo-contrib v0.11.0 github.com/labstack/echo/v4 v4.3.0 @@ -21,7 +21,7 @@ require ( github.com/spf13/viper v1.7.1 github.com/whyrusleeping/tar-utils v0.0.0-20201201191210-20a61371de5b gitlab.com/NebulousLabs/errors v0.0.0-20200929122200-06c536cf6975 // indirect - golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect + golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect golang.org/x/time v0.0.0-20210608053304-ed9ce3a009e4 // indirect google.golang.org/genproto v0.0.0-20210521181308-5ccab8a35a9a // indirect diff --git a/go.sum b/go.sum index 9bbfce54..fcc84d8e 100644 --- a/go.sum +++ b/go.sum @@ -72,6 +72,8 @@ github.com/Microsoft/hcsshim v0.8.16/go.mod h1:o5/SZqmR7x9JNKsW3pu+nqHm0MF8vbA+V github.com/Microsoft/hcsshim/test v0.0.0-20201218223536-d3e5debf77da/go.mod h1:5hlzMzRKMLyo42nCZ9oml8AdTlq/0cvIaBv6tK1RehU= github.com/Microsoft/hcsshim/test v0.0.0-20210227013316-43a75bb4edd3/go.mod h1:mw7qgWloBUl75W/gVH3cQszUg1+gUITj7D6NY7ywVnY= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= +github.com/NebulousLabs/go-skynet/v2 v2.0.1 h1:c5x/onhWwap9p9QgcGZtep580vUYdcIdCgAHueyksyM= +github.com/NebulousLabs/go-skynet/v2 v2.0.1/go.mod h1:uhGD1M7Qe1nXsnqRD90B5CJVgjqSqwJUmDosgAn4CdQ= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= diff --git a/main.go b/main.go index eaded988..1452b7ea 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,8 @@ func main() { // GET GET /v2//blobs/uploads/ router.Add(http.MethodGet, "/blobs/uploads/:uuid", reg.UploadProgress) - // router.Add(http.MethodGet, "/blobs/:digest", reg.DownloadBlob) + router.Add(http.MethodDelete, "/blobs/:digest", reg.DeleteLayer).Name = "DeleteLayer" + router.Add(http.MethodDelete, "/manifests/:digest", reg.DeleteImage).Name = "DeleteImage" e.Add(http.MethodGet, "/v2/", reg.ApiVersion, BasicAuth(authSvc.BasicAuth)) @@ -148,17 +149,13 @@ Strict-Transport-Security: max-age=31536000 func BasicAuth(authfn func(string, string) (map[string]interface{}, error)) echo.MiddlewareFunc { return middleware.BasicAuth(func(username string, password string, ctx echo.Context) (bool, error) { - if ctx.Request().RequestURI != "/v2/" { if ctx.Request().Method == http.MethodHead || ctx.Request().Method == http.MethodGet { return true, nil } } - color.Red("request uri %s", ctx.Request().RequestURI) - if ctx.Request().RequestURI == "/v2/" { - color.Blue("username %s password %s\n", username, password) _, err := authfn(username, password) if err != nil { return false, ctx.NoContent(http.StatusUnauthorized) diff --git a/server/registry/v2/blobs.go b/server/registry/v2/blobs.go index 0fa525ba..a3cbc8df 100644 --- a/server/registry/v2/blobs.go +++ b/server/registry/v2/blobs.go @@ -153,7 +153,7 @@ func (b *blobs) UploadBlob(ctx echo.Context) error { buf := bytes.NewBuffer(b.uploads[uuid]) // 90 io.Copy(buf, ctx.Request().Body) // 10 - defer ctx.Request().Body.Close() + ctx.Request().Body.Close() b.uploads[uuid] = buf.Bytes() locationHeader := fmt.Sprintf("/v2/%s/blobs/uploads/%s", namespace, uuid) diff --git a/server/registry/v2/types.go b/server/registry/v2/types.go index a85b9184..14c7bc1f 100644 --- a/server/registry/v2/types.go +++ b/server/registry/v2/types.go @@ -42,7 +42,15 @@ func NewRegistry(skynetClient *skynet.Client, logger zerolog.Logger, c cache.Sto } func (r *registry) DeleteLayer(ctx echo.Context) error { - return nil + dig := ctx.Param("digest") + + _, err := r.localCache.GetDigest(dig) + if err != nil { + errMsg := r.errorResponse(RegistryErrorCodeBlobUnknown, err.Error(), nil) + return ctx.JSONBlob(http.StatusNotFound, errMsg) + } + + return ctx.NoContent(http.StatusOK) } // PUT /v2//blobs/uploads/?digest= @@ -56,7 +64,7 @@ func (r *registry) MonolithicUpload(ctx echo.Context) error { errMsg := r.errorResponse(RegistryErrorCodeBlobUploadInvalid, err.Error(), nil) return ctx.JSONBlob(http.StatusBadRequest, errMsg) } - defer ctx.Request().Body.Close() + ctx.Request().Body.Close() link, err := r.skynet.Upload(namespace, digest, bz) if err != nil { @@ -89,6 +97,7 @@ func (r *registry) CompleteUpload(ctx echo.Context) error { errMsg := r.errorResponse(RegistryErrorCodeDigestInvalid, err.Error(), nil) return ctx.JSONBlob(http.StatusBadRequest, errMsg) } + ctx.Request().Body.Close() buf := bytes.NewBuffer(r.b.uploads[uuid]) buf.Write(bz) @@ -131,7 +140,11 @@ func (r *registry) CompleteUpload(ctx echo.Context) error { Manifest: types.ImageManifest{ SchemaVersion: 2, MediaType: "", - Layers: []*types.Layer{{MediaType: "", Size: len(bz), Digest: dig, SkynetLink: skylink, UUID: uuid}}, + Layers: []*types.Layer{ + { + MediaType: "", Size: len(bz), Digest: dig, SkynetLink: skylink, UUID: uuid, + }, + }, }, } @@ -301,7 +314,7 @@ func (r *registry) PushManifest(ctx echo.Context) error { errMsg := r.errorResponse(RegistryErrorCodeManifestInvalid, err.Error(), nil) return ctx.JSONBlob(http.StatusBadRequest, errMsg) } - defer ctx.Request().Body.Close() + ctx.Request().Body.Close() dig := digest(bz) @@ -406,7 +419,26 @@ func (r *registry) PushManifest(ctx echo.Context) error { } func (r *registry) DeleteImage(ctx echo.Context) error { - return nil + clientDigest := ctx.Param("digest") + if clientDigest == "" { + path := strings.Split(ctx.Request().RequestURI, "/") + if len(path) == 6 { + clientDigest = path[5] + } + } + namespace := ctx.Param("username") + "/" + ctx.Param("imagename") + + if d, err := r.localCache.ResolveManifestRef(namespace, clientDigest); err != nil { + details := map[string]interface{}{ + "namespace": namespace, + "digest": clientDigest, + "data": d, + } + errMsg := r.errorResponse(RegistryErrorCodeManifestUnknown, err.Error(), details) + return ctx.JSONBlob(http.StatusNotFound, errMsg) + } + + return ctx.NoContent(http.StatusOK) } // GET /v2//blobs/