machines.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package database
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/go-openapi/strfmt"
  6. "github.com/pkg/errors"
  7. "golang.org/x/crypto/bcrypt"
  8. "github.com/crowdsecurity/crowdsec/pkg/database/ent"
  9. "github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
  10. "github.com/crowdsecurity/crowdsec/pkg/types"
  11. )
  12. const CapiMachineID = types.CAPIOrigin
  13. const CapiListsMachineID = types.ListOrigin
  14. func (c *Client) CreateMachine(machineID *string, password *strfmt.Password, ipAddress string, isValidated bool, force bool, authType string) (*ent.Machine, error) {
  15. hashPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
  16. if err != nil {
  17. c.Log.Warningf("CreateMachine : %s", err)
  18. return nil, errors.Wrap(HashError, "")
  19. }
  20. machineExist, err := c.Ent.Machine.
  21. Query().
  22. Where(machine.MachineIdEQ(*machineID)).
  23. Select(machine.FieldMachineId).Strings(c.CTX)
  24. if err != nil {
  25. return nil, errors.Wrapf(QueryFail, "machine '%s': %s", *machineID, err)
  26. }
  27. if len(machineExist) > 0 {
  28. if force {
  29. _, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(*machineID)).SetPassword(string(hashPassword)).Save(c.CTX)
  30. if err != nil {
  31. c.Log.Warningf("CreateMachine : %s", err)
  32. return nil, errors.Wrapf(UpdateFail, "machine '%s'", *machineID)
  33. }
  34. machine, err := c.QueryMachineByID(*machineID)
  35. if err != nil {
  36. return nil, errors.Wrapf(QueryFail, "machine '%s': %s", *machineID, err)
  37. }
  38. return machine, nil
  39. }
  40. return nil, errors.Wrapf(UserExists, "user '%s'", *machineID)
  41. }
  42. machine, err := c.Ent.Machine.
  43. Create().
  44. SetMachineId(*machineID).
  45. SetPassword(string(hashPassword)).
  46. SetIpAddress(ipAddress).
  47. SetIsValidated(isValidated).
  48. SetAuthType(authType).
  49. Save(c.CTX)
  50. if err != nil {
  51. c.Log.Warningf("CreateMachine : %s", err)
  52. return nil, errors.Wrapf(InsertFail, "creating machine '%s'", *machineID)
  53. }
  54. return machine, nil
  55. }
  56. func (c *Client) QueryMachineByID(machineID string) (*ent.Machine, error) {
  57. machine, err := c.Ent.Machine.
  58. Query().
  59. Where(machine.MachineIdEQ(machineID)).
  60. Only(c.CTX)
  61. if err != nil {
  62. c.Log.Warningf("QueryMachineByID : %s", err)
  63. return &ent.Machine{}, errors.Wrapf(UserNotExists, "user '%s'", machineID)
  64. }
  65. return machine, nil
  66. }
  67. func (c *Client) ListMachines() ([]*ent.Machine, error) {
  68. machines, err := c.Ent.Machine.Query().All(c.CTX)
  69. if err != nil {
  70. return []*ent.Machine{}, errors.Wrapf(QueryFail, "listing machines: %s", err)
  71. }
  72. return machines, nil
  73. }
  74. func (c *Client) ValidateMachine(machineID string) error {
  75. rets, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(machineID)).SetIsValidated(true).Save(c.CTX)
  76. if err != nil {
  77. return errors.Wrapf(UpdateFail, "validating machine: %s", err)
  78. }
  79. if rets == 0 {
  80. return fmt.Errorf("machine not found")
  81. }
  82. return nil
  83. }
  84. func (c *Client) QueryPendingMachine() ([]*ent.Machine, error) {
  85. var machines []*ent.Machine
  86. var err error
  87. machines, err = c.Ent.Machine.Query().Where(machine.IsValidatedEQ(false)).All(c.CTX)
  88. if err != nil {
  89. c.Log.Warningf("QueryPendingMachine : %s", err)
  90. return []*ent.Machine{}, errors.Wrapf(QueryFail, "querying pending machines: %s", err)
  91. }
  92. return machines, nil
  93. }
  94. func (c *Client) DeleteWatcher(name string) error {
  95. nbDeleted, err := c.Ent.Machine.
  96. Delete().
  97. Where(machine.MachineIdEQ(name)).
  98. Exec(c.CTX)
  99. if err != nil {
  100. return err
  101. }
  102. if nbDeleted == 0 {
  103. return fmt.Errorf("machine doesn't exist")
  104. }
  105. return nil
  106. }
  107. func (c *Client) BulkDeleteWatchers(machines []*ent.Machine) (int, error) {
  108. ids := make([]int, len(machines))
  109. for i, b := range machines {
  110. ids[i] = b.ID
  111. }
  112. nbDeleted, err := c.Ent.Machine.Delete().Where(machine.IDIn(ids...)).Exec(c.CTX)
  113. if err != nil {
  114. return nbDeleted, err
  115. }
  116. return nbDeleted, nil
  117. }
  118. func (c *Client) UpdateMachineLastPush(machineID string) error {
  119. _, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(machineID)).SetLastPush(time.Now().UTC()).Save(c.CTX)
  120. if err != nil {
  121. return errors.Wrapf(UpdateFail, "updating machine last_push: %s", err)
  122. }
  123. return nil
  124. }
  125. func (c *Client) UpdateMachineLastHeartBeat(machineID string) error {
  126. _, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(machineID)).SetLastHeartbeat(time.Now().UTC()).Save(c.CTX)
  127. if err != nil {
  128. return errors.Wrapf(UpdateFail, "updating machine last_heartbeat: %s", err)
  129. }
  130. return nil
  131. }
  132. func (c *Client) UpdateMachineScenarios(scenarios string, ID int) error {
  133. _, err := c.Ent.Machine.UpdateOneID(ID).
  134. SetUpdatedAt(time.Now().UTC()).
  135. SetScenarios(scenarios).
  136. Save(c.CTX)
  137. if err != nil {
  138. return fmt.Errorf("unable to update machine in database: %s", err)
  139. }
  140. return nil
  141. }
  142. func (c *Client) UpdateMachineIP(ipAddr string, ID int) error {
  143. _, err := c.Ent.Machine.UpdateOneID(ID).
  144. SetIpAddress(ipAddr).
  145. Save(c.CTX)
  146. if err != nil {
  147. return fmt.Errorf("unable to update machine IP in database: %s", err)
  148. }
  149. return nil
  150. }
  151. func (c *Client) UpdateMachineVersion(ipAddr string, ID int) error {
  152. _, err := c.Ent.Machine.UpdateOneID(ID).
  153. SetVersion(ipAddr).
  154. Save(c.CTX)
  155. if err != nil {
  156. return fmt.Errorf("unable to update machine version in database: %s", err)
  157. }
  158. return nil
  159. }
  160. func (c *Client) IsMachineRegistered(machineID string) (bool, error) {
  161. exist, err := c.Ent.Machine.Query().Where().Select(machine.FieldMachineId).Strings(c.CTX)
  162. if err != nil {
  163. return false, err
  164. }
  165. if len(exist) == 1 {
  166. return true, nil
  167. }
  168. if len(exist) > 1 {
  169. return false, fmt.Errorf("More than one item with the same machineID in database")
  170. }
  171. return false, nil
  172. }
  173. func (c *Client) QueryLastValidatedHeartbeatLT(t time.Time) ([]*ent.Machine, error) {
  174. return c.Ent.Machine.Query().Where(machine.LastHeartbeatLT(t), machine.IsValidatedEQ(true)).All(c.CTX)
  175. }