Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint/deep-exit: avoid log.Fatal #3360

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,6 @@ issues:
path: "cmd/crowdsec-cli/main.go"
text: "deep-exit: .*"

- linters:
- revive
path: "cmd/crowdsec-cli/clihub/item_metrics.go"
text: "deep-exit: .*"

- linters:
- revive
path: "cmd/crowdsec-cli/idgen/password.go"
text: "deep-exit: .*"

- linters:
- revive
path: "pkg/leakybucket/overflows.go"
Expand Down
7 changes: 6 additions & 1 deletion cmd/crowdsec-cli/clicapi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@
return fmt.Errorf("unable to generate machine id: %w", err)
}

password := strfmt.Password(idgen.GeneratePassword(idgen.PasswordLength))
pstr, err := idgen.GeneratePassword(idgen.PasswordLength)
if err != nil {
return err
}

Check warning on line 72 in cmd/crowdsec-cli/clicapi/capi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clicapi/capi.go#L71-L72

Added lines #L71 - L72 were not covered by tests

password := strfmt.Password(pstr)

apiurl, err := url.Parse(types.CAPIBaseURL)
if err != nil {
Expand Down
67 changes: 48 additions & 19 deletions cmd/crowdsec-cli/clihub/item_metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clihub

import (
"fmt"
"net/http"
"strconv"
"strings"
Expand All @@ -19,10 +20,16 @@
func showMetrics(prometheusURL string, hubItem *cwhub.Item, wantColor string) error {
switch hubItem.Type {
case cwhub.PARSERS:
metrics := getParserMetric(prometheusURL, hubItem.Name)
metrics, err := getParserMetric(prometheusURL, hubItem.Name)
if err != nil {
return err
}

Check warning on line 26 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L25-L26

Added lines #L25 - L26 were not covered by tests
parserMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
case cwhub.SCENARIOS:
metrics := getScenarioMetric(prometheusURL, hubItem.Name)
metrics, err := getScenarioMetric(prometheusURL, hubItem.Name)
if err != nil {
return err
}

Check warning on line 32 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L31-L32

Added lines #L31 - L32 were not covered by tests
scenarioMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
case cwhub.COLLECTIONS:
for _, sub := range hubItem.SubItems() {
Expand All @@ -31,7 +38,10 @@
}
}
case cwhub.APPSEC_RULES:
metrics := getAppsecRuleMetric(prometheusURL, hubItem.Name)
metrics, err := getAppsecRuleMetric(prometheusURL, hubItem.Name)
if err != nil {
return err
}

Check warning on line 44 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L41-L44

Added lines #L41 - L44 were not covered by tests
appsecMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
default: // no metrics for this item type
}
Expand All @@ -40,11 +50,15 @@
}

// getParserMetric is a complete rip from prom2json
func getParserMetric(url string, itemName string) map[string]map[string]int {
func getParserMetric(url string, itemName string) (map[string]map[string]int, error) {
stats := make(map[string]map[string]int)

result := getPrometheusMetric(url)
for idx, fam := range result {
results, err := getPrometheusMetric(url)
if err != nil {
return nil, err
}

Check warning on line 59 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L58-L59

Added lines #L58 - L59 were not covered by tests

for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
}
Expand Down Expand Up @@ -128,10 +142,10 @@
}
}

return stats
return stats, nil
}

func getScenarioMetric(url string, itemName string) map[string]int {
func getScenarioMetric(url string, itemName string) (map[string]int, error) {
stats := make(map[string]int)

stats["instantiation"] = 0
Expand All @@ -140,8 +154,12 @@
stats["pour"] = 0
stats["underflow"] = 0

result := getPrometheusMetric(url)
for idx, fam := range result {
results, err := getPrometheusMetric(url)
if err != nil {
return nil, err
}

Check warning on line 160 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L159-L160

Added lines #L159 - L160 were not covered by tests

for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
}
Expand Down Expand Up @@ -192,16 +210,20 @@
}
}

return stats
return stats, nil
}

func getAppsecRuleMetric(url string, itemName string) map[string]int {
func getAppsecRuleMetric(url string, itemName string) (map[string]int, error) {

Check warning on line 216 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L216

Added line #L216 was not covered by tests
stats := make(map[string]int)

stats["inband_hits"] = 0
stats["outband_hits"] = 0

results := getPrometheusMetric(url)
results, err := getPrometheusMetric(url)
if err != nil {
return nil, err
}

Check warning on line 225 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L222-L225

Added lines #L222 - L225 were not covered by tests

for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
Expand Down Expand Up @@ -257,10 +279,10 @@
}
}

return stats
return stats, nil

Check warning on line 282 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L282

Added line #L282 was not covered by tests
}

