Skip to content

Commit

Permalink
update: make extract agnostic about artifact types again
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Susini <[email protected]>
  • Loading branch information
loresuso authored and poiana committed Dec 13, 2023
1 parent 36aa56c commit cda38ca
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/artifact/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/follower/follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions internal/utils/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package utils

import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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
}
43 changes: 5 additions & 38 deletions internal/utils/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
6 changes: 1 addition & 5 deletions pkg/driver/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit cda38ca

Please sign in to comment.