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

feat: take v1.Image as input #21

Merged
merged 2 commits into from
Jul 30, 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
35 changes: 31 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
module github.com/aquasecurity/testdocker

go 1.14
go 1.21

require (
github.com/containerd/containerd v1.6.26 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v26.1.3+incompatible
github.com/docker/go-connections v0.4.0
github.com/golang-jwt/jwt/v4 v4.0.0
github.com/google/go-containerregistry v0.19.1
github.com/gorilla/mux v1.7.4
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
google.golang.org/grpc v1.58.3
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/containerd/containerd v1.6.26 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/vbatts/tar-split v0.11.3 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2,596 changes: 3 additions & 2,593 deletions go.sum

Large diffs are not rendered by default.

41 changes: 11 additions & 30 deletions registry/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ import (
"net/http"
"strings"

"github.com/docker/docker/api/server/router"
"github.com/docker/docker/errdefs"
"github.com/google/go-containerregistry/pkg/v1"
"golang.org/x/xerrors"

"github.com/docker/docker/api/server/router"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"

"github.com/aquasecurity/testdocker/tarfile"
)

// registryRouter is a router to talk with the image controller
type registryRouter struct {
routes []router.Route
images map[string]string
images map[string]v1.Image
}

// NewRouter initializes a new image router
func NewRouter(images map[string]string) router.Router {
func NewRouter(images map[string]v1.Image) router.Router {
r := &registryRouter{
images: images,
}
Expand Down Expand Up @@ -59,20 +55,14 @@ func (s *registryRouter) pingHandler(ctx context.Context, w http.ResponseWriter,

func (s *registryRouter) manifestHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
imageName := fmt.Sprintf("v%s/%s:%s", vars["version"], vars["name"], vars["reference"])
filePath, ok := s.images[imageName]
if strings.HasPrefix(vars["reference"], "sha256:") {
imageName = fmt.Sprintf("v%s/%s@%s", vars["version"], vars["name"], vars["reference"])
}
img, ok := s.images[imageName]
if !ok {
return errdefs.NotFound(xerrors.Errorf("unknown image: %s", imageName))
}

opener := func() (io.ReadCloser, error) {
return tarfile.Open(filePath)
}

img, err := tarball.Image(opener, nil)
if err != nil {
return errdefs.NotFound(xerrors.Errorf("unknown image: %s", filePath))
}

m, err := img.Manifest()
if err != nil {
return errdefs.Unavailable(err)
Expand All @@ -94,20 +84,11 @@ func (s *registryRouter) manifestHandler(ctx context.Context, w http.ResponseWri

func (s *registryRouter) blobHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
imageName := fmt.Sprintf("v%s/%s", vars["version"], vars["name"])
for image, filePath := range s.images {
if !strings.HasPrefix(image, imageName) {
for name, img := range s.images {
if !strings.HasPrefix(name, imageName) {
continue
}

opener := func() (io.ReadCloser, error) {
return tarfile.Open(filePath)
}

img, err := tarball.Image(opener, nil)
if err != nil {
return errdefs.NotFound(xerrors.Errorf("unknown image: %s", filePath))
}

h, err := v1.NewHash(vars["digest"])
if err != nil {
return errdefs.InvalidParameter(err)
Expand All @@ -132,7 +113,7 @@ func (s *registryRouter) blobHandler(ctx context.Context, w http.ResponseWriter,
// return the layer content
l, err := img.LayerByDigest(h)
if err != nil {
return errdefs.Unavailable(err)
continue // Not found, try the next image
}

rc, err := l.Compressed()
Expand Down
6 changes: 4 additions & 2 deletions registry/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package registry
import (
"net/http/httptest"

"github.com/docker/docker/api/server/router"
v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/aquasecurity/testdocker/auth"
"github.com/aquasecurity/testdocker/server"
"github.com/docker/docker/api/server/router"
)

type Option struct {
Images map[string]string
Images map[string]v1.Image
Auth auth.Auth
}

Expand Down
68 changes: 19 additions & 49 deletions registry/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package registry

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"os"
"regexp"
"testing"
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/aquasecurity/testdocker/auth"
"github.com/aquasecurity/testdocker/tarfile"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -103,8 +107,8 @@ func TestNewDockerRegistry_manifestHandler(t *testing.T) {
name: "happy path, alpine",
urlPath: "/v2/alpine/manifests/ref123",
option: Option{
Images: map[string]string{
"v2/alpine:ref123": "testdata/alpine/alpine.tar",
Images: map[string]v1.Image{
"v2/alpine:ref123": mustImageFromPath(t, "testdata/alpine/alpine.tar"),
},
},
expectedStatusCode: http.StatusOK,
Expand All @@ -115,26 +119,6 @@ func TestNewDockerRegistry_manifestHandler(t *testing.T) {
urlPath: "/v2/bogusimage/manifests/ref123",
expectedStatusCode: http.StatusNotFound,
},
{
name: "sad path, image exists but tar not found",
urlPath: "/v2/notarfile/manifests/ref123",
option: Option{
Images: map[string]string{
"v2/notarfile:ref123": "doesntexist.tar",
},
},
expectedStatusCode: http.StatusNotFound,
},
{
name: "sad path, image exists but tar is corrupt",
urlPath: "/v2/corrupt/manifests/ref123",
option: Option{
Images: map[string]string{
"v2/corrupt:ref123": "testdata/corrupt.tar",
},
},
expectedStatusCode: http.StatusNotFound,
},
}

for _, tc := range testCases {
Expand All @@ -153,7 +137,7 @@ func TestNewDockerRegistry_manifestHandler(t *testing.T) {
if resp.StatusCode != http.StatusOK {
return
}
respBody, _ := ioutil.ReadAll(resp.Body)
respBody, _ := io.ReadAll(resp.Body)
assert.Equal(t, tc.expectedResponseBody, string(respBody), tc.name)
})
}
Expand All @@ -173,8 +157,8 @@ func TestNewDockerRegistry_blobHandler(t *testing.T) {
name: "happy path, blob returns binary data",
urlPath: "/v2/alpine/blobs/sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130",
option: Option{
Images: map[string]string{
"v2/alpine:ref123": "testdata/alpine/alpine.tar",
Images: map[string]v1.Image{
"v2/alpine:ref123": mustImageFromPath(t, "testdata/alpine/alpine.tar"),
},
},
expectedStatusCode: http.StatusOK,
Expand All @@ -187,26 +171,6 @@ func TestNewDockerRegistry_blobHandler(t *testing.T) {
urlPath: "/v2/alpine/blobs/invalidreference",
expectedStatusCode: http.StatusNotFound,
},
{
name: "sad path, image entry and layers entry exists but no image file",
urlPath: "/v2/alpine/blobs/71dba1fabbde4baabcdebcde4895d3f3887e388b09cef162f8159cf7daffa1b8",
option: Option{
Images: map[string]string{
"v2/alpine:ref123": "doesnt/exist/image/path",
},
},
expectedStatusCode: http.StatusNotFound,
},
{
name: "sad path, image entry and layers entry exists but corrupt image file",
urlPath: "/v2/alpine/blobs/71dba1fabbde4baabcdebcde4895d3f3887e388b09cef162f8159cf7daffa1b8",
option: Option{
Images: map[string]string{
"v2/alpine:ref123": "testdata/corrupt.tar",
},
},
expectedStatusCode: http.StatusNotFound,
},
}

for _, tc := range testCases {
Expand All @@ -221,10 +185,10 @@ func TestNewDockerRegistry_blobHandler(t *testing.T) {
return
}

expectedContent, err := ioutil.ReadFile(tc.expectedResponseFile)
expectedContent, err := os.ReadFile(tc.expectedResponseFile)
require.NoError(t, err)

actualResp, _ := ioutil.ReadAll(resp.Body)
actualResp, _ := io.ReadAll(resp.Body)
assert.Equal(t, expectedContent, actualResp, tc.name)

assert.Equal(t, tc.expectedContentType, resp.Header.Get("Content-Type"), tc.name)
Expand Down Expand Up @@ -317,7 +281,7 @@ func TestNewDockerRegistry_tokenHandler(t *testing.T) {
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedStatusCode, resp.StatusCode, tc.name)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err, tc.name)

var got auth.TokenResponse
Expand All @@ -338,3 +302,9 @@ func TestNewDockerRegistry_tokenHandler(t *testing.T) {
})
}
}

func mustImageFromPath(t *testing.T, filePath string) v1.Image {
img, err := tarfile.ImageFromPath(filePath)
require.NoError(t, err)
return img
}
Empty file removed registry/testdata/corrupt.tar
Empty file.
17 changes: 17 additions & 0 deletions tarfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,25 @@ import (
"io"
"os"
"strings"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"golang.org/x/xerrors"
)

func ImageFromPath(filePath string) (v1.Image, error) {
opener := func() (io.ReadCloser, error) {
return Open(filePath)
}

img, err := tarball.Image(opener, nil)
if err != nil {
return nil, xerrors.Errorf("unknown image: %s", filePath)
}

return img, nil
}

func Open(filePath string) (io.ReadCloser, error) {
f, err := os.Open(filePath)
if err != nil {
Expand Down
Loading