Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding winzip artifact support #64

Merged
merged 4 commits into from
Jun 1, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 85 additions & 35 deletions kf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package kf

import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"container/ring"
Expand Down Expand Up @@ -556,54 +557,103 @@ func (b *BPF) GetArtifacts(conf *config.Config) error {
return fmt.Errorf("get request returned unexpected status code: %d (%s), %d was expected\n\tResponse Body: %s", resp.StatusCode, http.StatusText(resp.StatusCode), http.StatusOK, buf.Bytes())
}

archive, err := gzip.NewReader(buf)
if err != nil {
return fmt.Errorf("failed to create Gzip reader: %w", err)
}
defer archive.Close()
if strings.HasSuffix(b.Program.Artifact, ".zip") {
c := bytes.NewReader(buf.Bytes())
zipReader, err := zip.NewReader(c, int64(c.Len()))
if err != nil {
return fmt.Errorf("failed to create zip reader: %w", err)
}
tempDir := filepath.Join(conf.BPFDir, b.Program.Name, b.Program.Version)

tarReader := tar.NewReader(archive)
tempDir := filepath.Join(conf.BPFDir, b.Program.Name, b.Program.Version)
for _, file := range zipReader.File {

for {
header, err := tarReader.Next()
zippedFile, err := file.Open()
if err != nil {
return fmt.Errorf("unzip failed: %w", err)
}
defer zippedFile.Close()

if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("untar failed: %w", err)
}
extractedFilePath := filepath.Join(
tempDir,
file.Name,
)

if strings.Contains(header.Name, "..") {
return fmt.Errorf("zipped file contians filepath (%s) that includes (..)", header.Name)
}
if file.FileInfo().IsDir() {
os.MkdirAll(extractedFilePath, file.Mode())
} else {
outputFile, err := os.OpenFile(
extractedFilePath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
file.Mode(),
)
if err != nil {
return fmt.Errorf("unzip failed to create file: %w", err)
}
defer outputFile.Close()

fPath = filepath.Join(tempDir, header.Name)
info := header.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(fPath, info.Mode()); err != nil {
return fmt.Errorf("untar failed to create directories: %w", err)
buf := copyBufPool.Get().(*bytes.Buffer)
_, err = io.CopyBuffer(outputFile, zippedFile, buf.Bytes())
if err != nil {
return fmt.Errorf("GetArtifacts failed to copy files: %w", err)
}
copyBufPool.Put(buf)
}
continue
}
newDir := strings.Split(b.Program.Artifact, ".")
b.FilePath = filepath.Join(tempDir, newDir[0])
return nil

file, err := os.OpenFile(fPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
} else if strings.HasSuffix(b.Program.Artifact, ".tar.gz") {
archive, err := gzip.NewReader(buf)
if err != nil {
return fmt.Errorf("untar failed to create file: %w", err)
return fmt.Errorf("failed to create Gzip reader: %w", err)
}
defer file.Close()
defer archive.Close()
tarReader := tar.NewReader(archive)
tempDir := filepath.Join(conf.BPFDir, b.Program.Name, b.Program.Version)

buf := copyBufPool.Get().(*bytes.Buffer)
_, err = io.CopyBuffer(file, tarReader, buf.Bytes())
if err != nil {
return fmt.Errorf("GetArtifacts failed to copy files: %w", err)
}
copyBufPool.Put(buf)
}
for {
header, err := tarReader.Next()

newDir := strings.Split(b.Program.Artifact, ".")
b.FilePath = filepath.Join(tempDir, newDir[0])
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("untar failed: %w", err)
}

if strings.Contains(header.Name, "..") {
return fmt.Errorf("zipped file contians filepath (%s) that includes (..)", header.Name)
}

fPath = filepath.Join(tempDir, header.Name)
info := header.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(fPath, info.Mode()); err != nil {
return fmt.Errorf("untar failed to create directories: %w", err)
}
continue
}

file, err := os.OpenFile(fPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return fmt.Errorf("untar failed to create file: %w", err)
}
defer file.Close()

buf := copyBufPool.Get().(*bytes.Buffer)
_, err = io.CopyBuffer(file, tarReader, buf.Bytes())
if err != nil {
return fmt.Errorf("GetArtifacts failed to copy files: %w", err)
}
copyBufPool.Put(buf)
}

newDir := strings.Split(b.Program.Artifact, ".")
b.FilePath = filepath.Join(tempDir, newDir[0])
return nil
} else {
return fmt.Errorf("Unknown Artifact format ")
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is never reachable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed unreachable code
can you review it again ?

}

Expand Down