From e36df40ba77e0688b47f29c1f339d360cde5f2fc Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Thu, 24 Aug 2023 09:44:46 +0200 Subject: [PATCH] pkg/types cleanup (#2398) * move function GetLineCountForFile from pkg/types to cscli * move ParseDuration from pkg/types to pkg/database * remove unused types.Profile, types.RemediationProfile --- cmd/crowdsec-cli/explain.go | 20 +++++++++++++++++-- pkg/database/alerts.go | 6 +++--- pkg/database/database.go | 8 ++++---- pkg/database/utils.go | 23 ++++++++++++++++++++++ pkg/types/profile.go | 25 ------------------------ pkg/types/utils.go | 38 ------------------------------------- 6 files changed, 48 insertions(+), 72 deletions(-) delete mode 100644 pkg/types/profile.go diff --git a/cmd/crowdsec-cli/explain.go b/cmd/crowdsec-cli/explain.go index d9b1ae31d..c130259f5 100644 --- a/cmd/crowdsec-cli/explain.go +++ b/cmd/crowdsec-cli/explain.go @@ -12,9 +12,22 @@ import ( "github.com/spf13/cobra" "github.com/crowdsecurity/crowdsec/pkg/hubtest" - "github.com/crowdsecurity/crowdsec/pkg/types" ) +func GetLineCountForFile(filepath string) (int, error) { + f, err := os.Open(filepath) + if err != nil { + return 0, err + } + defer f.Close() + lc := 0 + fs := bufio.NewScanner(f) + for fs.Scan() { + lc++ + } + return lc, nil +} + func runExplain(cmd *cobra.Command, args []string) error { flags := cmd.Flags() @@ -123,7 +136,10 @@ func runExplain(cmd *cobra.Command, args []string) error { return fmt.Errorf("unable to get absolute path of '%s', exiting", logFile) } dsn = fmt.Sprintf("file://%s", absolutePath) - lineCount := types.GetLineCountForFile(absolutePath) + lineCount, err := GetLineCountForFile(absolutePath) + if err != nil { + return err + } if lineCount > 100 { log.Warnf("log file contains %d lines. This may take lot of resources.", lineCount) } diff --git a/pkg/database/alerts.go b/pkg/database/alerts.go index afdf51688..d7b7db08f 100644 --- a/pkg/database/alerts.go +++ b/pkg/database/alerts.go @@ -809,7 +809,7 @@ func AlertPredicatesFromFilter(filter map[string][]string) ([]predicate.Alert, e return nil, errors.Wrapf(InvalidIPOrRange, "unable to convert '%s' to int: %s", value[0], err) } case "since": - duration, err := types.ParseDuration(value[0]) + duration, err := ParseDuration(value[0]) if err != nil { return nil, fmt.Errorf("while parsing duration: %w", err) } @@ -819,7 +819,7 @@ func AlertPredicatesFromFilter(filter map[string][]string) ([]predicate.Alert, e } predicates = append(predicates, alert.StartedAtGTE(since)) case "created_before": - duration, err := types.ParseDuration(value[0]) + duration, err := ParseDuration(value[0]) if err != nil { return nil, fmt.Errorf("while parsing duration: %w", err) } @@ -829,7 +829,7 @@ func AlertPredicatesFromFilter(filter map[string][]string) ([]predicate.Alert, e } predicates = append(predicates, alert.CreatedAtLTE(since)) case "until": - duration, err := types.ParseDuration(value[0]) + duration, err := ParseDuration(value[0]) if err != nil { return nil, fmt.Errorf("while parsing duration: %w", err) } diff --git a/pkg/database/database.go b/pkg/database/database.go index 2d8ddd51e..9b4d7e41e 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -119,14 +119,14 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched // Init & Start cronjob every hour for bouncers/agents if config.AgentsGC != nil { if config.AgentsGC.Cert != nil { - duration, err := types.ParseDuration(*config.AgentsGC.Cert) + duration, err := ParseDuration(*config.AgentsGC.Cert) if err != nil { return nil, fmt.Errorf("while parsing agents cert auto-delete duration: %w", err) } config.AgentsGC.CertDuration = &duration } if config.AgentsGC.LoginPassword != nil { - duration, err := types.ParseDuration(*config.AgentsGC.LoginPassword) + duration, err := ParseDuration(*config.AgentsGC.LoginPassword) if err != nil { return nil, fmt.Errorf("while parsing agents login/password auto-delete duration: %w", err) } @@ -138,14 +138,14 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched } if config.BouncersGC != nil { if config.BouncersGC.Cert != nil { - duration, err := types.ParseDuration(*config.BouncersGC.Cert) + duration, err := ParseDuration(*config.BouncersGC.Cert) if err != nil { return nil, fmt.Errorf("while parsing bouncers cert auto-delete duration: %w", err) } config.BouncersGC.CertDuration = &duration } if config.BouncersGC.Api != nil { - duration, err := types.ParseDuration(*config.BouncersGC.Api) + duration, err := ParseDuration(*config.BouncersGC.Api) if err != nil { return nil, fmt.Errorf("while parsing bouncers api auto-delete duration: %w", err) } diff --git a/pkg/database/utils.go b/pkg/database/utils.go index 5d6d4a442..2414e7027 100644 --- a/pkg/database/utils.go +++ b/pkg/database/utils.go @@ -4,6 +4,9 @@ import ( "encoding/binary" "fmt" "net" + "strconv" + "strings" + "time" ) func IP2Int(ip net.IP) uint32 { @@ -63,3 +66,23 @@ func GetIpsFromIpRange(host string) (int64, int64, error) { return ipStart, ipEnd, nil } + +func ParseDuration(d string) (time.Duration, error) { + durationStr := d + if strings.HasSuffix(d, "d") { + days := strings.Split(d, "d")[0] + if len(days) == 0 { + return 0, fmt.Errorf("'%s' can't be parsed as duration", d) + } + daysInt, err := strconv.Atoi(days) + if err != nil { + return 0, err + } + durationStr = strconv.Itoa(daysInt*24) + "h" + } + duration, err := time.ParseDuration(durationStr) + if err != nil { + return 0, err + } + return duration, nil +} diff --git a/pkg/types/profile.go b/pkg/types/profile.go deleted file mode 100644 index e8034210c..000000000 --- a/pkg/types/profile.go +++ /dev/null @@ -1,25 +0,0 @@ -package types - -import ( - "time" - - "github.com/antonmedv/expr/vm" -) - -/*Action profiles*/ -type RemediationProfile struct { - Apply bool - Ban bool - Slow bool - Captcha bool - Duration string - TimeDuration time.Duration -} -type Profile struct { - Profile string `yaml:"profile"` - Filter string `yaml:"filter"` - Remediation RemediationProfile `yaml:"remediation"` - RunTimeFilter *vm.Program - ApiPush *bool `yaml:"api"` - OutputConfigs []map[string]string `yaml:"outputs,omitempty"` -} diff --git a/pkg/types/utils.go b/pkg/types/utils.go index 0485db59e..8a49c9d32 100644 --- a/pkg/types/utils.go +++ b/pkg/types/utils.go @@ -1,12 +1,8 @@ package types import ( - "bufio" "fmt" - "os" "path/filepath" - "strconv" - "strings" "time" log "github.com/sirupsen/logrus" @@ -68,40 +64,6 @@ func ConfigureLogger(clog *log.Logger) error { return nil } -func ParseDuration(d string) (time.Duration, error) { - durationStr := d - if strings.HasSuffix(d, "d") { - days := strings.Split(d, "d")[0] - if len(days) == 0 { - return 0, fmt.Errorf("'%s' can't be parsed as duration", d) - } - daysInt, err := strconv.Atoi(days) - if err != nil { - return 0, err - } - durationStr = strconv.Itoa(daysInt*24) + "h" - } - duration, err := time.ParseDuration(durationStr) - if err != nil { - return 0, err - } - return duration, nil -} - func UtcNow() time.Time { return time.Now().UTC() } - -func GetLineCountForFile(filepath string) int { - f, err := os.Open(filepath) - if err != nil { - log.Fatalf("unable to open log file %s : %s", filepath, err) - } - defer f.Close() - lc := 0 - fs := bufio.NewScanner(f) - for fs.Scan() { - lc++ - } - return lc -}