From 6b8ce2fba23240e1baf24639f2f9af7211ec4f8e Mon Sep 17 00:00:00 2001 From: Joe Leon Date: Wed, 23 Aug 2023 15:43:37 -0400 Subject: [PATCH] saving test case progress --- pkg/handlers/archive_test.go | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/pkg/handlers/archive_test.go b/pkg/handlers/archive_test.go index 4f9270119699..4115592f9f2a 100644 --- a/pkg/handlers/archive_test.go +++ b/pkg/handlers/archive_test.go @@ -12,6 +12,10 @@ import ( diskbufferreader "github.com/bill-rich/disk-buffer-reader" "github.com/stretchr/testify/assert" + "github.com/google/go-containerregistry/pkg/v1/remote" + + "github.com/google/go-containerregistry/pkg/name" + "github.com/google/go-containerregistry/pkg/v1/tarball" logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context" "github.com/trufflesecurity/trufflehog/v3/pkg/sources" ) @@ -184,3 +188,84 @@ func TestExtractRPMContent(t *testing.T) { expectedLength := 1822720 assert.Equal(t, expectedLength, len(string(content))) } + +func TestFoundKeyInDockerTar(t *testing.T) { + // URI of the Docker image to clone + imageURI := "ghcr.io/joeleonjr/getting-started-app-with-canary-token:main" + + var imageName name.Reference + imageName, err := name.NewTag(imageURI) + if err != nil { + t.Errorf("%s: error creating image name: %s", imageURI, err) + } + + // Pull the image + img, err := remote.Image(imageName) + if err != nil { + t.Errorf("%s: error pulling image: %s", imageURI, err) + } + + tempFile, err := os.CreateTemp("", "archive_test_docker_img.tar") + if err != nil { + t.Errorf("%s: error creating temporary file: %s", imageURI, err) + return + } + + defer os.Remove(tempFile.Name()) // Clean up the temporary file + + // Save the image as a tar file + err = tarball.WriteToFile(tempFile.Name(), imageName, img) + if err != nil { + t.Errorf("%s: error saving image as tar file: %s", imageURI, err) + return + } + + // ctx := logContext.AddLogger(context.Background()) + + // inputFile, err := os.Open(tempFile.Name()) + // if err != nil { + // t.Errorf("%s: error opening tar file: %s", imageURI, err) + // return + // } + // defer inputFile.Close() + + // reReader, err := diskbufferreader.New(inputFile) + // if err != nil { + // t.Errorf("%s: error creating re-readable reader: %s", imageURI, err) + // return + // } + // defer reReader.Close() + + // chunkSkel := &sources.Chunk{ + // SourceType: 1, + // SourceName: "filesystem", + // SourceID: 1, + // SourceMetadata: &source_metadatapb.MetaData{ + // Data: &source_metadatapb.MetaData_Filesystem{ + // Filesystem: &source_metadatapb.Filesystem{ + // File: sanitizer.UTF8(tempFile.Name()), + // }, + // }, + // }, + // Verify: true, + // } + + // chunksChan := make(chan *sources.Chunk, 1) + + // HandleFile(ctx, reReader, chunkSkel, chunksChan) + + // println("here") + // fmt.Printf("chunksChan: %v\n", chunksChan) + // fmt.Printf("chunksChan: %v\n", &chunksChan) + + // secret := "AKIA2OGYBAH6Q2PQJUGN" + + // // Read from the channel and validate the secrets. + // foundSecret := "" + // for chunkCh := range chunksChan { + // foundSecret += string(chunkCh.Data) + // } + + // assert.Contains(t, foundSecret, secret) + +}