Skip to content

Commit

Permalink
add explicit error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 16, 2025
1 parent 20995ae commit 00ab15c
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 44 deletions.
21 changes: 3 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,23 @@ linters-settings:
# List of functions to exclude from checking, where each entry is a single function to exclude.
# See https://github.com/kisielk/errcheck#excluding-functions for details.
exclude-functions:
- (*bytes.Buffer).ReadFrom
- (*encoding/csv.Writer).Write
- (*encoding/csv.Writer).WriteAll
- io.Copy
- (io.Writer).Write
- io.WriteString
- (net/http.ResponseWriter).Write
- (*net/http.Server).Shutdown
- (*bytes.Buffer).ReadFrom # TODO:
- io.Copy # TODO:
- (net/http.ResponseWriter).Write # TODO:
- (*os/exec.Cmd).Start
- (*os/exec.Cmd).Wait
- (*os.File).Sync
- (*os.Process).Kill
- (*text/template.Template).ExecuteTemplate

- github.com/AlecAivazis/survey/v2.AskOne
- (github.com/bluele/gcache.Cache).Set
- (github.com/gin-gonic/gin.ResponseWriter).WriteString
- (*github.com/segmentio/kafka-go.Reader).SetOffsetAt
- (*github.com/spf13/cobra.Command).GenBashCompletion
- (*github.com/spf13/cobra.Command).GenFishCompletion
- (*github.com/spf13/cobra.Command).GenPowerShellCompletion
- (*github.com/spf13/cobra.Command).GenZshCompletion
- (*gopkg.in/tomb.v2.Tomb).Wait

- (*github.com/crowdsecurity/crowdsec/pkg/appsec.ReqDumpFilter).FilterArgs
- (*github.com/crowdsecurity/crowdsec/pkg/appsec.ReqDumpFilter).FilterBody
- (*github.com/crowdsecurity/crowdsec/pkg/appsec.ReqDumpFilter).FilterHeaders
- github.com/crowdsecurity/crowdsec/pkg/cache.SetKey
- github.com/crowdsecurity/crowdsec/pkg/exprhelpers.RegexpCacheInit
- (*github.com/crowdsecurity/crowdsec/pkg/hubops.ActionPlan).AddCommand
- (*github.com/crowdsecurity/crowdsec/pkg/longpollclient.LongPollClient).Stop
- github.com/crowdsecurity/go-cs-lib/csdaemon.Notify

gci:
sections:
Expand Down
8 changes: 5 additions & 3 deletions cmd/crowdsec-cli/climachine/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (cli *cliMachines) add(ctx context.Context, args []string, machinePassword
qs := &survey.Password{
Message: "Please provide a password for the machine:",
}
survey.AskOne(qs, &machinePassword)
if err := survey.AskOne(qs, &machinePassword); err != nil {
return err
}

Check warning on line 78 in cmd/crowdsec-cli/climachine/add.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/climachine/add.go#L76-L78

Added lines #L76 - L78 were not covered by tests
}

