* 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>
274 lines
10 KiB
Go
274 lines
10 KiB
Go
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")
|
|
}
|