-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added symlink processing to linux file accessor (#3173)
- Loading branch information
Showing
12 changed files
with
155 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// +build linux | ||
|
||
package file | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/Velocidex/ordereddict" | ||
"github.com/alecthomas/assert" | ||
"github.com/sebdah/goldie" | ||
"github.com/stretchr/testify/suite" | ||
"www.velocidex.com/golang/velociraptor/accessors" | ||
"www.velocidex.com/golang/velociraptor/config" | ||
"www.velocidex.com/golang/velociraptor/glob" | ||
"www.velocidex.com/golang/velociraptor/json" | ||
vql_subsystem "www.velocidex.com/golang/velociraptor/vql" | ||
"www.velocidex.com/golang/velociraptor/vql/acl_managers" | ||
) | ||
|
||
type AccessorLinuxTestSuite struct { | ||
suite.Suite | ||
tmpdir string | ||
} | ||
|
||
func (self *AccessorLinuxTestSuite) TestLinuxSymlinks() { | ||
tmpdir, err := ioutil.TempDir("", "accessor_test") | ||
assert.NoError(self.T(), err) | ||
|
||
// Create two symlinks. | ||
// tmp/second_bin/ -> tmp/zbin | ||
// tmp/zbin -> /bin/ | ||
|
||
err = os.Symlink("/bin", filepath.Join(tmpdir, "zbin")) | ||
assert.NoError(self.T(), err) | ||
|
||
err = os.Symlink(filepath.Join(tmpdir, "zbin"), | ||
filepath.Join(tmpdir, "second_bin")) | ||
assert.NoError(self.T(), err) | ||
|
||
// Create a symlink cycle: | ||
// tmp/subdir is a directory | ||
// tmp/sym1 -> tmp/subdir | ||
// tmp/subdir/sym2 -> tmp | ||
|
||
dirname := filepath.Join(tmpdir, "subdir") | ||
err = os.Mkdir(dirname, 0777) | ||
assert.NoError(self.T(), err) | ||
|
||
err = os.Mkdir(filepath.Join(dirname, "ls"), 0777) | ||
assert.NoError(self.T(), err) | ||
|
||
err = os.Symlink(dirname, filepath.Join(tmpdir, "sym1")) | ||
assert.NoError(self.T(), err) | ||
|
||
err = os.Symlink(tmpdir, filepath.Join(dirname, "sym2")) | ||
assert.NoError(self.T(), err) | ||
|
||
scope := vql_subsystem.MakeScope().AppendVars(ordereddict.NewDict(). | ||
Set(vql_subsystem.ACL_MANAGER_VAR, acl_managers.NullACLManager{})) | ||
scope.SetLogger(log.New(os.Stderr, " ", 0)) | ||
|
||
glob_path, _ := accessors.NewLinuxOSPath("/**/ls") | ||
tmp_path, _ := accessors.NewLinuxOSPath(tmpdir) | ||
|
||
options := glob.GlobOptions{ | ||
DoNotFollowSymlinks: false, | ||
} | ||
globber := glob.NewGlobber().WithOptions(options) | ||
|
||
globber.Add(glob_path) | ||
|
||
accessor, err := accessors.GetAccessor("file", scope) | ||
assert.NoError(self.T(), err) | ||
|
||
config_obj := config.GetDefaultConfig() | ||
hits := []string{} | ||
for hit := range globber.ExpandWithContext( | ||
context.Background(), scope, | ||
config_obj, tmp_path, accessor) { | ||
hits = append(hits, hit.OSPath().TrimComponents( | ||
tmp_path.Components...).String()) | ||
} | ||
|
||
sort.Strings(hits) | ||
|
||
goldie.Assert(self.T(), "TestLinuxSymlinks", json.MustMarshalIndent(hits)) | ||
} | ||
|
||
// Test Linux specific File accessor. | ||
func TestFileLinux(t *testing.T) { | ||
suite.Run(t, &AccessorLinuxTestSuite{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
"/second_bin/ls", | ||
"/subdir/ls", | ||
"/subdir/sym2/subdir/ls", | ||
"/sym1/ls" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters