Skip to content

Commit

Permalink
reduce log verbosity, minor CI fixes, lint (crowdsecurity#3157)
Browse files Browse the repository at this point in the history
* pkg/cwhub: redundant log messages

* CI: fixture output and elapsed time

* CI: preload only essential hub items

* report full version (including -rc2 etc.) with cscli hub update --debug

* lint
  • Loading branch information
mmetc authored Aug 1, 2024
1 parent 6f5d75c commit 136dba6
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 100 deletions.
6 changes: 4 additions & 2 deletions cmd/crowdsec-cli/require/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/mod/semver"

"github.com/crowdsecurity/go-cs-lib/version"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
)
Expand Down Expand Up @@ -74,13 +76,13 @@ func chooseBranch(ctx context.Context, cfg *csconfig.Config) string {
}

if csVersion == latest {
log.Debugf("Latest crowdsec version (%s), using hub branch 'master'", csVersion)
log.Debugf("Latest crowdsec version (%s), using hub branch 'master'", version.String())
return "master"
}

// if current version is greater than the latest we are in pre-release
if semver.Compare(csVersion, latest) == 1 {
log.Debugf("Your current crowdsec version seems to be a pre-release (%s), using hub branch 'master'", csVersion)
log.Debugf("Your current crowdsec version seems to be a pre-release (%s), using hub branch 'master'", version.String())
return "master"
}

Expand Down
31 changes: 16 additions & 15 deletions pkg/acquisition/modules/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudwatchacquisition

