parent
17ad9de738
commit
c1abf69979
|
@ -131,7 +131,7 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
|
|||
if errors.Cause(err) != UserNotExists {
|
||||
return []string{}, errors.Wrapf(QueryFail, "machine '%s': %s", alertItem.MachineID, err)
|
||||
}
|
||||
log.Debugf("CreateAlertBulk: Machine Id %s doesn't exist", machineId)
|
||||
c.Log.Debugf("CreateAlertBulk: Machine Id %s doesn't exist", machineId)
|
||||
owner = nil
|
||||
}
|
||||
startAtTime, err := time.Parse(time.RFC3339, *alertItem.StartAt)
|
||||
|
@ -145,7 +145,7 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
|
|||
}
|
||||
/*display proper alert in logs*/
|
||||
for _, disp := range formatAlertAsString(machineId, alertItem) {
|
||||
log.Info(disp)
|
||||
c.Log.Info(disp)
|
||||
}
|
||||
|
||||
if len(alertItem.Events) > 0 {
|
||||
|
@ -185,7 +185,7 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
|
|||
|
||||
ts, err := time.Parse(time.RFC3339, *alertItem.StopAt)
|
||||
if err != nil {
|
||||
log.Errorf("While parsing StartAt of item %s : %s", *alertItem.StopAt, err)
|
||||
c.Log.Errorf("While parsing StartAt of item %s : %s", *alertItem.StopAt, err)
|
||||
ts = time.Now()
|
||||
}
|
||||
if len(alertItem.Decisions) > 0 {
|
||||
|
@ -462,7 +462,7 @@ func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert,
|
|||
sort := "DESC" // we sort by desc by default
|
||||
if val, ok := filter["sort"]; ok {
|
||||
if val[0] != "ASC" && val[0] != "DESC" {
|
||||
log.Errorf("invalid 'sort' parameter: %s", val)
|
||||
c.Log.Errorf("invalid 'sort' parameter: %s", val)
|
||||
} else {
|
||||
sort = val[0]
|
||||
}
|
||||
|
@ -507,7 +507,7 @@ func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert,
|
|||
if diff := limit - len(ret); diff < paginationSize {
|
||||
if len(result) < diff {
|
||||
ret = append(ret, result...)
|
||||
log.Debugf("Pagination done, %d < %d", len(result), diff)
|
||||
c.Log.Debugf("Pagination done, %d < %d", len(result), diff)
|
||||
break
|
||||
}
|
||||
ret = append(ret, result[0:diff]...)
|
||||
|
@ -515,7 +515,7 @@ func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert,
|
|||
ret = append(ret, result...)
|
||||
}
|
||||
if len(ret) == limit || len(ret) == 0 {
|
||||
log.Debugf("Pagination done len(ret) = %d", len(ret))
|
||||
c.Log.Debugf("Pagination done len(ret) = %d", len(ret))
|
||||
break
|
||||
}
|
||||
offset += paginationSize
|
||||
|
@ -529,7 +529,7 @@ func (c *Client) DeleteAlertGraph(alertItem *ent.Alert) error {
|
|||
_, err := c.Ent.Event.Delete().
|
||||
Where(event.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteAlertGraph : %s", err)
|
||||
c.Log.Warningf("DeleteAlertGraph : %s", err)
|
||||
return errors.Wrapf(DeleteFail, "event with alert ID '%d'", alertItem.ID)
|
||||
}
|
||||
|
||||
|
@ -537,7 +537,7 @@ func (c *Client) DeleteAlertGraph(alertItem *ent.Alert) error {
|
|||
_, err = c.Ent.Meta.Delete().
|
||||
Where(meta.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteAlertGraph : %s", err)
|
||||
c.Log.Warningf("DeleteAlertGraph : %s", err)
|
||||
return errors.Wrapf(DeleteFail, "meta with alert ID '%d'", alertItem.ID)
|
||||
}
|
||||
|
||||
|
@ -545,14 +545,14 @@ func (c *Client) DeleteAlertGraph(alertItem *ent.Alert) error {
|
|||
_, err = c.Ent.Decision.Delete().
|
||||
Where(decision.HasOwnerWith(alert.IDEQ(alertItem.ID))).Exec(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteAlertGraph : %s", err)
|
||||
c.Log.Warningf("DeleteAlertGraph : %s", err)
|
||||
return errors.Wrapf(DeleteFail, "decision with alert ID '%d'", alertItem.ID)
|
||||
}
|
||||
|
||||
// delete the alert
|
||||
err = c.Ent.Alert.DeleteOne(alertItem).Exec(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteAlertGraph : %s", err)
|
||||
c.Log.Warningf("DeleteAlertGraph : %s", err)
|
||||
return errors.Wrapf(DeleteFail, "alert with ID '%d'", alertItem.ID)
|
||||
}
|
||||
|
||||
|
@ -568,7 +568,7 @@ func (c *Client) DeleteAlertWithFilter(filter map[string][]string) (int, error)
|
|||
for _, alertItem := range alertsToDelete {
|
||||
err = c.DeleteAlertGraph(alertItem)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteAlertWithFilter : %s", err)
|
||||
c.Log.Warningf("DeleteAlertWithFilter : %s", err)
|
||||
return 0, errors.Wrapf(DeleteFail, "event with alert ID '%d'", alertItem.ID)
|
||||
}
|
||||
}
|
||||
|
@ -582,7 +582,7 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
|||
var err error
|
||||
totalAlerts, err = c.TotalAlerts()
|
||||
if err != nil {
|
||||
log.Warningf("FlushAlerts (max items count) : %s", err)
|
||||
c.Log.Warningf("FlushAlerts (max items count) : %s", err)
|
||||
return errors.Wrap(err, "unable to get alerts count")
|
||||
}
|
||||
if MaxAge != "" {
|
||||
|
@ -591,7 +591,7 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
|||
}
|
||||
nbDeleted, err := c.DeleteAlertWithFilter(filter)
|
||||
if err != nil {
|
||||
log.Warningf("FlushAlerts (max age) : %s", err)
|
||||
c.Log.Warningf("FlushAlerts (max age) : %s", err)
|
||||
return errors.Wrapf(err, "unable to flush alerts with filter until: %s", MaxAge)
|
||||
}
|
||||
deletedByAge = nbDeleted
|
||||
|
@ -604,14 +604,14 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
|||
"limit": {strconv.Itoa(nbToDelete)},
|
||||
}) // we want to delete older alerts if we reach the max number of items
|
||||
if err != nil {
|
||||
log.Warningf("FlushAlerts (max items query) : %s", err)
|
||||
c.Log.Warningf("FlushAlerts (max items query) : %s", err)
|
||||
return errors.Wrap(err, "unable to get all alerts")
|
||||
}
|
||||
for itemNb, alert := range alerts {
|
||||
if itemNb < nbToDelete {
|
||||
err := c.DeleteAlertGraph(alert)
|
||||
if err != nil {
|
||||
log.Warningf("FlushAlerts : %s", err)
|
||||
c.Log.Warningf("FlushAlerts : %s", err)
|
||||
return errors.Wrap(err, "unable to flush alert")
|
||||
}
|
||||
deletedByNbItem++
|
||||
|
@ -620,10 +620,10 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
|||
}
|
||||
}
|
||||
if deletedByNbItem > 0 {
|
||||
log.Infof("flushed %d/%d alerts because max number of alerts has been reached (%d max)", deletedByNbItem, totalAlerts, MaxItems)
|
||||
c.Log.Infof("flushed %d/%d alerts because max number of alerts has been reached (%d max)", deletedByNbItem, totalAlerts, MaxItems)
|
||||
}
|
||||
if deletedByAge > 0 {
|
||||
log.Infof("flushed %d/%d alerts because they were created %s ago or more", deletedByAge, totalAlerts, MaxAge)
|
||||
c.Log.Infof("flushed %d/%d alerts because they were created %s ago or more", deletedByAge, totalAlerts, MaxAge)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -636,7 +636,7 @@ func (c *Client) GetAlertByID(alertID int) (*ent.Alert, error) {
|
|||
log.Warningf("GetAlertByID (not found): %s", err)
|
||||
return &ent.Alert{}, ItemNotFound
|
||||
}
|
||||
log.Warningf("GetAlertByID : %s", err)
|
||||
c.Log.Warningf("GetAlertByID : %s", err)
|
||||
return &ent.Alert{}, QueryFail
|
||||
}
|
||||
return alert, nil
|
||||
|
|
|
@ -69,10 +69,11 @@ func NewClient(config *csconfig.DatabaseCfg) (*Client, error) {
|
|||
if err := types.ConfigureLogger(clog); err != nil {
|
||||
return nil, errors.Wrap(err, "while configuring db logger")
|
||||
}
|
||||
log.Infof("Log level: %+v", config.LogLevel)
|
||||
if config.LogLevel != nil {
|
||||
clog.SetLevel(*config.LogLevel)
|
||||
if *config.LogLevel >= log.TraceLevel {
|
||||
log.Debugf("Enabling request debug")
|
||||
if *config.LogLevel >= log.DebugLevel {
|
||||
clog.Debugf("Enabling request debug")
|
||||
client = client.Debug()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func BuildDecisionRequestWithFilter(query *ent.DecisionQuery, filter map[string][]string) (*ent.DecisionQuery, error) {
|
||||
|
@ -159,7 +158,7 @@ func (c *Client) QueryDecisionWithFilter(filter map[string][]string) ([]*ent.Dec
|
|||
decision.FieldOrigin,
|
||||
).Scan(c.CTX, &data)
|
||||
if err != nil {
|
||||
log.Warningf("QueryDecisionWithFilter : %s", err)
|
||||
c.Log.Warningf("QueryDecisionWithFilter : %s", err)
|
||||
return []*ent.Decision{}, errors.Wrap(QueryFail, "query decision failed")
|
||||
}
|
||||
|
||||
|
@ -169,7 +168,7 @@ func (c *Client) QueryDecisionWithFilter(filter map[string][]string) ([]*ent.Dec
|
|||
func (c *Client) QueryAllDecisions() ([]*ent.Decision, error) {
|
||||
data, err := c.Ent.Decision.Query().Where(decision.UntilGT(time.Now())).All(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("QueryAllDecisions : %s", err)
|
||||
c.Log.Warningf("QueryAllDecisions : %s", err)
|
||||
return []*ent.Decision{}, errors.Wrap(QueryFail, "get all decisions")
|
||||
}
|
||||
return data, nil
|
||||
|
@ -178,7 +177,7 @@ func (c *Client) QueryAllDecisions() ([]*ent.Decision, error) {
|
|||
func (c *Client) QueryExpiredDecisions() ([]*ent.Decision, error) {
|
||||
data, err := c.Ent.Decision.Query().Where(decision.UntilLT(time.Now())).All(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("QueryExpiredDecisions : %s", err)
|
||||
c.Log.Warningf("QueryExpiredDecisions : %s", err)
|
||||
return []*ent.Decision{}, errors.Wrap(QueryFail, "expired decisions")
|
||||
}
|
||||
return data, nil
|
||||
|
@ -187,7 +186,7 @@ func (c *Client) QueryExpiredDecisions() ([]*ent.Decision, error) {
|
|||
func (c *Client) QueryExpiredDecisionsSince(since time.Time) ([]*ent.Decision, error) {
|
||||
data, err := c.Ent.Decision.Query().Where(decision.UntilLT(time.Now())).Where(decision.UntilGT(since)).All(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("QueryExpiredDecisionsSince : %s", err)
|
||||
c.Log.Warningf("QueryExpiredDecisionsSince : %s", err)
|
||||
return []*ent.Decision{}, errors.Wrap(QueryFail, "expired decisions")
|
||||
}
|
||||
return data, nil
|
||||
|
@ -196,7 +195,7 @@ func (c *Client) QueryExpiredDecisionsSince(since time.Time) ([]*ent.Decision, e
|
|||
func (c *Client) QueryNewDecisionsSince(since time.Time) ([]*ent.Decision, error) {
|
||||
data, err := c.Ent.Decision.Query().Where(decision.CreatedAtGT(since)).All(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("QueryNewDecisionsSince : %s", err)
|
||||
c.Log.Warningf("QueryNewDecisionsSince : %s", err)
|
||||
return []*ent.Decision{}, errors.Wrapf(QueryFail, "new decisions since '%s'", since.String())
|
||||
}
|
||||
return data, nil
|
||||
|
@ -205,7 +204,7 @@ func (c *Client) QueryNewDecisionsSince(since time.Time) ([]*ent.Decision, error
|
|||
func (c *Client) DeleteDecisionById(decisionId int) error {
|
||||
err := c.Ent.Decision.DeleteOneID(decisionId).Exec(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteDecisionById : %s", err)
|
||||
c.Log.Warningf("DeleteDecisionById : %s", err)
|
||||
return errors.Wrapf(DeleteFail, "decision with id '%d' doesn't exist", decisionId)
|
||||
}
|
||||
return nil
|
||||
|
@ -312,7 +311,7 @@ func (c *Client) DeleteDecisionsWithFilter(filter map[string][]string) (string,
|
|||
|
||||
nbDeleted, err := decisions.Exec(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("DeleteDecisionsWithFilter : %s", err)
|
||||
c.Log.Warningf("DeleteDecisionsWithFilter : %s", err)
|
||||
return "0", errors.Wrap(DeleteFail, "decisions with provided filter")
|
||||
}
|
||||
return strconv.Itoa(nbDeleted), nil
|
||||
|
@ -422,7 +421,7 @@ func (c *Client) SoftDeleteDecisionsWithFilter(filter map[string][]string) (stri
|
|||
}
|
||||
nbDeleted, err := decisions.SetUntil(time.Now()).Save(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("SoftDeleteDecisionsWithFilter : %s", err)
|
||||
c.Log.Warningf("SoftDeleteDecisionsWithFilter : %s", err)
|
||||
return "0", errors.Wrap(DeleteFail, "soft delete decisions with provided filter")
|
||||
}
|
||||
return strconv.Itoa(nbDeleted), nil
|
||||
|
@ -432,7 +431,7 @@ func (c *Client) SoftDeleteDecisionsWithFilter(filter map[string][]string) (stri
|
|||
func (c *Client) SoftDeleteDecisionByID(decisionID int) error {
|
||||
nbUpdated, err := c.Ent.Decision.Update().Where(decision.IDEQ(decisionID)).SetUntil(time.Now()).Save(c.CTX)
|
||||
if err != nil || nbUpdated == 0 {
|
||||
log.Warningf("SoftDeleteDecisionByID : %v (nb soft deleted: %d)", err, nbUpdated)
|
||||
c.Log.Warningf("SoftDeleteDecisionByID : %v (nb soft deleted: %d)", err, nbUpdated)
|
||||
return errors.Wrapf(DeleteFail, "decision with id '%d' doesn't exist", decisionID)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/go-openapi/strfmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
|
||||
|
@ -16,7 +15,7 @@ import (
|
|||
func (c *Client) CreateMachine(machineID *string, password *strfmt.Password, ipAddress string, isValidated bool, force bool) (int, error) {
|
||||
hashPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Warningf("CreateMachine : %s", err)
|
||||
c.Log.Warningf("CreateMachine : %s", err)
|
||||
return 0, errors.Wrap(HashError, "")
|
||||
}
|
||||
|
||||
|
@ -31,7 +30,7 @@ func (c *Client) CreateMachine(machineID *string, password *strfmt.Password, ipA
|
|||
if force {
|
||||
_, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(*machineID)).SetPassword(string(hashPassword)).Save(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("CreateMachine : %s", err)
|
||||
c.Log.Warningf("CreateMachine : %s", err)
|
||||
return 0, errors.Wrapf(UpdateFail, "machine '%s'", *machineID)
|
||||
}
|
||||
return 1, nil
|
||||
|
@ -48,7 +47,7 @@ func (c *Client) CreateMachine(machineID *string, password *strfmt.Password, ipA
|
|||
Save(c.CTX)
|
||||
|
||||
if err != nil {
|
||||
log.Warningf("CreateMachine : %s", err)
|
||||
c.Log.Warningf("CreateMachine : %s", err)
|
||||
return 0, errors.Wrapf(InsertFail, "creating machine '%s'", *machineID)
|
||||
}
|
||||
|
||||
|
@ -61,7 +60,7 @@ func (c *Client) QueryMachineByID(machineID string) (*ent.Machine, error) {
|
|||
Where(machine.MachineIdEQ(machineID)).
|
||||
Only(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("QueryMachineByID : %s", err)
|
||||
c.Log.Warningf("QueryMachineByID : %s", err)
|
||||
return &ent.Machine{}, errors.Wrapf(UserNotExists, "user '%s'", machineID)
|
||||
}
|
||||
return machine, nil
|
||||
|
@ -89,7 +88,7 @@ func (c *Client) QueryPendingMachine() ([]*ent.Machine, error) {
|
|||
|
||||
machines, err = c.Ent.Machine.Query().Where(machine.IsValidatedEQ(false)).All(c.CTX)
|
||||
if err != nil {
|
||||
log.Warningf("QueryPendingMachine : %s", err)
|
||||
c.Log.Warningf("QueryPendingMachine : %s", err)
|
||||
return []*ent.Machine{}, errors.Wrapf(QueryFail, "querying pending machines: %s", err)
|
||||
}
|
||||
return machines, nil
|
||||
|
|
Loading…
Reference in a new issue