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

do not merge - experimental context branch #3073

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@
return err
}
log.Info("Flushing alerts. !! This may take a long time !!")
err = db.FlushAlerts(maxAge, maxItems)
err = db.FlushAlerts(cmd.Context(), maxAge, maxItems)

Check warning on line 578 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L578

Added line #L578 was not covered by tests
if err != nil {
return fmt.Errorf("unable to flush alerts: %w", err)
}
Expand Down
39 changes: 20 additions & 19 deletions cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/csv"
"encoding/json"
"errors"
Expand Down Expand Up @@ -83,10 +84,10 @@
return cmd
}

func (cli *cliBouncers) list() error {
func (cli *cliBouncers) list(ctx context.Context) error {
out := color.Output

bouncers, err := cli.db.ListBouncers()
bouncers, err := cli.db.ListBouncers(ctx)
if err != nil {
return fmt.Errorf("unable to list bouncers: %w", err)
}
Expand Down Expand Up @@ -134,15 +135,15 @@
Example: `cscli bouncers list`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.list()
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.list(cmd.Context())
},
}

return cmd
}

func (cli *cliBouncers) add(bouncerName string, key string) error {
func (cli *cliBouncers) add(ctx context.Context, bouncerName string, key string) error {
var err error

keyLength := 32
Expand All @@ -154,7 +155,7 @@
}
}

_, err = cli.db.CreateBouncer(bouncerName, "", middlewares.HashSHA512(key), types.ApiKeyAuthType)
_, err = cli.db.CreateBouncer(ctx, bouncerName, "", middlewares.HashSHA512(key), types.ApiKeyAuthType)
if err != nil {
return fmt.Errorf("unable to create bouncer: %w", err)
}
Expand Down Expand Up @@ -188,8 +189,8 @@
cscli bouncers add MyBouncerName --key <random-key>`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
return cli.add(args[0], key)
RunE: func(cmd *cobra.Command, args []string) error {
return cli.add(cmd.Context(), args[0], key)
},
}

Expand All @@ -201,8 +202,8 @@
return cmd
}

func (cli *cliBouncers) deleteValid(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
bouncers, err := cli.db.ListBouncers()
func (cli *cliBouncers) deleteValid(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
bouncers, err := cli.db.ListBouncers(cmd.Context())

Check warning on line 206 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L205-L206

Added lines #L205 - L206 were not covered by tests
if err != nil {
cobra.CompError("unable to list bouncers " + err.Error())
}
Expand All @@ -218,9 +219,9 @@
return ret, cobra.ShellCompDirectiveNoFileComp
}

func (cli *cliBouncers) delete(bouncers []string) error {
func (cli *cliBouncers) delete(ctx context.Context, bouncers []string) error {
for _, bouncerID := range bouncers {
err := cli.db.DeleteBouncer(bouncerID)
err := cli.db.DeleteBouncer(ctx, bouncerID)
if err != nil {
return fmt.Errorf("unable to delete bouncer '%s': %w", bouncerID, err)
}
Expand All @@ -239,15 +240,15 @@
Aliases: []string{"remove"},
DisableAutoGenTag: true,
ValidArgsFunction: cli.deleteValid,
RunE: func(_ *cobra.Command, args []string) error {
return cli.delete(args)
RunE: func(cmd *cobra.Command, args []string) error {
return cli.delete(cmd.Context(), args)
},
}

return cmd
}

func (cli *cliBouncers) prune(duration time.Duration, force bool) error {
func (cli *cliBouncers) prune(ctx context.Context, duration time.Duration, force bool) error {
if duration < 2*time.Minute {
if yes, err := askYesNo(
"The duration you provided is less than 2 minutes. " +
Expand All @@ -259,7 +260,7 @@
}
}

bouncers, err := cli.db.QueryBouncersLastPulltimeLT(time.Now().UTC().Add(-duration))
bouncers, err := cli.db.QueryBouncersLastPulltimeLT(ctx, time.Now().UTC().Add(-duration))
if err != nil {
return fmt.Errorf("unable to query bouncers: %w", err)
}
Expand All @@ -282,7 +283,7 @@
}
}

deleted, err := cli.db.BulkDeleteBouncers(bouncers)
deleted, err := cli.db.BulkDeleteBouncers(ctx, bouncers)

Check warning on line 286 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L286

Added line #L286 was not covered by tests
if err != nil {
return fmt.Errorf("unable to prune bouncers: %w", err)
}
Expand All @@ -307,8 +308,8 @@
DisableAutoGenTag: true,
Example: `cscli bouncers prune -d 45m
cscli bouncers prune -d 45m --force`,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.prune(duration, force)
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.prune(cmd.Context(), duration, force)
},
}

Expand Down
49 changes: 25 additions & 24 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
saferand "crypto/rand"
"encoding/csv"
"encoding/json"
Expand Down Expand Up @@ -151,10 +152,10 @@
return cmd
}

func (cli *cliMachines) list() error {
func (cli *cliMachines) list(ctx context.Context) error {
out := color.Output

machines, err := cli.db.ListMachines()
machines, err := cli.db.ListMachines(ctx)
if err != nil {
return fmt.Errorf("unable to list machines: %w", err)
}
Expand Down Expand Up @@ -206,8 +207,8 @@
Example: `cscli machines list`,
Args: cobra.NoArgs,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.list()
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.list(cmd.Context())
},
}

Expand All @@ -233,8 +234,8 @@
cscli machines add MyTestMachine --auto
cscli machines add MyTestMachine --password MyPassword
cscli machines add -f- --auto > /tmp/mycreds.yaml`,
RunE: func(_ *cobra.Command, args []string) error {
return cli.add(args, string(password), dumpFile, apiURL, interactive, autoAdd, force)
RunE: func(cmd *cobra.Command, args []string) error {
return cli.add(cmd.Context(), args, string(password), dumpFile, apiURL, interactive, autoAdd, force)
},
}

