decouple bouncer dependencies: use go-cs-lib/pkg/ptr (#2228)

This commit is contained in:
mmetc 2023-05-25 15:43:39 +02:00 committed by GitHub
parent b2d3520519
commit 9167bd107d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 60 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
"github.com/crowdsecurity/go-cs-lib/pkg/version" "github.com/crowdsecurity/go-cs-lib/pkg/version"
"github.com/crowdsecurity/crowdsec/pkg/apiclient" "github.com/crowdsecurity/crowdsec/pkg/apiclient"
@ -250,7 +251,7 @@ func SetConsoleOpts(args []string, wanted bool) {
} }
} else { } else {
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted) log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
csConfig.API.Server.ConsoleConfig.ConsoleManagement = types.BoolPtr(wanted) csConfig.API.Server.ConsoleConfig.ConsoleManagement = ptr.Of(wanted)
} }
if csConfig.API.Server.OnlineClient.Credentials != nil { if csConfig.API.Server.OnlineClient.Credentials != nil {
changed := false changed := false
@ -284,7 +285,7 @@ func SetConsoleOpts(args []string, wanted bool) {
} }
} else { } else {
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted) log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
csConfig.API.Server.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(wanted) csConfig.API.Server.ConsoleConfig.ShareCustomScenarios = ptr.Of(wanted)
} }
case csconfig.SEND_TAINTED_SCENARIOS: case csconfig.SEND_TAINTED_SCENARIOS:
/*for each flag check if it's already set before setting it*/ /*for each flag check if it's already set before setting it*/
@ -297,7 +298,7 @@ func SetConsoleOpts(args []string, wanted bool) {
} }
} else { } else {
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted) log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(wanted) csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios = ptr.Of(wanted)
} }
case csconfig.SEND_MANUAL_SCENARIOS: case csconfig.SEND_MANUAL_SCENARIOS:
/*for each flag check if it's already set before setting it*/ /*for each flag check if it's already set before setting it*/
@ -310,7 +311,7 @@ func SetConsoleOpts(args []string, wanted bool) {
} }
} else { } else {
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted) log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
csConfig.API.Server.ConsoleConfig.ShareManualDecisions = types.BoolPtr(wanted) csConfig.API.Server.ConsoleConfig.ShareManualDecisions = ptr.Of(wanted)
} }
case csconfig.SEND_CONTEXT: case csconfig.SEND_CONTEXT:
/*for each flag check if it's already set before setting it*/ /*for each flag check if it's already set before setting it*/
@ -323,7 +324,7 @@ func SetConsoleOpts(args []string, wanted bool) {
} }
} else { } else {
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted) log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
csConfig.API.Server.ConsoleConfig.ShareContext = types.BoolPtr(wanted) csConfig.API.Server.ConsoleConfig.ShareContext = ptr.Of(wanted)
} }
default: default:
log.Fatalf("unknown flag %s", arg) log.Fatalf("unknown flag %s", arg)

View file

