-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
artifact_info.go
138 lines (130 loc) · 3.87 KB
/
artifact_info.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright Project Harbor Authors
//
// 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 artifactinfo
import (
"fmt"
"github.com/docker/distribution/reference"
lib_http "github.com/goharbor/harbor/src/lib/http"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/opencontainers/go-digest"
)
const (
blobMountQuery = "mount"
blobFromQuery = "from"
blobMountDigest = "blob_mount_digest"
blobMountRepo = "blob_mount_repo"
tag = "tag"
)
var (
urlPatterns = map[string]*regexp.Regexp{
"manifest": lib.V2ManifestURLRe,
"tag_list": lib.V2TagListURLRe,
"blob_upload": lib.V2BlobUploadURLRe,
"blob": lib.V2BlobURLRe,
}
)
// Middleware gets the information of artifact via url of the request and inject it into the context
func Middleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
log.Debugf("In artifact info middleware, url: %s", req.URL.String())
m, ok, err := parse(req.URL)
if err != nil {
lib_http.SendError(rw, err)
return
}
if !ok {
next.ServeHTTP(rw, req)
return
}
repo := m[lib.RepositorySubexp]
pn, err := projectNameFromRepo(repo)
if err != nil {
lib_http.SendError(rw, errors.BadRequestError(err))
return
}
art := lib.ArtifactInfo{
Repository: repo,
ProjectName: pn,
}
if d, ok := m[lib.DigestSubexp]; ok {
art.Digest = d
}
if ref, ok := m[lib.ReferenceSubexp]; ok {
art.Reference = ref
}
if t, ok := m[tag]; ok {
art.Tag = t
}
if bmr, ok := m[blobMountRepo]; ok {
// Fail early for now, though in docker registry an invalid may return 202
// it's not clear in OCI spec how to handle invalid from parm
bmp, err := projectNameFromRepo(bmr)
if err != nil {
lib_http.SendError(rw, errors.BadRequestError(err))
return
}
art.BlobMountDigest = m[blobMountDigest]
art.BlobMountProjectName = bmp
art.BlobMountRepository = bmr
}
ctx := lib.WithArtifactInfo(req.Context(), art)
next.ServeHTTP(rw, req.WithContext(ctx))
})
}
}
func projectNameFromRepo(repo string) (string, error) {
components := strings.SplitN(repo, "/", 2)
if len(components) < 2 {
return "", fmt.Errorf("invalid repository name: %s", repo)
}
return components[0], nil
}
func parse(url *url.URL) (map[string]string, bool, error) {
path := url.Path
query := url.Query()
m := make(map[string]string)
match := false
for key, re := range urlPatterns {
l := re.FindStringSubmatch(path)
if len(l) > 0 {
match = true
for i := 1; i < len(l); i++ {
m[re.SubexpNames()[i]] = l[i]
}
if key == "blob_upload" && len(query.Get(blobFromQuery)) > 0 {
m[blobMountDigest] = query.Get(blobMountQuery)
m[blobMountRepo] = query.Get(blobFromQuery)
}
break
}
}
// parse reference, for invalid reference format, just give 404.
if m[lib.ReferenceSubexp] != "" {
if digest.DigestRegexp.MatchString(m[lib.ReferenceSubexp]) {
m[lib.DigestSubexp] = m[lib.ReferenceSubexp]
} else if reference.TagRegexp.MatchString(m[lib.ReferenceSubexp]) {
m[tag] = m[lib.ReferenceSubexp]
} else {
return m, match, errors.New("invalid reference format").WithCode(errors.NotFoundCode)
}
}
return m, match, nil
}