Expand All @@ -249,7 +250,7 @@
return cmd
}

func (cli *cliMachines) add(args []string, machinePassword string, dumpFile string, apiURL string, interactive bool, autoAdd bool, force bool) error {
func (cli *cliMachines) add(ctx context.Context, args []string, machinePassword string, dumpFile string, apiURL string, interactive bool, autoAdd bool, force bool) error {
var (
err error
machineID string
Expand Down Expand Up @@ -308,7 +309,7 @@

password := strfmt.Password(machinePassword)

_, err = cli.db.CreateMachine(&machineID, &password, "", true, force, types.PasswordAuthType)
_, err = cli.db.CreateMachine(ctx, &machineID, &password, "", true, force, types.PasswordAuthType)
if err != nil {
return fmt.Errorf("unable to create machine: %w", err)
}
Expand Down Expand Up @@ -349,8 +350,8 @@
return nil
}

func (cli *cliMachines) deleteValid(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
machines, err := cli.db.ListMachines()
func (cli *cliMachines) deleteValid(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
machines, err := cli.db.ListMachines(cmd.Context())

Check warning on line 354 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L353-L354

Added lines #L353 - L354 were not covered by tests
if err != nil {
cobra.CompError("unable to list machines " + err.Error())
}
Expand All @@ -366,9 +367,9 @@
return ret, cobra.ShellCompDirectiveNoFileComp
}

func (cli *cliMachines) delete(machines []string) error {
func (cli *cliMachines) delete(ctx context.Context, machines []string) error {
for _, machineID := range machines {
if err := cli.db.DeleteWatcher(machineID); err != nil {
if err := cli.db.DeleteWatcher(ctx, machineID); err != nil {
log.Errorf("unable to delete machine '%s': %s", machineID, err)
return nil
}
Expand All @@ -388,15 +389,15 @@
Aliases: []string{"remove"},
DisableAutoGenTag: true,
ValidArgsFunction: cli.deleteValid,
RunE: func(_ *cobra.Command, args []string) error {
return cli.delete(args)
RunE: func(cmd *cobra.Command, args []string) error {
return cli.delete(cmd.Context(), args)
},
}

return cmd
}

func (cli *cliMachines) prune(duration time.Duration, notValidOnly bool, force bool) error {
func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notValidOnly bool, force bool) error {
if duration < 2*time.Minute && !notValidOnly {
if yes, err := askYesNo(
"The duration you provided is less than 2 minutes. " +
Expand All @@ -409,12 +410,12 @@
}

machines := []*ent.Machine{}
if pending, err := cli.db.QueryPendingMachine(); err == nil {
if pending, err := cli.db.QueryPendingMachine(ctx); err == nil {
machines = append(machines, pending...)
}

if !notValidOnly {
if pending, err := cli.db.QueryLastValidatedHeartbeatLT(time.Now().UTC().Add(-duration)); err == nil {
if pending, err := cli.db.QueryLastValidatedHeartbeatLT(ctx, time.Now().UTC().Add(-duration)); err == nil {
machines = append(machines, pending...)
}
}
Expand All @@ -437,7 +438,7 @@
}
}

deleted, err := cli.db.BulkDeleteWatchers(machines)
deleted, err := cli.db.BulkDeleteWatchers(ctx, machines)

Check warning on line 441 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L441

Added line #L441 was not covered by tests
if err != nil {
return fmt.Errorf("unable to prune machines: %w", err)
}
Expand Down Expand Up @@ -465,8 +466,8 @@
cscli machines prune --not-validated-only --force`,
Args: cobra.NoArgs,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.prune(duration, notValidOnly, force)
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.prune(cmd.Context(), duration, notValidOnly, force)
},
}

Expand All @@ -478,8 +479,8 @@
return cmd
}

func (cli *cliMachines) validate(machineID string) error {
if err := cli.db.ValidateMachine(machineID); err != nil {
func (cli *cliMachines) validate(ctx context.Context, machineID string) error {
if err := cli.db.ValidateMachine(ctx, machineID); err != nil {
return fmt.Errorf("unable to validate machine '%s': %w", machineID, err)
}

Expand All @@ -496,8 +497,8 @@
Example: `cscli machines validate "machine_name"`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
return cli.validate(args[0])
RunE: func(cmd *cobra.Command, args []string) error {
return cli.validate(cmd.Context(), args[0])
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
return fmt.Errorf("unable to get PAPI permissions: %w", err)
}
var lastTimestampStr *string
lastTimestampStr, err = db.GetConfigItem(apiserver.PapiPullKey)
lastTimestampStr, err = db.GetConfigItem(cmd.Context(), apiserver.PapiPullKey)

Check warning on line 81 in cmd/crowdsec-cli/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/papi.go#L81

Added line #L81 was not covered by tests
if err != nil {
lastTimestampStr = ptr.Of("never")
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/crowdsec-cli/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (cli *cliSupport) dumpHubItems(zw *zip.Writer, hub *cwhub.Hub, itemType str
return nil
}

func (cli *cliSupport) dumpBouncers(zw *zip.Writer, db *database.Client) error {
func (cli *cliSupport) dumpBouncers(ctx context.Context, zw *zip.Writer, db *database.Client) error {
log.Info("Collecting bouncers")

if db == nil {
Expand All @@ -193,7 +193,7 @@ func (cli *cliSupport) dumpBouncers(zw *zip.Writer, db *database.Client) error {

out := new(bytes.Buffer)

bouncers, err := db.ListBouncers()
bouncers, err := db.ListBouncers(ctx)
if err != nil {
return fmt.Errorf("unable to list bouncers: %w", err)
}
Expand All @@ -207,7 +207,7 @@ func (cli *cliSupport) dumpBouncers(zw *zip.Writer, db *database.Client) error {
return nil
}

func (cli *cliSupport) dumpAgents(zw *zip.Writer, db *database.Client) error {
func (cli *cliSupport) dumpAgents(ctx context.Context, zw *zip.Writer, db *database.Client) error {
log.Info("Collecting agents")

if db == nil {
Expand All @@ -216,7 +216,7 @@ func (cli *cliSupport) dumpAgents(zw *zip.Writer, db *database.Client) error {

out := new(bytes.Buffer)

machines, err := db.ListMachines()
machines, err := db.ListMachines(ctx)
if err != nil {
return fmt.Errorf("unable to list machines: %w", err)
}
Expand Down Expand Up @@ -518,11 +518,11 @@ func (cli *cliSupport) dump(ctx context.Context, outFile string) error {
}
}

if err = cli.dumpBouncers(zipWriter, db); err != nil {
if err = cli.dumpBouncers(ctx, zipWriter, db); err != nil {
log.Warnf("could not collect bouncers information: %s", err)
}

if err = cli.dumpAgents(zipWriter, db); err != nil {
if err = cli.dumpAgents(ctx, zipWriter, db); err != nil {
log.Warnf("could not collect agents information: %s", err)
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/crowdsec/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"runtime"
Expand Down Expand Up @@ -63,7 +64,8 @@ func serveAPIServer(apiServer *apiserver.APIServer) {
go func() {
defer trace.CatchPanic("crowdsec/runAPIServer")
log.Debugf("serving API after %s ms", time.Since(crowdsecT0))
if err := apiServer.Run(apiReady); err != nil {
ctx := context.TODO()
if err := apiServer.Run(ctx, apiReady); err != nil {
log.Fatal(err)
}
}()
Expand Down
4 changes: 3 additions & 1 deletion cmd/crowdsec/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"

Expand Down Expand Up @@ -118,7 +119,8 @@ func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.Ha
return
}

decisions, err := dbClient.QueryDecisionCountByScenario()
ctx := context.TODO()
decisions, err := dbClient.QueryDecisionCountByScenario(ctx)
if err != nil {
log.Errorf("Error querying decisions for metrics: %v", err)
next.ServeHTTP(w, r)
Expand Down
Loading
Loading