github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/bouncers.go (about) 1 package database 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/pkg/errors" 8 9 "github.com/crowdsecurity/crowdsec/pkg/database/ent" 10 "github.com/crowdsecurity/crowdsec/pkg/database/ent/bouncer" 11 ) 12 13 func (c *Client) SelectBouncer(apiKeyHash string) (*ent.Bouncer, error) { 14 result, err := c.Ent.Bouncer.Query().Where(bouncer.APIKeyEQ(apiKeyHash)).First(c.CTX) 15 if err != nil { 16 return nil, err 17 } 18 19 return result, nil 20 } 21 22 func (c *Client) SelectBouncerByName(bouncerName string) (*ent.Bouncer, error) { 23 result, err := c.Ent.Bouncer.Query().Where(bouncer.NameEQ(bouncerName)).First(c.CTX) 24 if err != nil { 25 return nil, err 26 } 27 28 return result, nil 29 } 30 31 func (c *Client) ListBouncers() ([]*ent.Bouncer, error) { 32 result, err := c.Ent.Bouncer.Query().All(c.CTX) 33 if err != nil { 34 return nil, errors.Wrapf(QueryFail, "listing bouncers: %s", err) 35 } 36 37 return result, nil 38 } 39 40 func (c *Client) CreateBouncer(name string, ipAddr string, apiKey string, authType string) (*ent.Bouncer, error) { 41 bouncer, err := c.Ent.Bouncer. 42 Create(). 43 SetName(name). 44 SetAPIKey(apiKey). 45 SetRevoked(false). 46 SetAuthType(authType). 47 Save(c.CTX) 48 if err != nil { 49 if ent.IsConstraintError(err) { 50 return nil, fmt.Errorf("bouncer %s already exists", name) 51 } 52 53 return nil, fmt.Errorf("unable to create bouncer: %w", err) 54 } 55 56 return bouncer, nil 57 } 58 59 func (c *Client) DeleteBouncer(name string) error { 60 nbDeleted, err := c.Ent.Bouncer. 61 Delete(). 62 Where(bouncer.NameEQ(name)). 63 Exec(c.CTX) 64 if err != nil { 65 return err 66 } 67 68 if nbDeleted == 0 { 69 return errors.New("bouncer doesn't exist") 70 } 71 72 return nil 73 } 74 75 func (c *Client) BulkDeleteBouncers(bouncers []*ent.Bouncer) (int, error) { 76 ids := make([]int, len(bouncers)) 77 for i, b := range bouncers { 78 ids[i] = b.ID 79 } 80 81 nbDeleted, err := c.Ent.Bouncer.Delete().Where(bouncer.IDIn(ids...)).Exec(c.CTX) 82 if err != nil { 83 return nbDeleted, fmt.Errorf("unable to delete bouncers: %w", err) 84 } 85 86 return nbDeleted, nil 87 } 88 89 func (c *Client) UpdateBouncerLastPull(lastPull time.Time, id int) error { 90 _, err := c.Ent.Bouncer.UpdateOneID(id). 91 SetLastPull(lastPull). 92 Save(c.CTX) 93 if err != nil { 94 return fmt.Errorf("unable to update machine last pull in database: %w", err) 95 } 96 97 return nil 98 } 99 100 func (c *Client) UpdateBouncerIP(ipAddr string, id int) error { 101 _, err := c.Ent.Bouncer.UpdateOneID(id).SetIPAddress(ipAddr).Save(c.CTX) 102 if err != nil { 103 return fmt.Errorf("unable to update bouncer ip address in database: %w", err) 104 } 105 106 return nil 107 } 108 109 func (c *Client) UpdateBouncerTypeAndVersion(bType string, version string, id int) error { 110 _, err := c.Ent.Bouncer.UpdateOneID(id).SetVersion(version).SetType(bType).Save(c.CTX) 111 if err != nil { 112 return fmt.Errorf("unable to update bouncer type and version in database: %w", err) 113 } 114 115 return nil 116 } 117 118 func (c *Client) QueryBouncersLastPulltimeLT(t time.Time) ([]*ent.Bouncer, error) { 119 return c.Ent.Bouncer.Query().Where(bouncer.LastPullLT(t)).All(c.CTX) 120 }