Compare commits

...

3 Commits

Author SHA1 Message Date
Joe Leon 92f1978a85 reworked to apply docker identification in handlers.go 2023-08-16 16:29:08 -04:00
Joe Leon c8f8af8771 poc working with docker scanner invoked when filesystem finds tarball 2023-08-14 16:28:04 -04:00
joeleonjr fa9469cfc7 Docker scanning by digest (#1615)
* added functionality to scan docker images with digests instead of tags

* cleaned import statement

* added unit test for baseAndTag parsing + remote digest scan
2023-08-11 16:53:12 -05:00
3 changed files with 162 additions and 10 deletions
+75 -2
View File
@@ -1,10 +1,20 @@
package handlers
import (
"archive/tar"
"context"
"fmt"
"io"
"math/rand"
"os"
"runtime"
"github.com/google/go-containerregistry/pkg/v1/tarball"
context2 "github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/docker"
"google.golang.org/protobuf/types/known/anypb"
)
func DefaultHandlers() []Handler {
@@ -19,8 +29,8 @@ type Handler interface {
New()
}
func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {
// Find a handler for this file.
func HandleFile(ctx context2.Context, file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {
var handler Handler
for _, h := range DefaultHandlers() {
h.New()
@@ -34,6 +44,34 @@ func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, c
return false
}
if IsReaderTarball(ctx, file) {
isDocker, path := IsDockerImage(ctx, file)
if isDocker {
// if docker image then run
ctx.logger.V(3).Info("Docker image detected in tarball: " + path)
//scan with docker scanner
dockerConn := sourcespb.Docker{
Images: []string{"file://" + path},
Credential: &sourcespb.Docker_DockerKeychain{
DockerKeychain: true,
},
}
anyConn, err := anypb.New(&dockerConn)
if err != nil {
ctx.logger.V(3).Info("error marshalling Docker connection", "error", err)
}
dockerSource := docker.Source{}
if err := dockerSource.Init(ctx, "trufflehog - docker", 0, chunkSkel.SourceID, chunkSkel.Verify, anyConn, runtime.NumCPU()); err != nil {
// will need to pas this in later s.jobId
return false
}
if err := dockerSource.Chunks(ctx, chunksChan); err != nil {
return false
}
return true
}
}
// Process the file and read all []byte chunks from handlerChan.
handlerChan := handler.FromFile(ctx, file)
for {
@@ -56,3 +94,38 @@ func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, c
}
}
}
func IsReaderTarball(ctx context2.Context, reader io.Reader) bool {
// Check if file is a tarball, if so, check if it is a Docker image.
_, err := tar.NewReader(reader).Next()
if err != nil {
return false
}
return true
}
func IsDockerImage(ctx context2.Context, file io.Reader) (isDocker bool, tmpFilename string) {
// Generate a random name for the temporary file.
tmpFilename = fmt.Sprintf("%s/temp_%d.tar", os.TempDir(), rand.Int())
// Create a temporary file to write image data from ReadCloser.
tmpFile, err := os.Create(tmpFilename)
if err != nil {
fmt.Printf("Error creating temporary file: %s\n", err)
return false, ""
}
defer os.Remove(tmpFilename)
// Copy data from ReadCloser to the temporary file.
if _, err := io.Copy(tmpFile, file); err != nil {
fmt.Printf("Error copying data to temporary file: %s\n", err)
return false, ""
}
tmpFile.Close()
_, err = tarball.ImageFromPath(tmpFilename, nil)
if err != nil {
return false, ""
}
return true, tmpFilename
}
+26 -6
View File
@@ -84,6 +84,8 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err
var img v1.Image
var err error
var base, tag string
var hasDigest bool
var imageName name.Reference
if strings.HasPrefix(image, "file://") {
image = strings.TrimPrefix(image, "file://")
@@ -93,10 +95,18 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err
return err
}
} else {
base, tag = baseAndTagFromImage(image)
imageName, err := name.NewTag(image)
if err != nil {
return err
base, tag, hasDigest = baseAndTagFromImage(image)
if hasDigest {
imageName, err = name.NewDigest(image)
if err != nil {
return err
}
} else {
imageName, err = name.NewTag(image)
if err != nil {
return err
}
}
img, err = remote.Image(imageName, remoteOpts...)
@@ -194,10 +204,20 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err
return nil
}
func baseAndTagFromImage(image string) (base, tag string) {
func baseAndTagFromImage(image string) (base, tag string, hasDigest bool) {
regRepoDelimiter := "/"
tagDelim := ":"
parts := strings.Split(image, tagDelim)
digestDelim := "@"
// Split on digest first, if present.
parts := strings.SplitN(image, digestDelim, 2)
if len(parts) > 1 {
base = parts[0]
tag = parts[1]
hasDigest = true
return
}
parts = strings.Split(image, tagDelim)
// Verify that we aren't confusing a tag for a hostname w/ port for the purposes of weak validation.
if len(parts) > 1 && !strings.Contains(parts[len(parts)-1], regRepoDelimiter) {
base = strings.Join(parts[:len(parts)-1], tagDelim)
+61 -2
View File
@@ -1,5 +1,3 @@
//go:build integration
package docker
import (
@@ -51,3 +49,64 @@ func TestDockerImageScan(t *testing.T) {
assert.Equal(t, 1, chunkCounter)
}
func TestDockerImageScanWithDigest(t *testing.T) {
dockerConn := &sourcespb.Docker{
Credential: &sourcespb.Docker_Unauthenticated{
Unauthenticated: &credentialspb.Unauthenticated{},
},
Images: []string{"trufflesecurity/secrets@sha256:864f6d41209462d8e37fc302ba1532656e265f7c361f11e29fed6ca1f4208e11"},
}
conn := &anypb.Any{}
err := conn.MarshalFrom(dockerConn)
assert.NoError(t, err)
s := &Source{}
err = s.Init(context.TODO(), "test source", 0, 0, false, conn, 1)
assert.NoError(t, err)
var wg sync.WaitGroup
chunksChan := make(chan *sources.Chunk, 1)
chunkCounter := 0
wg.Add(1)
go func() {
defer wg.Done()
for chunk := range chunksChan {
assert.NotEmpty(t, chunk)
chunkCounter++
}
}()
err = s.Chunks(context.TODO(), chunksChan)
assert.NoError(t, err)
close(chunksChan)
wg.Wait()
assert.Equal(t, 1, chunkCounter)
}
func TestBaseAndTagFromImage(t *testing.T) {
tests := []struct {
image string
wantBase string
wantTag string
wantDigest bool
}{
{"golang:1.16", "golang", "1.16", false},
{"golang@sha256:abcdef", "golang", "sha256:abcdef", true},
{"ghcr.io/golang:1.16", "ghcr.io/golang", "1.16", false},
{"ghcr.io/golang:nightly", "ghcr.io/golang", "nightly", false},
{"ghcr.io/golang", "ghcr.io/golang", "latest", false},
{"ghcr.io/trufflesecurity/secrets", "ghcr.io/trufflesecurity/secrets", "latest", false},
}
for _, tt := range tests {
gotBase, gotTag, gotDigest := baseAndTagFromImage(tt.image)
if gotBase != tt.wantBase || gotTag != tt.wantTag || gotDigest != tt.wantDigest {
t.Errorf("baseAndTagFromImage(%q) = (%q, %q, %v), want (%q, %q, %v)",
tt.image, gotBase, gotTag, gotDigest, tt.wantBase, tt.wantTag, tt.wantDigest)
}
}
}