From cda38ca8758ef2590b4703a139b570d7efa7b6bc Mon Sep 17 00:00:00 2001 From: Lorenzo Susini Date: Wed, 13 Dec 2023 12:20:32 +0000 Subject: [PATCH] update: make extract agnostic about artifact types again Signed-off-by: Lorenzo Susini --- cmd/artifact/install/install.go | 2 +- internal/follower/follower.go | 2 +- internal/utils/compress_test.go | 30 +++++++++++++++++++++++ internal/utils/extract.go | 43 ++++----------------------------- pkg/driver/distro/distro.go | 6 +---- 5 files changed, 38 insertions(+), 45 deletions(-) diff --git a/cmd/artifact/install/install.go b/cmd/artifact/install/install.go index 47538b4d..b8f49a26 100644 --- a/cmd/artifact/install/install.go +++ b/cmd/artifact/install/install.go @@ -337,7 +337,7 @@ func (o *artifactInstallOptions) RunArtifactInstall(ctx context.Context, args [] return err } // Extract artifact and move it to its destination directory - _, err = utils.ExtractTarGz(f, destDir, result.Type, 0) + _, err = utils.ExtractTarGz(f, destDir, 0) if err != nil { return fmt.Errorf("cannot extract %q to %q: %w", result.Filename, destDir, err) } diff --git a/internal/follower/follower.go b/internal/follower/follower.go index 7d63e6b1..398c6fcf 100644 --- a/internal/follower/follower.go +++ b/internal/follower/follower.go @@ -291,7 +291,7 @@ func (f *Follower) pull(ctx context.Context) (filePaths []string, res *oci.Regis } // Extract artifact and move it to its destination directory - filePaths, err = utils.ExtractTarGz(file, f.tmpDir, res.Type, 0) + filePaths, err = utils.ExtractTarGz(file, f.tmpDir, 0) if err != nil { return filePaths, res, fmt.Errorf("unable to extract %q to %q: %w", res.Filename, f.tmpDir, err) } diff --git a/internal/utils/compress_test.go b/internal/utils/compress_test.go index e82aaafd..38a49725 100644 --- a/internal/utils/compress_test.go +++ b/internal/utils/compress_test.go @@ -16,7 +16,11 @@ package utils import ( + "archive/tar" + "compress/gzip" + "errors" "fmt" + "io" "os" "path/filepath" "testing" @@ -114,3 +118,29 @@ func TestCreateTarGzArchiveDir(t *testing.T) { t.Errorf("Expected file2, got %s", p) } } + +func listHeaders(gzipStream io.Reader) ([]string, error) { + uncompressedStream, err := gzip.NewReader(gzipStream) + if err != nil { + return nil, err + } + + tarReader := tar.NewReader(uncompressedStream) + + var files []string + for { + header, err := tarReader.Next() + + if errors.Is(err, io.EOF) { + break + } + + if err != nil { + return nil, err + } + + files = append(files, header.Name) + } + + return files, nil +} diff --git a/internal/utils/extract.go b/internal/utils/extract.go index 5f18f0d0..92e31421 100644 --- a/internal/utils/extract.go +++ b/internal/utils/extract.go @@ -24,13 +24,11 @@ import ( "os" "path/filepath" "strings" - - "github.com/falcosecurity/falcoctl/pkg/oci" ) // ExtractTarGz extracts a *.tar.gz compressed archive and moves its content to destDir. // Returns a slice containing the full path of the extracted files. -func ExtractTarGz(gzipStream io.Reader, destDir string, artifactType oci.ArtifactType, stripPathComponents int) ([]string, error) { +func ExtractTarGz(gzipStream io.Reader, destDir string, stripPathComponents int) ([]string, error) { var files []string uncompressedStream, err := gzip.NewReader(gzipStream) @@ -59,16 +57,11 @@ func ExtractTarGz(gzipStream io.Reader, destDir string, artifactType oci.Artifac switch header.Typeflag { case tar.TypeDir: - if artifactType == oci.Plugin || artifactType == oci.Rulesfile { - return nil, fmt.Errorf("unexepected dir inside the archive, "+ - "expected to find only files without any tree structure for %q artifacts", artifactType.String()) - } else if artifactType == oci.Asset { - d := filepath.Join(destDir, strippedName) - if err = os.Mkdir(filepath.Clean(d), 0o750); err != nil { - return nil, err - } - files = append(files, d) + d := filepath.Join(destDir, strippedName) + if err = os.Mkdir(filepath.Clean(d), 0o750); err != nil { + return nil, err } + files = append(files, d) case tar.TypeReg: f := filepath.Join(destDir, strippedName) outFile, err := os.Create(filepath.Clean(f)) @@ -105,29 +98,3 @@ func stripComponents(headerName string, stripComponents int) string { } return filepath.Clean(strings.Join(names[stripComponents:], "/")) } - -func listHeaders(gzipStream io.Reader) ([]string, error) { - uncompressedStream, err := gzip.NewReader(gzipStream) - if err != nil { - return nil, err - } - - tarReader := tar.NewReader(uncompressedStream) - - var files []string - for { - header, err := tarReader.Next() - - if errors.Is(err, io.EOF) { - break - } - - if err != nil { - return nil, err - } - - files = append(files, header.Name) - } - - return files, nil -} diff --git a/pkg/driver/distro/distro.go b/pkg/driver/distro/distro.go index 2114b2c2..17ac5bf8 100644 --- a/pkg/driver/distro/distro.go +++ b/pkg/driver/distro/distro.go @@ -35,7 +35,6 @@ import ( "github.com/falcosecurity/falcoctl/internal/utils" drivertype "github.com/falcosecurity/falcoctl/pkg/driver/type" - "github.com/falcosecurity/falcoctl/pkg/oci" "github.com/falcosecurity/falcoctl/pkg/output" ) @@ -320,10 +319,7 @@ func downloadKernelSrc(ctx context.Context, return env, err } - // todo(loresuso,fededp): use oci.Asset as the artifact type - // oci.Asset is generic enough to be used for kernel sources but we might want to find - // a better way to handle this. - _, err = utils.ExtractTarGz(resp.Body, fullKernelDir, oci.Asset, stripComponents) + _, err = utils.ExtractTarGz(resp.Body, fullKernelDir, stripComponents) if err != nil { return env, err }