machines.go 4.4 KB

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