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

fix: SNMP MIBs: use the correct path when evaluating symlink #10748

Merged
merged 2 commits into from
Mar 11, 2022
Merged
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
17 changes: 11 additions & 6 deletions internal/snmp/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ var once sync.Once
var cache = make(map[string]bool)

type MibLoader interface {
loadModule(path string) error
// appendPath takes the path of a directory
appendPath(path string)

// loadModule takes the name of a file in one of the
// directories. Basename only, no relative or absolute path
loadModule(path string) error
}

type GosmiMibLoader struct{}
Expand Down Expand Up @@ -60,17 +64,18 @@ func LoadMibsFromPath(paths []string, log telegraf.Logger, loader MibLoader) err

for _, info := range modules {
if info.Mode()&os.ModeSymlink != 0 {
target, err := filepath.EvalSymlinks(path)
symlink := filepath.Join(path, info.Name())
target, err := filepath.EvalSymlinks(symlink)
if err != nil {
log.Warnf("Couldn't evaluate symbolic links for %v: %v", target, err)
log.Warnf("Couldn't evaluate symbolic links for %v: %v", symlink, err)
continue
}
info, err = os.Lstat(filepath.Join(path, target))
//replace symlink's info with the target's info
info, err = os.Lstat(target)
if err != nil {
log.Warnf("Couldn't stat target %v: %v", target, err)
continue
}
path = target
}
if info.Mode().IsRegular() {
err := loader.loadModule(info.Name())
Expand Down Expand Up @@ -113,7 +118,7 @@ func walkPaths(paths []string, log telegraf.Logger) ([]string, error) {
if info.Mode()&os.ModeSymlink != 0 {
target, err := filepath.EvalSymlinks(path)
if err != nil {
log.Warnf("Couldn't evaluate symbolic links for %v: %v", target, err)
log.Warnf("Couldn't evaluate symbolic links for %v: %v", path, err)
}
info, err = os.Lstat(target)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions internal/snmp/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ func TestFolderLookup(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping on windows")
}
var folders []string
var givenPath []string

tests := []struct {
name string
Expand All @@ -132,17 +130,23 @@ func TestFolderLookup(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
loader := TestingMibLoader{}

var givenPath []string
for _, paths := range tt.mibPath {
rootPath := filepath.Join(paths...)
givenPath = append(givenPath, rootPath)
}

err := LoadMibsFromPath(givenPath, testutil.Logger{}, &loader)
require.NoError(t, err)

var folders []string
for _, pathSlice := range tt.paths {
path := filepath.Join(pathSlice...)
folders = append(folders, path)
}
require.Equal(t, folders, loader.folders)

srebhan marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, tt.files, loader.files)
})
}
Expand Down