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

Consistently set archived file permissions #41

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ IMPROVEMENTS:

* The provider is now compatible with Terraform v0.12, while retaining compatibility with prior versions.

BUG FIXES:

* Fix file permissions affecting zip contents and causing spurious diffs ([#34](https://github.com/terraform-providers/terraform-provider-archive/issues/34))

## 1.1.0 (July 30, 2018)

ENHANCEMENTS:
Expand Down
12 changes: 12 additions & 0 deletions archive/zip_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func (a *ZipArchiver) ArchiveFile(infilename string) error {
// fh.Modified alone isn't enough when using a zero value
fh.SetModTime(time.Time{})

// check if file is executable
info, err := os.Stat(infilename)
if err != nil {
return err
}
mode := info.Mode().Perm()
if mode&0100 == 0100 {
fh.SetMode(0555)
} else {
fh.SetMode(0444)
}

f, err := a.writer.CreateHeader(fh)
if err != nil {
return fmt.Errorf("error creating file inside archive: %s", err)
Expand Down
89 changes: 87 additions & 2 deletions archive/zip_archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,92 @@ func TestZipArchiver_File(t *testing.T) {
"test-file.txt": []byte("This is test content"),
})
}
func TestZipArchiver_FileModified(t *testing.T) {

func TestZipArchiver_FilePermissionsModified(t *testing.T) {
var (
zipFilePath = filepath.FromSlash("archive-file.zip")
toZipPath = filepath.FromSlash("./test-fixtures/test-file.txt")
)

var zip = func() {
archiver := NewZipArchiver(zipFilePath)
if err := archiver.ArchiveFile(toZipPath); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}

//set initial file permissions
if err := os.Chmod(toZipPath, 0600); err != nil {
t.Fatalf("unexpected error: %s", err)
}

zip()

expectedContents, err := ioutil.ReadFile(zipFilePath)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

//change file permissions
if err := os.Chmod(toZipPath, 0666); err != nil {
t.Fatalf("unexpected error: %s", err)
}

zip()

actualContents, err := ioutil.ReadFile(zipFilePath)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !bytes.Equal(expectedContents, actualContents) {
t.Fatalf("zip contents do not match, potentially a permission issue")
}
}

func TestZipArchiver_FileExecutablePermissionsModified(t *testing.T) {
var (
zipFilePath = filepath.FromSlash("archive-file.zip")
toZipPath = filepath.FromSlash("./test-fixtures/test-file.txt")
)

var zip = func() {
archiver := NewZipArchiver(zipFilePath)
if err := archiver.ArchiveFile(toZipPath); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}

//set initial file permissions
if err := os.Chmod(toZipPath, 0700); err != nil {
t.Fatalf("unexpected error: %s", err)
}

zip()

expectedContents, err := ioutil.ReadFile(zipFilePath)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

//make file executable
if err := os.Chmod(toZipPath, 0644); err != nil {
t.Fatalf("unexpected error: %s", err)
}

zip()

actualContents, err := ioutil.ReadFile(zipFilePath)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if bytes.Equal(expectedContents, actualContents) {
t.Fatalf("zip contents match, potentially a permission issue")
}
}

func TestZipArchiver_FileTimeModified(t *testing.T) {
var (
zipFilePath = filepath.FromSlash("archive-file.zip")
toZipPath = filepath.FromSlash("./test-fixtures/test-file.txt")
Expand Down Expand Up @@ -63,7 +148,7 @@ func TestZipArchiver_FileModified(t *testing.T) {

actualContents, err := ioutil.ReadFile(zipFilePath)
if err != nil {
t.Fatalf("unexpecte error: %s", err)
t.Fatalf("unexpected error: %s", err)
}

if !bytes.Equal(expectedContents, actualContents) {
Expand Down