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 all commits
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
125 changes: 87 additions & 38 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,55 +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()

extractedFilePath := filepath.Join(
tempDir,
file.Name,
)
if !strings.HasPrefix(extractedFilePath, filepath.Clean(tempDir)+string(os.PathSeparator)) {
return fmt.Errorf("invalid file path: %s", extractedFilePath)
}
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()

if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("untar failed: %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)
}
}

if strings.Contains(header.Name, "..") {
return fmt.Errorf("zipped file contians filepath (%s) that includes (..)", header.Name)
newDir := strings.Split(b.Program.Artifact, ".")
b.FilePath = filepath.Join(tempDir, newDir[0])
return nil
} else if strings.HasSuffix(b.Program.Artifact, ".tar.gz") {
archive, err := gzip.NewReader(buf)
if err != nil {
return fmt.Errorf("failed to create Gzip reader: %w", err)
}
defer archive.Close()
tarReader := tar.NewReader(archive)
tempDir := filepath.Join(conf.BPFDir, b.Program.Name, b.Program.Version)

for {
header, err := tarReader.Next()

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)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("untar failed: %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()
if strings.Contains(header.Name, "..") {
return fmt.Errorf("zipped file contians filepath (%s) that includes (..)", header.Name)
}

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)
}
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
}

newDir := strings.Split(b.Program.Artifact, ".")
b.FilePath = filepath.Join(tempDir, newDir[0])
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()

return nil
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 ")
}
}

// create rules file
Expand Down