/
device.go
331 lines (283 loc) · 7.44 KB
/
device.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package core
import (
"errors"
"os"
"reflect"
"strings"
"time"
model "github.com/nettica-com/nettica-admin/model"
mongo "github.com/nettica-com/nettica-admin/mongo"
util "github.com/nettica-com/nettica-admin/util"
log "github.com/sirupsen/logrus"
)
// CreateDevice device with all necessary data
func CreateDevice(device *model.Device) (*model.Device, error) {
var err error
device.Id, err = util.RandomString(12)
if err != nil {
return nil, err
}
device.Id = "device-" + device.Id
device.Created = time.Now().UTC()
device.Updated = device.Created
if device.ApiKey == "" {
var err error
device.ApiKey, err = util.RandomString(32)
if err != nil {
return nil, err
}
device.ApiKey = "device-api-" + device.ApiKey
}
if device.Server == "" {
device.Server = os.Getenv("SERVER")
}
// check if device is valid
errs := device.IsValid()
if len(errs) != 0 {
for _, err := range errs {
log.WithFields(log.Fields{
"err": err,
}).Error("device validation error")
}
return nil, errors.New("failed to validate device")
}
err = mongo.Serialize(device.Id, "id", "devices", device)
if err != nil {
return nil, err
}
v, err := mongo.Deserialize(device.Id, "id", "devices", reflect.TypeOf(model.Device{}))
if err != nil {
return nil, err
}
device = v.(*model.Device)
// data modified, dump new config
return device, nil
}
// ReadDevice device by id
func ReadDevice(id string) (*model.Device, error) {
v, err := mongo.Deserialize(id, "id", "devices", reflect.TypeOf(model.Device{}))
if err != nil {
id = strings.TrimPrefix(id, "device-id-")
if id == "" || strings.HasPrefix(id, "device-") {
return nil, err
}
v, err = mongo.Deserialize(id, "instanceid", "devices", reflect.TypeOf(model.Device{}))
if err != nil {
return nil, err
}
}
device := v.(*model.Device)
// vpns, err := mongo.ReadAllVPNs("deviceid", device.Id)
// if err != nil {
// return nil, err
// }
// device.VPNs = vpns
return device, nil
}
// UpdateDevice preserve keys
func UpdateDevice(Id string, device *model.Device, fUpdated bool) (*model.Device, error) {
v, err := mongo.Deserialize(Id, "id", "devices", reflect.TypeOf(model.Device{}))
if err != nil {
return nil, err
}
current := v.(*model.Device)
if current.Id != device.Id {
return nil, errors.New("records Id mismatch")
}
if !fUpdated {
device.Updated = time.Now().UTC()
}
if !strings.HasPrefix(device.AccountID, "account-") {
device.AccountID = current.AccountID
}
device.VPNs = nil
// check if device is valid
errs := device.IsValid()
if len(errs) != 0 {
for _, err := range errs {
log.WithFields(log.Fields{
"err": err,
}).Error("device validation error")
}
return nil, errors.New("failed to validate device")
}
// copy each field individually
if device.Name != "" {
current.Name = device.Name
}
current.Description = device.Description
if device.AccountID != "" {
current.AccountID = device.AccountID
}
current.Updated = device.Updated
if device.Server != "" {
current.Server = device.Server
}
if device.ApiKey != "" {
current.ApiKey = device.ApiKey
}
if device.UpdatedBy != "" {
current.UpdatedBy = device.UpdatedBy
} else {
if !fUpdated {
current.UpdatedBy = "API"
}
}
current.ApiID = device.ApiID
current.Enable = device.Enable
current.Tags = device.Tags
current.Platform = device.Platform
current.OS = device.OS
if current.Platform == "" {
if current.OS == "windows" {
current.Platform = "Windows"
}
if current.OS == "linux" {
current.Platform = "Linux"
}
}
current.Architecture = device.Architecture
current.Version = device.Version
current.ClientID = device.ClientID
current.AuthDomain = device.AuthDomain
current.AppData = device.AppData
if device.CheckInterval != 0 {
current.CheckInterval = device.CheckInterval
}
if current.CheckInterval == 0 {
current.CheckInterval = 10
}
if device.ServiceGroup != "" {
current.ServiceGroup = device.ServiceGroup
}
if device.ServiceApiKey != "" {
current.ServiceApiKey = device.ServiceApiKey
}
current.Debug = device.Debug
current.Quiet = device.Quiet
// stupidity is not a right
changed := false
if current.Registered != device.Registered {
changed = true
}
current.Registered = device.Registered
if current.InstanceID != device.InstanceID && !changed {
current.Registered = false
}
current.InstanceID = device.InstanceID
current.LastSeen = device.LastSeen
err = mongo.Serialize(device.Id, "id", "devices", current)
if err != nil {
return nil, err
}
v, err = mongo.Deserialize(Id, "id", "devices", reflect.TypeOf(model.Device{}))
if err != nil {
return nil, err
}
device = v.(*model.Device)
device.VPNs = current.VPNs
// data modified, dump new config
return device, nil
}
// DeleteDevice from database
func DeleteDevice(id string) error {
vpns, err := mongo.ReadAllVPNs("deviceid", id)
if err != nil {
return err
}
for _, vpn := range vpns {
err = DeleteVPN(vpn.Id)
if err != nil {
return err
}
}
return mongo.Delete(id, "id", "devices")
}
// ReadDeviceByApiKey(device.ApiKey)
func ReadDeviceByApiKey(apikey string) (*model.Device, error) {
v, err := mongo.Deserialize(apikey, "apiKey", "devices", reflect.TypeOf(model.Device{}))
if err != nil {
return nil, err
}
device := v.(*model.Device)
return device, nil
}
// ReadDevice2 device by param and id
func ReadDevice2(param string, id string) ([]*model.Device, error) {
return mongo.ReadAllDevices(param, id)
}
// ReadDevices all devices
func ReadDevices() ([]*model.Device, error) {
return mongo.ReadAllDevices("", "")
}
// ReadDevicesForAccount
func ReadDevicesForAccount(accountid string) ([]*model.Device, error) {
return mongo.ReadAllDevices("accountid", accountid)
}
// ReadDevices all devices
// This code needs a severe rewrite
func ReadDevicesForUser(email string) ([]*model.Device, error) {
accounts, err := mongo.ReadAllAccounts(email)
if err != nil {
return nil, err
}
results := make([]*model.Device, 0)
for _, account := range accounts {
if account.Status == "Active" {
if account.Role == "User" || account.Role == "Guest" {
// users and guests cannot see devices they did not create
} else {
if account.NetId != "" {
// read all the vpns with this netid
vpns, err := mongo.ReadVPNsforNetwork(account.NetId)
if err != nil {
return nil, err
}
for _, vpn := range vpns {
device := vpn.Devices[0]
vpn.Devices = nil
device.VPNs = append(device.VPNs, vpn)
results = append(results, device)
}
} else {
devices, err := mongo.ReadDevicesAndVPNsForAccount(account.Parent)
if err != nil {
return nil, err
}
results = append(results, devices...)
}
}
}
}
// now handle users and guests who can only see devices they created
vpns, err := mongo.ReadAllVPNs("createdBy", email)
if err != nil {
return nil, err
}
// now read devices created by this user and add any missing
devices, err := mongo.ReadAllDevices("createdBy", email)
if err != nil {
return nil, err
}
// loop through the results and add any missing devices
for _, device := range devices {
// add the device if it hasn't already been added
found := false
for _, result := range results {
if device.Id == result.Id {
found = true
break
}
}
if !found {
// associate any vpns to the device
for _, vpn := range vpns {
if device.Id == vpn.DeviceID {
device.VPNs = append(device.VPNs, vpn)
}
}
results = append(results, device)
}
}
return results, err
}