github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/machines.go (about)

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