From 2d46cf1f74bdcbeac4a1ebfd4e8dea5ae9fd4ec7 Mon Sep 17 00:00:00 2001 From: Joe Leon Date: Mon, 21 Aug 2023 12:15:34 -0400 Subject: [PATCH] temp increase file size in maxreader, handler improvements --- pkg/handlers/archive.go | 37 ++++++++++++++++++------------------- pkg/handlers/handlers.go | 13 ++++++++++--- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/pkg/handlers/archive.go b/pkg/handlers/archive.go index b32dfd1d..fdb70ed5 100644 --- a/pkg/handlers/archive.go +++ b/pkg/handlers/archive.go @@ -28,13 +28,19 @@ const ( var ( maxDepth = 5 - maxSize = 250 * 1024 * 1024 // 20MB + maxSize = 250 * 1024 * 1024 * 400 // 20MB maxTimeout = time.Duration(30) * time.Second ) // Ensure the Archive satisfies the interfaces at compile time. var _ SpecializedHandler = (*Archive)(nil) +type tempEnv struct { + tempFile *os.File + tempFileName string + extractPath string +} + // Archive is a handler for extracting and decompressing archives. type Archive struct { size int @@ -44,7 +50,7 @@ type Archive struct { // DockerTarReader is a wrapper for io.Reader that also stores the temp filepath of the tar file. type DockerTarReader struct { io.Reader - filepath string + tempEnv } // New sets a default maximum size and current size counter. @@ -275,15 +281,15 @@ func (a *Archive) HandleSpecialized(ctx logContext.Context, reader io.Reader) (i reader, err = a.extractRpmContent(ctx, reader) case tarMimeType: //check if tar is a docker image - isImg, imgPath, err := a.isDockerImage(ctx, reader) + isImg, tmpEnv, err := a.isDockerImage(ctx, reader) if err != nil { return nil, false, err } if isImg { // Build dockerReader reader = DockerTarReader{ - Reader: reader, - filepath: imgPath, + Reader: reader, + tempEnv: tmpEnv, } } else { return nil, false, nil @@ -450,12 +456,6 @@ func (a *Archive) handleExtractedFiles(ctx logContext.Context, env tempEnv, hand return dataArchiveName, nil } -type tempEnv struct { - tempFile *os.File - tempFileName string - extractPath string -} - // createTempEnv creates a temporary file and a temporary directory for extracting archives. // The caller is responsible for removing these temporary resources // (both the file and directory) when they are no longer needed. @@ -500,18 +500,17 @@ func openDataArchive(extractPath string, dataArchiveName string) (io.ReadCloser, return dataFile, nil } -func (a *Archive) isDockerImage(ctx context.Context, file io.Reader) (isImg bool, filePath string, err error) { +func (a *Archive) isDockerImage(ctx context.Context, file io.Reader) (isImg bool, tmpEnv tempEnv, err error) { - tmpEnv, err := a.createTempEnv(ctx, file) + tmpEnv, err = a.createTempEnv(ctx, file) if err != nil { - return false, "", err + return false, tempEnv{}, err } - defer os.Remove(tmpEnv.tempFileName) - defer os.RemoveAll(tmpEnv.extractPath) - _, err = tarball.ImageFromPath(tmpEnv.tempFileName, nil) if err != nil { - return false, "", nil + os.Remove(tmpEnv.tempFileName) + os.RemoveAll(tmpEnv.extractPath) + return false, tempEnv{}, nil } - return true, tmpEnv.tempFileName, nil + return true, tmpEnv, nil } diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index 39f2e4bf..63075fe0 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -3,6 +3,7 @@ package handlers import ( "context" "io" + "os" "runtime" diskbufferreader "github.com/bill-rich/disk-buffer-reader" @@ -61,7 +62,7 @@ func HandleFile(ctx logContext.Context, file io.Reader, chunkSkel *sources.Chunk file, isSpecial, err := specialHandler.HandleSpecialized(aCtx, reReader) if isSpecial { if dockerTarReader, ok := file.(DockerTarReader); ok { - return handleDockerTar(ctx, dockerTarReader.filepath, chunkSkel, chunksChan) + return handleDockerTar(ctx, dockerTarReader.tempEnv, chunkSkel, chunksChan) } return handleChunks(aCtx, h.FromFile(ctx, file), chunkSkel, chunksChan) } @@ -109,12 +110,18 @@ func handleChunks(ctx context.Context, handlerChan chan []byte, chunkSkel *sourc } } -func handleDockerTar(ctx logContext.Context, filePath string, chunkSkel *sources.Chunk, chunksChan chan *sources.Chunk) bool { +func handleDockerTar(ctx logContext.Context, tmpEnv tempEnv, chunkSkel *sources.Chunk, chunksChan chan *sources.Chunk) bool { // aCtx := logContext.AddLogger(ctx) // aCtx.logger.V(3).Info("Docker image detected in tarball: " + filePath) //scan with docker scanner + + // Known logging issue: it shows the tempFileName as the image name, but it really should say the .tar we're scanning + + defer os.Remove(tmpEnv.tempFileName) + defer os.RemoveAll(tmpEnv.extractPath) + dockerConn := sourcespb.Docker{ - Images: []string{"file://" + filePath}, + Images: []string{"file://" + tmpEnv.tempFileName}, Credential: &sourcespb.Docker_DockerKeychain{ DockerKeychain: true, },