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
This commit is contained in:
parent
86d9384954
commit
e36df40ba7
6 changed files with 48 additions and 72 deletions
|
@ -12,9 +12,22 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
"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 {
|
func runExplain(cmd *cobra.Command, args []string) error {
|
||||||
flags := cmd.Flags()
|
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)
|
return fmt.Errorf("unable to get absolute path of '%s', exiting", logFile)
|
||||||
}
|
}
|
||||||
dsn = fmt.Sprintf("file://%s", absolutePath)
|
dsn = fmt.Sprintf("file://%s", absolutePath)
|
||||||
lineCount := types.GetLineCountForFile(absolutePath)
|
lineCount, err := GetLineCountForFile(absolutePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if lineCount > 100 {
|
if lineCount > 100 {
|
||||||
log.Warnf("log file contains %d lines. This may take lot of resources.", lineCount)
|
log.Warnf("log file contains %d lines. This may take lot of resources.", lineCount)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
return nil, errors.Wrapf(InvalidIPOrRange, "unable to convert '%s' to int: %s", value[0], err)
|
||||||
}
|
}
|
||||||
case "since":
|
case "since":
|
||||||
duration, err := types.ParseDuration(value[0])
|
duration, err := ParseDuration(value[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing duration: %w", err)
|
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))
|
predicates = append(predicates, alert.StartedAtGTE(since))
|
||||||
case "created_before":
|
case "created_before":
|
||||||
duration, err := types.ParseDuration(value[0])
|
duration, err := ParseDuration(value[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing duration: %w", err)
|
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))
|
predicates = append(predicates, alert.CreatedAtLTE(since))
|
||||||
case "until":
|
case "until":
|
||||||
duration, err := types.ParseDuration(value[0])
|
duration, err := ParseDuration(value[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing duration: %w", err)
|
return nil, fmt.Errorf("while parsing duration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,14 +119,14 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched
|
||||||
// Init & Start cronjob every hour for bouncers/agents
|
// Init & Start cronjob every hour for bouncers/agents
|
||||||
if config.AgentsGC != nil {
|
if config.AgentsGC != nil {
|
||||||
if config.AgentsGC.Cert != nil {
|
if config.AgentsGC.Cert != nil {
|
||||||
duration, err := types.ParseDuration(*config.AgentsGC.Cert)
|
duration, err := ParseDuration(*config.AgentsGC.Cert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing agents cert auto-delete duration: %w", err)
|
return nil, fmt.Errorf("while parsing agents cert auto-delete duration: %w", err)
|
||||||
}
|
}
|
||||||
config.AgentsGC.CertDuration = &duration
|
config.AgentsGC.CertDuration = &duration
|
||||||
}
|
}
|
||||||
if config.AgentsGC.LoginPassword != nil {
|
if config.AgentsGC.LoginPassword != nil {
|
||||||
duration, err := types.ParseDuration(*config.AgentsGC.LoginPassword)
|
duration, err := ParseDuration(*config.AgentsGC.LoginPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing agents login/password auto-delete duration: %w", err)
|
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 != nil {
|
||||||
if config.BouncersGC.Cert != nil {
|
if config.BouncersGC.Cert != nil {
|
||||||
duration, err := types.ParseDuration(*config.BouncersGC.Cert)
|
duration, err := ParseDuration(*config.BouncersGC.Cert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing bouncers cert auto-delete duration: %w", err)
|
return nil, fmt.Errorf("while parsing bouncers cert auto-delete duration: %w", err)
|
||||||
}
|
}
|
||||||
config.BouncersGC.CertDuration = &duration
|
config.BouncersGC.CertDuration = &duration
|
||||||
}
|
}
|
||||||
if config.BouncersGC.Api != nil {
|
if config.BouncersGC.Api != nil {
|
||||||
duration, err := types.ParseDuration(*config.BouncersGC.Api)
|
duration, err := ParseDuration(*config.BouncersGC.Api)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while parsing bouncers api auto-delete duration: %w", err)
|
return nil, fmt.Errorf("while parsing bouncers api auto-delete duration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IP2Int(ip net.IP) uint32 {
|
func IP2Int(ip net.IP) uint32 {
|
||||||
|
@ -63,3 +66,23 @@ func GetIpsFromIpRange(host string) (int64, int64, error) {
|
||||||
|
|
||||||
return ipStart, ipEnd, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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"`
|
|
||||||
}
|
|
|
@ -1,12 +1,8 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -68,40 +64,6 @@ func ConfigureLogger(clog *log.Logger) error {
|
||||||
return nil
|
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 {
|
func UtcNow() time.Time {
|
||||||
return time.Now().UTC()
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue