Skip to content

Commit

Permalink
Support regexp exclusions (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramyahasini authored Oct 12, 2020
1 parent 4fca19a commit 3aac2f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
37 changes: 27 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -122,12 +123,13 @@ Initialises all the consumers along with pre-populating genericDiffPaths used by
func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (consumers pkg.BaseConsumers) {
fs := afero.NewOsFs()
var existingConsumersFiles = make(map[string]bool)
listOfRegexpsExcludes := c.compileRegex(c.Consumers.Excludes)

if c.Consumers.Root != "" {
fs = afero.NewBasePathFs(fs, c.Consumers.Root)
}
if c.Consumers.Access != "" {
if !c.isFileToBeExcluded(c.Consumers.Access, existingConsumersFiles) {
if !c.isFileToBeExcluded(c.Consumers.Access, existingConsumersFiles, listOfRegexpsExcludes) {
state := &pkg.AccessState{
AccessListener: pkg.NewAccessListener(
pkg.AccessFileOpt(fs, c.Consumers.Access, c.logger()),
Expand All @@ -138,7 +140,8 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
}
}
if c.Consumers.Users.Shadow != "" && c.Consumers.Users.Passwd != "" {
if !c.isFileToBeExcluded(c.Consumers.Users.Shadow, existingConsumersFiles) || !c.isFileToBeExcluded(c.Consumers.Users.Passwd, existingConsumersFiles) {
if !c.isFileToBeExcluded(c.Consumers.Users.Shadow, existingConsumersFiles, listOfRegexpsExcludes) ||
!c.isFileToBeExcluded(c.Consumers.Users.Passwd, existingConsumersFiles, listOfRegexpsExcludes) {
state := &pkg.UsersState{
UsersListener: pkg.NewUsersListener(func(l *pkg.UsersListener) {
l.Passwd = c.Consumers.Users.Passwd
Expand All @@ -155,7 +158,7 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
//get list of files to watch
genericDiffFiles := c.getListOfFiles(fs, c.Consumers.GenericDiff)
for _, genericDiffFile := range genericDiffFiles {
if !c.isFileToBeExcluded(genericDiffFile.File, existingConsumersFiles) {
if !c.isFileToBeExcluded(genericDiffFile.File, existingConsumersFiles, listOfRegexpsExcludes) {
state := &pkg.GenericDiffState{
GenericDiffListener: pkg.NewGenericDiffListener(
pkg.GenericDiffFileOpt(fs, genericDiffFile.File, c.logger()),
Expand All @@ -171,7 +174,7 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
if len(c.Consumers.Generic) > 0 {
genericFiles := c.getListOfFiles(fs, c.Consumers.Generic)
for _, genericFile := range genericFiles {
if !c.isFileToBeExcluded(genericFile.File, existingConsumersFiles) {
if !c.isFileToBeExcluded(genericFile.File, existingConsumersFiles, listOfRegexpsExcludes) {
genericFile := genericFile
state := &pkg.GenericState{
GenericListener: pkg.NewGenericListener(func(l *pkg.GenericListener) {
Expand All @@ -189,22 +192,36 @@ func (c Configuration) consumers(db *pkg.AgentDB, genericDiffPaths *[]string) (c
return consumers
}

// Gets list of regexp objects from regexp paths
func (c Configuration) compileRegex(listofPaths []string) []*regexp.Regexp {
logger := c.logger()
var regexpObjects []*regexp.Regexp
for _, path := range listofPaths {
reg, err := regexp.Compile(path)
if err != nil {
logger.Error().Err(err).Msgf("Error while compiling regex: %v", err)
continue
}
regexpObjects = append(regexpObjects, reg)
}
return regexpObjects
}

/* Checks if file belongs to exclusion list or is already assigned to a consumer and excludes it accordingly
true: if file needs to be excluded
false: otherwise
*/
func (c Configuration) isFileToBeExcluded(file string, existingConsumersFiles map[string]bool) bool {
func (c Configuration) isFileToBeExcluded(file string, existingConsumersFiles map[string]bool, listOfRegexpsExcludes []*regexp.Regexp) bool {
logger := c.logger()
isFileExcluded := false

for _, excludeFile := range c.Consumers.Excludes {
if strings.HasPrefix(file, excludeFile) {
for _, excludeRegexp := range listOfRegexpsExcludes {
matches := excludeRegexp.MatchString(file)
if matches {
logger.Debug().Msgf("File belongs to exclusion list, excluding from monitoring: %v", file)
isFileExcluded = true
break
}
}

return isFileExcluded || existingConsumersFiles[file]
}

Expand Down Expand Up @@ -396,7 +413,7 @@ func (c Configuration) watcher() (*pkg.Watcher, error) {
}
}
return pkg.NewWatcher(func(w *pkg.Watcher) {
w.Logger, w.Consumers, w.FIM, w.Database, w.Key, w.Excludes, w.GenericDiff = logger, consumers.Consumers(), fim, database, c.key, c.Consumers.Excludes, genericDiffPaths
w.Logger, w.Consumers, w.FIM, w.Database, w.Key, w.Excludes, w.GenericDiff = logger, consumers.Consumers(), fim, database, c.key, c.compileRegex(c.Consumers.Excludes), genericDiffPaths
}), nil
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"path"
"regexp"
"strings"
"sync"

Expand All @@ -20,7 +21,7 @@ type (
Consumers []Consumer
consumers Consumers
CloseChannels chan struct{}
Excludes []string
Excludes []*regexp.Regexp
GenericDiff []string
Metrics *Metrics
}
Expand Down Expand Up @@ -130,17 +131,21 @@ func (w *Watcher) addInode(event *Event, isdir bool) {
fullPath := path.Join(file, event.Path)
event.Path = fullPath

// Exclude file from monitoring if it belongs to exclusion list
for _, excludeFile := range w.Excludes {
if strings.HasPrefix(event.Path, excludeFile) {
w.Debug().Msgf("File belongs to exclusion list, excluding from monitoring: %v", event.Path)
/* Exclude file from monitoring if it belongs to exclusion list
w.Excludes is a list of compiled regexp objects
*/
for _, excludeRegexp := range w.Excludes {
matches := excludeRegexp.MatchString(event.Path)
if matches {
w.Debug().Msgf("File belongs to exclusion list, excluding from monitoring: %v", file)
return
}
}
var isGenericDiffFile bool
for _, genericDiffFile := range w.GenericDiff {
if strings.HasPrefix(event.Path, genericDiffFile) {
isGenericDiffFile = true
break
}
}
if isGenericDiffFile {
Expand Down

0 comments on commit 3aac2f3

Please sign in to comment.