add regression tests for issues resolved by the stereoscope port (#740)
* add regression test for the reported tar-slip payload in extract selects /etc/motd in a layer that also has etc/motd/../../../victim/.bashrc, the name from the report, and checks nothing lands outside the cwd while the real file is still extracted. Refs #722 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * cover extract from a docker 25+ OCI layout save runs the extract test against test-oci-docker-image (the blobs/sha256 layout docker 25+ writes) as well as the legacy save, so ctrl+e on those images actually writes the selected file. Refs #687 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for loading through the docker desktop context Fake docker daemon on ~/.docker/run/docker.sock behind a current desktop-linux context, the way Docker Desktop sets it up, and the image loads from it without a pull. Refs #542, #453 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for pulling through a rootless docker context Fake daemon on a rootless socket behind a current rootless context, missing the image until pulled. The pull and the save both land on that daemon, with no docker cli on PATH. Refs #418 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for docker API version negotiation Fake docker 29 daemon that only takes API 1.44 to 1.52 and answers outside that with the daemon's own errors. A load through it works and every call uses the negotiated 1.52. Refs #664 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for DOCKER_TLS_VERIFY with certs in the docker config dir DOCKER_HOST=tcp:// plus DOCKER_TLS_VERIFY=1, no DOCKER_CERT_PATH, ca/cert/key in the config dir, against a fake daemon that requires a client cert. The load goes through, and fails once the client cert is gone. Refs #545 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for the podman source on macOS The podman source on darwin finds the machine API socket through a faked `podman machine inspect` and loads the image from a fake podman API there, with nothing else pointing at a socket. Refs #432 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add oci-dir layout load test writes an OCI image layout dir to disk (what skopeo oci:<dir> or crane produce) and loads it through oci-dir: and --source oci-dir. Refs #423 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add registry source load test pushes an image to an in-process go-containerregistry registry on loopback and loads it via registry:// and --source registry, no daemon involved. Refs #406, #130 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add multi-arch platform selection tests builds a linux/amd64 + linux/arm64 OCI index (with a buildx style attestation entry) and checks --platform loads the matching image from an oci-dir and a registry, and fails for a platform the index lacks. Also runs the CLI with --platform end to end. Refs #442 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add container-engine config file test runs dive on a scheme-less archive path with only container-engine set in the config file, with a docker control that has to fail. v0.13.1 read the --source flag's viper key and ignored container-engine. Refs #643 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for loading a local image from the docker daemon a fake docker daemon that already holds the image serves inspect and save, the test checks the image is read from it and never pulled. Refs #359 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for podman pulling a missing image a fake podman API (via CONTAINER_HOST) 404s the image until it is pulled, the test checks dive pulls it through the API and loads it, for podman:// and --source podman. Refs #310 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for containerd image store saves - builds what `docker save` writes for an image in the containerd store: gzip layers (one under 1KB), index.json pointing at the multi-platform index with the other platform's blobs missing, and a buildkit attestation manifest - loads it as a docker-archive and through a fake docker daemon (`dive test`), and checks the export matches the same image saved as a plain single manifest archive Refs #718, #452 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for kaniko image archives writes the image with go-containerregistry's tarball writer (what kaniko's `--tarPath` uses, config named `sha256:<hex>` and `<hex>.tar.gz` layers) and checks `docker-archive://` reads it the same as a `docker save` of that image. Refs #318 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add regression test for whiteouts of missing paths in the TUI layers whiteout paths no lower layer has (Windows images) and repeat a whiteout whose target is already gone (kaniko and apt's auxfiles), plus a char device. The file tree view model builds every layer selection without error and marks the real removal. Refs #306 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * tidy go.sum for the in-process registry test go-containerregistry's pkg/registry pulls golang.org/x/mod and x/tools sums into the test graph. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
parent
e97febbeb6
commit
06f9a64c72
11 changed files with 1015 additions and 13 deletions
|
|
@ -1,13 +1,30 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/tarball"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/wagoodman/dive/cmd/dive/cli/internal/command/export"
|
||||
"github.com/wagoodman/dive/internal/imagefixture"
|
||||
)
|
||||
|
||||
|
|
@ -72,3 +89,233 @@ func Test_Archive_CI(t *testing.T) {
|
|||
assert.Contains(t, combined, "Loading image")
|
||||
})
|
||||
}
|
||||
|
||||
// containerdStoreSave writes spec (an OCISave) the way `docker save` lays out an image pulled into the containerd
|
||||
// image store: index.json points at the image's multi-platform index instead of a single manifest, the other
|
||||
// platform's manifest has no blobs in the archive, and buildkit's attestation manifest (unknown/unknown platform,
|
||||
// in-toto layer) sits next to the image. The host platform is the one that was saved, as docker does.
|
||||
func containerdStoreSave(t *testing.T, spec imagefixture.Spec) string {
|
||||
t.Helper()
|
||||
require.Equal(t, imagefixture.OCISave, spec.Format)
|
||||
|
||||
var buf bytes.Buffer
|
||||
require.NoError(t, spec.Write(&buf))
|
||||
files := map[string][]byte{}
|
||||
dirs := map[string]bool{}
|
||||
tr := tar.NewReader(&buf)
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
require.NoError(t, err)
|
||||
if hdr.Typeflag == tar.TypeDir {
|
||||
dirs[hdr.Name] = true
|
||||
continue
|
||||
}
|
||||
files[hdr.Name], err = io.ReadAll(tr)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
type platform struct {
|
||||
Architecture string `json:"architecture"`
|
||||
OS string `json:"os"`
|
||||
}
|
||||
type descriptor struct {
|
||||
MediaType string `json:"mediaType"`
|
||||
Digest string `json:"digest"`
|
||||
Size int `json:"size"`
|
||||
Platform *platform `json:"platform,omitempty"`
|
||||
Annotations map[string]string `json:"annotations,omitempty"`
|
||||
}
|
||||
addBlob := func(v any) descriptor {
|
||||
data, ok := v.([]byte)
|
||||
if !ok {
|
||||
var err error
|
||||
data, err = json.Marshal(v)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
files["blobs/sha256/"+hex.EncodeToString(sum[:])] = data
|
||||
return descriptor{Digest: "sha256:" + hex.EncodeToString(sum[:]), Size: len(data)}
|
||||
}
|
||||
|
||||
var index struct {
|
||||
Manifests []descriptor `json:"manifests"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(files["index.json"], &index))
|
||||
require.Len(t, index.Manifests, 1)
|
||||
image := index.Manifests[0]
|
||||
image.Annotations = nil
|
||||
image.Platform = &platform{Architecture: runtime.GOARCH, OS: "linux"}
|
||||
|
||||
other := "arm64"
|
||||
if runtime.GOARCH == "arm64" {
|
||||
other = "amd64"
|
||||
}
|
||||
missing := descriptor{
|
||||
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
||||
Digest: "sha256:" + strings.Repeat("0", 64),
|
||||
Size: 1234,
|
||||
Platform: &platform{Architecture: other, OS: "linux"},
|
||||
}
|
||||
|
||||
statement := addBlob(map[string]any{"_type": "https://in-toto.io/Statement/v0.1", "predicateType": "https://slsa.dev/provenance/v0.2", "subject": []any{}, "predicate": map[string]any{}})
|
||||
statement.MediaType = "application/vnd.in-toto+json"
|
||||
statement.Annotations = map[string]string{"in-toto.io/predicate-type": "https://slsa.dev/provenance/v0.2"}
|
||||
attConfig := addBlob(map[string]any{"architecture": "unknown", "os": "unknown", "config": map[string]any{}, "rootfs": map[string]any{"type": "layers", "diff_ids": []string{statement.Digest}}})
|
||||
attConfig.MediaType = "application/vnd.oci.image.config.v1+json"
|
||||
attestation := addBlob(map[string]any{"schemaVersion": 2, "mediaType": "application/vnd.oci.image.manifest.v1+json", "config": attConfig, "layers": []descriptor{statement}})
|
||||
attestation.MediaType = "application/vnd.oci.image.manifest.v1+json"
|
||||
attestation.Platform = &platform{Architecture: "unknown", OS: "unknown"}
|
||||
attestation.Annotations = map[string]string{
|
||||
"vnd.docker.reference.digest": image.Digest,
|
||||
"vnd.docker.reference.type": "attestation-manifest",
|
||||
}
|
||||
|
||||
imageIndex := addBlob(map[string]any{
|
||||
"schemaVersion": 2,
|
||||
"mediaType": "application/vnd.oci.image.index.v1+json",
|
||||
"manifests": []descriptor{image, missing, attestation},
|
||||
})
|
||||
imageIndex.MediaType = "application/vnd.oci.image.index.v1+json"
|
||||
imageIndex.Annotations = map[string]string{
|
||||
"io.containerd.image.name": "docker.io/library/test:latest",
|
||||
"org.opencontainers.image.ref.name": "latest",
|
||||
}
|
||||
var err error
|
||||
files["index.json"], err = json.Marshal(map[string]any{
|
||||
"schemaVersion": 2,
|
||||
"mediaType": "application/vnd.oci.image.index.v1+json",
|
||||
"manifests": []descriptor{imageIndex},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
path := filepath.Join(t.TempDir(), "containerd-store-save.tar")
|
||||
f, err := os.Create(path)
|
||||
require.NoError(t, err)
|
||||
defer f.Close()
|
||||
tw := tar.NewWriter(f)
|
||||
for _, name := range slices.Sorted(maps.Keys(dirs)) {
|
||||
require.NoError(t, tw.WriteHeader(&tar.Header{Name: name, Typeflag: tar.TypeDir, Mode: 0o755}))
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(files)) {
|
||||
data := files[name]
|
||||
require.NoError(t, tw.WriteHeader(&tar.Header{Name: name, Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(data))}))
|
||||
_, err := tw.Write(data)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.NoError(t, tw.Close())
|
||||
return path
|
||||
}
|
||||
|
||||
// regression for #718 and #452: `docker save` output from docker 25+ and from the containerd image store failed to
|
||||
// load ("could not find image config", "could not find 'blobs/sha256/...' in parsed layers"). The save here has
|
||||
// gzip layers (one a tiny near empty layer), the image's multi-platform index with a missing platform, and an
|
||||
// attestation manifest. It is read as a docker-archive and through a docker daemon, as `dive test` does.
|
||||
func Test_Archive_ContainerdStoreSave(t *testing.T) {
|
||||
spec := imagefixture.Fixtures()["oci-save-gzip"]
|
||||
spec.Layers = append(spec.Layers, []imagefixture.Entry{{Name: "tmp/", Type: tar.TypeDir}})
|
||||
spec.History = append(spec.History, imagefixture.History{CreatedBy: "RUN mkdir -p /tmp # buildkit"})
|
||||
|
||||
// the same image as a plain single manifest save is the reference
|
||||
want := exportArchive(t, "docker-archive:"+writeSpec(t, spec))
|
||||
require.Len(t, want.Layer, 4)
|
||||
assert.Equal(t, "RUN mkdir -p /tmp # buildkit", want.Layer[3].Command)
|
||||
require.Len(t, want.Layer[3].FileList, 1)
|
||||
assert.Equal(t, "tmp", want.Layer[3].FileList[0].Path)
|
||||
|
||||
saved := containerdStoreSave(t, spec)
|
||||
|
||||
t.Run("docker-archive", func(t *testing.T) {
|
||||
assert.Equal(t, want, exportArchive(t, "docker-archive:"+saved))
|
||||
})
|
||||
|
||||
t.Run("docker daemon", func(t *testing.T) {
|
||||
saves := fakeDockerDaemon(t, saved)
|
||||
assert.Equal(t, want, exportArchive(t, "docker:test"))
|
||||
assert.Equal(t, int32(1), saves.Load())
|
||||
})
|
||||
}
|
||||
|
||||
// exportArchive runs dive with --json on the reference and returns the export.
|
||||
func exportArchive(t *testing.T, ref string) export.Export {
|
||||
t.Helper()
|
||||
file := filepath.Join(t.TempDir(), "output.json")
|
||||
rootCmd := getTestCommand(t, ref+" --json "+file)
|
||||
Capture().WithSuppress().Run(t, func() {
|
||||
require.NoError(t, rootCmd.Execute())
|
||||
})
|
||||
data, err := os.ReadFile(file)
|
||||
require.NoError(t, err)
|
||||
var got export.Export
|
||||
require.NoError(t, json.Unmarshal(data, &got))
|
||||
return got
|
||||
}
|
||||
|
||||
// fakeDockerDaemon serves the docker API calls dive makes for an image that is already local (ping, inspect, save)
|
||||
// from DOCKER_HOST, with the save returning the given tarball. Any other call (e.g. a pull) fails the test. It
|
||||
// returns the number of saves served.
|
||||
func fakeDockerDaemon(t *testing.T, tarball string) *atomic.Int32 {
|
||||
t.Helper()
|
||||
var saves atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Api-Version", "1.47")
|
||||
w.Header().Set("Ostype", "linux")
|
||||
switch {
|
||||
case strings.HasSuffix(r.URL.Path, "/_ping"):
|
||||
_, _ = io.WriteString(w, "OK")
|
||||
case strings.Contains(r.URL.Path, "/images/") && strings.HasSuffix(r.URL.Path, "/json"):
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"Id":"sha256:`+strings.Repeat("a", 64)+`","RepoTags":["test:latest"],"Os":"linux","Architecture":"`+runtime.GOARCH+`","Size":1}`)
|
||||
case strings.HasSuffix(r.URL.Path, "/images/get"):
|
||||
saves.Add(1)
|
||||
http.ServeFile(w, r, tarball)
|
||||
default:
|
||||
t.Errorf("unexpected docker API call: %s %s", r.Method, r.URL)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
t.Setenv("DOCKER_HOST", "tcp://"+srv.Listener.Addr().String())
|
||||
t.Setenv("DOCKER_CONTEXT", "")
|
||||
t.Setenv("DOCKER_TLS_VERIFY", "")
|
||||
t.Setenv("DOCKER_TLS", "")
|
||||
return &saves
|
||||
}
|
||||
|
||||
// regression for #318: kaniko's `--tarPath` archive (written by go-containerregistry's tarball writer) failed with
|
||||
// "could not find image config", since its config is named `sha256:<hex>` and its layers `<hex>.tar.gz`.
|
||||
func Test_Archive_KanikoTarPath(t *testing.T) {
|
||||
plain := writeSpec(t, imagefixture.Fixtures()["legacy-save"])
|
||||
want := exportArchive(t, "docker-archive:"+plain)
|
||||
|
||||
img, err := tarball.ImageFromPath(plain, nil)
|
||||
require.NoError(t, err)
|
||||
ref, err := name.ParseReference("image")
|
||||
require.NoError(t, err)
|
||||
kaniko := filepath.Join(t.TempDir(), "image.tar")
|
||||
require.NoError(t, tarball.MultiRefWriteToFile(kaniko, map[name.Reference]v1.Image{ref: img}))
|
||||
|
||||
// the archive has kaniko's naming, not docker's
|
||||
manifest, err := tarball.LoadManifest(func() (io.ReadCloser, error) { return os.Open(kaniko) })
|
||||
require.NoError(t, err)
|
||||
require.Len(t, manifest, 1)
|
||||
assert.True(t, strings.HasPrefix(manifest[0].Config, "sha256:"), manifest[0].Config)
|
||||
for _, l := range manifest[0].Layers {
|
||||
assert.True(t, strings.HasSuffix(l, ".tar.gz"), l)
|
||||
}
|
||||
|
||||
assert.Equal(t, want, exportArchive(t, "docker-archive://"+kaniko))
|
||||
}
|
||||
|
||||
// writeSpec writes the spec's image tarball to a temp file.
|
||||
func writeSpec(t *testing.T, spec imagefixture.Spec) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "image.tar")
|
||||
f, err := os.Create(path)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, spec.Write(f))
|
||||
require.NoError(t, f.Close())
|
||||
return path
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Config(t *testing.T) {
|
||||
|
|
@ -15,3 +19,31 @@ func Test_Config(t *testing.T) {
|
|||
|
||||
snaps.MatchSnapshot(t, all)
|
||||
}
|
||||
|
||||
// regression for #643: `container-engine` in the config file (not just --source) picks where a reference without a
|
||||
// scheme is loaded from
|
||||
func Test_Config_containerEngine(t *testing.T) {
|
||||
archive := repoPath(t, ".data/test-docker-image.tar")
|
||||
|
||||
run := func(t *testing.T, engine string) error {
|
||||
cfg := filepath.Join(t.TempDir(), "dive.yaml")
|
||||
require.NoError(t, os.WriteFile(cfg, []byte("container-engine: "+engine+"\n"), 0o600))
|
||||
t.Setenv("DIVE_CONFIG", cfg)
|
||||
|
||||
rootCmd := getTestCommand(t, archive+" --json "+filepath.Join(t.TempDir(), "out.json"))
|
||||
var err error
|
||||
Capture().WithStdout().WithStderr().Run(t, func() {
|
||||
err = rootCmd.Execute()
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
t.Run("docker-archive", func(t *testing.T) {
|
||||
require.NoError(t, run(t, "docker-archive"))
|
||||
})
|
||||
|
||||
// control: the same reference handed to the docker engine is not a loadable image
|
||||
t.Run("docker", func(t *testing.T) {
|
||||
assert.ErrorContains(t, run(t, "docker"), "cannot load image")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,19 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/empty"
|
||||
"github.com/google/go-containerregistry/pkg/v1/layout"
|
||||
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
||||
"github.com/google/go-containerregistry/pkg/v1/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -49,6 +57,59 @@ func Test_LoadImage(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// regression for #442: `dive --platform <os/arch> <multi-arch image>` analyzes that platform's image
|
||||
func Test_LoadImage_platform(t *testing.T) {
|
||||
// an OCI layout holding a linux/amd64 + linux/arm64 index, each image's only file is named after its arch
|
||||
idx := mutate.IndexMediaType(empty.Index, types.OCIImageIndex)
|
||||
for _, arch := range []string{"amd64", "arm64"} {
|
||||
layer, err := crane.Layer(map[string][]byte{arch: []byte(arch)})
|
||||
require.NoError(t, err)
|
||||
img, err := mutate.AppendLayers(empty.Image, layer)
|
||||
require.NoError(t, err)
|
||||
cf, err := img.ConfigFile()
|
||||
require.NoError(t, err)
|
||||
cf = cf.DeepCopy()
|
||||
cf.OS, cf.Architecture = "linux", arch
|
||||
img, err = mutate.ConfigFile(img, cf)
|
||||
require.NoError(t, err)
|
||||
idx = mutate.AppendManifests(idx, mutate.IndexAddendum{
|
||||
Add: img,
|
||||
Descriptor: v1.Descriptor{Platform: &v1.Platform{OS: "linux", Architecture: arch}},
|
||||
})
|
||||
}
|
||||
dir := t.TempDir()
|
||||
_, err := layout.Write(dir, idx)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, arch := range []string{"amd64", "arm64"} {
|
||||
t.Run(arch, func(t *testing.T) {
|
||||
out := filepath.Join(t.TempDir(), "out.json")
|
||||
rootCmd := getTestCommand(t, fmt.Sprintf("--platform linux/%s --json %s oci-dir:%s", arch, out, dir))
|
||||
Capture().WithStdout().WithStderr().Run(t, func() {
|
||||
require.NoError(t, rootCmd.Execute())
|
||||
})
|
||||
|
||||
data, err := os.ReadFile(out)
|
||||
require.NoError(t, err)
|
||||
var got struct {
|
||||
Layer []struct {
|
||||
FileList []struct {
|
||||
Path string `json:"path"`
|
||||
} `json:"fileList"`
|
||||
} `json:"layer"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(data, &got))
|
||||
var paths []string
|
||||
for _, l := range got.Layer {
|
||||
for _, f := range l.FileList {
|
||||
paths = append(paths, f.Path)
|
||||
}
|
||||
}
|
||||
assert.Equal(t, []string{arch}, paths)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func runWithCombinedOutput(t testing.TB, cmd string) {
|
||||
t.Helper()
|
||||
rootCmd := getTestCommand(t, cmd)
|
||||
|
|
|
|||
|
|
@ -29,22 +29,28 @@ func readFile(t *testing.T, p string) string {
|
|||
return string(b)
|
||||
}
|
||||
|
||||
// also a regression for #687: extract wrote nothing from docker 25+ saves (OCI layout, blobs/sha256/<digest> layers)
|
||||
func TestExtract(t *testing.T) {
|
||||
img := imagefixture.Load(t, "test-docker-image")
|
||||
dirs(t)
|
||||
// same Dockerfile, but the OCI save was built with a different README (somefile1.txt is a copy of it)
|
||||
for fixture, size := range map[string]int{"test-docker-image": 6405, "test-oci-docker-image": 10169} {
|
||||
t.Run(fixture, func(t *testing.T) {
|
||||
img := imagefixture.Load(t, fixture)
|
||||
dirs(t)
|
||||
|
||||
// layer 4 adds root/example/somefile1.txt (the chmod one)
|
||||
require.NoError(t, extract(img, 4, "/root", "."))
|
||||
// layer 4 adds root/example/somefile1.txt (the chmod one)
|
||||
require.NoError(t, extract(img, 4, "/root", "."))
|
||||
|
||||
got, err := os.ReadFile(filepath.Join("root", "example", "somefile1.txt"))
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, got, 6405)
|
||||
got, err := os.ReadFile(filepath.Join("root", "example", "somefile1.txt"))
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, got, size)
|
||||
|
||||
// the tree as of the selected layer (root/saved.txt is added by a later one), only under the selected path
|
||||
_, err = os.Stat(filepath.Join("root", "saved.txt"))
|
||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||
_, err = os.Stat("somefile.txt")
|
||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||
// the tree as of the selected layer (root/saved.txt is added by a later one), only under the selected path
|
||||
_, err = os.Stat(filepath.Join("root", "saved.txt"))
|
||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||
_, err = os.Stat("somefile.txt")
|
||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtract_SelectsPathNotPrefix(t *testing.T) {
|
||||
|
|
@ -110,6 +116,25 @@ func TestExtract_DoesNotEscape(t *testing.T) {
|
|||
assert.Equal(t, "fine", readFile(t, "fine.txt"))
|
||||
}
|
||||
|
||||
// regression for #722: the reported payload, a tar name sharing the selected path's prefix that climbs out of the cwd
|
||||
func TestExtract_PrefixSharingNameDoesNotEscape(t *testing.T) {
|
||||
img := imagefixture.LoadSpec(t, imagefixture.Spec{Layers: [][]imagefixture.Entry{{
|
||||
{Name: "etc/", Type: tar.TypeDir},
|
||||
{Name: "etc/motd", Content: "motd"},
|
||||
{Name: "etc/motd/../../../victim/.bashrc", Content: "pwned"},
|
||||
}}})
|
||||
base, _ := dirs(t)
|
||||
|
||||
// the old prefix check wrote base/victim/.bashrc when /etc/motd was selected
|
||||
require.NoError(t, extract(img, 0, "/etc/motd", "."))
|
||||
assert.Equal(t, "motd", readFile(t, filepath.Join("etc", "motd")))
|
||||
// selecting a parent of where the name cleans to reaches it, and it is refused
|
||||
assert.ErrorContains(t, extract(img, 0, "/", "."), "etc/motd/../../../victim/.bashrc")
|
||||
|
||||
_, err := os.Stat(filepath.Join(base, "victim"))
|
||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||
}
|
||||
|
||||
func TestExtract_SquashedTree(t *testing.T) {
|
||||
img := imagefixture.LoadSpec(t, imagefixture.Spec{Layers: [][]imagefixture.Entry{
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
package viewmodel
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
|
@ -397,3 +399,87 @@ func TestFileTreeHideTypeWithFilter(t *testing.T) {
|
|||
|
||||
runTestCase(t, vm, width, height, regex)
|
||||
}
|
||||
|
||||
// regression for #306: kaniko built and Windows images flooded the TUI with "path error at layer index ...: unable
|
||||
// to remove/add ..." and refused to start without --ignore-errors. The layers here whiteout paths no lower layer
|
||||
// has (as in the Windows images), repeat the same whiteout in later layers (as kaniko does for apt's auxfiles) and
|
||||
// carry a character device.
|
||||
func TestFileTreeWhiteoutsOfMissingPaths(t *testing.T) {
|
||||
dir := func(name string) imagefixture.Entry { return imagefixture.Entry{Name: name, Type: tar.TypeDir} }
|
||||
file := func(name string) imagefixture.Entry { return imagefixture.Entry{Name: name, Content: name} }
|
||||
whiteout := func(name string) imagefixture.Entry { return imagefixture.Entry{Name: name} }
|
||||
aptLists := []imagefixture.Entry{dir("var/"), dir("var/lib/"), dir("var/lib/apt/"), dir("var/lib/apt/lists/")}
|
||||
scm := []imagefixture.Entry{dir("Files/"), dir("Files/Windows/"), dir("Files/Windows/System32/"), dir("Files/Windows/System32/LogFiles/"), dir("Files/Windows/System32/LogFiles/Scm/")}
|
||||
|
||||
layers := [][]imagefixture.Entry{
|
||||
append(append(slices.Clone(aptLists),
|
||||
dir("var/lib/apt/lists/auxfiles/"),
|
||||
file("var/lib/apt/lists/auxfiles/a"),
|
||||
file("var/lib/apt/lists/lock"),
|
||||
dir("dev/"),
|
||||
imagefixture.Entry{Name: "dev/null", Type: tar.TypeChar, Mode: 0o666},
|
||||
dir("Hives/"),
|
||||
file("Hives/DefaultUser_Delta"),
|
||||
), scm...),
|
||||
append(append(slices.Clone(aptLists),
|
||||
whiteout("var/lib/apt/lists/.wh.auxfiles"),
|
||||
), append(slices.Clone(scm),
|
||||
whiteout("Files/Windows/System32/LogFiles/Scm/.wh.scm.evm"),
|
||||
file("Files/Windows/System32/LogFiles/Scm/new.evm"),
|
||||
)...),
|
||||
append(slices.Clone(aptLists), whiteout("var/lib/apt/lists/.wh.auxfiles"), file("var/lib/apt/lists/two")),
|
||||
append(slices.Clone(aptLists), whiteout("var/lib/apt/lists/.wh.auxfiles"), file("var/lib/apt/lists/three")),
|
||||
}
|
||||
img := imagefixture.LoadSpec(t, imagefixture.Spec{Format: imagefixture.OCISave, Compression: imagefixture.Gzip, Layers: layers})
|
||||
analysis, err := dive.Analyze(context.Background(), "spec", img)
|
||||
require.NoError(t, err)
|
||||
|
||||
vm, err := NewFileTreeViewModel(tui.Config{Analysis: analysis, Preferences: tui.DefaultPreferences()}, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
// every layer selection the TUI can make builds a tree
|
||||
for _, mode := range []LayerCompareMode{CompareSingleLayer, CompareAllLayers} {
|
||||
for idx := range layers {
|
||||
state := LayerSetState{LayerIndex: idx, CompareMode: mode}
|
||||
require.NoError(t, vm.SetTreeByLayer(state.GetCompareIndexes()), "layer %d mode %d", idx, mode)
|
||||
}
|
||||
}
|
||||
|
||||
marks := func(t *testing.T, bottomStop, topStart, topStop int, want map[string]filetree.DiffType, absent ...string) {
|
||||
t.Helper()
|
||||
require.NoError(t, vm.SetTreeByLayer(0, bottomStop, topStart, topStop))
|
||||
for p, d := range want {
|
||||
n, err := vm.ModelTree.GetNode(p)
|
||||
if assert.NoError(t, err, p) {
|
||||
assert.Equal(t, d, n.Data.DiffType, p)
|
||||
}
|
||||
}
|
||||
for _, p := range absent {
|
||||
_, err := vm.ModelTree.GetNode(p)
|
||||
assert.Error(t, err, p)
|
||||
}
|
||||
}
|
||||
|
||||
// the valid whiteout removes auxfiles, the one with no target is skipped and its sibling still counts
|
||||
marks(t, 0, 1, 1, map[string]filetree.DiffType{
|
||||
"/var/lib/apt/lists/auxfiles": filetree.Removed,
|
||||
"/var/lib/apt/lists/auxfiles/a": filetree.Removed,
|
||||
"/var/lib/apt/lists/lock": filetree.Unmodified,
|
||||
"/Files/Windows/System32/LogFiles/Scm/new.evm": filetree.Added,
|
||||
"/dev/null": filetree.Unmodified,
|
||||
"/Hives/DefaultUser_Delta": filetree.Unmodified,
|
||||
}, "/Files/Windows/System32/LogFiles/Scm/scm.evm")
|
||||
|
||||
// the repeated whiteout has nothing left to remove
|
||||
marks(t, 1, 2, 2, map[string]filetree.DiffType{
|
||||
"/var/lib/apt/lists/two": filetree.Added,
|
||||
"/var/lib/apt/lists/lock": filetree.Unmodified,
|
||||
}, "/var/lib/apt/lists/auxfiles")
|
||||
|
||||
// aggregated: everything after the base layer at once
|
||||
marks(t, 0, 1, 3, map[string]filetree.DiffType{
|
||||
"/var/lib/apt/lists/auxfiles": filetree.Removed,
|
||||
"/var/lib/apt/lists/two": filetree.Added,
|
||||
"/var/lib/apt/lists/three": filetree.Added,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -430,6 +430,8 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
|||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c=
|
||||
golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
|
|
@ -494,6 +496,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
|
|||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||
golang.org/x/tools v0.50.0 h1:c2ifzfcuY7L90lZ2aKd8S4K2NpASF08SZx9ZuJkHmSU=
|
||||
golang.org/x/tools v0.50.0/go.mod h1:7ulVMw3831Mwi5EZD6RomGyffr4VFjuNYXf2BbCEAV0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
|
|
|
|||
274
internal/source/docker_test.go
Normal file
274
internal/source/docker_test.go
Normal file
|
|
@ -0,0 +1,274 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cliconfig "github.com/docker/cli/cli/config"
|
||||
ddocker "github.com/docker/cli/cli/context/docker"
|
||||
ctxstore "github.com/docker/cli/cli/context/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/wagoodman/dive/internal/imagefixture"
|
||||
)
|
||||
|
||||
// dockerHostDaemon fakes the parts of the docker engine API that stereoscope's daemon provider uses (ping, inspect,
|
||||
// pull, save). It holds one image, answered under any name, and accepts API versions 1.<minAPI> to 1.<maxAPI> the way
|
||||
// the real daemon does. With missing set the image only shows up after a pull.
|
||||
type dockerHostDaemon struct {
|
||||
saved []byte
|
||||
minAPI, maxAPI int
|
||||
missing bool
|
||||
|
||||
mu sync.Mutex
|
||||
pulled bool
|
||||
pulls []string // "fromImage:tag" of each pull
|
||||
versions []string // the API version of every versioned request
|
||||
}
|
||||
|
||||
// newDockerHostDaemon is a docker 29 daemon (API 1.44 to 1.52) holding the oci-save fixture (3 layers)
|
||||
func newDockerHostDaemon(t *testing.T) *dockerHostDaemon {
|
||||
saved, err := os.ReadFile(imagefixture.Path(t, "oci-save"))
|
||||
require.NoError(t, err)
|
||||
return &dockerHostDaemon{saved: saved, minAPI: 44, maxAPI: 52}
|
||||
}
|
||||
|
||||
func (d *dockerHostDaemon) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Api-Version", fmt.Sprintf("1.%d", d.maxAPI))
|
||||
fail := func(code int, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"message": msg})
|
||||
}
|
||||
if r.URL.Path == "/_ping" {
|
||||
_, _ = w.Write([]byte("OK"))
|
||||
return
|
||||
}
|
||||
v, rest, ok := strings.Cut(strings.TrimPrefix(r.URL.Path, "/v"), "/")
|
||||
minor, err := strconv.Atoi(strings.TrimPrefix(v, "1."))
|
||||
if !ok || err != nil {
|
||||
fail(http.StatusNotFound, "page not found")
|
||||
return
|
||||
}
|
||||
// same wording as the daemon
|
||||
switch {
|
||||
case minor < d.minAPI:
|
||||
fail(http.StatusBadRequest, fmt.Sprintf("client version %s is too old. Minimum supported API version is 1.%d, please upgrade your client to a newer version", v, d.minAPI))
|
||||
return
|
||||
case minor > d.maxAPI:
|
||||
fail(http.StatusBadRequest, fmt.Sprintf("client version %s is too new. Maximum supported API version is 1.%d", v, d.maxAPI))
|
||||
return
|
||||
}
|
||||
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
d.versions = append(d.versions, v)
|
||||
present := !d.missing || d.pulled
|
||||
switch {
|
||||
case r.Method == http.MethodPost && rest == "images/create":
|
||||
d.pulled = true
|
||||
d.pulls = append(d.pulls, r.URL.Query().Get("fromImage")+":"+r.URL.Query().Get("tag"))
|
||||
_, _ = w.Write([]byte(`{"status":"Pulling fs layer","id":"abc"}` + "\n" + `{"status":"Status: Downloaded newer image"}` + "\n"))
|
||||
case !present && r.Method == http.MethodGet && strings.HasPrefix(rest, "images/"):
|
||||
fail(http.StatusNotFound, "No such image")
|
||||
case r.Method == http.MethodGet && strings.HasPrefix(rest, "images/") && strings.HasSuffix(rest, "/json"):
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"Id": "sha256:" + strings.Repeat("a", 64), "Size": len(d.saved), "Os": "linux", "Architecture": "amd64"})
|
||||
case r.Method == http.MethodGet && rest == "images/get":
|
||||
w.Header().Set("Content-Type", "application/x-tar")
|
||||
_, _ = w.Write(d.saved)
|
||||
default:
|
||||
fail(http.StatusNotFound, "page not found")
|
||||
}
|
||||
}
|
||||
|
||||
// dockerHostServeUnix serves h on a unix socket at path
|
||||
func dockerHostServeUnix(t *testing.T, path string, h http.Handler) {
|
||||
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700))
|
||||
l, err := net.Listen("unix", path)
|
||||
require.NoError(t, err)
|
||||
srv := httptest.NewUnstartedServer(h)
|
||||
_ = srv.Listener.Close()
|
||||
srv.Listener = l
|
||||
srv.Start()
|
||||
t.Cleanup(srv.Close)
|
||||
}
|
||||
|
||||
// dockerHostShortTempDir is a temp dir short enough to hold a unix socket (macOS caps socket paths at 104 bytes,
|
||||
// t.TempDir() includes the test name)
|
||||
func dockerHostShortTempDir(t *testing.T) string {
|
||||
dir, err := os.MkdirTemp("", "dive")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
||||
return dir
|
||||
}
|
||||
|
||||
// dockerHostUseConfig makes dir the docker config dir (dive's and stereoscope's), with the given contexts (name to
|
||||
// host) and current context, and clears the docker env vars so only the test's own settings apply.
|
||||
func dockerHostUseConfig(t *testing.T, dir, current string, contexts map[string]string) {
|
||||
require.NoError(t, os.MkdirAll(dir, 0o700))
|
||||
if current != "" {
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"currentContext":"`+current+`"}`), 0o600))
|
||||
}
|
||||
st := ctxstore.New(filepath.Join(dir, "contexts"), ctxstore.NewConfig(
|
||||
func() any { return &ddocker.EndpointMeta{} },
|
||||
ctxstore.EndpointTypeGetter(ddocker.DockerEndpoint, func() any { return &ddocker.EndpointMeta{} }),
|
||||
))
|
||||
for name, host := range contexts {
|
||||
require.NoError(t, st.CreateOrUpdate(ctxstore.Metadata{
|
||||
Name: name,
|
||||
Endpoints: map[string]any{ddocker.DockerEndpoint: ddocker.EndpointMeta{Host: host}},
|
||||
}))
|
||||
}
|
||||
for _, k := range []string{"DOCKER_HOST", "DOCKER_CONTEXT", "DOCKER_TLS", "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH", "DOCKER_API_VERSION"} {
|
||||
t.Setenv(k, "")
|
||||
}
|
||||
t.Setenv("DOCKER_CONFIG", dir)
|
||||
// cliconfig.Dir() reads DOCKER_CONFIG once per process
|
||||
prev := cliconfig.Dir()
|
||||
cliconfig.SetDir(dir)
|
||||
t.Cleanup(func() { cliconfig.SetDir(prev) })
|
||||
}
|
||||
|
||||
// dockerHostLoad loads ref through the docker source and checks it is the fake daemon's image
|
||||
func dockerHostLoad(t *testing.T, ref string) {
|
||||
img, err := Load(context.Background(), "docker://"+ref, Config{})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
assert.Len(t, img.Layers, 3)
|
||||
}
|
||||
|
||||
// regression for #542 and #453: Docker Desktop's daemon only listens on ~/.docker/run/docker.sock, reached through
|
||||
// the desktop-linux context it makes current, not on /var/run/docker.sock
|
||||
func TestLoad_dockerDesktopContext(t *testing.T) {
|
||||
home := dockerHostShortTempDir(t)
|
||||
configDir := filepath.Join(home, ".docker")
|
||||
sock := filepath.Join(configDir, "run", "docker.sock")
|
||||
d := newDockerHostDaemon(t)
|
||||
dockerHostServeUnix(t, sock, d)
|
||||
dockerHostUseConfig(t, configDir, "desktop-linux", map[string]string{"desktop-linux": "unix://" + sock})
|
||||
|
||||
dockerHostLoad(t, "work-database:latest")
|
||||
// the image was local to that daemon, nothing should have been pulled
|
||||
assert.False(t, d.pulled)
|
||||
}
|
||||
|
||||
// regression for #418: with a rootless docker context current, dive pulled through the docker cli (into the rootless
|
||||
// daemon) and then read from /var/run/docker.sock, which never had the image. Pull and save now go through the same
|
||||
// API client, and no docker cli is needed.
|
||||
func TestLoad_rootlessContextPull(t *testing.T) {
|
||||
dir := dockerHostShortTempDir(t)
|
||||
sock := filepath.Join(dir, "run", "user", "1000", "docker.sock")
|
||||
d := newDockerHostDaemon(t)
|
||||
d.missing = true
|
||||
dockerHostServeUnix(t, sock, d)
|
||||
dockerHostUseConfig(t, filepath.Join(dir, ".docker"), "rootless", map[string]string{"rootless": "unix://" + sock})
|
||||
t.Setenv("PATH", t.TempDir())
|
||||
|
||||
dockerHostLoad(t, "quay.io/openshifttest/alpine:latest")
|
||||
assert.True(t, d.pulled)
|
||||
}
|
||||
|
||||
// regression for #664: docker 29 refuses API versions below 1.44 ("client version 1.43 is too old"). The client
|
||||
// negotiates the version from the daemon's ping instead of sending a fixed one, so it lands on what the daemon
|
||||
// supports (1.52 here, below the client's own max).
|
||||
func TestLoad_dockerAPIVersionNegotiation(t *testing.T) {
|
||||
dir := dockerHostShortTempDir(t)
|
||||
sock := filepath.Join(dir, "docker.sock")
|
||||
d := newDockerHostDaemon(t)
|
||||
dockerHostServeUnix(t, sock, d)
|
||||
dockerHostUseConfig(t, filepath.Join(dir, ".docker"), "", nil)
|
||||
t.Setenv("DOCKER_HOST", "unix://"+sock)
|
||||
|
||||
dockerHostLoad(t, "alpine:latest")
|
||||
require.NotEmpty(t, d.versions)
|
||||
for _, v := range d.versions {
|
||||
assert.Equal(t, "1.52", v)
|
||||
}
|
||||
}
|
||||
|
||||
// regression for #545: DOCKER_HOST=tcp://...:2376 with DOCKER_TLS_VERIFY=1 and no DOCKER_CERT_PATH, against a
|
||||
// --tlsverify daemon that wants a client cert. The ca.pem, cert.pem and key.pem in ~/.docker are what the docker
|
||||
// cli uses there (dive used to look in a literal "~/.docker").
|
||||
func TestLoad_dockerHostTLSVerify(t *testing.T) {
|
||||
// client cert the daemon trusts
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(1),
|
||||
Subject: pkix.Name{CommonName: "client"},
|
||||
NotBefore: time.Now().Add(-time.Hour),
|
||||
NotAfter: time.Now().Add(time.Hour),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
||||
require.NoError(t, err)
|
||||
clientCert, err := x509.ParseCertificate(der)
|
||||
require.NoError(t, err)
|
||||
clientCAs := x509.NewCertPool()
|
||||
clientCAs.AddCert(clientCert)
|
||||
|
||||
srv := httptest.NewUnstartedServer(newDockerHostDaemon(t))
|
||||
srv.TLS = &tls.Config{ClientAuth: tls.RequireAndVerifyClientCert, ClientCAs: clientCAs, MinVersion: tls.VersionTLS12}
|
||||
srv.Config.ErrorLog = log.New(io.Discard, "", 0)
|
||||
srv.StartTLS()
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
configDir := t.TempDir()
|
||||
dockerHostUseConfig(t, configDir, "", nil)
|
||||
keyDER, err := x509.MarshalECPrivateKey(key)
|
||||
require.NoError(t, err)
|
||||
for name, b := range map[string]*pem.Block{
|
||||
"ca.pem": {Type: "CERTIFICATE", Bytes: srv.Certificate().Raw},
|
||||
"cert.pem": {Type: "CERTIFICATE", Bytes: der},
|
||||
"key.pem": {Type: "EC PRIVATE KEY", Bytes: keyDER},
|
||||
} {
|
||||
require.NoError(t, os.WriteFile(filepath.Join(configDir, name), pem.EncodeToMemory(b), 0o600))
|
||||
}
|
||||
t.Setenv("DOCKER_HOST", "tcp://"+srv.Listener.Addr().String())
|
||||
t.Setenv("DOCKER_TLS_VERIFY", "1")
|
||||
|
||||
dockerHostLoad(t, "tomcat:9-jdk21-openjdk-slim-bullseye")
|
||||
|
||||
// the daemon really does refuse a client without the cert
|
||||
require.NoError(t, os.Remove(filepath.Join(configDir, "cert.pem")))
|
||||
require.NoError(t, os.Remove(filepath.Join(configDir, "key.pem")))
|
||||
_, err = Load(context.Background(), "docker://tomcat:9-jdk21-openjdk-slim-bullseye", Config{})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
// regression for #359: an image the docker daemon already has is read from it, not pulled from Docker Hub again
|
||||
func TestLoad_dockerUsesLocalImage(t *testing.T) {
|
||||
dir := dockerHostShortTempDir(t)
|
||||
sock := filepath.Join(dir, "docker.sock")
|
||||
d := newDockerHostDaemon(t)
|
||||
dockerHostServeUnix(t, sock, d)
|
||||
dockerHostUseConfig(t, filepath.Join(dir, ".docker"), "", nil)
|
||||
t.Setenv("DOCKER_HOST", "unix://"+sock)
|
||||
|
||||
dockerHostLoad(t, "charity1475/app:latest")
|
||||
assert.False(t, d.pulled, "a local image was pulled again")
|
||||
}
|
||||
58
internal/source/ocidir_test.go
Normal file
58
internal/source/ocidir_test.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/empty"
|
||||
"github.com/google/go-containerregistry/pkg/v1/layout"
|
||||
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
||||
"github.com/google/go-containerregistry/pkg/v1/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// archImage is a one layer OCI image whose config says linux/<arch> and whose only file, /<arch>, names it, so a
|
||||
// test can tell which image of an index was loaded from the files alone.
|
||||
func archImage(t *testing.T, arch string) v1.Image {
|
||||
t.Helper()
|
||||
layer, err := crane.Layer(map[string][]byte{arch: []byte(arch)})
|
||||
require.NoError(t, err)
|
||||
base := mutate.ConfigMediaType(mutate.MediaType(empty.Image, types.OCIManifestSchema1), types.OCIConfigJSON)
|
||||
img, err := mutate.Append(base, mutate.Addendum{Layer: layer, MediaType: types.OCILayer})
|
||||
require.NoError(t, err)
|
||||
cf, err := img.ConfigFile()
|
||||
require.NoError(t, err)
|
||||
cf = cf.DeepCopy()
|
||||
cf.OS, cf.Architecture = "linux", arch
|
||||
img, err = mutate.ConfigFile(img, cf)
|
||||
require.NoError(t, err)
|
||||
return img
|
||||
}
|
||||
|
||||
// regression for #423: an OCI image layout dir (skopeo `oci:<dir>`, `crane pull --format=oci`) loads via `oci-dir:`
|
||||
func TestLoad_ociDirLayout(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
p, err := layout.Write(dir, empty.Index)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, p.AppendImage(archImage(t, "amd64")))
|
||||
|
||||
for _, scheme := range []string{"oci-dir:", "oci-dir://"} {
|
||||
t.Run(scheme, func(t *testing.T) {
|
||||
img, err := Load(context.Background(), scheme+dir, Config{})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
|
||||
require.Len(t, img.Layers, 1)
|
||||
assert.True(t, img.SquashedTree().HasPath("/amd64"))
|
||||
})
|
||||
}
|
||||
|
||||
// same layout through --source / container-engine instead of a scheme
|
||||
img, err := Load(context.Background(), dir, Config{Source: OCIDir})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
assert.Len(t, img.Layers, 1)
|
||||
}
|
||||
73
internal/source/platform_test.go
Normal file
73
internal/source/platform_test.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/empty"
|
||||
"github.com/google/go-containerregistry/pkg/v1/layout"
|
||||
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||
"github.com/google/go-containerregistry/pkg/v1/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// multiArchIndex is a linux/amd64 + linux/arm64 index with the "unknown/unknown" attestation manifest buildx adds
|
||||
func multiArchIndex(t *testing.T) v1.ImageIndex {
|
||||
t.Helper()
|
||||
idx := mutate.IndexMediaType(empty.Index, types.OCIImageIndex)
|
||||
for _, arch := range []string{"amd64", "arm64"} {
|
||||
idx = mutate.AppendManifests(idx, mutate.IndexAddendum{
|
||||
Add: archImage(t, arch),
|
||||
Descriptor: v1.Descriptor{Platform: &v1.Platform{OS: "linux", Architecture: arch}},
|
||||
})
|
||||
}
|
||||
return mutate.AppendManifests(idx, mutate.IndexAddendum{
|
||||
Add: archImage(t, "attestation"),
|
||||
Descriptor: v1.Descriptor{
|
||||
Platform: &v1.Platform{OS: "unknown", Architecture: "unknown"},
|
||||
Annotations: map[string]string{"vnd.docker.reference.type": "attestation-manifest"},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// regression for #442: --platform picks one image out of a multi-arch index, whatever the host arch is
|
||||
func TestLoad_platform(t *testing.T) {
|
||||
idx := multiArchIndex(t)
|
||||
|
||||
dir := t.TempDir()
|
||||
_, err := layout.Write(dir, idx)
|
||||
require.NoError(t, err)
|
||||
|
||||
host := startRegistry(t)
|
||||
ref := host + "/dive/multiarch:1"
|
||||
r, err := name.ParseReference(ref)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, remote.WriteIndex(r, idx))
|
||||
|
||||
for src, input := range map[string]string{OCIDir: "oci-dir:" + dir, Registry: "registry://" + ref} {
|
||||
t.Run(src, func(t *testing.T) {
|
||||
for arch, other := range map[string]string{"amd64": "arm64", "arm64": "amd64"} {
|
||||
t.Run(arch, func(t *testing.T) {
|
||||
img, err := Load(context.Background(), input, Config{Platform: "linux/" + arch})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
|
||||
assert.Equal(t, arch, img.Metadata.Architecture)
|
||||
tree := img.SquashedTree()
|
||||
assert.True(t, tree.HasPath(file.Path("/"+arch)), "expected the %s image's files", arch)
|
||||
assert.False(t, tree.HasPath(file.Path("/"+other)), "got the %s image's files", other)
|
||||
})
|
||||
}
|
||||
|
||||
// a platform the index doesn't have fails instead of quietly loading another one
|
||||
_, err := Load(context.Background(), input, Config{Platform: "linux/s390x"})
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
82
internal/source/podman_test.go
Normal file
82
internal/source/podman_test.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// regression for #432: --source podman on an M1 mac said "unsupported platform". On darwin the podman source is the
|
||||
// podman machine's API socket, which dive finds through `podman machine inspect` (faked here) and loads from.
|
||||
func TestPodmanProvider_macMachineSocket(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("fakes podman with a shell script")
|
||||
}
|
||||
if _, err := os.Stat("/run/podman/podman.sock"); err == nil {
|
||||
t.Skip("a real rootful podman socket would be picked before the machine socket")
|
||||
}
|
||||
|
||||
// podman 5 puts the machine's API socket under $TMPDIR
|
||||
sock := filepath.Join(dockerHostShortTempDir(t), "podman", "podman-machine-default-api.sock")
|
||||
d := newDockerHostDaemon(t)
|
||||
// podman's docker compatible API
|
||||
d.minAPI, d.maxAPI = 24, 41
|
||||
dockerHostServeUnix(t, sock, d)
|
||||
|
||||
bin := t.TempDir()
|
||||
require.NoError(t, os.WriteFile(filepath.Join(bin, "podman"), []byte("#!/bin/sh\necho '"+sock+"'\n"), 0o700))
|
||||
t.Setenv("PATH", bin)
|
||||
|
||||
// nothing else may point at a socket: no CONTAINER_HOST, containers.conf or rootless socket
|
||||
t.Setenv("CONTAINER_HOST", "")
|
||||
prev := podmanConfigs
|
||||
podmanConfigs = nil
|
||||
t.Cleanup(func() { podmanConfigs = prev })
|
||||
t.Cleanup(xdg.Reload)
|
||||
t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
|
||||
xdg.Reload()
|
||||
dockerHostUseConfig(t, t.TempDir(), "", nil)
|
||||
|
||||
tmp := file.NewTempDirGenerator("dive-test")
|
||||
t.Cleanup(func() { _ = tmp.Cleanup() })
|
||||
ps, err := providers(Podman, "docker.io/library/ubuntu", "darwin", nil, tmp)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, ps, 1)
|
||||
img, err := ps[0].Provide(context.Background())
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
assert.Len(t, img.Layers, 3)
|
||||
}
|
||||
|
||||
// regression for #310: `dive podman://<image>` pulls an image podman doesn't have yet instead of failing with
|
||||
// "no such image"
|
||||
func TestLoad_podmanPullsMissingImage(t *testing.T) {
|
||||
for _, tt := range []struct{ name, input, source string }{
|
||||
{name: "scheme", input: "podman://quay.io/podman/stable:latest"},
|
||||
{name: "source", input: "quay.io/podman/stable:latest", source: Podman},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
sock := filepath.Join(dockerHostShortTempDir(t), "podman.sock")
|
||||
d := newDockerHostDaemon(t)
|
||||
d.minAPI, d.maxAPI = 24, 41
|
||||
d.missing = true
|
||||
dockerHostServeUnix(t, sock, d)
|
||||
dockerHostUseConfig(t, t.TempDir(), "", nil)
|
||||
t.Setenv("CONTAINER_HOST", "unix://"+sock)
|
||||
|
||||
img, err := Load(context.Background(), tt.input, Config{Source: tt.source})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
|
||||
assert.Len(t, img.Layers, 3)
|
||||
assert.Equal(t, []string{"quay.io/podman/stable:latest"}, d.pulls)
|
||||
})
|
||||
}
|
||||
}
|
||||
60
internal/source/registry_test.go
Normal file
60
internal/source/registry_test.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/registry"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// startRegistry serves an in-memory registry on loopback and returns its host:port. DOCKER_CONFIG points at an empty
|
||||
// dir so no real credentials or credential helpers get involved.
|
||||
func startRegistry(t *testing.T) string {
|
||||
t.Helper()
|
||||
t.Setenv("DOCKER_CONFIG", t.TempDir())
|
||||
srv := httptest.NewServer(registry.New(registry.Logger(log.New(io.Discard, "", 0))))
|
||||
t.Cleanup(srv.Close)
|
||||
return strings.TrimPrefix(srv.URL, "http://")
|
||||
}
|
||||
|
||||
// regression for #406 and #130: an image is read straight from a registry with go-containerregistry, no docker
|
||||
// daemon (or skopeo) involved
|
||||
func TestLoad_registry(t *testing.T) {
|
||||
host := startRegistry(t)
|
||||
ref := host + "/dive/app:1"
|
||||
r, err := name.ParseReference(ref)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, remote.Write(r, archImage(t, "amd64")))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
cfg Config
|
||||
}{
|
||||
{name: "registry scheme", input: "registry://" + ref},
|
||||
{name: "oci-registry scheme", input: "oci-registry://" + ref},
|
||||
{name: "registry source", input: ref, cfg: Config{Source: Registry}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
img, err := Load(context.Background(), tt.input, tt.cfg)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, img.Cleanup()) })
|
||||
|
||||
require.Len(t, img.Layers, 1)
|
||||
assert.True(t, img.SquashedTree().HasPath("/amd64"))
|
||||
})
|
||||
}
|
||||
|
||||
// a tag the registry doesn't have is an error, not a fallback to some other source
|
||||
_, err = Load(context.Background(), "registry://"+host+"/dive/app:missing", Config{})
|
||||
require.Error(t, err)
|
||||
}
|
||||
Loading…
Reference in a new issue