Use mholt/archives instead of mholt/archiver (#3128)

* Use mholt/archives instead of mholt/archiver
* Fix validations
* Fix iszip function
* Fix directory
* Add better path sanitization
* ensure safe dir is passed
* Fix file permissions
* Fix config path
* Sanitize builder source path

---------

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Sanket Sudake
2025-01-08 11:00:54 +05:30
committed by GitHub
parent 54b67b5171
commit 4bce904c96
12 changed files with 721 additions and 155 deletions
+4 -4
View File
@@ -185,7 +185,7 @@ func CreateArchive(client cmd.Client, input cli.Input, includeFiles []string, no
return &archive, nil
}
archivePath, err := makeArchiveFile("", includeFiles, noZip)
archivePath, err := makeArchiveFile(input.Context(), "", includeFiles, noZip)
if err != nil {
return nil, err
}
@@ -200,7 +200,7 @@ func CreateArchive(client cmd.Client, input cli.Input, includeFiles []string, no
// returned as-is with no zipping. (This is used for compatibility
// with v1 envs.) noZip is IGNORED if there is more than one input
// file.
func makeArchiveFile(archiveNameHint string, archiveInput []string, noZip bool) (string, error) {
func makeArchiveFile(ctx context.Context, archiveNameHint string, archiveInput []string, noZip bool) (string, error) {
// Unique name for the archive
archiveFileName := archiveName(archiveNameHint, archiveInput) + ".zip"
@@ -219,7 +219,7 @@ func makeArchiveFile(archiveNameHint string, archiveInput []string, noZip bool)
}
// if it's an existing zip file OR we're not supposed to zip it, don't do anything
if match, _ := utils.IsZip(files[0]); match || noZip {
if match, _ := utils.IsZip(ctx, files[0]); match || noZip {
return files[0], nil
}
}
@@ -230,7 +230,7 @@ func makeArchiveFile(archiveNameHint string, archiveInput []string, noZip bool)
return "", errors.Wrap(err, "error create temporary archive directory")
}
archivePath, err := utils.MakeZipArchive(filepath.Join(tmpDir, archiveFileName), archiveInput...)
archivePath, err := utils.MakeZipArchiveWithGlobs(ctx, filepath.Join(tmpDir, archiveFileName), archiveInput...)
if err != nil {
return "", errors.Wrap(err, "create archive file")
}
+4 -7
View File
@@ -27,7 +27,6 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/go-git/go-git/v5"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -358,7 +357,7 @@ func applyArchives(input cli.Input, fclient cmd.Client, specDir string, fr *Fiss
// create archives locally and calculate checksums
for _, aus := range fr.ArchiveUploadSpecs {
ar, err := localArchiveFromSpec(specDir, &aus)
ar, err := localArchiveFromSpec(input.Context(), specDir, &aus)
if err != nil {
return err
}
@@ -501,7 +500,7 @@ func applyResources(input cli.Input, fclient cmd.Client, specDir string, fr *Fis
// localArchiveFromSpec creates an archive on the local filesystem from the given spec,
// and returns its path and checksum.
func localArchiveFromSpec(specDir string, aus *spectypes.ArchiveUploadSpec) (*fv1.Archive, error) {
func localArchiveFromSpec(ctx context.Context, specDir string, aus *spectypes.ArchiveUploadSpec) (*fv1.Archive, error) {
// get root dir
var rootDir string
@@ -518,7 +517,7 @@ func localArchiveFromSpec(specDir string, aus *spectypes.ArchiveUploadSpec) (*fv
files := make([]string, 0)
// checking if file is a zip
if match, _ := utils.IsZip(aus.IncludeGlobs[0]); match && len(aus.IncludeGlobs) == 1 {
if match, _ := utils.IsZip(ctx, aus.IncludeGlobs[0]); match && len(aus.IncludeGlobs) == 1 {
files = append(files, aus.IncludeGlobs[0])
} else {
for _, relativeGlob := range aus.IncludeGlobs {
@@ -560,9 +559,7 @@ func localArchiveFromSpec(specDir string, aus *spectypes.ArchiveUploadSpec) (*fv
}
archiveFileName = archiveFile.Name()
// This instance is required to allow overwriting and not changing DefaultZip
zipOverwrite := archiver.Zip{OverwriteExisting: true}
err = zipOverwrite.Archive(files, archiveFileName)
_, err = utils.MakeZipArchiveWithGlobs(ctx, archiveFileName, files...)
if err != nil {
return nil, err
}
+1 -1
View File
@@ -176,7 +176,7 @@ func (opts *DestroySubCommand) insertNSToResource(input cli.Input, fr *FissionRe
return result.ErrorOrNil()
}
func deleteResources(ctx context.Context, fclient cmd.Client, fr *FissionResources, forceDelete bool) error {
func deleteResources(ctx context.Context, fclient cmd.Client, fr *FissionResources, _ bool) error {
var err error
+1 -1
View File
@@ -142,7 +142,7 @@ func (opts *DumpSubCommand) do(input cli.Input) error {
if !nozip {
defer os.RemoveAll(tempDir)
path := filepath.Join(outputDir, fmt.Sprintf("%v.zip", dumpName))
_, err := utils.MakeZipArchive(path, tempDir)
_, err := utils.MakeZipArchiveWithGlobs(input.Context(), path, tempDir)
if err != nil {
fmt.Printf("Error creating archive for dump files: %v", err)
return err