Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 2, 2025
1 parent 8cd4a12 commit 5dabb29
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 44 deletions.
1 change: 0 additions & 1 deletion cmd/crowdsec-cli/clibouncer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func (cli *cliBouncers) listCSV(out io.Writer, bouncers ent.Bouncers) error {
func (cli *cliBouncers) List(ctx context.Context, out io.Writer, db *database.Client) error {
// XXX: must use the provided db object, the one in the struct might be nil
// (calling List directly skips the PersistentPreRunE)

bouncers, err := db.ListBouncers(ctx)
if err != nil {
return fmt.Errorf("unable to list bouncers: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/cliitem/inspect.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cliitem

import (
"fmt"
"encoding/json"
"fmt"
"os"
"path/filepath"

Expand Down
1 change: 0 additions & 1 deletion cmd/crowdsec-cli/cliitem/metrics_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
)


func appsecMetricsTable(out io.Writer, wantColor string, itemName string, metrics map[string]int) {
t := cstable.NewLight(out, wantColor).Writer
t.AppendHeader(table.Row{"Inband Hits", "Outband Hits"})
Expand Down
1 change: 0 additions & 1 deletion cmd/crowdsec-cli/climachine/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (cli *cliMachines) listCSV(out io.Writer, machines ent.Machines) error {
func (cli *cliMachines) List(ctx context.Context, out io.Writer, db *database.Client) error {
// XXX: must use the provided db object, the one in the struct might be nil
// (calling List directly skips the PersistentPreRunE)

machines, err := db.ListMachines(ctx)
if err != nil {
return fmt.Errorf("unable to list machines: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions cmd/crowdsec-cli/climetrics/statbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,20 @@ func (*statBouncer) extractRawMetrics(metrics []*ent.Metric) ([]bouncerMetricIte

if item.Name == nil {
logWarningOnce(warningsLogged, "missing 'name' field in metrics reported by "+bouncerName)

Check warning on line 179 in cmd/crowdsec-cli/climetrics/statbouncer.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/climetrics/statbouncer.go#L179

Added line #L179 was not covered by tests
// no continue - keep checking the rest
valid = false
}

if item.Unit == nil {
logWarningOnce(warningsLogged, "missing 'unit' field in metrics reported by "+bouncerName)

Check warning on line 186 in cmd/crowdsec-cli/climetrics/statbouncer.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/climetrics/statbouncer.go#L186

Added line #L186 was not covered by tests
valid = false
}

if item.Value == nil {
logWarningOnce(warningsLogged, "missing 'value' field in metrics reported by "+bouncerName)

Check warning on line 192 in cmd/crowdsec-cli/climetrics/statbouncer.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/climetrics/statbouncer.go#L192

Added line #L192 was not covered by tests
valid = false
}

Expand Down Expand Up @@ -449,6 +452,7 @@ func (s *statBouncer) Table(out io.Writer, wantColor string, noUnit bool, showEm

for _, bouncerName := range maptools.SortedKeys(s.aggOverOrigin) {
s.bouncerTable(out, bouncerName, wantColor, noUnit)

found = true
}

Expand Down
1 change: 1 addition & 0 deletions cmd/crowdsec-cli/climetrics/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func (ms metricStore) Format(out io.Writer, wantColor string, sections []string,
if err != nil {
return fmt.Errorf("failed to serialize metrics: %w", err)
}

fmt.Fprint(out, string(x))
default:
return fmt.Errorf("output format '%s' not supported for this command", outputFormat)
Expand Down
3 changes: 2 additions & 1 deletion pkg/acquisition/modules/appsec/appsec_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package appsecacquisition
import (
"testing"

"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
)

func TestAppsecRuleLoad(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion pkg/acquisition/modules/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"

"gopkg.in/tomb.v2"
"gopkg.in/yaml.v3"

Expand Down
8 changes: 5 additions & 3 deletions pkg/acquisition/modules/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
"testing"
"time"

"github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/crowdsecurity/go-cs-lib/cstest"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/tomb.v2"

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

"github.com/crowdsecurity/crowdsec/pkg/types"
)

const (
Expand Down Expand Up @@ -241,7 +243,7 @@ func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLev

func TestStreamingAcquisitionWrongHTTPMethod(t *testing.T) {
h := &HTTPSource{}
_, _, tomb:= SetupAndRunHTTPSource(t, h, []byte(`
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
source: http
listen_addr: 127.0.0.1:8080
path: /test
Expand Down
3 changes: 2 additions & 1 deletion pkg/alertcontext/alertcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/crowdsecurity/go-cs-lib/ptr"
)

func TestNewAlertContext(t *testing.T) {
Expand Down
52 changes: 25 additions & 27 deletions pkg/cwhub/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,37 @@ type Dependencies struct {

// a group of items of the same type
type itemgroup struct {
typeName string
itemNames []string
typeName string
itemNames []string
}

func (d Dependencies) byType() []itemgroup {
return []itemgroup{
{PARSERS, d.Parsers},
{POSTOVERFLOWS, d.PostOverflows},
{SCENARIOS, d.Scenarios},
{CONTEXTS, d.Contexts},
{APPSEC_CONFIGS, d.AppsecConfigs},
{APPSEC_RULES, d.AppsecRules},
{COLLECTIONS, d.Collections},
}
return []itemgroup{
{PARSERS, d.Parsers},
{POSTOVERFLOWS, d.PostOverflows},
{SCENARIOS, d.Scenarios},
{CONTEXTS, d.Contexts},
{APPSEC_CONFIGS, d.AppsecConfigs},
{APPSEC_RULES, d.AppsecRules},
{COLLECTIONS, d.Collections},
}
}

// SubItems iterates over the sub-items in the struct, excluding the ones that were not found in the hub.
func (d Dependencies) SubItems(hub *Hub) func(func(*Item) bool) {
return func(yield func(*Item) bool) {
for _, typeGroup := range d.byType() {
for _, name := range typeGroup.itemNames {
s := hub.GetItem(typeGroup.typeName, name)
if s == nil {
continue
}
if !yield(s) {
return
}
}
}
}
return func(yield func(*Item) bool) {
for _, typeGroup := range d.byType() {
for _, name := range typeGroup.itemNames {
s := hub.GetItem(typeGroup.typeName, name)
if s == nil {
continue
}
if !yield(s) {
return
}
}
}
}
}

// Item is created from an index file and enriched with local info.
Expand Down Expand Up @@ -285,11 +285,10 @@ func (i *Item) CurrentDependencies() Dependencies {
i.hub.logger.Warningf("can't parse dependencies for %s, using index", i.FQName())
return i.Dependencies
}

return d
}


func (i *Item) logMissingSubItems() {
if !i.HasSubItems() {
return
Expand Down Expand Up @@ -405,7 +404,6 @@ func (i *Item) SafeToRemoveDeps() ([]*Item, error) {
return ret, nil
}


// descendants returns a list of all (direct or indirect) dependencies of the item's current version.
func (i *Item) descendants() ([]*Item, error) {
var collectSubItems func(item *Item, visited map[*Item]bool, result *[]*Item) error
Expand Down
8 changes: 4 additions & 4 deletions pkg/hubops/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2"
isatty "github.com/mattn/go-isatty"
"github.com/fatih/color"
isatty "github.com/mattn/go-isatty"

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

Expand Down Expand Up @@ -59,21 +59,21 @@ func UniqueKey(c Command) string {
type ActionPlan struct {
// hold the list of Commands to be executed as part of the action plan.
// If a command is skipped (i.e. calling Prepare() returned false), it won't be included in the slice.
commands []Command
commands []Command

// Tracks unique commands
commandsTracker map[string]struct{}

// A reference to the Hub instance, required for dependency lookup.
hub *cwhub.Hub
hub *cwhub.Hub

// Indicates whether a reload of the CrowdSec service is required after executing the action plan.
ReloadNeeded bool
}

func NewActionPlan(hub *cwhub.Hub) *ActionPlan {
return &ActionPlan{
hub: hub,
hub: hub,
commandsTracker: make(map[string]struct{}),
}
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/hubtest/hubtest_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (t *HubTestItem) RunWithNucleiTemplate() error {
crowdsecDaemon.Start()

// wait for the appsec port to be available
if _, err := IsAlive(t.AppSecHost); err != nil {
if _, err = IsAlive(t.AppSecHost); err != nil {

Check warning on line 303 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L303

Added line #L303 was not covered by tests
crowdsecLog, err2 := os.ReadFile(crowdsecLogFile)
if err2 != nil {
log.Errorf("unable to read crowdsec log file '%s': %s", crowdsecLogFile, err)
Expand All @@ -319,7 +319,7 @@ func (t *HubTestItem) RunWithNucleiTemplate() error {
}

nucleiTargetHost := nucleiTargetParsedURL.Host
if _, err := IsAlive(nucleiTargetHost); err != nil {
if _, err = IsAlive(nucleiTargetHost); err != nil {

Check warning on line 322 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L322

Added line #L322 was not covered by tests
return fmt.Errorf("target is down: %w", err)
}

Expand Down Expand Up @@ -631,8 +631,11 @@ func (t *HubTestItem) Run(patternDir string) error {

if t.Config.LogFile != "" {
return t.RunWithLogFile(patternDir)
} else if t.Config.NucleiTemplate != "" {
}

Check warning on line 634 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L634

Added line #L634 was not covered by tests

if t.Config.NucleiTemplate != "" {

Check warning on line 636 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L636

Added line #L636 was not covered by tests
return t.RunWithNucleiTemplate()
}

return fmt.Errorf("log file or nuclei template must be set in '%s'", t.Name)
}

0 comments on commit 5dabb29

Please sign in to comment.