password := strfmt.Password(machinePassword)
Expand Down Expand Up @@ -147,9 +149,9 @@ cscli machines add -f- --auto > /tmp/mycreds.yaml`,
flags.VarP(&password, "password", "p", "machine password to login to the API")
flags.StringVarP(&dumpFile, "file", "f", "", "output file destination (defaults to "+csconfig.DefaultConfigPath("local_api_credentials.yaml")+")")
flags.StringVarP(&apiURL, "url", "u", "", "URL of the local API")
flags.BoolVarP(&interactive, "interactive", "i", false, "interfactive mode to enter the password")
flags.BoolVarP(&interactive, "interactive", "i", false, "interactive mode to enter the password")
flags.BoolVarP(&autoAdd, "auto", "a", false, "automatically generate password (and username if not provided)")
flags.BoolVar(&force, "force", false, "will force add the machine if it already exist")
flags.BoolVar(&force, "force", false, "will force add the machine if it already exists")

return cmd
}
8 changes: 4 additions & 4 deletions cmd/crowdsec-cli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func NewCompletionCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
_ = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
_ = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
_ = cmd.Root().GenPowerShellCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
_ = cmd.Root().GenFishCompletion(os.Stdout, true)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
}

if cConfig.Common != nil && cConfig.Common.Daemonize {
csdaemon.Notify(csdaemon.Ready, log.StandardLogger())
_ = csdaemon.Notify(csdaemon.Ready, log.StandardLogger())
// wait for signals
return HandleSignals(cConfig)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/acquisition/modules/appsec/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ func (w *AppsecSource) StreamingAcquisition(ctx context.Context, out chan types.
w.logger.Info("Shutting down Appsec server")
// xx let's clean up the appsec runners :)
appsec.AppsecRulesDetails = make(map[int]appsec.RulesDetails)
w.server.Shutdown(ctx)
if err := w.server.Shutdown(ctx); err != nil {
w.logger.Errorf("Error shutting down Appsec server: %s", err.Error())
}

Check warning on line 328 in pkg/acquisition/modules/appsec/appsec.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec.go#L326-L328

Added lines #L326 - L328 were not covered by tests
return nil
})

Expand Down
4 changes: 3 additions & 1 deletion pkg/acquisition/modules/kubernetesaudit/k8s_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ func (ka *KubernetesAuditSource) StreamingAcquisition(ctx context.Context, out c
})
<-t.Dying()
ka.logger.Infof("Stopping k8s-audit server on %s:%d%s", ka.config.ListenAddr, ka.config.ListenPort, ka.config.WebhookPath)
ka.server.Shutdown(ctx)
if err := ka.server.Shutdown(ctx); err != nil {
ka.logger.Errorf("Error shutting down k8s-audit server: %s", err.Error())
}

Check warning on line 159 in pkg/acquisition/modules/kubernetesaudit/k8s_audit.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/kubernetesaudit/k8s_audit.go#L158-L159

Added lines #L158 - L159 were not covered by tests

return nil
})
Expand Down
10 changes: 8 additions & 2 deletions pkg/cticlient/example/fire.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func main() {
})
}
}
csvWriter.Write(csvHeader)
csvWriter.WriteAll(allItems)

if err = csvWriter.Write(csvHeader); err != nil {
panic(err)

Check warning on line 62 in pkg/cticlient/example/fire.go

View check run for this annotation

Codecov / codecov/patch

pkg/cticlient/example/fire.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

if err = csvWriter.WriteAll(allItems); err != nil {
panic(err)

Check warning on line 66 in pkg/cticlient/example/fire.go

View check run for this annotation

Codecov / codecov/patch

pkg/cticlient/example/fire.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}
}
18 changes: 9 additions & 9 deletions pkg/exprhelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"github.com/umahmood/haversine"
"github.com/wasilibs/go-re2"

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

"github.com/crowdsecurity/crowdsec/pkg/cache"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
Expand Down Expand Up @@ -144,25 +142,27 @@ func RegexpCacheInit(filename string, CacheCfg types.DataSource) error {
}
// cache is enabled

if CacheCfg.Size == nil {
CacheCfg.Size = ptr.Of(50)
size := 50
if CacheCfg.Size != nil {
size = *CacheCfg.Size
}

gc := gcache.New(*CacheCfg.Size)
gc := gcache.New(size)

if CacheCfg.Strategy == nil {
CacheCfg.Strategy = ptr.Of("LRU")
strategy := "LRU"
if CacheCfg.Strategy != nil {
strategy = *CacheCfg.Strategy

Check warning on line 154 in pkg/exprhelpers/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/exprhelpers/helpers.go#L154

Added line #L154 was not covered by tests
}

switch *CacheCfg.Strategy {
switch strategy {
case "LRU":
gc = gc.LRU()
case "LFU":
gc = gc.LFU()
case "ARC":
gc = gc.ARC()
default:
return fmt.Errorf("unknown cache strategy '%s'", *CacheCfg.Strategy)
return fmt.Errorf("unknown cache strategy '%s'", strategy)

Check warning on line 165 in pkg/exprhelpers/helpers.go

View check run for this annotation

Codecov / codecov/patch

pkg/exprhelpers/helpers.go#L165

Added line #L165 was not covered by tests
}

if CacheCfg.TTL != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/leakybucket/manager_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ func LoadBucket(bucketFactory *BucketFactory, tomb *tomb.Tomb) error {
}

if data.Type == "regexp" { // cache only makes sense for regexp
exprhelpers.RegexpCacheInit(data.DestPath, *data)
if err := exprhelpers.RegexpCacheInit(data.DestPath, *data); err != nil {
bucketFactory.logger.Error(err.Error())
}

Check warning on line 463 in pkg/leakybucket/manager_load.go

View check run for this annotation

Codecov / codecov/patch

pkg/leakybucket/manager_load.go#L461-L463

Added lines #L461 - L463 were not covered by tests
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/parser/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
clog.Warningf("unexpected type %t (%v) while running '%s'", output, output, stash.Key)
continue
}
cache.SetKey(stash.Name, key, value, &stash.TTLVal)
if err = cache.SetKey(stash.Name, key, value, &stash.TTLVal); err != nil {
clog.Warningf("failed to store data in cache: %s", err.Error())
}

Check warning on line 358 in pkg/parser/node.go

View check run for this annotation

Codecov / codecov/patch

pkg/parser/node.go#L357-L358

Added lines #L357 - L358 were not covered by tests
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/parser/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ func LoadStages(stageFiles []Stagefile, pctx *UnixParserCtx, ectx EnricherCtx) (
for _, data := range node.Data {
err = exprhelpers.FileInit(pctx.DataFolder, data.DestPath, data.Type)
if err != nil {
log.Error(err)
log.Error(err.Error())

Check warning on line 117 in pkg/parser/stage.go

View check run for this annotation

Codecov / codecov/patch

pkg/parser/stage.go#L117

Added line #L117 was not covered by tests
}
if data.Type == "regexp" { //cache only makes sense for regexp
exprhelpers.RegexpCacheInit(data.DestPath, *data)
if err = exprhelpers.RegexpCacheInit(data.DestPath, *data); err != nil {
log.Error(err.Error())
}

Check warning on line 122 in pkg/parser/stage.go

View check run for this annotation

Codecov / codecov/patch

pkg/parser/stage.go#L120-L122

Added lines #L120 - L122 were not covered by tests
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/setup/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ func marshalAcquisDocuments(ads []AcquisDocument, toDir string) (string, error)
return "", fmt.Errorf("while writing to %s: %w", ad.AcquisFilename, err)
}

f.Sync()
if err = f.Sync(); err != nil {
return "", fmt.Errorf("while syncing %s: %w", ad.AcquisFilename, err)
}

Check warning on line 197 in pkg/setup/install.go

View check run for this annotation

Codecov / codecov/patch

pkg/setup/install.go#L196-L197

Added lines #L196 - L197 were not covered by tests

continue
}
Expand Down

0 comments on commit 00ab15c

Please sign in to comment.