Skip to content

Commit

Permalink
Checks for windows permissions (gomods#902)
Browse files Browse the repository at this point in the history
* Checks for windows permissions

* Fix bug

* fix test

* Update perms

* Fix perms
  • Loading branch information
manugupt1 authored and marpio committed Nov 20, 2018
1 parent 1984dbd commit c112126
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
12 changes: 10 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/BurntSushi/toml"
"github.com/gomods/athens/pkg/errors"
Expand Down Expand Up @@ -140,8 +141,15 @@ func checkFilePerms(files ...string) error {
continue
}

if fInfo.Mode() != 0600 {
return errors.E(op, f+" should have 0600 as permission")
if runtime.GOOS == "windows" {
if (fInfo.Mode() & 0600) != 0600 {
return errors.E(op, f+" should have 0600 as permission")
}
} else {
// Assume unix based system (MacOS and Linux)
if fInfo.Mode() != 0640 {
return errors.E(op, f+" should have 0640 as permission")
}
}
}

Expand Down
22 changes: 15 additions & 7 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,26 +297,34 @@ func restoreEnv(envVars map[string]string) {
}
}

func Test_checkFilePerms(t *testing.T) {
// TODO: os.Chmod(..) doesn't work on Windows as it does on Unix
// Skip for now
// issue: https://github.com/gomods/athens/issues/879
func invalidPerm() os.FileMode {
if runtime.GOOS == "windows" {
return 0200
}
return 0777
}

func correctPerm() os.FileMode {
if runtime.GOOS == "windows" {
t.SkipNow()
return 0600
}
return 0640
}

func Test_checkFilePerms(t *testing.T) {
f1, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
t.FailNow()
}
defer os.Remove(f1.Name())
err = os.Chmod(f1.Name(), 0777)
err = os.Chmod(f1.Name(), invalidPerm())

f2, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
t.FailNow()
}
defer os.Remove(f2.Name())
err = os.Chmod(f2.Name(), 0600)
err = os.Chmod(f2.Name(), correctPerm())

type args struct {
files []string
Expand Down

0 comments on commit c112126

Please sign in to comment.