Skip to content

Commit

Permalink
Add option to exclude commands and add default commands to be excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzvonimir committed Nov 11, 2024
1 parent 76de4cd commit 1a3cfa5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func collect(cmd *cobra.Command, _ []string) error {
intervalConfig,
auth,
config.AppConfig.ExcludeRegex,
config.AppConfig.ExcludeCommands,
procCol,
)

Expand Down
14 changes: 8 additions & 6 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Collector struct {
client *client.Client
logger zerolog.Logger
excludeRegex string
excludeCommands []string
collectionConfig collectionConfig
authConfig AuthConfig
protoAuthConfig *gen.Auth
Expand Down Expand Up @@ -70,7 +71,7 @@ type collectionConfig struct {
}

// NewCollector creates a new collector instance
func NewCollector(socketPath string, client *client.Client, logger zerolog.Logger, config IntervalConfig, auth AuthConfig, excludeRegex string, process process.SystemProcess) *Collector {
func NewCollector(socketPath string, client *client.Client, logger zerolog.Logger, config IntervalConfig, auth AuthConfig, excludeRegex string, excludeCommands []string, process process.SystemProcess) *Collector {

collector := &Collector{
socketPath: socketPath,
Expand All @@ -80,9 +81,10 @@ func NewCollector(socketPath string, client *client.Client, logger zerolog.Logge
ongoingCommands: make(map[string]Command),
process: process,
},
intervalConfig: config,
authConfig: auth,
excludeRegex: excludeRegex,
intervalConfig: config,
authConfig: auth,
excludeRegex: excludeRegex,
excludeCommands: excludeCommands,
}

if auth.TeamID != "" && auth.UserEmail != "" {
Expand Down Expand Up @@ -310,7 +312,7 @@ func (c *Collector) handleSocketCollection(con net.Conn) error {
}

func (c *Collector) handleStartCommand(parts []string) error {
if !IsCommandAcceptable(parts[1], c.excludeRegex) {
if !IsCommandAcceptable(parts[1], c.excludeRegex, c.excludeCommands) {
c.logger.Debug().Msg("Command is not acceptable")
return fmt.Errorf("command is not acceptable")
}
Expand Down Expand Up @@ -343,7 +345,7 @@ func (c *Collector) handleStartCommand(parts []string) error {

func (c *Collector) handleEndCommand(parts []string) error {

if !IsCommandAcceptable(parts[1], c.excludeRegex) {
if !IsCommandAcceptable(parts[1], c.excludeRegex, c.excludeCommands) {
c.logger.Debug().Msg("Command is not acceptable")
return fmt.Errorf("command is not acceptable")
}
Expand Down
16 changes: 15 additions & 1 deletion collector/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,27 @@ func ParseCommand(command string) string {
// IsCommandAcceptable checks if a command string matches a configured regex pattern.
// Commands that match the regex are considered unacceptable, and it returns false.
// If the regex is empty or the command does not match, it returns true.
func IsCommandAcceptable(command string, excludeRegex string) bool {
func IsCommandAcceptable(command string, excludeRegex string, excludeCommands []string) bool {
if excludeRegex != "" {
logging.Log.Debug().Msgf("Checking if command %s is acceptable for regex: %s", command, config.AppConfig.ExcludeRegex)
var pattern = regexp.MustCompile(excludeRegex)
return !pattern.MatchString(command)
}

if len(excludeCommands) > 0 {
logging.Log.Debug().Msgf("Checking if command %s is acceptable for commands: %v", command, excludeCommands)
acceptable := true
for _, excludeCommand := range excludeCommands {
var pattern = regexp.MustCompile(excludeCommand)
acceptable = !pattern.MatchString(command)
if !acceptable {
break
}
}

return acceptable
}

return true
}

Expand Down
4 changes: 4 additions & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ server_host = "pulse.devzero.dev:443"
# Default: (empty, meaning no processes are excluded)
# exclude_regex = ""

# Regular expression pattern to exclude certain commands from being collected.
# This can be used to omit sensitive or irrelevant processes from the data collection.
exclude_commands = ["^vim", "^nano", "^less", "^top", "^htop", "^ssh", "^scp", "^rsync", "^screen", "^tmux", "^dz", "^oda"]

# Whether to establish a secure connection for remote data collection.
# When enabled, data transmitted to and from the remote server will be encrypted.
# Requires 'cert_file' to be specified if true.
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Config struct {
CertFile string `mapstructure:"cert_file"`
// ExcludeRegex regular expression to exclude processes from collection
ExcludeRegex string `mapstructure:"exclude_regex"`
// ExcludeCommands regular expression to exclude commands from collection
ExcludeCommands []string `mapstructure:"exclude_commands"`
// ProcessCollectionType type of process collection to use, ps or psutil
ProcessCollectionType string `mapstructure:"process_collection_type"`
// TeamID is the team identifier for the workspace
Expand Down

0 comments on commit 1a3cfa5

Please sign in to comment.