Unverified Commit 7cffd521 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Warn when artifacts/cache may use .git directory

parent 43a4e400
......@@ -61,6 +61,12 @@ func (c *ArtifactsDownloaderCommand) Execute(context *cli.Context) {
logrus.Fatalln(err)
}
paths, err := archives.ListZipFile(file.Name())
if err != nil {
logrus.Fatalln(err)
}
warnIfTryingToExtractGitDirectory(paths)
// Extract artifacts file
err = archives.ExtractZipFile(file.Name())
if err != nil {
......
......@@ -30,6 +30,8 @@ func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) {
pr, pw := io.Pipe()
defer pr.Close()
warnIfTryingToArchiveGitDirectory(c.sortedFiles())
// Create the archive
go func() {
err := archives.CreateZipArchive(pw, c.sortedFiles())
......
......@@ -70,6 +70,8 @@ func (c *CacheArchiverCommand) Execute(*cli.Context) {
logrus.Fatalln(err)
}
warnIfTryingToArchiveGitDirectory(c.sortedFiles())
// Check if list of files changed
if !c.isFileChanged(c.File) {
logrus.Infoln("Archive is up to date!")
......
......@@ -83,7 +83,13 @@ func (c *CacheExtractorCommand) Execute(context *cli.Context) {
}
}
err := archives.ExtractZipFile(c.File)
paths, err := archives.ListZipFile(c.File)
if err != nil {
logrus.Fatalln(err)
}
warnIfTryingToExtractGitDirectory(paths)
err = archives.ExtractZipFile(c.File)
if err != nil && !os.IsNotExist(err) {
logrus.Fatalln(err)
}
......
package helpers
import (
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
)
func doesPathsListContainGitDirectory(paths []string) bool {
for _, path := range paths {
parts := strings.Split(filepath.Clean(path), string(filepath.Separator))
if len(parts) > 0 && parts[0] == ".git" {
return true
}
}
return false
}
func warnIfTryingToArchiveGitDirectory(paths []string) {
if doesPathsListContainGitDirectory(paths) {
logrus.Warn("Part of .git directory is on the list of files to archive")
logrus.Warn("This may introduce unexpected problems")
}
}
func warnIfTryingToExtractGitDirectory(paths []string) {
if doesPathsListContainGitDirectory(paths) {
logrus.Warn("Part of .git directory is on the list of files to extract")
logrus.Warn("This may introduce unexpected problems")
}
}
package helpers
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDoesPathsListContainGitDirectory(t *testing.T) {
examples := []struct {
paths []string
unsafe bool
}{
{[]string{".git"}, true},
{[]string{".git/"}, true},
{[]string{"././././.git/"}, true},
{[]string{"././.git/.././.git/"}, true},
{[]string{".git/test"}, true},
{[]string{"./.git/test"}, true},
{[]string{"test/.git"}, false},
{[]string{"test/.git/test"}, false},
}
for id, example := range examples {
t.Run(fmt.Sprintf("example-%d", id), func(t *testing.T) {
unsafe := doesPathsListContainGitDirectory(example.paths)
assert.Equal(t, example.unsafe, unsafe)
})
}
}
......@@ -103,12 +103,38 @@ func ExtractZipArchive(archive *zip.Reader) error {
return nil
}
func ExtractZipFile(fileName string) error {
type ZipArchiveLister struct {
paths []string
}
func (a *ZipArchiveLister) list(archive *zip.Reader) error {
for _, file := range archive.File {
a.paths = append(a.paths, file.Name)
}
return nil
}
func workOnZipArchive(fileName string, handler func(archive *zip.Reader) error) error {
archive, err := zip.OpenReader(fileName)
if err != nil {
return err
}
defer archive.Close()
return ExtractZipArchive(&archive.Reader)
return handler(&archive.Reader)
}
func ExtractZipFile(fileName string) error {
return workOnZipArchive(fileName, ExtractZipArchive)
}
func ListZipFile(fileName string) ([]string, error) {
al := &ZipArchiveLister{}
err := workOnZipArchive(fileName, al.list)
if err != nil {
return []string{}, err
}
return al.paths, nil
}
......@@ -21,7 +21,7 @@ func writeArchive(t *testing.T, w io.Writer) {
io.WriteString(testFile, "test file")
}
func TestExtractZipFile(t *testing.T) {
func onTemporaryArchive(t *testing.T, handler func(t *testing.T, tempFile *os.File)) {
tempFile, err := ioutil.TempFile("", "archive")
if !assert.NoError(t, err) {
return
......@@ -31,24 +31,39 @@ func TestExtractZipFile(t *testing.T) {
writeArchive(t, tempFile)
tempFile.Close()
err = ExtractZipFile(tempFile.Name())
if !assert.NoError(t, err) {
return
}
handler(t, tempFile)
}
stat, err := os.Stat("temporary_file.txt")
assert.False(t, os.IsNotExist(err), "Expected temporary_file.txt to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
func TestExtractZipFile(t *testing.T) {
onTemporaryArchive(t, func(t *testing.T, tempFile *os.File) {
err := ExtractZipFile(tempFile.Name())
if !assert.NoError(t, err) {
return
}
if stat != nil {
defer os.Remove("temporary_file.txt")
assert.Equal(t, int64(9), stat.Size())
}
stat, err := os.Stat("temporary_file.txt")
assert.False(t, os.IsNotExist(err), "Expected temporary_file.txt to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
if stat != nil {
defer os.Remove("temporary_file.txt")
assert.Equal(t, int64(9), stat.Size())
}
})
}
func TestExtractZipFileNotFound(t *testing.T) {
err := ExtractZipFile("non_existing_zip_file.zip")
assert.Error(t, err)
}
func TestListZipFile(t *testing.T) {
onTemporaryArchive(t, func(t *testing.T, tempFile *os.File) {
paths, err := ListZipFile(tempFile.Name())
assert.NoError(t, err)
assert.NotEmpty(t, paths)
assert.Contains(t, paths, "temporary_file.txt")
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment