-
Notifications
You must be signed in to change notification settings - Fork 9
/
blob_delete.go
112 lines (99 loc) · 4.12 KB
/
blob_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blob
import (
"errors"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/opencontainers/go-digest"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/imagerefs"
"github.com/go-sigma/sigma/pkg/utils/ptr"
"github.com/go-sigma/sigma/pkg/validators"
"github.com/go-sigma/sigma/pkg/xerrors"
)
// DeleteBlob handles the delete blob request
// Note: if blob associate with artifact, it cannot be deleted
func (h *handler) DeleteBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err
}
if needRet {
return nil
}
uri := c.Request().URL.Path
repository := strings.TrimPrefix(strings.TrimSuffix(uri[:strings.LastIndex(uri, "/")], "/blobs"), "/v2/")
_, namespace, _, _, err := imagerefs.Parse(repository)
if err != nil {
log.Error().Err(err).Str("Repository", repository).Msg("Repository must container a valid namespace")
return xerrors.NewDSError(c, xerrors.DSErrCodeManifestWithNamespace)
}
if !(validators.ValidateNamespaceRaw(namespace) && validators.ValidateRepositoryRaw(repository)) {
log.Error().Err(err).Str("Repository", repository).Msg("Repository must container a valid namespace")
return xerrors.NewDSError(c, xerrors.DSErrCodeManifestWithNamespace)
}
namespaceObj, err := h.namespaceServiceFactory.New().GetByName(ctx, namespace)
if err != nil {
log.Error().Err(err).Str("Name", repository).Msg("Get repository by name failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeBlobUnknown)
}
authChecked, err := h.authServiceFactory.New().Repository(ptr.To(user), namespaceObj.ID, enums.AuthManage)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Error().Err(errors.New(utils.UnwrapJoinedErrors(err))).Msg("Resource not found")
return xerrors.GenDSErrCodeResourceNotFound(err)
}
return xerrors.NewDSError(c, xerrors.DSErrCodeUnknown)
}
if !authChecked {
log.Error().Int64("UserID", user.ID).Int64("NamespaceID", namespaceObj.ID).Msg("Auth check failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeDenied)
}
dgest, err := digest.Parse(strings.TrimPrefix(uri[strings.LastIndex(uri, "/"):], "/"))
if err != nil {
log.Error().Err(err).Str("digest", c.QueryParam("digest")).Msg("Parse digest failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeDigestInvalid)
}
blobService := h.blobServiceFactory.New()
blobObj, err := blobService.FindByDigest(ctx, dgest.String())
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Find blob failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeUnknown)
}
log.Error().Err(err).Str("digest", dgest.String()).Msg("Parse content length failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeBlobUnknown)
}
result, err := blobService.FindAssociateWithArtifact(ctx, []int64{blobObj.ID})
if err != nil {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Find associate with artifact failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeUnknown)
}
if len(result) > 0 {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Blob associate with artifact")
return xerrors.NewDSError(c, xerrors.DSErrCodeBlobAssociated)
}
err = blobService.DeleteByID(ctx, blobObj.ID)
if err != nil {
log.Error().Err(err).Str("digest", dgest.String()).Msg("Delete blob failed")
return xerrors.NewDSError(c, xerrors.DSErrCodeUnknown)
}
return c.NoContent(http.StatusAccepted)
}