-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from build-security/feature/file_config
Feature/file config
- Loading branch information
Showing
8 changed files
with
245 additions
and
61 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,86 @@ | ||
package beater | ||
|
||
import ( | ||
"github.com/elastic/beats/v7/libbeat/logp" | ||
"os" | ||
"os/user" | ||
"strconv" | ||
"syscall" | ||
) | ||
|
||
// FileSystemFetcher implement the Fetcher interface | ||
// The FileSystemFetcher meant to fetch file/directories from the file system and ship it | ||
// to the Kubebeat | ||
type FileSystemFetcher struct { | ||
filesPaths []string // Files and directories paths for the fetcher to extract info from | ||
} | ||
|
||
const ( | ||
FileSystemInputType = "file-system" | ||
) | ||
|
||
// FileSystemResourceData represents a struct for a system resource data | ||
// This struct is being used by the fileSystemFetcher when | ||
type FileSystemResourceData struct { | ||
FileName string `json:"fileName"` | ||
FileMode string `json:"fileMode"` | ||
Gid string `json:"gid"` | ||
Uid string `json:"uid"` | ||
InputType string `json:"inputType"` | ||
Path string `json:"path"` | ||
} | ||
|
||
func NewFileFetcher(filesPaths []string) Fetcher { | ||
return &FileSystemFetcher{ | ||
filesPaths: filesPaths, | ||
} | ||
} | ||
|
||
func (f *FileSystemFetcher) Fetch() (interface{}, error) { | ||
results := make([]FileSystemResourceData, 0) | ||
|
||
for _, filePath := range f.filesPaths { | ||
info, err := os.Stat(filePath) | ||
|
||
// If errors occur during file system resource, just skip on the file and log the error | ||
if err != nil { | ||
logp.Err("Failed to fetch %s, error - %+v", filePath, err) | ||
continue | ||
} | ||
|
||
result := FromFileInfo(info, filePath) | ||
results = append(results, result) | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func (f *FileSystemFetcher) Stop() { | ||
} | ||
|
||
func FromFileInfo(info os.FileInfo, path string) FileSystemResourceData { | ||
|
||
if info == nil { | ||
return FileSystemResourceData{} | ||
} | ||
|
||
stat := info.Sys().(*syscall.Stat_t) | ||
uid := stat.Uid | ||
gid := stat.Gid | ||
u := strconv.FormatUint(uint64(uid), 10) | ||
g := strconv.FormatUint(uint64(gid), 10) | ||
usr, _ := user.LookupId(u) | ||
group, _ := user.LookupGroupId(g) | ||
mod := strconv.FormatUint(uint64(info.Mode().Perm()), 8) | ||
|
||
data := FileSystemResourceData{ | ||
FileName: info.Name(), | ||
FileMode: mod, | ||
Uid: usr.Name, | ||
Gid: group.Name, | ||
Path: path, | ||
InputType: FileSystemInputType, | ||
} | ||
|
||
return data | ||
} |
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,35 @@ | ||
package beater | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestFileFetcherFetchFilesFromFileSystem(t *testing.T) { | ||
|
||
dir, err := ioutil.TempDir("", "file-fetcher-test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer os.RemoveAll(dir) | ||
file := filepath.Join(dir, "file.txt") | ||
if err = ioutil.WriteFile(file, []byte("test txt\n"), 0600); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
filePaths := []string{file} | ||
fileFetcher := NewFileFetcher(filePaths) | ||
results, err := fileFetcher.Fetch() | ||
|
||
if err != nil { | ||
assert.Fail(t, "Fetcher did not work") | ||
} | ||
result := results.([]FileSystemResourceData)[0] | ||
|
||
assert.Equal(t, file, result.Path) | ||
assert.Equal(t, "600", result.FileMode) | ||
} |
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