Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor distribution ref to simplify registry routing #477

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- [#475](https://github.com/XenitAB/spegel/pull/475) Move resolving ref to digest to manifest handler.
- [#477](https://github.com/XenitAB/spegel/pull/477) Refactor distribution ref to simplify registry routing.

### Deprecated

Expand Down
47 changes: 37 additions & 10 deletions pkg/registry/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ package registry
import (
"fmt"
"regexp"
"strings"

"github.com/opencontainers/go-digest"
)

type referenceType string
type referenceKind string

const (
referenceTypeManifest = "Manifest"
referenceTypeBlob = "Blob"
referenceKindManifest = "Manifest"
referenceKindBlob = "Blob"
)

type reference struct {
kind referenceKind
name string
dgst digest.Digest
}

func (r reference) hasLatestTag() bool {
if r.name == "" {
return false
}
_, tag, _ := strings.Cut(r.name, ":")
return tag == "latest"
}

// Package is used to parse components from requests which comform with the OCI distribution spec.
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md
// /v2/<name>/manifests/<reference>
Expand All @@ -27,22 +42,34 @@ var (
blobsRegexDigest = regexp.MustCompile(`/v2/` + nameRegex.String() + `/blobs/(.*)`)
)

func parsePathComponents(registry, path string) (string, digest.Digest, referenceType, error) {
func parsePathComponents(registry, path string) (reference, error) {
comps := manifestRegexTag.FindStringSubmatch(path)
if len(comps) == 6 {
if registry == "" {
return "", "", "", fmt.Errorf("registry parameter needs to be set for tag references")
return reference{}, fmt.Errorf("registry parameter needs to be set for tag references")
}
name := fmt.Sprintf("%s/%s:%s", registry, comps[1], comps[5])
ref := reference{
kind: referenceKindManifest,
name: name,
}
ref := fmt.Sprintf("%s/%s:%s", registry, comps[1], comps[5])
return ref, "", referenceTypeManifest, nil
return ref, nil
}
comps = manifestRegexDigest.FindStringSubmatch(path)
if len(comps) == 6 {
return "", digest.Digest(comps[5]), referenceTypeManifest, nil
ref := reference{
kind: referenceKindManifest,
dgst: digest.Digest(comps[5]),
}
return ref, nil
}
comps = blobsRegexDigest.FindStringSubmatch(path)
if len(comps) == 6 {
return "", digest.Digest(comps[5]), referenceTypeBlob, nil
ref := reference{
kind: referenceKindBlob,
dgst: digest.Digest(comps[5]),
}
return ref, nil
}
return "", "", "", fmt.Errorf("distribution path could not be parsed")
return reference{}, fmt.Errorf("distribution path could not be parsed")
}
24 changes: 12 additions & 12 deletions pkg/registry/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,44 @@ func TestParsePathComponents(t *testing.T) {
name string
registry string
path string
expectedRef string
expectedName string
expectedDgst digest.Digest
expectedRefType referenceType
expectedRefKind referenceKind
}{
{
name: "valid manifest tag",
registry: "example.com",
path: "/v2/foo/bar/manifests/hello-world",
expectedRef: "example.com/foo/bar:hello-world",
expectedName: "example.com/foo/bar:hello-world",
expectedDgst: "",
expectedRefType: referenceTypeManifest,
expectedRefKind: referenceKindManifest,
},
{
name: "valid blob digest",
registry: "docker.io",
path: "/v2/library/nginx/blobs/sha256:295c7be079025306c4f1d65997fcf7adb411c88f139ad1d34b537164aa060369",
expectedRef: "",
expectedName: "",
expectedDgst: digest.Digest("sha256:295c7be079025306c4f1d65997fcf7adb411c88f139ad1d34b537164aa060369"),
expectedRefType: referenceTypeBlob,
expectedRefKind: referenceKindBlob,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ref, dgst, refType, err := parsePathComponents(tt.registry, tt.path)
ref, err := parsePathComponents(tt.registry, tt.path)
require.NoError(t, err)
require.Equal(t, tt.expectedRef, ref)
require.Equal(t, tt.expectedDgst, dgst)
require.Equal(t, tt.expectedRefType, refType)
require.Equal(t, tt.expectedName, ref.name)
require.Equal(t, tt.expectedDgst, ref.dgst)
require.Equal(t, tt.expectedRefKind, ref.kind)
})
}
}

func TestParsePathComponentsInvalidPath(t *testing.T) {
_, _, _, err := parsePathComponents("example.com", "/v2/spegel-org/spegel/v0.0.1")
_, err := parsePathComponents("example.com", "/v2/spegel-org/spegel/v0.0.1")
require.EqualError(t, err, "distribution path could not be parsed")
}

func TestParsePathComponentsMissingRegistry(t *testing.T) {
_, _, _, err := parsePathComponents("", "/v2/spegel-org/spegel/manifests/v0.0.1")
_, err := parsePathComponents("", "/v2/spegel-org/spegel/manifests/v0.0.1")
require.EqualError(t, err, "registry parameter needs to be set for tag references")
}
63 changes: 32 additions & 31 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/opencontainers/go-digest"

"github.com/spegel-org/spegel/internal/mux"
"github.com/spegel-org/spegel/pkg/metrics"
Expand Down Expand Up @@ -172,30 +171,17 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) str

