Skip to content

Commit

Permalink
Fix collector_whitelist feature (#284)
Browse files Browse the repository at this point in the history
* Fix collector_whitelist feature

* Fix collector_whitelist feature

The previous fix for #279 introduced an error,
because result.Path was not always set.

Instead of failing on non-existant files, still perform the PathMatch
but report them in result.DoesExist.

Broken symlinks are always reported as an error, because we cannot
resolve their actual path using EvalSymlinks.

Add some tests.

Fixes #279
  • Loading branch information
Marius Sturm authored Sep 11, 2018
1 parent b80ee3c commit c5a6eda
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
25 changes: 16 additions & 9 deletions common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,32 @@ func Sprintf(format string, values ...interface{}) (string, error) {
}

type PathMatchResult struct {
Path string
Match bool
IsLink bool
Path string
Match bool
IsLink bool
DoesExist bool
}

func PathMatch(path string, patternList []string) (PathMatchResult, error) {
result := PathMatchResult{}
if _, err := os.Stat(path); os.IsExist(err) {
result := PathMatchResult{Path: path, DoesExist: true}

if _, err := os.Lstat(path); err == nil {
resolvedPath, err := filepath.EvalSymlinks(path)
if err != nil {
// error out on broken symlinks, because we cannot resolve
// their path with EvalSymlinks
result.DoesExist = false
return result, err
} else {
result.Path = resolvedPath
if resolvedPath != path {
result.IsLink = true
result.Path = resolvedPath
}
}
} else {
result.DoesExist = false
}

if result.Path != path {
result.IsLink = true
}
for _, pattern := range patternList {
match, err := filepath.Match(pattern, result.Path)
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions common/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -98,3 +99,74 @@ func TestEncloseWithoutData(t *testing.T) {
t.Fail()
}
}

func TestPathMatch(t *testing.T) {
dir, err := ioutil.TempDir("", "test-path-match")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

execfile := filepath.Join(dir, "myexec")
patternList := []string{"/usr/bin/moo", "/sbin/bar"}
result, err := PathMatch(execfile, patternList)
if err != nil || result.Match {
t.Fatalf("'%s' should not match patternList %v err '%v' result '%v'", execfile, patternList, err, result)
}
if result.DoesExist {
t.Fatalf("DoesExist should not be set")
}

os.Create(execfile)
patternList = []string{"/usr/bin/moo", "/sbin/bar", execfile}
result, err = PathMatch(execfile, patternList)
if err != nil || !result.Match {
t.Fatalf("'%s' should match patternList %v err '%v' result '%v'", execfile, patternList, err, result)
}
if !result.DoesExist {
t.Fatalf("DoesExist should be set")
}

patternList = []string{"/usr/bin/moo", "/sbin/bar", dir + "/*"}
result, err = PathMatch(execfile, patternList)
if err != nil || !result.Match {
t.Fatalf("'%s' should match globbing patternList %v err '%v' result '%v'",
execfile, patternList, err, result)
}

}

func TestPathMatchSymlink(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
}

dir, err := ioutil.TempDir("", "test-path-match")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

execfile := filepath.Join(dir, "myexec")
symlink := filepath.Join(dir, "symlink")
if err = os.Symlink(execfile, symlink); err != nil {
t.Fatal()
}
patternList := []string{"moo", "bar", execfile}
result, err := PathMatch(symlink, patternList)
if err == nil {
t.Fatalf("broken symlinks should report an error")
}

os.Create(execfile)
result, err = PathMatch(symlink, patternList)
if err != nil || !result.Match {
t.Fatalf("'%s' should match patternList %v err '%v' result '%v'", execfile, patternList, err, result)
}
if !result.IsLink {
t.Fatalf("result.IsLink is false")
}
if result.Path != execfile {
t.Fatalf("result.Path did not contain resolved symlink")
}
}

0 comments on commit c5a6eda

Please sign in to comment.