import (
"context"
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -111,7 +112,7 @@ func (cw *CloudwatchSource) UnmarshalConfig(yamlConfig []byte) error {
}

if len(cw.Config.GroupName) == 0 {
return fmt.Errorf("group_name is mandatory for CloudwatchSource")
return errors.New("group_name is mandatory for CloudwatchSource")
}

if cw.Config.Mode == "" {
Expand Down Expand Up @@ -189,7 +190,7 @@ func (cw *CloudwatchSource) Configure(yamlConfig []byte, logger *log.Entry, Metr
} else {
if cw.Config.AwsRegion == nil {
cw.logger.Errorf("aws_region is not specified, specify it or aws_config_dir")
return fmt.Errorf("aws_region is not specified, specify it or aws_config_dir")
return errors.New("aws_region is not specified, specify it or aws_config_dir")
}
os.Setenv("AWS_REGION", *cw.Config.AwsRegion)
}
Expand Down Expand Up @@ -228,7 +229,7 @@ func (cw *CloudwatchSource) newClient() error {
}

if sess == nil {
return fmt.Errorf("failed to create aws session")
return errors.New("failed to create aws session")
}
if v := os.Getenv("AWS_ENDPOINT_FORCE"); v != "" {
cw.logger.Debugf("[testing] overloading endpoint with %s", v)
Expand All @@ -237,7 +238,7 @@ func (cw *CloudwatchSource) newClient() error {
cw.cwClient = cloudwatchlogs.New(sess)
}
if cw.cwClient == nil {
return fmt.Errorf("failed to create cloudwatch client")
return errors.New("failed to create cloudwatch client")
}
return nil
}
Expand Down Expand Up @@ -516,7 +517,7 @@ func (cw *CloudwatchSource) TailLogStream(cfg *LogStreamTailConfig, outChan chan
}
case <-cfg.t.Dying():
cfg.logger.Infof("logstream tail stopping")
return fmt.Errorf("killed")
return errors.New("killed")
}
}
}
Expand All @@ -527,11 +528,11 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,
dsn = strings.TrimPrefix(dsn, cw.GetName()+"://")
args := strings.Split(dsn, "?")
if len(args) != 2 {
return fmt.Errorf("query is mandatory (at least start_date and end_date or backlog)")
return errors.New("query is mandatory (at least start_date and end_date or backlog)")
}
frags := strings.Split(args[0], ":")
if len(frags) != 2 {
return fmt.Errorf("cloudwatch path must contain group and stream : /my/group/name:stream/name")
return errors.New("cloudwatch path must contain group and stream : /my/group/name:stream/name")
}
cw.Config.GroupName = frags[0]
cw.Config.StreamName = &frags[1]
Expand All @@ -547,7 +548,7 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,
switch k {
case "log_level":
if len(v) != 1 {
return fmt.Errorf("expected zero or one value for 'log_level'")
return errors.New("expected zero or one value for 'log_level'")
}
lvl, err := log.ParseLevel(v[0])
if err != nil {
Expand All @@ -557,30 +558,30 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,

case "profile":
if len(v) != 1 {
return fmt.Errorf("expected zero or one value for 'profile'")
return errors.New("expected zero or one value for 'profile'")
}
awsprof := v[0]
cw.Config.AwsProfile = &awsprof
cw.logger.Debugf("profile set to '%s'", *cw.Config.AwsProfile)
case "start_date":
if len(v) != 1 {
return fmt.Errorf("expected zero or one argument for 'start_date'")
return errors.New("expected zero or one argument for 'start_date'")
}
//let's reuse our parser helper so that a ton of date formats are supported
strdate, startDate := parser.GenDateParse(v[0])
cw.logger.Debugf("parsed '%s' as '%s'", v[0], strdate)
cw.Config.StartTime = &startDate
case "end_date":
if len(v) != 1 {
return fmt.Errorf("expected zero or one argument for 'end_date'")
return errors.New("expected zero or one argument for 'end_date'")
}
//let's reuse our parser helper so that a ton of date formats are supported
strdate, endDate := parser.GenDateParse(v[0])
cw.logger.Debugf("parsed '%s' as '%s'", v[0], strdate)
cw.Config.EndTime = &endDate
case "backlog":
if len(v) != 1 {
return fmt.Errorf("expected zero or one argument for 'backlog'")
return errors.New("expected zero or one argument for 'backlog'")
}
//let's reuse our parser helper so that a ton of date formats are supported
duration, err := time.ParseDuration(v[0])
Expand All @@ -605,10 +606,10 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,
}

if cw.Config.StreamName == nil || cw.Config.GroupName == "" {
return fmt.Errorf("missing stream or group name")
return errors.New("missing stream or group name")
}
if cw.Config.StartTime == nil || cw.Config.EndTime == nil {
return fmt.Errorf("start_date and end_date or backlog are mandatory in one-shot mode")
return errors.New("start_date and end_date or backlog are mandatory in one-shot mode")
}

cw.Config.Mode = configuration.CAT_MODE
Expand Down Expand Up @@ -699,7 +700,7 @@ func cwLogToEvent(log *cloudwatchlogs.OutputLogEvent, cfg *LogStreamTailConfig)
l := types.Line{}
evt := types.Event{}
if log.Message == nil {
return evt, fmt.Errorf("nil message")
return evt, errors.New("nil message")
}
msg := *log.Message
if cfg.PrependCloudwatchTimestamp != nil && *cfg.PrependCloudwatchTimestamp {
Expand Down
17 changes: 9 additions & 8 deletions pkg/acquisition/modules/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dockeracquisition
import (
"bufio"
"context"
"errors"
"fmt"
"net/url"
"regexp"
Expand Down Expand Up @@ -88,11 +89,11 @@ func (d *DockerSource) UnmarshalConfig(yamlConfig []byte) error {
}

if len(d.Config.ContainerName) == 0 && len(d.Config.ContainerID) == 0 && len(d.Config.ContainerIDRegexp) == 0 && len(d.Config.ContainerNameRegexp) == 0 && !d.Config.UseContainerLabels {
return fmt.Errorf("no containers names or containers ID configuration provided")
return errors.New("no containers names or containers ID configuration provided")
}

if d.Config.UseContainerLabels && (len(d.Config.ContainerName) > 0 || len(d.Config.ContainerID) > 0 || len(d.Config.ContainerIDRegexp) > 0 || len(d.Config.ContainerNameRegexp) > 0) {
return fmt.Errorf("use_container_labels and container_name, container_id, container_id_regexp, container_name_regexp are mutually exclusive")
return errors.New("use_container_labels and container_name, container_id, container_id_regexp, container_name_regexp are mutually exclusive")
}

d.CheckIntervalDuration, err = time.ParseDuration(d.Config.CheckInterval)
Expand Down Expand Up @@ -225,7 +226,7 @@ func (d *DockerSource) ConfigureByDSN(dsn string, labels map[string]string, logg
switch k {
case "log_level":
if len(v) != 1 {
return fmt.Errorf("only one 'log_level' parameters is required, not many")
return errors.New("only one 'log_level' parameters is required, not many")
}
lvl, err := log.ParseLevel(v[0])
if err != nil {
Expand All @@ -234,17 +235,17 @@ func (d *DockerSource) ConfigureByDSN(dsn string, labels map[string]string, logg
d.logger.Logger.SetLevel(lvl)
case "until":
if len(v) != 1 {
return fmt.Errorf("only one 'until' parameters is required, not many")
return errors.New("only one 'until' parameters is required, not many")
}
d.containerLogsOptions.Until = v[0]
case "since":
if len(v) != 1 {
return fmt.Errorf("only one 'since' parameters is required, not many")
return errors.New("only one 'since' parameters is required, not many")
}
d.containerLogsOptions.Since = v[0]
case "follow_stdout":
if len(v) != 1 {
return fmt.Errorf("only one 'follow_stdout' parameters is required, not many")
return errors.New("only one 'follow_stdout' parameters is required, not many")
}
followStdout, err := strconv.ParseBool(v[0])
if err != nil {
Expand All @@ -254,7 +255,7 @@ func (d *DockerSource) ConfigureByDSN(dsn string, labels map[string]string, logg
d.containerLogsOptions.ShowStdout = followStdout
case "follow_stderr":
if len(v) != 1 {
return fmt.Errorf("only one 'follow_stderr' parameters is required, not many")
return errors.New("only one 'follow_stderr' parameters is required, not many")
}
followStdErr, err := strconv.ParseBool(v[0])
if err != nil {
Expand All @@ -264,7 +265,7 @@ func (d *DockerSource) ConfigureByDSN(dsn string, labels map[string]string, logg
d.containerLogsOptions.ShowStderr = followStdErr
case "docker_host":
if len(v) != 1 {
return fmt.Errorf("only one 'docker_host' parameters is required, not many")
return errors.New("only one 'docker_host' parameters is required, not many")
}
if err := client.WithHost(v[0])(dockerClient); err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions pkg/acquisition/modules/journalctl/journalctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package journalctlacquisition
import (
"bufio"
"context"
"errors"
"fmt"
"net/url"
"os/exec"
Expand Down Expand Up @@ -98,15 +99,15 @@ func (j *JournalCtlSource) runJournalCtl(out chan types.Event, t *tomb.Tomb) err
if stdoutscanner == nil {
cancel()
cmd.Wait()
return fmt.Errorf("failed to create stdout scanner")
return errors.New("failed to create stdout scanner")
}

stderrScanner := bufio.NewScanner(stderr)

if stderrScanner == nil {
cancel()
cmd.Wait()
return fmt.Errorf("failed to create stderr scanner")
return errors.New("failed to create stderr scanner")
}
t.Go(func() error {
return readLine(stdoutscanner, stdoutChan, errChan)
Expand Down Expand Up @@ -189,7 +190,7 @@ func (j *JournalCtlSource) UnmarshalConfig(yamlConfig []byte) error {
}

if len(j.config.Filters) == 0 {
return fmt.Errorf("journalctl_filter is required")
return errors.New("journalctl_filter is required")
}
j.args = append(args, j.config.Filters...)
j.src = fmt.Sprintf("journalctl-%s", strings.Join(j.config.Filters, "."))
Expand Down Expand Up @@ -223,7 +224,7 @@ func (j *JournalCtlSource) ConfigureByDSN(dsn string, labels map[string]string,

qs := strings.TrimPrefix(dsn, "journalctl://")
if len(qs) == 0 {
return fmt.Errorf("empty journalctl:// DSN")
return errors.New("empty journalctl:// DSN")
}

params, err := url.ParseQuery(qs)
Expand All @@ -236,7 +237,7 @@ func (j *JournalCtlSource) ConfigureByDSN(dsn string, labels map[string]string,
j.config.Filters = append(j.config.Filters, value...)
case "log_level":
if len(value) != 1 {
return fmt.Errorf("expected zero or one value for 'log_level'")
return errors.New("expected zero or one value for 'log_level'")
}
lvl, err := log.ParseLevel(value[0])
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (kc *KafkaConfiguration) NewReader(dialer *kafka.Dialer, logger *log.Entry)
ErrorLogger: kafka.LoggerFunc(logger.Errorf),
}
if kc.GroupID != "" && kc.Partition != 0 {
return &kafka.Reader{}, fmt.Errorf("cannot specify both group_id and partition")
return &kafka.Reader{}, errors.New("cannot specify both group_id and partition")
}
if kc.GroupID != "" {
rConf.GroupID = kc.GroupID
Expand Down
11 changes: 6 additions & 5 deletions pkg/acquisition/modules/kubernetesaudit/k8s_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetesauditacquisition
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -73,15 +74,15 @@ func (ka *KubernetesAuditSource) UnmarshalConfig(yamlConfig []byte) error {
ka.config = k8sConfig

if ka.config.ListenAddr == "" {
return fmt.Errorf("listen_addr cannot be empty")
return errors.New("listen_addr cannot be empty")
}

if ka.config.ListenPort == 0 {
return fmt.Errorf("listen_port cannot be empty")
return errors.New("listen_port cannot be empty")
}

if ka.config.WebhookPath == "" {
return fmt.Errorf("webhook_path cannot be empty")
return errors.New("webhook_path cannot be empty")
}

if ka.config.WebhookPath[0] != '/' {
Expand Down Expand Up @@ -119,7 +120,7 @@ func (ka *KubernetesAuditSource) Configure(config []byte, logger *log.Entry, Met
}

func (ka *KubernetesAuditSource) ConfigureByDSN(dsn string, labels map[string]string, logger *log.Entry, uuid string) error {
return fmt.Errorf("k8s-audit datasource does not support command-line acquisition")
return errors.New("k8s-audit datasource does not support command-line acquisition")
}

func (ka *KubernetesAuditSource) GetMode() string {
Expand All @@ -131,7 +132,7 @@ func (ka *KubernetesAuditSource) GetName() string {
}

func (ka *KubernetesAuditSource) OneShotAcquisition(out chan types.Event, t *tomb.Tomb) error {
return fmt.Errorf("k8s-audit datasource does not support one-shot acquisition")
return errors.New("k8s-audit datasource does not support one-shot acquisition")
}

func (ka *KubernetesAuditSource) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) error {
Expand Down
Loading

0 comments on commit 136dba6

Please sign in to comment.