Skip to content

Commit

Permalink
Added logging for extract and ignore list.
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanHnter committed Feb 16, 2024
1 parent 5757e06 commit 476190e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
22 changes: 16 additions & 6 deletions Client/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,29 @@ func (fc *IO) CopyDirToZip(src string, dstZipPath string) error {
}

// Unzip extracts a zip file's contents into a specified destination directory.
func (fc *IO) Unzip(srcZipPath, dstDir string) error {
func (fc *IO) Unzip(srcZipPath, dstDir string, ignoredPaths ...string) error {
r, err := zip.OpenReader(srcZipPath)
if err != nil {
return err
}
defer r.Close()

ignoredSet := make(map[string]struct{})
for _, ignoredPath := range ignoredPaths {
ignoredSet[filepath.Clean(ignoredPath)] = struct{}{}
}

for _, file := range r.File {
fPath := filepath.Join(dstDir, file.Name)
relativePath, err := filepath.Rel(dstDir, fPath)
if err != nil {
return err
}

// Ignore specified paths
if _, found := ignoredSet[relativePath]; found {
continue
}

// Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(fPath, filepath.Clean(dstDir)+string(os.PathSeparator)) {
Expand All @@ -100,6 +114,7 @@ func (fc *IO) Unzip(srcZipPath, dstDir string) error {
return err
}

fmt.Println("extracting :"+fPath)
outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return err
Expand All @@ -125,10 +140,5 @@ func (fc *IO) Unzip(srcZipPath, dstDir string) error {
}
}

// Once all files have been extracted, delete the source zip file
if err := os.Remove(srcZipPath); err != nil {
return fmt.Errorf("failed to delete the source zip file: %s, error: %w", srcZipPath, err)
}

return nil
}
4 changes: 3 additions & 1 deletion Client/kaniko/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (kd *KanikoDocker) ParseDockerImageTag(imageTag string) (shared.DockerImage

func (kd *KanikoDocker) BuildImage(options shared.BuildOptions, contextPath string, dockerfilePath string) {
fileHandler := io.New()
fmt.Println("Copying root")
fileHandler.CopyDirToZip("/", "/kaniko/root.zip")
stages := []string{}
if kanikoExecutor, ok := kd.Executor.(*KanikoExecutor); ok {
Expand Down Expand Up @@ -94,7 +95,8 @@ func (kd *KanikoDocker) BuildImage(options shared.BuildOptions, contextPath stri
} else {
fmt.Println("Executor is not of type *KanikoExecutor and does not have a Context field.")
}
fileHandler.Unzip("/kaniko/root.zip","/")
fmt.Println("Replacing root")
fileHandler.Unzip("/kaniko/root.zip","/","/kaniko","/azp")
fmt.Println("Kaniko build complete.")
}

Expand Down

0 comments on commit 476190e

Please sign in to comment.