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