func getPrometheusMetric(url string) []*prom2json.Family {
func getPrometheusMetric(url string) ([]*prom2json.Family, error) {
mfChan := make(chan *dto.MetricFamily, 1024)

// Start with the DefaultTransport for sane defaults.
Expand All @@ -271,12 +293,15 @@
// Timeout early if the server doesn't even return the headers.
transport.ResponseHeaderTimeout = time.Minute

var fetchErr error

go func() {
defer trace.CatchPanic("crowdsec/GetPrometheusMetric")

err := prom2json.FetchMetricFamilies(url, mfChan, transport)
if err != nil {
log.Fatalf("failed to fetch prometheus metrics : %v", err)
// mfChan is closed by prom2json.FetchMetricFamilies in all cases.
if err := prom2json.FetchMetricFamilies(url, mfChan, transport); err != nil {
fetchErr = fmt.Errorf("failed to fetch prometheus metrics: %w", err)
return

Check warning on line 304 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L303-L304

Added lines #L303 - L304 were not covered by tests
}
}()

Expand All @@ -285,7 +310,11 @@
result = append(result, prom2json.NewFamily(mf))
}

if fetchErr != nil {
return nil, fetchErr
}

Check warning on line 315 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L314-L315

Added lines #L314 - L315 were not covered by tests

log.Debugf("Finished reading prometheus output, %d entries", len(result))

return result
return result, nil
}
7 changes: 6 additions & 1 deletion cmd/crowdsec-cli/clilapi/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
}
}

password := strfmt.Password(idgen.GeneratePassword(idgen.PasswordLength))
pstr, err := idgen.GeneratePassword(idgen.PasswordLength)
if err != nil {
return err
}

Check warning on line 34 in cmd/crowdsec-cli/clilapi/register.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clilapi/register.go#L33-L34

Added lines #L33 - L34 were not covered by tests

password := strfmt.Password(pstr)

apiurl, err := prepareAPIURL(cfg.API.Client, apiURL)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/crowdsec-cli/climachine/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
return errors.New("please specify a password with --password or use --auto")
}

machinePassword = idgen.GeneratePassword(idgen.PasswordLength)
machinePassword, err = idgen.GeneratePassword(idgen.PasswordLength)
if err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/climachine/add.go#L70-L71

Added lines #L70 - L71 were not covered by tests
} else if machinePassword == "" && interactive {
qs := &survey.Password{
Message: "Please provide a password for the machine:",
Expand Down
6 changes: 5 additions & 1 deletion cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@
if metabasePassword == "" {
isValid := passwordIsValid(metabasePassword)
for !isValid {
metabasePassword = idgen.GeneratePassword(16)
var err error
metabasePassword, err = idgen.GeneratePassword(16)
if err != nil {
return err
}

Check warning on line 151 in cmd/crowdsec-cli/dashboard.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L147-L151

Added lines #L147 - L151 were not covered by tests
isValid = passwordIsValid(metabasePassword)
}
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/crowdsec-cli/idgen/machineid.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
}

prefix = strings.ReplaceAll(prefix, "-", "")[:32]
suffix := GeneratePassword(16)

suffix, err := GeneratePassword(16)
if err != nil {
return "", err
}

Check warning on line 49 in cmd/crowdsec-cli/idgen/machineid.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L48-L49

Added lines #L48 - L49 were not covered by tests

return prefix + suffix, nil
}
9 changes: 4 additions & 5 deletions cmd/crowdsec-cli/idgen/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import (
saferand "crypto/rand"
"fmt"
"math/big"

log "github.com/sirupsen/logrus"
)

const PasswordLength = 64

func GeneratePassword(length int) string {
func GeneratePassword(length int) (string, error) {
upper := "ABCDEFGHIJKLMNOPQRSTUVWXY"
lower := "abcdefghijklmnopqrstuvwxyz"
digits := "0123456789"
Expand All @@ -22,11 +21,11 @@
for i := range length {
rInt, err := saferand.Int(saferand.Reader, big.NewInt(int64(charsetLength)))
if err != nil {
log.Fatalf("failed getting data from prng for password generation : %s", err)
return "", fmt.Errorf("prng failed to generate unique id or password: %w", err)

Check warning on line 24 in cmd/crowdsec-cli/idgen/password.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/password.go#L24

Added line #L24 was not covered by tests
}

buf[i] = charset[rInt.Int64()]
}

return string(buf)
return string(buf), nil
}
2 changes: 2 additions & 0 deletions pkg/leakybucket/overflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
leaky.logger.Tracef("Valid range from %s : %s", src.IP, src.Range)
}
}

if leaky.scopeType.Scope == types.Ip {
src.Value = &src.IP
} else if leaky.scopeType.Scope == types.Range {
Expand Down Expand Up @@ -364,6 +365,7 @@
if err := newApiAlert.Validate(strfmt.Default); err != nil {
log.Errorf("Generated alerts isn't valid")
log.Errorf("->%s", spew.Sdump(newApiAlert))
// XXX: deep-exit - note other errors returned from this function are not fatal

Check warning on line 368 in pkg/leakybucket/overflows.go

View check run for this annotation

Codecov / codecov/patch

pkg/leakybucket/overflows.go#L368

Added line #L368 was not covered by tests
log.Fatalf("error : %s", err)
}

Expand Down
Loading