@ -19,6 +19,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
"github.com/crowdsecurity/go-cs-lib/pkg/version" "github.com/crowdsecurity/go-cs-lib/pkg/version"
"github.com/crowdsecurity/crowdsec/pkg/apiclient" "github.com/crowdsecurity/crowdsec/pkg/apiclient"
@ -580,12 +581,12 @@ decisions.json :
log.Debugf("'scope' line %d, using supplied value: '%s'", line, importScope) log.Debugf("'scope' line %d, using supplied value: '%s'", line, importScope)
} }
decision := models.Decision{ decision := models.Decision{
Value: types.StrPtr(decisionLine.Value), Value: ptr.Of(decisionLine.Value),
Duration: types.StrPtr(decisionLine.Duration), Duration: ptr.Of(decisionLine.Duration),
Origin: types.StrPtr(decisionLine.Origin), Origin: ptr.Of(decisionLine.Origin),
Scenario: types.StrPtr(decisionLine.Scenario), Scenario: ptr.Of(decisionLine.Scenario),
Type: types.StrPtr(decisionLine.Type), Type: ptr.Of(decisionLine.Type),
Scope: types.StrPtr(decisionLine.Scope), Scope: ptr.Of(decisionLine.Scope),
Simulated: new(bool), Simulated: new(bool),
} }
decisionsList = append(decisionsList, &decision) decisionsList = append(decisionsList, &decision)
@ -601,22 +602,22 @@ decisions.json :
decisionBatch := decisionsList[i:end] decisionBatch := decisionsList[i:end]
importAlert := models.Alert{ importAlert := models.Alert{
CreatedAt: time.Now().UTC().Format(time.RFC3339), CreatedAt: time.Now().UTC().Format(time.RFC3339),
Scenario: types.StrPtr(fmt.Sprintf("import %s : %d IPs", importFile, len(decisionBatch))), Scenario: ptr.Of(fmt.Sprintf("import %s : %d IPs", importFile, len(decisionBatch))),
Message: types.StrPtr(""), Message: ptr.Of(""),
Events: []*models.Event{}, Events: []*models.Event{},
Source: &models.Source{ Source: &models.Source{
Scope: types.StrPtr(""), Scope: ptr.Of(""),
Value: types.StrPtr(""), Value: ptr.Of(""),
}, },
StartAt: types.StrPtr(time.Now().UTC().Format(time.RFC3339)), StartAt: ptr.Of(time.Now().UTC().Format(time.RFC3339)),
StopAt: types.StrPtr(time.Now().UTC().Format(time.RFC3339)), StopAt: ptr.Of(time.Now().UTC().Format(time.RFC3339)),
Capacity: types.Int32Ptr(0), Capacity: ptr.Of(int32(0)),
Simulated: types.BoolPtr(false), Simulated: ptr.Of(false),
EventsCount: types.Int32Ptr(int32(len(decisionBatch))), EventsCount: ptr.Of(int32(len(decisionBatch))),
Leakspeed: types.StrPtr(""), Leakspeed: ptr.Of(""),
ScenarioHash: types.StrPtr(""), ScenarioHash: ptr.Of(""),
ScenarioVersion: types.StrPtr(""), ScenarioVersion: ptr.Of(""),
Decisions: decisionBatch, Decisions: decisionBatch,
} }
alerts = append(alerts, &importAlert) alerts = append(alerts, &importAlert)
@ -624,21 +625,21 @@ decisions.json :
} else { } else {
importAlert := models.Alert{ importAlert := models.Alert{
CreatedAt: time.Now().UTC().Format(time.RFC3339), CreatedAt: time.Now().UTC().Format(time.RFC3339),
Scenario: types.StrPtr(fmt.Sprintf("import %s : %d IPs", importFile, len(decisionsList))), Scenario: ptr.Of(fmt.Sprintf("import %s : %d IPs", importFile, len(decisionsList))),
Message: types.StrPtr(""), Message: ptr.Of(""),
Events: []*models.Event{}, Events: []*models.Event{},
Source: &models.Source{ Source: &models.Source{
Scope: types.StrPtr(""), Scope: ptr.Of(""),
Value: types.StrPtr(""), Value: ptr.Of(""),
}, },
StartAt: types.StrPtr(time.Now().UTC().Format(time.RFC3339)), StartAt: ptr.Of(time.Now().UTC().Format(time.RFC3339)),
StopAt: types.StrPtr(time.Now().UTC().Format(time.RFC3339)), StopAt: ptr.Of(time.Now().UTC().Format(time.RFC3339)),
Capacity: types.Int32Ptr(0), Capacity: ptr.Of(int32(0)),
Simulated: types.BoolPtr(false), Simulated: ptr.Of(false),
EventsCount: types.Int32Ptr(int32(len(decisionsList))), EventsCount: ptr.Of(int32(len(decisionsList))),
Leakspeed: types.StrPtr(""), Leakspeed: ptr.Of(""),
ScenarioHash: types.StrPtr(""), ScenarioHash: ptr.Of(""),
ScenarioVersion: types.StrPtr(""), ScenarioVersion: ptr.Of(""),
Decisions: decisionsList, Decisions: decisionsList,
} }
alerts = append(alerts, &importAlert) alerts = append(alerts, &importAlert)

View file

@ -17,6 +17,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2" "gopkg.in/tomb.v2"
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
"github.com/crowdsecurity/go-cs-lib/pkg/trace" "github.com/crowdsecurity/go-cs-lib/pkg/trace"
"github.com/crowdsecurity/go-cs-lib/pkg/version" "github.com/crowdsecurity/go-cs-lib/pkg/version"
@ -96,15 +97,15 @@ func decisionsToApiDecisions(decisions []*models.Decision) models.AddSignalsRequ
apiDecisions := models.AddSignalsRequestItemDecisions{} apiDecisions := models.AddSignalsRequestItemDecisions{}
for _, decision := range decisions { for _, decision := range decisions {
x := &models.AddSignalsRequestItemDecisionsItem{ x := &models.AddSignalsRequestItemDecisionsItem{
Duration: types.StrPtr(*decision.Duration), Duration: ptr.Of(*decision.Duration),
ID: new(int64), ID: new(int64),
Origin: types.StrPtr(*decision.Origin), Origin: ptr.Of(*decision.Origin),
Scenario: types.StrPtr(*decision.Scenario), Scenario: ptr.Of(*decision.Scenario),
Scope: types.StrPtr(*decision.Scope), Scope: ptr.Of(*decision.Scope),
//Simulated: *decision.Simulated, //Simulated: *decision.Simulated,
Type: types.StrPtr(*decision.Type), Type: ptr.Of(*decision.Type),
Until: decision.Until, Until: decision.Until,
Value: types.StrPtr(*decision.Value), Value: ptr.Of(*decision.Value),
UUID: decision.UUID, UUID: decision.UUID,
} }
*x.ID = decision.ID *x.ID = decision.ID
@ -429,7 +430,7 @@ func (a *apic) HandleDeletedDecisionsV3(deletedDecisions []*modelscapi.GetDecisi
if err != nil { if err != nil {
return 0, errors.Wrapf(err, "converting db ret %d", dbCliDel) return 0, errors.Wrapf(err, "converting db ret %d", dbCliDel)
} }
updateCounterForDecision(delete_counters, types.StrPtr(types.CAPIOrigin), nil, dbCliDel) updateCounterForDecision(delete_counters, ptr.Of(types.CAPIOrigin), nil, dbCliDel)
nbDeleted += dbCliDel nbDeleted += dbCliDel
} }
} }
@ -475,26 +476,26 @@ func createAlertsForDecisions(decisions []*models.Decision) []*models.Alert {
func createAlertForDecision(decision *models.Decision) *models.Alert { func createAlertForDecision(decision *models.Decision) *models.Alert {
newAlert := &models.Alert{} newAlert := &models.Alert{}
newAlert.Source = &models.Source{} newAlert.Source = &models.Source{}
newAlert.Source.Scope = types.StrPtr("") newAlert.Source.Scope = ptr.Of("")
if *decision.Origin == types.CAPIOrigin { //to make things more user friendly, we replace CAPI with community-blocklist if *decision.Origin == types.CAPIOrigin { //to make things more user friendly, we replace CAPI with community-blocklist
newAlert.Scenario = types.StrPtr(types.CAPIOrigin) newAlert.Scenario = ptr.Of(types.CAPIOrigin)
newAlert.Source.Scope = types.StrPtr(types.CAPIOrigin) newAlert.Source.Scope = ptr.Of(types.CAPIOrigin)
} else if *decision.Origin == types.ListOrigin { } else if *decision.Origin == types.ListOrigin {
newAlert.Scenario = types.StrPtr(*decision.Scenario) newAlert.Scenario = ptr.Of(*decision.Scenario)
newAlert.Source.Scope = types.StrPtr(types.ListOrigin) newAlert.Source.Scope = ptr.Of(types.ListOrigin)
} else { } else {
log.Warningf("unknown origin %s", *decision.Origin) log.Warningf("unknown origin %s", *decision.Origin)
} }
newAlert.Message = types.StrPtr("") newAlert.Message = ptr.Of("")
newAlert.Source.Value = types.StrPtr("") newAlert.Source.Value = ptr.Of("")
newAlert.StartAt = types.StrPtr(time.Now().UTC().Format(time.RFC3339)) newAlert.StartAt = ptr.Of(time.Now().UTC().Format(time.RFC3339))
newAlert.StopAt = types.StrPtr(time.Now().UTC().Format(time.RFC3339)) newAlert.StopAt = ptr.Of(time.Now().UTC().Format(time.RFC3339))
newAlert.Capacity = types.Int32Ptr(0) newAlert.Capacity = ptr.Of(int32(0))
newAlert.Simulated = types.BoolPtr(false) newAlert.Simulated = ptr.Of(false)
newAlert.EventsCount = types.Int32Ptr(0) newAlert.EventsCount = ptr.Of(int32(0))
newAlert.Leakspeed = types.StrPtr("") newAlert.Leakspeed = ptr.Of("")
newAlert.ScenarioHash = types.StrPtr("") newAlert.ScenarioHash = ptr.Of("")
newAlert.ScenarioVersion = types.StrPtr("") newAlert.ScenarioVersion = ptr.Of("")
newAlert.MachineID = database.CapiMachineID newAlert.MachineID = database.CapiMachineID
return newAlert return newAlert
} }
@ -771,10 +772,10 @@ func (a *apic) UpdateBlocklists(links *modelscapi.GetDecisionsStreamResponseLink
func setAlertScenario(add_counters map[string]map[string]int, delete_counters map[string]map[string]int, alert *models.Alert) *models.Alert { func setAlertScenario(add_counters map[string]map[string]int, delete_counters map[string]map[string]int, alert *models.Alert) *models.Alert {
if *alert.Source.Scope == types.CAPIOrigin { if *alert.Source.Scope == types.CAPIOrigin {
*alert.Source.Scope = SCOPE_CAPI_ALIAS_ALIAS *alert.Source.Scope = SCOPE_CAPI_ALIAS_ALIAS
alert.Scenario = types.StrPtr(fmt.Sprintf("update : +%d/-%d IPs", add_counters[types.CAPIOrigin]["all"], delete_counters[types.CAPIOrigin]["all"])) alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", add_counters[types.CAPIOrigin]["all"], delete_counters[types.CAPIOrigin]["all"]))
} else if *alert.Source.Scope == types.ListOrigin { } else if *alert.Source.Scope == types.ListOrigin {
*alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario) *alert.Source.Scope = fmt.Sprintf("%s:%s", types.ListOrigin, *alert.Scenario)
alert.Scenario = types.StrPtr(fmt.Sprintf("update : +%d/-%d IPs", add_counters[types.ListOrigin][*alert.Scenario], delete_counters[types.ListOrigin][*alert.Scenario])) alert.Scenario = ptr.Of(fmt.Sprintf("update : +%d/-%d IPs", add_counters[types.ListOrigin][*alert.Scenario], delete_counters[types.ListOrigin][*alert.Scenario]))
} }
return alert return alert
} }
@ -822,7 +823,7 @@ func (a *apic) Pull() error {
func (a *apic) GetMetrics() (*models.Metrics, error) { func (a *apic) GetMetrics() (*models.Metrics, error) {
metric := &models.Metrics{ metric := &models.Metrics{
ApilVersion: types.StrPtr(version.String()), ApilVersion: ptr.Of(version.String()),
Machines: make([]*models.MetricsAgentInfo, 0), Machines: make([]*models.MetricsAgentInfo, 0),
Bouncers: make([]*models.MetricsBouncerInfo, 0), Bouncers: make([]*models.MetricsBouncerInfo, 0),
} }