// Parse out path components from request.
registryName := req.URL.Query().Get("ns")
ref, dgst, refType, err := parsePathComponents(registryName, req.URL.Path)
ref, err := parsePathComponents(registryName, req.URL.Path)
if err != nil {
rw.WriteError(http.StatusNotFound, err)
return ""
}

// Check if latest tag should be resolved
if !r.resolveLatestTag && ref != "" {
_, tag, _ := strings.Cut(ref, ":")
if tag == "latest" {
rw.WriteHeader(http.StatusNotFound)
return ""
}
}

// Request with mirror header are proxied.
if req.Header.Get(MirroredHeaderKey) != "true" {
// Set mirrored header in request to stop infinite loops
req.Header.Set(MirroredHeaderKey, "true")
key := dgst.String()
if key == "" {
key = ref
}
r.handleMirror(rw, req, key)
r.handleMirror(rw, req, ref)
sourceType := "internal"
if r.isExternalRequest(req) {
sourceType = "external"
Expand All @@ -209,12 +195,12 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) str
}

// Serve registry endpoints.
switch refType {
case referenceTypeManifest:
r.handleManifest(rw, req, ref, dgst)
switch ref.kind {
case referenceKindManifest:
r.handleManifest(rw, req, ref)
return "manifest"
case referenceTypeBlob:
r.handleBlob(rw, req, dgst)
case referenceKindBlob:
r.handleBlob(rw, req, ref)
return "blob"
}

Expand All @@ -223,7 +209,18 @@ func (r *Registry) registryHandler(rw mux.ResponseWriter, req *http.Request) str
return ""
}

func (r *Registry) handleMirror(rw mux.ResponseWriter, req *http.Request, key string) {
func (r *Registry) handleMirror(rw mux.ResponseWriter, req *http.Request, ref reference) {
if !r.resolveLatestTag && ref.hasLatestTag() {
r.log.V(4).Info("skipping mirror request for image with latest tag", "image", ref.name)
rw.WriteHeader(http.StatusNotFound)
return
}

key := ref.dgst.String()
if key == "" {
key = ref.name
}

log := r.log.WithValues("key", key, "path", req.URL.Path, "ip", getClientIP(req))

// Resolve mirror with the requested key
Expand Down Expand Up @@ -289,23 +286,23 @@ func (r *Registry) handleMirror(rw mux.ResponseWriter, req *http.Request, key st
}
}

func (r *Registry) handleManifest(rw mux.ResponseWriter, req *http.Request, ref string, dgst digest.Digest) {
if dgst == "" {
func (r *Registry) handleManifest(rw mux.ResponseWriter, req *http.Request, ref reference) {
if ref.dgst == "" {
var err error
dgst, err = r.ociClient.Resolve(req.Context(), ref)
ref.dgst, err = r.ociClient.Resolve(req.Context(), ref.name)
if err != nil {
rw.WriteError(http.StatusNotFound, err)
return
}
}
b, mediaType, err := r.ociClient.GetManifest(req.Context(), dgst)
b, mediaType, err := r.ociClient.GetManifest(req.Context(), ref.dgst)
if err != nil {
rw.WriteError(http.StatusNotFound, err)
return
}
rw.Header().Set("Content-Type", mediaType)
rw.Header().Set("Content-Length", strconv.FormatInt(int64(len(b)), 10))
rw.Header().Set("Docker-Content-Digest", dgst.String())
rw.Header().Set("Docker-Content-Digest", ref.dgst.String())
if req.Method == http.MethodHead {
return
}
Expand All @@ -316,22 +313,26 @@ func (r *Registry) handleManifest(rw mux.ResponseWriter, req *http.Request, ref
}
}

func (r *Registry) handleBlob(rw mux.ResponseWriter, req *http.Request, dgst digest.Digest) {
size, err := r.ociClient.Size(req.Context(), dgst)
func (r *Registry) handleBlob(rw mux.ResponseWriter, req *http.Request, ref reference) {
if ref.dgst == "" {
rw.WriteHeader(http.StatusNotFound)
return
}
size, err := r.ociClient.Size(req.Context(), ref.dgst)
if err != nil {
rw.WriteError(http.StatusInternalServerError, err)
return
}
rw.Header().Set("Content-Length", strconv.FormatInt(size, 10))
rw.Header().Set("Docker-Content-Digest", dgst.String())
rw.Header().Set("Docker-Content-Digest", ref.dgst.String())
if req.Method == http.MethodHead {
return
}
var w io.Writer = rw
if r.throttler != nil {
w = r.throttler.Writer(rw)
}
rc, err := r.ociClient.GetBlob(req.Context(), dgst)
rc, err := r.ociClient.GetBlob(req.Context(), ref.dgst)
if err != nil {
rw.WriteError(http.StatusInternalServerError, err)
return
Expand Down