2020-11-30 09:37:17 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-06-22 09:31:41 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/bouncer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) SelectBouncer(apiKeyHash string) (*ent.Bouncer, error) {
|
|
|
|
result, err := c.Ent.Bouncer.Query().Where(bouncer.APIKeyEQ(apiKeyHash)).First(c.CTX)
|
|
|
|
if err != nil {
|
2023-12-04 22:06:01 +00:00
|
|
|
return nil, err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 14:05:52 +00:00
|
|
|
func (c *Client) SelectBouncerByName(bouncerName string) (*ent.Bouncer, error) {
|
|
|
|
result, err := c.Ent.Bouncer.Query().Where(bouncer.NameEQ(bouncerName)).First(c.CTX)
|
|
|
|
if err != nil {
|
2023-12-04 22:06:01 +00:00
|
|
|
return nil, err
|
2022-06-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func (c *Client) ListBouncers() ([]*ent.Bouncer, error) {
|
|
|
|
result, err := c.Ent.Bouncer.Query().All(c.CTX)
|
|
|
|
if err != nil {
|
2023-12-04 22:06:01 +00:00
|
|
|
return nil, errors.Wrapf(QueryFail, "listing bouncers: %s", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 14:05:52 +00:00
|
|
|
func (c *Client) CreateBouncer(name string, ipAddr string, apiKey string, authType string) (*ent.Bouncer, error) {
|
|
|
|
bouncer, err := c.Ent.Bouncer.
|
2020-11-30 09:37:17 +00:00
|
|
|
Create().
|
|
|
|
SetName(name).
|
|
|
|
SetAPIKey(apiKey).
|
|
|
|
SetRevoked(false).
|
2022-06-08 14:05:52 +00:00
|
|
|
SetAuthType(authType).
|
2020-11-30 09:37:17 +00:00
|
|
|
Save(c.CTX)
|
|
|
|
if err != nil {
|
2020-12-01 15:16:01 +00:00
|
|
|
if ent.IsConstraintError(err) {
|
2022-06-08 14:05:52 +00:00
|
|
|
return nil, fmt.Errorf("bouncer %s already exists", name)
|
2020-12-01 15:16:01 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to create bouncer: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2022-06-08 14:05:52 +00:00
|
|
|
return bouncer, nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeleteBouncer(name string) error {
|
2022-07-28 15:32:12 +00:00
|
|
|
nbDeleted, err := c.Ent.Bouncer.
|
2020-11-30 09:37:17 +00:00
|
|
|
Delete().
|
|
|
|
Where(bouncer.NameEQ(name)).
|
|
|
|
Exec(c.CTX)
|
|
|
|
if err != nil {
|
2022-07-28 15:32:12 +00:00
|
|
|
return err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2022-07-28 15:32:12 +00:00
|
|
|
|
|
|
|
if nbDeleted == 0 {
|
2024-03-06 13:27:05 +00:00
|
|
|
return errors.New("bouncer doesn't exist")
|
2022-07-28 15:32:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-28 14:37:39 +00:00
|
|
|
func (c *Client) BulkDeleteBouncers(bouncers []*ent.Bouncer) (int, error) {
|
|
|
|
ids := make([]int, len(bouncers))
|
|
|
|
for i, b := range bouncers {
|
|
|
|
ids[i] = b.ID
|
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2023-07-28 14:37:39 +00:00
|
|
|
nbDeleted, err := c.Ent.Bouncer.Delete().Where(bouncer.IDIn(ids...)).Exec(c.CTX)
|
|
|
|
if err != nil {
|
2024-03-06 13:27:05 +00:00
|
|
|
return nbDeleted, fmt.Errorf("unable to delete bouncers: %w", err)
|
2023-07-28 14:37:39 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2023-07-28 14:37:39 +00:00
|
|
|
return nbDeleted, nil
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:27:05 +00:00
|
|
|
func (c *Client) UpdateBouncerLastPull(lastPull time.Time, id int) error {
|
|
|
|
_, err := c.Ent.Bouncer.UpdateOneID(id).
|
2020-11-30 09:37:17 +00:00
|
|
|
SetLastPull(lastPull).
|
|
|
|
Save(c.CTX)
|
|
|
|
if err != nil {
|
2024-03-06 13:27:05 +00:00
|
|
|
return fmt.Errorf("unable to update machine last pull in database: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:27:05 +00:00
|
|
|
func (c *Client) UpdateBouncerIP(ipAddr string, id int) error {
|
|
|
|
_, err := c.Ent.Bouncer.UpdateOneID(id).SetIPAddress(ipAddr).Save(c.CTX)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2024-03-06 13:27:05 +00:00
|
|
|
return fmt.Errorf("unable to update bouncer ip address in database: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:27:05 +00:00
|
|
|
func (c *Client) UpdateBouncerTypeAndVersion(bType string, version string, id int) error {
|
|
|
|
_, err := c.Ent.Bouncer.UpdateOneID(id).SetVersion(version).SetType(bType).Save(c.CTX)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2024-03-06 13:27:05 +00:00
|
|
|
return fmt.Errorf("unable to update bouncer type and version in database: %w", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2024-03-06 13:27:05 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-28 14:37:39 +00:00
|
|
|
|
|
|
|
func (c *Client) QueryBouncersLastPulltimeLT(t time.Time) ([]*ent.Bouncer, error) {
|
|
|
|
return c.Ent.Bouncer.Query().Where(bouncer.LastPullLT(t)).All(c.CTX)
|
|
|
|
}
|