Skip to content

Commit

Permalink
fix: Fix Windows support (aquasecurity#481)
Browse files Browse the repository at this point in the history
* fix: Fix Windows support

Signed-off-by: Liam Galvin <liam.galvin@aquasec.com>

* Update writable.go

* Update writable_windows.go
  • Loading branch information
liamg authored Apr 5, 2022
1 parent 362403d commit 96c2336
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
8 changes: 1 addition & 7 deletions pkg/scanners/terraform/parser/resolvers/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"io/fs"
"os"
"path/filepath"

"golang.org/x/sys/unix"
)

type cacheResolver struct{}
Expand Down Expand Up @@ -43,17 +41,13 @@ func cacheDir() string {
if err := os.MkdirAll(attempt, 0o755); err != nil {
continue
}
if writable(attempt) {
if isWritable(attempt) {
return attempt
}
}
return ""
}

func writable(path string) bool {
return unix.Access(path, unix.W_OK) == nil
}

func (r *cacheResolver) Resolve(_ context.Context, _ fs.FS, opt Options) (filesystem fs.FS, prefix string, downloadPath string, applies bool, err error) {
if !opt.AllowCache {
opt.Debug("Cache is disabled.")
Expand Down
36 changes: 36 additions & 0 deletions pkg/scanners/terraform/parser/resolvers/writable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build !windows
// +build !windows

package resolvers

import (
"os"
"syscall"
)

func isWritable(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}

if !info.IsDir() {
return false
}

// Check if the user bit is enabled in file permission
if info.Mode().Perm()&(1<<(uint(7))) == 0 {
return false
}

var stat syscall.Stat_t
if err = syscall.Stat(path, &stat); err != nil {
return false
}

if uint32(os.Geteuid()) != stat.Uid {
return false
}

return true
}
24 changes: 24 additions & 0 deletions pkg/scanners/terraform/parser/resolvers/writable_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package resolvers

import (
"os"
)

func isWritable(path string) bool {

info, err := os.Stat(path)
if err != nil {
return false
}

if !info.IsDir() {
return false
}

// Check if the user bit is enabled in file permission
if info.Mode().Perm()&(1<<(uint(7))) == 0 {
return false
}

return true
}

0 comments on commit 96c2336

Please sign in to comment.