machines.go 5.0 KB

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