From 748d5ad2dc63425b4fb229215352c6de70199a7e Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 7 Oct 2022 12:00:15 +0000 Subject: [PATCH 1/2] Add support for GET and DELETE. --- routers/api/packages/api.go | 12 +++-- routers/api/packages/container/container.go | 45 +++++++++++++++++++ .../api_packages_container_test.go | 40 ++++++++++++++++- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/routers/api/packages/api.go b/routers/api/packages/api.go index 3354fe12d43ca..0889006dd7571 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -316,8 +316,10 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route { r.Group("/blobs/uploads", func() { r.Post("", container.InitiateUploadBlob) r.Group("/{uuid}", func() { + r.Get("", container.GetUploadBlob) r.Patch("", container.UploadBlob) r.Put("", container.EndUploadBlob) + r.Delete("", container.CancelUploadBlob) }) }, reqPackageAccess(perm.AccessModeWrite)) r.Group("/blobs/{digest}", func() { @@ -377,7 +379,7 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route { } m := blobsUploadsPattern.FindStringSubmatch(path) - if len(m) == 3 && (isPut || isPatch) { + if len(m) == 3 && (isGet || isPut || isPatch || isDelete) { reqPackageAccess(perm.AccessModeWrite)(ctx) if ctx.Written() { return @@ -391,10 +393,14 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route { ctx.SetParams("uuid", m[2]) - if isPatch { + if isGet { + container.GetUploadBlob(ctx) + } else if isPatch { container.UploadBlob(ctx) - } else { + } else if isPut { container.EndUploadBlob(ctx) + } else { + container.CancelUploadBlob(ctx) } return } diff --git a/routers/api/packages/container/container.go b/routers/api/packages/container/container.go index b961cd4afb393..5bc64e1b29f65 100644 --- a/routers/api/packages/container/container.go +++ b/routers/api/packages/container/container.go @@ -248,6 +248,27 @@ func InitiateUploadBlob(ctx *context.Context) { }) } +// https://docs.docker.com/registry/spec/api/#get-blob-upload +func GetUploadBlob(ctx *context.Context) { + uuid := ctx.Params("uuid") + + upload, err := packages_model.GetBlobUploadByID(ctx, uuid) + if err != nil { + if err == packages_model.ErrPackageBlobUploadNotExist { + apiErrorDefined(ctx, errBlobUploadUnknown) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } + return + } + + setResponseHeaders(ctx.Resp, &containerHeaders{ + Range: fmt.Sprintf("0-%d", upload.BytesReceived), + UploadUUID: upload.ID, + Status: http.StatusNoContent, + }) +} + // https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pushing-a-blob-in-chunks func UploadBlob(ctx *context.Context) { image := ctx.Params("image") @@ -354,6 +375,30 @@ func EndUploadBlob(ctx *context.Context) { }) } +// https://docs.docker.com/registry/spec/api/#delete-blob-upload +func CancelUploadBlob(ctx *context.Context) { + uuid := ctx.Params("uuid") + + _, err := packages_model.GetBlobUploadByID(ctx, uuid) + if err != nil { + if err == packages_model.ErrPackageBlobUploadNotExist { + apiErrorDefined(ctx, errBlobUploadUnknown) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } + return + } + + if err := container_service.RemoveBlobUploadByID(ctx, uuid); err != nil { + apiError(ctx, http.StatusInternalServerError, err) + return + } + + setResponseHeaders(ctx.Resp, &containerHeaders{ + Status: http.StatusNoContent, + }) +} + func getBlobFromContext(ctx *context.Context) (*packages_model.PackageFileDescriptor, error) { digest := ctx.Params("digest") diff --git a/tests/integration/api_packages_container_test.go b/tests/integration/api_packages_container_test.go index adced5d661ab4..84c3724577c43 100644 --- a/tests/integration/api_packages_container_test.go +++ b/tests/integration/api_packages_container_test.go @@ -205,18 +205,54 @@ func TestPackageContainer(t *testing.T) { assert.Equal(t, uuid, resp.Header().Get("Docker-Upload-Uuid")) assert.Equal(t, contentRange, resp.Header().Get("Range")) + uploadURL = resp.Header().Get("Location") + + req = NewRequest(t, "GET", setting.AppURL+uploadURL[1:]) + addTokenAuthHeader(req, userToken) + resp = MakeRequest(t, req, http.StatusNoContent) + + assert.Equal(t, uuid, resp.Header().Get("Docker-Upload-Uuid")) + assert.Equal(t, fmt.Sprintf("0-%d", len(blobContent)), resp.Header().Get("Range")) + pbu, err = packages_model.GetBlobUploadByID(db.DefaultContext, uuid) assert.NoError(t, err) assert.EqualValues(t, len(blobContent), pbu.BytesReceived) - uploadURL = resp.Header().Get("Location") - req = NewRequest(t, "PUT", fmt.Sprintf("%s?digest=%s", setting.AppURL+uploadURL[1:], blobDigest)) addTokenAuthHeader(req, userToken) resp = MakeRequest(t, req, http.StatusCreated) assert.Equal(t, fmt.Sprintf("/v2/%s/%s/blobs/%s", user.Name, image, blobDigest), resp.Header().Get("Location")) assert.Equal(t, blobDigest, resp.Header().Get("Docker-Content-Digest")) + + t.Run("Cancel", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequest(t, "POST", fmt.Sprintf("%s/blobs/uploads", url)) + addTokenAuthHeader(req, userToken) + resp := MakeRequest(t, req, http.StatusAccepted) + + uuid := resp.Header().Get("Docker-Upload-Uuid") + assert.NotEmpty(t, uuid) + + uploadURL := resp.Header().Get("Location") + assert.NotEmpty(t, uploadURL) + + req = NewRequest(t, "GET", setting.AppURL+uploadURL[1:]) + addTokenAuthHeader(req, userToken) + resp = MakeRequest(t, req, http.StatusNoContent) + + assert.Equal(t, uuid, resp.Header().Get("Docker-Upload-Uuid")) + assert.Equal(t, "0-0", resp.Header().Get("Range")) + + req = NewRequest(t, "DELETE", setting.AppURL+uploadURL[1:]) + addTokenAuthHeader(req, userToken) + MakeRequest(t, req, http.StatusNoContent) + + req = NewRequest(t, "GET", setting.AppURL+uploadURL[1:]) + addTokenAuthHeader(req, userToken) + MakeRequest(t, req, http.StatusNotFound) + }) }) for _, tag := range tags { From 72d8acfd2972a860e34dae6244c33b92b44b5a89 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 7 Oct 2022 12:57:55 +0000 Subject: [PATCH 2/2] lint --- tests/integration/api_packages_container_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/api_packages_container_test.go b/tests/integration/api_packages_container_test.go index 84c3724577c43..b8a3884371b20 100644 --- a/tests/integration/api_packages_container_test.go +++ b/tests/integration/api_packages_container_test.go @@ -227,11 +227,11 @@ func TestPackageContainer(t *testing.T) { t.Run("Cancel", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - + req := NewRequest(t, "POST", fmt.Sprintf("%s/blobs/uploads", url)) addTokenAuthHeader(req, userToken) resp := MakeRequest(t, req, http.StatusAccepted) - + uuid := resp.Header().Get("Docker-Upload-Uuid") assert.NotEmpty(t, uuid)