-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.go
659 lines (545 loc) · 18.4 KB
/
items.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package sdp
import (
"context"
"crypto/sha256"
"encoding/base32"
"encoding/json"
"errors"
"fmt"
"reflect"
"sort"
"strings"
"time"
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
const WILDCARD = "*"
// Copy copies all information from one item pointer to another
func (liq *BlastPropagation) Copy(dest *BlastPropagation) {
dest.In = liq.GetIn()
dest.Out = liq.GetOut()
}
// Copy copies all information from one item pointer to another
func (liq *LinkedItemQuery) Copy(dest *LinkedItemQuery) {
dest.Query = &Query{}
if liq.GetQuery() != nil {
liq.GetQuery().Copy(dest.GetQuery())
}
dest.BlastPropagation = &BlastPropagation{}
if liq.GetBlastPropagation() != nil {
liq.GetBlastPropagation().Copy(dest.GetBlastPropagation())
}
}
// Copy copies all information from one item pointer to another
func (li *LinkedItem) Copy(dest *LinkedItem) {
dest.Item = &Reference{}
if li.GetItem() != nil {
li.GetItem().Copy(dest.GetItem())
}
dest.BlastPropagation = &BlastPropagation{}
if li.GetBlastPropagation() != nil {
li.GetBlastPropagation().Copy(dest.GetBlastPropagation())
}
}
// UniqueAttributeValue returns the value of whatever the Unique Attribute is
// for this item. This will then be converted to a string and returned
func (i *Item) UniqueAttributeValue() string {
var value interface{}
var err error
value, err = i.GetAttributes().Get(i.GetUniqueAttribute())
if err == nil {
return fmt.Sprint(value)
}
return ""
}
// Reference returns an SDP reference for the item
func (i *Item) Reference() *Reference {
return &Reference{
Scope: i.GetScope(),
Type: i.GetType(),
UniqueAttributeValue: i.UniqueAttributeValue(),
}
}
// GloballyUniqueName Returns a string that defines the Item globally. This a
// combination of the following values:
//
// - scope
// - type
// - uniqueAttributeValue
//
// They are concatenated with dots (.)
func (i *Item) GloballyUniqueName() string {
return strings.Join([]string{
i.GetScope(),
i.GetType(),
i.UniqueAttributeValue(),
},
".",
)
}
// Copy copies all information from one item pointer to another
func (i *Item) Copy(dest *Item) {
// Values can be copied directly
dest.Type = i.GetType()
dest.UniqueAttribute = i.GetUniqueAttribute()
dest.Scope = i.GetScope()
// We need to check that any pointers are actually populated with pointers
// to somewhere in memory. If they are nil then there is no data structure
// to copy the data into, therefore we need to create it first
if dest.GetMetadata() == nil {
dest.Metadata = &Metadata{}
}
if dest.GetAttributes() == nil {
dest.Attributes = &ItemAttributes{}
}
i.GetMetadata().Copy(dest.GetMetadata())
i.GetAttributes().Copy(dest.GetAttributes())
dest.LinkedItemQueries = make([]*LinkedItemQuery, 0)
dest.LinkedItems = make([]*LinkedItem, 0)
for _, r := range i.GetLinkedItemQueries() {
liq := &LinkedItemQuery{}
r.Copy(liq)
dest.LinkedItemQueries = append(dest.LinkedItemQueries, liq)
}
for _, r := range i.GetLinkedItems() {
newLinkedItem := &LinkedItem{}
r.Copy(newLinkedItem)
dest.LinkedItems = append(dest.LinkedItems, newLinkedItem)
}
if i.Health == nil {
dest.Health = nil
} else {
dest.Health = i.GetHealth().Enum()
}
if i.Tags != nil {
dest.Tags = make(map[string]string)
for k, v := range i.GetTags() {
dest.Tags[k] = v
}
}
}
// Hash Returns a 12 character hash for the item. This is likely but not
// guaranteed to be unique. The hash is calculated using the GloballyUniqueName
func (i *Item) Hash() string {
return hashSum(([]byte(fmt.Sprint(i.GloballyUniqueName()))))
}
// Hash Returns a 12 character hash for the item. This is likely but not
// guaranteed to be unique. The hash is calculated using the GloballyUniqueName
func (r *Reference) Hash() string {
return hashSum(([]byte(fmt.Sprint(r.GloballyUniqueName()))))
}
// GloballyUniqueName Returns a string that defines the Item globally. This a
// combination of the following values:
//
// - scope
// - type
// - uniqueAttributeValue
//
// They are concatenated with dots (.)
func (r *Reference) GloballyUniqueName() string {
return strings.Join([]string{
r.GetScope(),
r.GetType(),
r.GetUniqueAttributeValue(),
},
".",
)
}
// Copy copies all information from one Reference pointer to another
func (r *Reference) Copy(dest *Reference) {
dest.Type = r.GetType()
dest.UniqueAttributeValue = r.GetUniqueAttributeValue()
dest.Scope = r.GetScope()
}
// Copy copies all information from one Metadata pointer to another
func (m *Metadata) Copy(dest *Metadata) {
if m == nil {
// Protect from copy being called on a nil pointer
return
}
dest.SourceName = m.GetSourceName()
dest.Hidden = m.GetHidden()
if m.GetSourceQuery() != nil {
dest.SourceQuery = &Query{}
m.GetSourceQuery().Copy(dest.GetSourceQuery())
}
dest.Timestamp = ×tamppb.Timestamp{
Seconds: m.GetTimestamp().GetSeconds(),
Nanos: m.GetTimestamp().GetNanos(),
}
dest.SourceDuration = &durationpb.Duration{
Seconds: m.GetSourceDuration().GetSeconds(),
Nanos: m.GetSourceDuration().GetNanos(),
}
dest.SourceDurationPerItem = &durationpb.Duration{
Seconds: m.GetSourceDurationPerItem().GetSeconds(),
Nanos: m.GetSourceDurationPerItem().GetNanos(),
}
}
// Copy copies all information from one CancelQuery pointer to another
func (c *CancelQuery) Copy(dest *CancelQuery) {
if c == nil {
return
}
dest.UUID = c.GetUUID()
}
// Get Returns the value of a given attribute by name. If the attribute is
// a nested hash, nested values can be referenced using dot notation e.g.
// location.country
func (a *ItemAttributes) Get(name string) (interface{}, error) {
var result interface{}
// Start at the beginning of the map, we will then traverse down as required
result = a.GetAttrStruct().AsMap()
for _, section := range strings.Split(name, ".") {
// Check that the data we're using is in the supported format
var m map[string]interface{}
m, isMap := result.(map[string]interface{})
if !isMap {
return nil, fmt.Errorf("attribute %v not found", name)
}
v, keyExists := m[section]
if keyExists {
result = v
} else {
return nil, fmt.Errorf("attribute %v not found", name)
}
}
return result, nil
}
// Set sets an attribute. Values are converted to structpb versions and an error
// will be returned if this fails. Note that this does *not* yet support
// dot notation e.g. location.country
func (a *ItemAttributes) Set(name string, value interface{}) error {
// Check to make sure that the pointer is not nil
if a == nil {
return errors.New("Set called on nil pointer")
}
// Ensure that this interface will be able to be converted to a struct value
sanitizedValue := sanitizeInterface(value, false, DefaultTransforms)
structValue, err := structpb.NewValue(sanitizedValue)
if err != nil {
return err
}
fields := a.GetAttrStruct().GetFields()
fields[name] = structValue
return nil
}
// Copy copies all information from one ItemAttributes pointer to another
func (a *ItemAttributes) Copy(dest *ItemAttributes) {
m := a.GetAttrStruct().AsMap()
dest.AttrStruct, _ = structpb.NewStruct(m)
}
// Copy copies all information from one Query pointer to another
func (qrb *Query_RecursionBehaviour) Copy(dest *Query_RecursionBehaviour) {
dest.LinkDepth = qrb.GetLinkDepth()
dest.FollowOnlyBlastPropagation = qrb.GetFollowOnlyBlastPropagation()
}
// Subject returns a NATS subject for all traffic relating to this query
func (q *Query) Subject() string {
return fmt.Sprintf("query.%v", q.ParseUuid())
}
// Copy copies all information from one Query pointer to another
func (q *Query) Copy(dest *Query) {
dest.Type = q.GetType()
dest.Method = q.GetMethod()
dest.Query = q.GetQuery()
dest.RecursionBehaviour = &Query_RecursionBehaviour{}
if q.GetRecursionBehaviour() != nil {
q.GetRecursionBehaviour().Copy(dest.GetRecursionBehaviour())
}
dest.Scope = q.GetScope()
dest.IgnoreCache = q.GetIgnoreCache()
dest.UUID = q.GetUUID()
if q.GetDeadline() != nil {
// allocate a new value
dest.Deadline = timestamppb.New(q.GetDeadline().AsTime())
}
}
// TimeoutContext returns a context and cancel function representing the timeout
// for this request
func (q *Query) TimeoutContext(ctx context.Context) (context.Context, context.CancelFunc) {
// If there is no deadline, treat that as infinite
if q == nil || !q.GetDeadline().IsValid() {
return context.WithCancel(ctx)
}
return context.WithDeadline(ctx, q.GetDeadline().AsTime())
}
// ParseUuid returns this request's UUID. If there's an error parsing it,
// generates and stores a fresh one
func (r *Query) ParseUuid() uuid.UUID {
// Extract and parse the UUID
reqUUID, uuidErr := uuid.FromBytes(r.GetUUID())
if uuidErr != nil {
reqUUID = uuid.New()
r.UUID = reqUUID[:]
}
return reqUUID
}
func (x *CancelQuery) GetUUIDParsed() *uuid.UUID {
u, err := uuid.FromBytes(x.GetUUID())
if err != nil {
return nil
}
return &u
}
func (x *UndoQuery) GetUUIDParsed() *uuid.UUID {
u, err := uuid.FromBytes(x.GetUUID())
if err != nil {
return nil
}
return &u
}
func (x *Expand) GetUUIDParsed() *uuid.UUID {
u, err := uuid.FromBytes(x.GetUUID())
if err != nil {
return nil
}
return &u
}
func (x *UndoExpand) GetUUIDParsed() *uuid.UUID {
u, err := uuid.FromBytes(x.GetUUID())
if err != nil {
return nil
}
return &u
}
// Converts to attributes using an additional set of custom transformers. These
// can be used to change the transform behaviour of known types to do things
// like redaction of sensitive data or simplification of complex types.
//
// For example this could be used to completely remove anything of type `Secret`:
//
// ```go
//
// TransformMap{
// reflect.TypeOf(Secret{}): func(i interface{}) interface{} {
// // Remove it
// return "REDACTED"
// },
// }
//
// ```
func ToAttributesCustom(m map[string]interface{}, sort bool, customTransforms TransformMap) (*ItemAttributes, error) {
// Add the default transforms
for k, v := range DefaultTransforms {
if _, ok := customTransforms[k]; !ok {
customTransforms[k] = v
}
}
return toAttributes(m, sort, customTransforms)
}
// Converts a map[string]interface{} to an ItemAttributes object, sorting all
// slices alphabetically.This should be used when the item doesn't contain array
// attributes that are explicitly sorted, especially if these are sometimes
// returned in a different order
func ToAttributesSorted(m map[string]interface{}) (*ItemAttributes, error) {
return toAttributes(m, true, DefaultTransforms)
}
// ToAttributes Converts a map[string]interface{} to an ItemAttributes object
func ToAttributes(m map[string]interface{}) (*ItemAttributes, error) {
return toAttributes(m, false, DefaultTransforms)
}
func toAttributes(m map[string]interface{}, sort bool, customTransforms TransformMap) (*ItemAttributes, error) {
if m == nil {
return nil, nil
}
var s map[string]*structpb.Value
var err error
s = make(map[string]*structpb.Value)
// Loop over the map
for k, v := range m {
sanitizedValue := sanitizeInterface(v, sort, customTransforms)
structValue, err := structpb.NewValue(sanitizedValue)
if err != nil {
return nil, err
}
s[k] = structValue
}
return &ItemAttributes{
AttrStruct: &structpb.Struct{
Fields: s,
},
}, err
}
// ToAttributesViaJson Converts any struct to a set of attributes by marshalling
// to JSON and then back again. This is less performant than ToAttributes() but
// does save work when copying large structs to attributes in their entirety
func ToAttributesViaJson(v interface{}) (*ItemAttributes, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
var m map[string]interface{}
err = json.Unmarshal(b, &m)
if err != nil {
return nil, err
}
return ToAttributes(m)
}
// A function that transforms one data type into another that is compatible with
// protobuf. This is used to convert things like time.Time into a string
type TransformFunc func(interface{}) interface{}
// A map of types to transform functions
type TransformMap map[reflect.Type]TransformFunc
// The default transforms that are used when converting to attributes
var DefaultTransforms = TransformMap{
// Time should be in RFC3339Nano format i.e. 2006-01-02T15:04:05.999999999Z07:00
reflect.TypeOf(time.Time{}): func(i interface{}) interface{} {
return i.(time.Time).Format(time.RFC3339Nano)
},
// Duration should be in string format
reflect.TypeOf(time.Duration(0)): func(i interface{}) interface{} {
return i.(time.Duration).String()
},
}
// sanitizeInterface Ensures that en interface is in a format that can be
// converted to a protobuf value. The structpb.ToValue() function expects things
// to be in one of the following formats:
//
// ╔════════════════════════╤════════════════════════════════════════════╗
// ║ Go type │ Conversion ║
// ╠════════════════════════╪════════════════════════════════════════════╣
// ║ nil │ stored as NullValue ║
// ║ bool │ stored as BoolValue ║
// ║ int, int32, int64 │ stored as NumberValue ║
// ║ uint, uint32, uint64 │ stored as NumberValue ║
// ║ float32, float64 │ stored as NumberValue ║
// ║ string │ stored as StringValue; must be valid UTF-8 ║
// ║ []byte │ stored as StringValue; base64-encoded ║
// ║ map[string]interface{} │ stored as StructValue ║
// ║ []interface{} │ stored as ListValue ║
// ╚════════════════════════╧════════════════════════════════════════════╝
//
// However this means that a data type like []string won't work, despite the
// function being perfectly able to represent it in a protobuf struct. This
// function does its best to example the available data type to ensure that as
// long as the data can in theory be represented by a protobuf struct, the
// conversion will work.
func sanitizeInterface(i interface{}, sortArrays bool, customTransforms TransformMap) interface{} {
if i == nil {
return nil
}
v := reflect.ValueOf(i)
t := v.Type()
// Use the transform for this specific type if it exists
if tFunc, ok := customTransforms[t]; ok {
// Reset the value and type to the transformed value. This means that
// even if the function returns something bad, we will then transform it
i = tFunc(i)
if i == nil {
return nil
}
v = reflect.ValueOf(i)
t = v.Type()
}
switch v.Kind() { // nolint:exhaustive // we fall through to the default case
case reflect.Bool:
return v.Bool()
case reflect.Int:
return v.Int()
case reflect.Int8:
return v.Int()
case reflect.Int16:
return v.Int()
case reflect.Int32:
return v.Int()
case reflect.Int64:
return v.Int()
case reflect.Uint:
return v.Uint()
case reflect.Uint8:
return v.Uint()
case reflect.Uint16:
return v.Uint()
case reflect.Uint32:
return v.Uint()
case reflect.Uint64:
return v.Uint()
case reflect.Float32:
return v.Float()
case reflect.Float64:
return v.Float()
case reflect.String:
return fmt.Sprint(v)
case reflect.Array, reflect.Slice:
// We need to check the type of each element in the array and do
// conversion on that
// returnSlice Returns the array in the format that protobuf can deal with
var returnSlice []interface{}
returnSlice = make([]interface{}, v.Len())
for index := 0; index < v.Len(); index++ {
returnSlice[index] = sanitizeInterface(v.Index(index).Interface(), sortArrays, customTransforms)
}
if sortArrays {
sortInterfaceArray(returnSlice)
}
return returnSlice
case reflect.Map:
var returnMap map[string]interface{}
returnMap = make(map[string]interface{})
for _, mapKey := range v.MapKeys() {
// Convert the key to a string
stringKey := fmt.Sprint(mapKey.Interface())
// Convert the value to a compatible interface
value := sanitizeInterface(v.MapIndex(mapKey).Interface(), sortArrays, customTransforms)
returnMap[stringKey] = value
}
return returnMap
case reflect.Struct:
// In the case of a struct we basically want to turn it into a
// map[string]interface{}
var returnMap map[string]interface{}
returnMap = make(map[string]interface{})
// Range over fields
n := t.NumField()
for i := 0; i < n; i++ {
field := t.Field(i)
if field.PkgPath != "" {
// If this has a PkgPath then it is an un-exported fiend and
// should be ignored
continue
}
// Get the zero value for this field
zeroValue := reflect.Zero(field.Type).Interface()
fieldValue := v.Field(i).Interface()
// Check if the field is it's nil value
// Check if there actually was a field with that name
if !reflect.DeepEqual(fieldValue, zeroValue) {
returnMap[field.Name] = fieldValue
}
}
return sanitizeInterface(returnMap, sortArrays, customTransforms)
case reflect.Ptr:
// Get the zero value for this field
zero := reflect.Zero(t)
// Check if the field is it's nil value
if reflect.DeepEqual(v, zero) {
return nil
}
return sanitizeInterface(v.Elem().Interface(), sortArrays, customTransforms)
default:
// If we don't recognize the type then we need to see what the
// underlying type is and see if we can convert that
return i
}
}
// Sorts an interface slice by converting each item to a string and sorting
// these strings
func sortInterfaceArray(input []interface{}) {
sort.Slice(input, func(i, j int) bool {
return fmt.Sprint(input[i]) < fmt.Sprint(input[j])
})
}
func hashSum(b []byte) string {
var paddedEncoding *base32.Encoding
var unpaddedEncoding *base32.Encoding
shaSum := sha256.Sum256(b)
// We need to specify a custom encoding here since dGraph has fairly strict
// requirements about what name a variable can have
paddedEncoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyzABCDEF")
// We also can't have padding since "=" is not allowed in variable names
unpaddedEncoding = paddedEncoding.WithPadding(base32.NoPadding)
return unpaddedEncoding.EncodeToString(shaSum[:11])
}