-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package crproxy | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/textproto" | ||
"path" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/distribution/distribution/v3/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) { | ||
cli := c.getClientset(info.Host, info.Image) | ||
resp, err := c.doWithAuth(cli, r, info.Host) | ||
if err != nil { | ||
if c.cachedManifest(rw, r, info) { | ||
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: | ||
errcode.ServeJSON(rw, errcode.ErrorCodeDenied) | ||
return | ||
} | ||
|
||
if resp.StatusCode >= http.StatusInternalServerError { | ||
if c.cachedManifest(rw, r, info) { | ||
return | ||
} | ||
} | ||
|
||
resp.Header.Del("Docker-Ratelimit-Source") | ||
|
||
header := rw.Header() | ||
for k, v := range resp.Header { | ||
key := textproto.CanonicalMIMEHeaderKey(k) | ||
header[key] = v | ||
} | ||
|
||
if r.Method == http.MethodHead { | ||
rw.WriteHeader(resp.StatusCode) | ||
return | ||
} | ||
|
||
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.WriteHeader(resp.StatusCode) | ||
rw.Write(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, %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 | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *CRProxy) cachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo) bool { | ||
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) | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters