-
Notifications
You must be signed in to change notification settings - Fork 98
/
crproxy_manifest.go
205 lines (179 loc) · 5.2 KB
/
crproxy_manifest.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package crproxy
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/textproto"
"path"
"strconv"
"strings"
"time"
"github.com/docker/distribution/registry/api/errcode"
)
func manifestRevisionsCachePath(host, image, tagOrBlob string) string {
return path.Join("/docker/registry/v2/repositories", host, image, "_manifests/revisions/sha256", tagOrBlob, "link")
}
func manifestTagCachePath(host, image, tagOrBlob string) string {
return path.Join("/docker/registry/v2/repositories", host, image, "_manifests/tags", tagOrBlob, "current/link")
}
func (c *CRProxy) cacheManifestResponse(rw http.ResponseWriter, r *http.Request, info *PathInfo) {
if c.cachedManifest(rw, r, info, true) {
return
}
cli := c.getClientset(info.Host, info.Image)
resp, err := c.doWithAuth(cli, r, info.Host)
if err != nil {
if c.cachedManifest(rw, r, info, false) {
return
}
if c.logger != nil {
c.logger.Println("failed to request", info.Host, info.Image, err)
}
errcode.ServeJSON(rw, errcode.ErrorCodeUnknown)
return
}
defer func() {
resp.Body.Close()
}()
switch resp.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
if c.cachedManifest(rw, r, info, false) {
if c.logger != nil {
c.logger.Println("origin manifest response 40x, but hit caches", info.Host, info.Image, err, dumpResponse(resp))
}
return
}
if c.logger != nil {
c.logger.Println("origin manifest response 40x", info.Host, info.Image, err, dumpResponse(resp))
}
errcode.ServeJSON(rw, errcode.ErrorCodeDenied)
return
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
if c.cachedManifest(rw, r, info, false) {
if c.logger != nil {
c.logger.Println("origin manifest response 5xx, but hit caches", info.Host, info.Image, err, dumpResponse(resp))
}
return
}
if c.logger != nil {
c.logger.Println("origin manifest response 5xx", info.Host, info.Image, err, dumpResponse(resp))
}
}
resp.Header.Del("Docker-Ratelimit-Source")
header := rw.Header()
for k, v := range resp.Header {
key := textproto.CanonicalMIMEHeaderKey(k)
header[key] = v
}
rw.WriteHeader(resp.StatusCode)
if r.Method == http.MethodHead {
return
}
if resp.StatusCode >= http.StatusOK || resp.StatusCode < http.StatusMultipleChoices {
body, err := io.ReadAll(resp.Body)
if err != nil {
c.errorResponse(rw, r, err)
return
}
err = c.cacheManifestContent(context.Background(), info, body)
if err != nil {
c.errorResponse(rw, r, err)
return
}
rw.Write(body)
} else {
io.Copy(rw, resp.Body)
}
}
func (c *CRProxy) cacheManifestContent(ctx context.Context, info *PathInfo, content []byte) error {
h := sha256.New()
h.Write(content)
hash := hex.EncodeToString(h.Sum(nil)[:])
if strings.HasPrefix(info.Manifests, "sha256:") {
if info.Manifests[7:] != hash {
return fmt.Errorf("expected hash %s is not same to %s", info.Manifests[7:], hash)
}
} else {
manifestLinkPath := manifestTagCachePath(info.Host, info.Image, info.Manifests)
err := c.storageDriver.PutContent(ctx, manifestLinkPath, []byte("sha256:"+hash))
if err != nil {
return err
}
}
manifestLinkPath := manifestRevisionsCachePath(info.Host, info.Image, hash)
err := c.storageDriver.PutContent(ctx, manifestLinkPath, []byte("sha256:"+hash))
if err != nil {
return err
}
blobCachePath := blobCachePath(hash)
err = c.storageDriver.PutContent(ctx, blobCachePath, content)
if err != nil {
return err
}
if c.manifestCacheDuration > 0 {
c.manifestCache.Store(manifestLinkPath, time.Now())
}
return nil
}
func (c *CRProxy) cachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo, try bool) bool {
if try && c.manifestCacheDuration == 0 {
return false
}
ctx := r.Context()
var manifestLinkPath string
if strings.HasPrefix(info.Manifests, "sha256:") {
manifestLinkPath = manifestRevisionsCachePath(info.Host, info.Image, info.Manifests[7:])
} else {
manifestLinkPath = manifestTagCachePath(info.Host, info.Image, info.Manifests)
}
if try {
last, ok := c.manifestCache.Load(manifestLinkPath)
if !ok {
return false
}
if time.Since(last) > c.manifestCacheDuration {
return false
}
}
content, err := c.storageDriver.GetContent(ctx, manifestLinkPath)
if err == nil {
digest := string(content)
blobCachePath := blobCachePath(digest)
content, err := c.storageDriver.GetContent(ctx, blobCachePath)
if err == nil {
mt := struct {
MediaType string `json:"mediaType"`
}{}
err := json.Unmarshal(content, &mt)
if err != nil {
if c.logger != nil {
c.logger.Println("Manifest blob cache err", blobCachePath, err)
}
return false
}
if c.logger != nil {
c.logger.Println("Manifest blob cache hit", blobCachePath)
}
rw.Header().Set("docker-content-digest", digest)
rw.Header().Set("Content-Type", mt.MediaType)
rw.Header().Set("Content-Length", strconv.FormatInt(int64(len(content)), 10))
if r.Method != http.MethodHead {
rw.Write(content)
}
return true
}
if c.logger != nil {
c.logger.Println("Manifest blob cache missed", blobCachePath, err)
}
} else {
if c.logger != nil {
c.logger.Println("Manifest cache missed", manifestLinkPath, err)
}
}
return false
}