Skip to content

Commit

Permalink
lint/deep-exit: avoid log.Fatal (#3360)
Browse files Browse the repository at this point in the history
* deep-exit: bubble up error from item_metrics.go
* deep-exit: bubble up error from password.go
  • Loading branch information
mmetc authored Dec 13, 2024
1 parent 118275f commit 082c1dd
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 39 deletions.
10 changes: 0 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,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 @@ func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputF
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
}

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 @@ import (
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
}
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
}
scenarioMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
case cwhub.COLLECTIONS:
for _, sub := range hubItem.SubItems() {
Expand All @@ -31,7 +38,10 @@ func showMetrics(prometheusURL string, hubItem *cwhub.Item, wantColor string) er
}
}
case cwhub.APPSEC_RULES:
metrics := getAppsecRuleMetric(prometheusURL, hubItem.Name)
metrics, err := getAppsecRuleMetric(prometheusURL, hubItem.Name)
if err != nil {
return err
}
appsecMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
default: // no metrics for this item type
}
Expand All @@ -40,11 +50,15 @@ func showMetrics(prometheusURL string, hubItem *cwhub.Item, wantColor string) er
}

// 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
}

for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
}
Expand Down Expand Up @@ -128,10 +142,10 @@ func getParserMetric(url string, itemName string) map[string]map[string]int {
}
}

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 @@ func getScenarioMetric(url string, itemName string) map[string]int {
stats["pour"] = 0
stats["underflow"] = 0

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

for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
}
Expand Down Expand Up @@ -192,16 +210,20 @@ func getScenarioMetric(url string, itemName string) map[string]int {
}
}

return stats
return stats, nil
}

func getAppsecRuleMetric(url string, itemName string) map[string]int {
func getAppsecRuleMetric(url string, itemName string) (map[string]int, error) {
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
}

for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
Expand Down Expand Up @@ -257,10 +279,10 @@ func getAppsecRuleMetric(url string, itemName string) map[string]int {
}
}

return stats
return stats, nil
}

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 @@ func getPrometheusMetric(url string) []*prom2json.Family {
// 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
}
}()

Expand All @@ -285,7 +310,11 @@ func getPrometheusMetric(url string) []*prom2json.Family {
result = append(result, prom2json.NewFamily(mf))
}

if fetchErr != nil {
return nil, fetchErr
}

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 @@ func (cli *cliLapi) register(ctx context.Context, apiURL string, outputFile stri
}
}

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

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 @@ func (cli *cliMachines) add(ctx context.Context, args []string, machinePassword
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
}
} 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 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
if metabasePassword == "" {
isValid := passwordIsValid(metabasePassword)
for !isValid {
metabasePassword = idgen.GeneratePassword(16)
var err error
metabasePassword, err = idgen.GeneratePassword(16)
if err != nil {
return err
}
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 @@ func GenerateMachineID(prefix string) (string, error) {
}

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

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

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 @@ package idgen

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 @@ func GeneratePassword(length int) string {
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)
}

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 @@ func eventSources(evt types.Event, leaky *Leaky) (map[string]models.Source, erro
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 @@ func NewAlert(leaky *Leaky, queue *types.Queue) (types.RuntimeAlert, error) {
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
log.Fatalf("error : %s", err)
}

Expand Down

0 comments on commit 082c1dd

Please sign in to comment.