/
things.go
505 lines (456 loc) · 11.6 KB
/
things.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package types
import (
"bytes"
"encoding/gob"
"encoding/json"
"net/http"
"time"
"github.com/google/uuid"
)
const (
emptyContentType = `empty`
)
type Access struct {
Global bool
GIDs []int32
}
// Things are stored in the datastore, a common class of blobs.
type Thing struct {
UUID uuid.UUID
UID int32
GIDs []int32
Global bool
WriteAccess Access
Contents []byte
Updated time.Time
Synced bool
}
type ThingHeader struct {
ThingUUID uuid.UUID `json:",omitempty"`
UID int32
GIDs []int32 `json:",omitempty"`
Global bool
WriteAccess Access
}
func (t *Thing) Header() ThingHeader {
if t.WriteAccess.GIDs == nil {
t.WriteAccess.GIDs = []int32{}
}
return ThingHeader{
ThingUUID: t.UUID,
UID: t.UID,
GIDs: t.GIDs,
Global: t.Global,
WriteAccess: t.WriteAccess,
}
}
func (t *Thing) Encode() ([]byte, error) {
bb := bytes.NewBuffer(nil)
if err := gob.NewEncoder(bb).Encode(t); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
func (t *Thing) Decode(v []byte) error {
bb := bytes.NewBuffer(v)
if err := gob.NewDecoder(bb).Decode(t); err != nil {
return err
}
return nil
}
func (t *Thing) EncodeContents(obj interface{}) error {
bb := bytes.NewBuffer(nil)
if err := gob.NewEncoder(bb).Encode(obj); err != nil {
return err
}
t.Contents = bb.Bytes()
return nil
}
func (t *Thing) DecodeContents(obj interface{}) error {
bb := bytes.NewBuffer(t.Contents)
if err := gob.NewDecoder(bb).Decode(obj); err != nil {
return err
}
return nil
}
// TemplateContents is what goes in the template's Contents field. This is entirely
// the domain of the GUI.
type TemplateContents struct {
Query string `json:"query,omitempty"`
Variables []TemplateVariable `json:"variables"`
}
type TemplateVariable struct {
Name string `json:"name"`
Label string `json:"label"`
Description string `json:"description"`
Required bool `json:"required"`
DefaultValue string `json:"defaultValue"`
PreviewValue string `json:"previewValue"`
}
// UserTemplate is what is stored in the "thing" object, it is encoded into Contents
type UserTemplate struct {
GUID uuid.UUID
Name string
Description string
Contents TemplateContents
Labels []string
}
func (t UserTemplate) WireUserTemplate(thing Thing) WireUserTemplate {
return WireUserTemplate{
GUID: t.GUID,
ThingHeader: thing.Header(),
Updated: thing.Updated,
Labels: t.Labels,
Name: t.Name,
Description: t.Description,
Contents: t.Contents,
}
}
// WireUserTemplate is constructed from the UserTemplate and the details in the Thing
// struct. This is what we send to the user via the API.
type WireUserTemplate struct {
ThingHeader
GUID uuid.UUID
Name string
Description string
Contents TemplateContents
Updated time.Time
Labels []string
}
func (w WireUserTemplate) UserTemplate() (ut UserTemplate) {
return UserTemplate{
GUID: w.GUID,
Name: w.Name,
Description: w.Description,
Contents: w.Contents,
Labels: w.Labels,
}
}
func (w WireUserTemplate) Thing() (t Thing, err error) {
t.UUID = w.ThingUUID
t.UID = w.UID
t.GIDs = w.GIDs
t.Global = w.Global
t.WriteAccess = w.WriteAccess
if t.WriteAccess.GIDs == nil {
t.WriteAccess.GIDs = []int32{}
}
t.Updated = w.Updated
//do not set the synced value
err = t.EncodeContents(w.UserTemplate())
return
}
// type used for templates in packages
type PackedUserTemplate struct {
UUID string
Name string
Description string
Data TemplateContents
Labels []string
}
type oldTemplateContents struct {
Query string `json:"query"`
Variable string `json:"variable"`
VariableLabel string `json:"variableLabel"`
VariableDescription string `json:"variableDescription"`
Required bool `json:"required"`
TestValue string `json:"testValue"`
}
// A compatibility solution.
type newPackedUserTemplate PackedUserTemplate
type oldPackedUserTemplate struct {
UUID string
Name string
Description string
Data oldTemplateContents
Labels []string
}
func (t UserTemplate) Pack() (put PackedUserTemplate) {
if put.UUID = t.GUID.String(); put.UUID == `` {
put.UUID = uuid.New().String()
}
put.Name = t.Name
put.Description = t.Description
put.Data = t.Contents
put.Labels = t.Labels
return
}
func (t *PackedUserTemplate) UnmarshalJSON(data []byte) error {
// First try the current type
var nt newPackedUserTemplate
if err := json.Unmarshal(data, &nt); err != nil {
// something is majorly wrong.
return err
}
if len(nt.Data.Variables) > 0 {
*t = PackedUserTemplate(nt)
return nil
}
// If there are no variables in the result, try the old way.
var ot oldPackedUserTemplate
if err := json.Unmarshal(data, &ot); err != nil {
// something is majorly wrong.
return err
}
t.UUID = ot.UUID
t.Name = ot.Name
t.Description = ot.Description
t.Labels = ot.Labels
t.Data.Query = ot.Data.Query
t.Data.Variables = []TemplateVariable{{
Name: ot.Data.Variable,
Label: ot.Data.VariableLabel,
Description: ot.Data.VariableDescription,
Required: ot.Data.Required,
DefaultValue: ot.Data.TestValue,
}}
return nil
}
func (put *PackedUserTemplate) JSONMetadata() (json.RawMessage, error) {
b, err := json.Marshal(&struct {
UUID string
Name string
Description string
}{
UUID: put.UUID,
Name: put.Name,
Description: put.Description,
})
return json.RawMessage(b), err
}
// Pivot is what is stored in the "thing" object, it is encoded into Contents
type Pivot struct {
GUID uuid.UUID
Name string
Description string
Contents RawObject
Labels []string
Disabled bool
}
func (pivot Pivot) WirePivot(thing Thing) WirePivot {
return WirePivot{
GUID: pivot.GUID,
ThingHeader: thing.Header(),
Updated: thing.Updated,
Name: pivot.Name,
Description: pivot.Description,
Contents: pivot.Contents,
Labels: pivot.Labels,
Disabled: pivot.Disabled,
}
}
// WirePivot is constructed from the Pivot and the details in the Thing
// struct. This is what we send to the user via the API.
type WirePivot struct {
ThingHeader
GUID uuid.UUID
Name string
Description string
Updated time.Time
Contents RawObject
Labels []string
Disabled bool
}
// Thing Generates an encoded Thing from the WirePivot structure
func (wp WirePivot) Thing() (t Thing, err error) {
t.UUID = wp.ThingUUID
t.UID = wp.UID
t.GIDs = wp.GIDs
t.Global = wp.Global
t.WriteAccess = wp.WriteAccess
if t.WriteAccess.GIDs == nil {
t.WriteAccess.GIDs = []int32{}
}
t.Updated = wp.Updated
//do not set the synced value
err = t.EncodeContents(wp.Pivot())
return
}
// Pivot creates a Pivot structure from the WiredPivot
func (wp WirePivot) Pivot() Pivot {
return Pivot{
GUID: wp.GUID,
Name: wp.Name,
Description: wp.Description,
Contents: wp.Contents,
Labels: wp.Labels,
Disabled: wp.Disabled,
}
}
// type used for pivots in packages
type PackedPivot struct {
UUID string
Name string
Description string
Data RawObject
Labels []string
}
func (t Pivot) Pack() (put PackedPivot) {
if put.UUID = t.GUID.String(); put.UUID == `` {
put.UUID = uuid.New().String()
}
put.Name = t.Name
put.Description = t.Description
put.Data = t.Contents
put.Labels = t.Labels
return
}
func (put *PackedPivot) JSONMetadata() (json.RawMessage, error) {
b, err := json.Marshal(&struct {
UUID string
Name string
Description string
}{
UUID: put.UUID,
Name: put.Name,
Description: put.Description,
})
return json.RawMessage(b), err
}
// UserFile is what is actually stored in the thing object, it is encoded into contents
type UserFile struct {
GUID uuid.UUID
Name string
Desc string
Contents []byte `json:",omitempty"`
Labels []string
}
type WireUserFile struct {
ThingHeader
UserFile
Updated time.Time
}
func (w WireUserFile) Thing() (t Thing, err error) {
t.UUID = w.ThingUUID
t.UID = w.UID
t.GIDs = w.GIDs
t.Global = w.Global
t.WriteAccess = w.WriteAccess
if t.WriteAccess.GIDs == nil {
t.WriteAccess.GIDs = []int32{}
}
t.Updated = w.Updated
//do not set the synced value
err = t.EncodeContents(w.UserFile)
return
}
// UserFileDetails is a structure that is used to relay additional ownership information about a UserFile object
// This structure is populated via the things metadata, and does not contain any of the contents
type UserFileDetails struct {
GUID uuid.UUID
ThingUUID uuid.UUID
UID int32
GIDs []int32
Global bool
WriteAccess Access
Size int64 //size of the file
Type string //content type as determined by the http content type detector
Name string
Desc string
Updated time.Time
Labels []string
}
func (ufd *UserFileDetails) String() string {
if ufd.Name != `` {
return ufd.Name
}
return ufd.GUID.String()
}
func (uf *UserFile) Info() (sz int64, tp string) {
if sz = int64(len(uf.Contents)); sz > 0 {
tp = http.DetectContentType(uf.Contents)
} else {
tp = emptyContentType
}
return
}
func (uf *UserFile) JSONMetadata() (json.RawMessage, error) {
st := &struct {
UUID string
Name string
Description string
Size int64
ContentType string
}{
UUID: uf.GUID.String(),
Name: uf.Name,
Description: uf.Desc,
}
st.Size, st.ContentType = uf.Info()
b, err := json.Marshal(st)
return json.RawMessage(b), err
}
// WireSearchLibrary is what we actually send back and forth over the API
type WireSearchLibrary struct {
ThingHeader
SearchLibrary
Can Actions
Updated time.Time
}
type Actions struct {
Delete bool
Modify bool
Share bool
}
func (wsl WireSearchLibrary) Thing() (t Thing, err error) {
t.UUID = wsl.ThingUUID
t.UID = wsl.UID
t.GIDs = wsl.GIDs
t.Global = wsl.Global
t.WriteAccess = wsl.WriteAccess
if t.WriteAccess.GIDs == nil {
t.WriteAccess.GIDs = []int32{}
}
t.Updated = wsl.Updated
err = t.EncodeContents(wsl.SearchLibrary)
return
}
// SearchLibrary is a structure to store a search string and optional set of info
// The GUI uses this to build up a search library with info about a search
type SearchLibrary struct {
Name string
Description string
Query string `json:",omitempty"`
GUID uuid.UUID
Labels []string `json:",omitempty"`
Metadata RawObject `json:",omitempty"`
}
func (psl SearchLibrary) Equal(other SearchLibrary) (ok bool) {
ok = psl.Name == other.Name && psl.Description == other.Description && psl.Query == other.Query && psl.GUID == other.GUID
if !ok {
return
}
if ok = bytes.Equal(psl.Metadata, other.Metadata); !ok {
return
}
if ok = len(psl.Labels) == len(other.Labels); !ok {
return
}
for i := range psl.Labels {
if ok = (psl.Labels[i] == other.Labels[i]); !ok {
return
}
}
return
}
func (sl SearchLibrary) JSONMetadata() (json.RawMessage, error) {
b, err := json.Marshal(&struct {
Name string
Description string
Query string
UUID string
}{
Name: sl.Name,
Description: sl.Description,
Query: sl.Query,
UUID: sl.GUID.String(),
})
return json.RawMessage(b), err
}