update
This commit is contained in:
parent
708e7b5d8a
commit
52cb64e9f5
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -17,7 +18,9 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
@ -211,13 +214,40 @@ Disable given information push to the central API.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
switch csConfig.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
fmt.Printf("Sharing options:\n")
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
fmt.Printf(" - Share Decisions : %t\n", *csConfig.API.Server.ConsoleConfig.ShareDecisions)
|
|
||||||
fmt.Printf(" - Share tainted scenarios alerts : %t\n", *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios)
|
|
||||||
fmt.Printf(" - Share custom scenarios alerts : %t\n", *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios)
|
|
||||||
fmt.Printf(" - Share manual decisions alerts : %t\n", *csConfig.API.Server.ConsoleConfig.ShareManualDecisions)
|
|
||||||
fmt.Printf(" - Share alerts in simulion mode : %t\n", *csConfig.API.Server.ConsoleConfig.ShareSimulatedDecisions)
|
|
||||||
|
|
||||||
|
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
||||||
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||||
|
table.SetHeader([]string{"Option Name", "Activated", "Description"})
|
||||||
|
for _, option := range csconfig.CONSOLE_CONFIGS {
|
||||||
|
switch option {
|
||||||
|
case csconfig.SEND_CUSTOM_SCENARIOS:
|
||||||
|
activated := fmt.Sprintf("%s", emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios == true {
|
||||||
|
activated = fmt.Sprintf("%s", emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
table.Append([]string{option, activated, "Send alerts from custom scenarios to the console"})
|
||||||
|
case csconfig.SEND_MANUAL_SCENARIOS:
|
||||||
|
activated := fmt.Sprintf("%s", emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareManualDecisions == true {
|
||||||
|
activated = fmt.Sprintf("%s", emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
table.Append([]string{option, activated, "Send manual decisions to the console"})
|
||||||
|
case csconfig.SEND_TAINTED_SCENARIOS:
|
||||||
|
activated := fmt.Sprintf("%s", emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios == true {
|
||||||
|
activated = fmt.Sprintf("%s", emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
table.Append([]string{option, activated, "Send alerts from tainted scenarios to the console"})
|
||||||
|
case csconfig.SEND_SIMULATED_DECISIONS:
|
||||||
|
activated := fmt.Sprintf("%s", emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareSimulatedDecisions == true {
|
||||||
|
activated = fmt.Sprintf("%s", emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
table.Append([]string{option, activated, "Send alerts from scenarios in simulation mode to the console"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table.Render()
|
||||||
case "json":
|
case "json":
|
||||||
data, err := json.MarshalIndent(csConfig.API.Server.ConsoleConfig, "", " ")
|
data, err := json.MarshalIndent(csConfig.API.Server.ConsoleConfig, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -287,26 +317,58 @@ Disable given information push to the central API.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
filter := make(map[string][]string)
|
filter := make(map[string][]string)
|
||||||
decisionsInDb, err := dbClient.QueryDecisionWithFilter(filter)
|
filter["has_active_decision"] = []string{"true"}
|
||||||
|
alertsWithDecisions, err := dbClient.QueryAlertWithFilter(filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
decisionsList := make([]*models.Decision, 0)
|
|
||||||
for _, dbDecision := range decisionsInDb {
|
alertList := make([]*models.Alert, 0)
|
||||||
duration := dbDecision.Until.Sub(time.Now()).String()
|
for _, alert := range alertsWithDecisions {
|
||||||
decision := &models.Decision{
|
startAt := alert.StartedAt.String()
|
||||||
ID: int64(dbDecision.ID),
|
StopAt := alert.StoppedAt.String()
|
||||||
Duration: &duration,
|
formatedAlert := models.Alert{
|
||||||
Scenario: &dbDecision.Scenario,
|
ID: int64(alert.ID),
|
||||||
Scope: &dbDecision.Scope,
|
MachineID: machineID,
|
||||||
Value: &dbDecision.Value,
|
CreatedAt: alert.CreatedAt.Format(time.RFC3339),
|
||||||
Type: &dbDecision.Type,
|
Scenario: &alert.Scenario,
|
||||||
Origin: &dbDecision.Origin,
|
ScenarioVersion: &alert.ScenarioVersion,
|
||||||
Until: dbDecision.Until.String(),
|
ScenarioHash: &alert.ScenarioHash,
|
||||||
|
Message: &alert.Message,
|
||||||
|
EventsCount: &alert.EventsCount,
|
||||||
|
StartAt: &startAt,
|
||||||
|
StopAt: &StopAt,
|
||||||
|
Capacity: &alert.Capacity,
|
||||||
|
Leakspeed: &alert.LeakSpeed,
|
||||||
|
Simulated: &alert.Simulated,
|
||||||
|
Source: &models.Source{
|
||||||
|
Scope: &alert.SourceScope,
|
||||||
|
Value: &alert.SourceValue,
|
||||||
|
IP: alert.SourceIp,
|
||||||
|
Range: alert.SourceRange,
|
||||||
|
AsNumber: alert.SourceAsNumber,
|
||||||
|
AsName: alert.SourceAsName,
|
||||||
|
Cn: alert.SourceCountry,
|
||||||
|
Latitude: alert.SourceLatitude,
|
||||||
|
Longitude: alert.SourceLongitude,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
decisionsList = append(decisionsList, decision)
|
for _, decisionItem := range alert.Edges.Decisions {
|
||||||
|
duration := decisionItem.Until.Sub(time.Now()).String()
|
||||||
|
formatedAlert.Decisions = append(formatedAlert.Decisions, &models.Decision{
|
||||||
|
Duration: &duration, // transform into time.Time ?
|
||||||
|
Scenario: &decisionItem.Scenario,
|
||||||
|
Type: &decisionItem.Type,
|
||||||
|
Scope: &decisionItem.Scope,
|
||||||
|
Value: &decisionItem.Value,
|
||||||
|
Origin: &decisionItem.Origin,
|
||||||
|
Simulated: formatedAlert.Simulated,
|
||||||
|
ID: int64(decisionItem.ID),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
alertList = append(alertList, &formatedAlert)
|
||||||
}
|
}
|
||||||
_, _, err = Client.Decisions.SyncDecisions(context.Background(), decisionsList)
|
_, _, err = Client.Decisions.SyncDecisions(context.Background(), alertList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to sync decisions with console: %s", err.Error())
|
log.Fatalf("unable to sync decisions with console: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,7 +143,7 @@ func (s *DecisionsService) DeleteManualDecisions(ctx context.Context, decisionsI
|
||||||
return &successReponse, resp, nil
|
return &successReponse, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DecisionsService) SyncDecisions(ctx context.Context, decisions []*models.Decision) (*SuccessReponse, *Response, error) {
|
func (s *DecisionsService) SyncDecisions(ctx context.Context, decisions []*models.Alert) (*SuccessReponse, *Response, error) {
|
||||||
var successReponse SuccessReponse
|
var successReponse SuccessReponse
|
||||||
|
|
||||||
u := fmt.Sprintf("%s/decisions/sync", s.client.URLPrefix)
|
u := fmt.Sprintf("%s/decisions/sync", s.client.URLPrefix)
|
||||||
|
|
Loading…
Reference in a new issue