-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgraphql.go
720 lines (647 loc) · 17.1 KB
/
graphql.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package retraced
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
)
type StructuredQuery struct {
Action string
CRUD string
ReceivedStart time.Time
ReceivedEnd time.Time
CreatedStart time.Time
CreatedEnd time.Time
ActorName string
ActorID string
Description string
Location string
}
func maybeRFC3339(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format(time.RFC3339)
}
func (sq *StructuredQuery) String() string {
var params []string
if sq.Action != "" {
param := fmt.Sprintf("action:%q", sq.Action)
params = append(params, param)
}
if sq.CRUD != "" {
param := fmt.Sprintf("crud:%q", sq.CRUD)
params = append(params, param)
}
received := fmt.Sprintf("%s,%s", maybeRFC3339(sq.ReceivedStart), maybeRFC3339(sq.ReceivedEnd))
if received != "," {
params = append(params, "received:"+received)
}
created := fmt.Sprintf("%s,%s", maybeRFC3339(sq.CreatedStart), maybeRFC3339(sq.CreatedEnd))
if created != "," {
params = append(params, "created:"+created)
}
if sq.ActorID != "" {
param := fmt.Sprintf("actor.id:%s", sq.ActorID)
params = append(params, param)
}
if sq.ActorName != "" {
param := fmt.Sprintf("actor.name:%q" + sq.ActorName)
params = append(params, param)
}
if sq.Description != "" {
param := fmt.Sprintf("description:%q", sq.Description)
params = append(params, param)
}
if sq.Location != "" {
param := fmt.Sprintf("location:%q", sq.Location)
params = append(params, param)
}
return strings.Join(params, " ")
}
// EventNode represents an event returned from the Retraced GraphQL API. Some
// fields are identical to the reported Event field and others are modified or
// added.
type EventNode struct {
// ID is the uuid generated by the Retraced API when the event was
// reported.
ID string `json:"id"`
// Action is the reported Action.
Action string `json:"action"`
// Group contains an augmented form of the reported group: if the
// reported event included a group.ID and no group.Name, but the group
// Name could be inferred from the group.ID at the time of creation,
// then group.Name will be defined.
Group *Group `json:"group"`
// Created is the reported Created timestamp.
Created time.Time `json:"created"`
// CRUD is the reported CRUD field.
CRUD string
// Target is an augmented form of the reported target. If the reported
// event included a target.ID, then target.Name, target.Type,
// target.Href, and target.Fields will be populated with any data known
// about the target at the time the event was reported.
Target *Target `json:"target"`
// Description is the reported Description.
Description string `json:"description"`
// SourceIP is the reported SourceIP.
SourceIP string `json:"source_ip"`
// Actor is an augmented form of the reported actor. If the reported
// event included an actor.ID, then actor.Name, actor.Href, and
// actor.Fields will be populated with any data known about the actor
// at the time the event was reported.
Actor *Actor `json:"actor"`
// Fields contains the reported Fields.
Fields Fields `json:"fields"`
// IsFailure is the reported IsFailure flag.
IsFailure bool `json:"is_failure"`
// IsAnonymous is the reported IsAnonymous flag.
IsAnonymous bool `json:"is_anonymous"`
// Component is the reported Component.
Component string `json:"component"`
// Version is the reported Version.
Version string `json:"version"`
// Display the holds the display templates for the event.
Display *Display `json:"display"`
// Country is populated from the reported SourceIP.
Country string `json:"country"`
// LocSubdiv1 is populated from the reported SourceIP.
LocSubdiv1 string `json:"loc_subdiv1"`
// LocSubdiv2 is populated from the reported SourceIP.
LocSubdiv2 string `json:"loc_subdiv2"`
// Received is the timestamp the Retraced API received the reported event.
Received time.Time `json:"received"`
// CanonicalTime is the reported Created timestamp if defined, else the
// Received time.
CanonicalTime time.Time `json:"canonical_time"`
// Raw is the json of the original reported event.
Raw string `json:"raw"`
ExternalID string `json:"external_id"`
Metadata Fields `json:"metadata"`
}
// https://preview.retraced.io/documentation/advanced-retraced/display-templates/
type Display struct {
Markdown string `json:"markdown"`
}
// EventNodeMask specifies fields to retrieve in a GraphQL search.
type EventNodeMask struct {
ID bool
Action bool
CRUD bool
Description bool
IsFailure bool
IsAnonymous bool
SourceIP bool
Country bool
LocSubdiv1 bool
LocSubdiv2 bool
Received bool
Created bool
CanonicalTime bool
Component bool
Version bool
Fields bool
Raw bool
ExternalID bool
Metadata bool
GroupID bool
GroupName bool
ActorID bool
ActorName bool
ActorHref bool
ActorFields bool
TargetID bool
TargetName bool
TargetHref bool
TargetType bool
TargetFields bool
DisplayMarkdown bool
}
// AnyGroup is true iff any group field (id or name) is on.
func (mask *EventNodeMask) AnyGroup() bool {
return mask.GroupID || mask.GroupName
}
// AnyActor is true iff any actor field is on.
func (mask *EventNodeMask) AnyActor() bool {
return mask.ActorID || mask.ActorName || mask.ActorHref || mask.ActorFields
}
// AnyTarget is true iff any target field is on.
func (mask *EventNodeMask) AnyTarget() bool {
return mask.TargetID || mask.TargetName || mask.TargetHref || mask.TargetType || mask.TargetFields
}
// AnyDisplay is true iff any display field is on.
func (mask *EventNodeMask) AnyDisplay() bool {
return mask.DisplayMarkdown
}
var searchTmplSource = `query Search($query: String!, $last: Int, $before: String) {
search(query: $query, last: $last, before: $before) {
totalCount,
pageInfo {
hasPreviousPage
}
edges {
cursor
node {
{{if .ID -}} id {{- end}}
{{if .Action -}} action {{- end}}
{{if .CRUD -}} crud {{- end}}
{{if .Description -}} description {{- end}}
{{if .IsFailure -}} is_failure {{- end}}
{{if .IsAnonymous -}} is_anonymous {{- end}}
{{if .SourceIP -}} source_ip {{- end}}
{{if .Country -}} country {{- end}}
{{if .LocSubdiv1 -}} loc_subdiv1 {{- end}}
{{if .LocSubdiv2 -}} loc_subdiv2 {{- end}}
{{if .Received -}} received {{- end}}
{{if .Created -}} created {{- end}}
{{if .CanonicalTime -}} canonical_time {{- end}}
{{if .Component -}} component {{- end}}
{{if .Version -}} version {{- end}}
{{if .Raw -}} raw {{- end}}
{{if .Fields -}} fields {
key
value
} {{- end}}
{{if .ExternalID -}} external_id {{- end}}
{{if .Metadata -}} metadata {
key
value
} {{- end}}
{{if .AnyGroup -}} group {
{{if .GroupID -}} id {{- end}}
{{if .GroupName -}} name {{- end}}
}{{end}}
{{if .AnyActor -}} actor {
{{if .ActorID -}} id {{- end}}
{{if .ActorName -}} name {{- end}}
{{if .ActorHref -}} href {{- end}}
{{if .ActorFields -}} fields {
key
value
} {{- end}}
}{{end}}
{{if .AnyTarget -}} target {
{{if .TargetID -}} id {{- end}}
{{if .TargetName -}} name {{- end}}
{{if .TargetHref -}} href {{- end}}
{{if .TargetType -}} type {{- end}}
{{if .TargetFields -}} fields {
key
value
} {{- end}}
}{{end}}
{{if .AnyDisplay -}} display{
{{if .DisplayMarkdown }}markdown{{end}}
}{{end}}
}
}
}
}`
var searchTmpl = template.Must(template.New("search").Parse(searchTmplSource))
// SearchOpQuery generates the graphQL query body string for a search operation.
func (mask *EventNodeMask) SearchOpQuery() (string, error) {
var buf bytes.Buffer
err := searchTmpl.Execute(&buf, mask)
if err != nil {
return "", err
}
return buf.String(), nil
}
// CSVHeaders generates a row of header fields
func (mask *EventNodeMask) CSVHeaders() []string {
var headers []string
if mask.ID {
headers = append(headers, "id")
}
if mask.Action {
headers = append(headers, "action")
}
if mask.CRUD {
headers = append(headers, "crud")
}
if mask.Description {
headers = append(headers, "description")
}
if mask.IsFailure {
headers = append(headers, "is_failure")
}
if mask.IsAnonymous {
headers = append(headers, "is_anonymous")
}
if mask.SourceIP {
headers = append(headers, "source_ip")
}
if mask.Country {
headers = append(headers, "country")
}
if mask.LocSubdiv1 {
headers = append(headers, "loc_subdiv1")
}
if mask.LocSubdiv2 {
headers = append(headers, "loc_subdiv2")
}
if mask.Received {
headers = append(headers, "received")
}
if mask.Created {
headers = append(headers, "created")
}
if mask.CanonicalTime {
headers = append(headers, "canonical_time")
}
if mask.Component {
headers = append(headers, "component")
}
if mask.Version {
headers = append(headers, "version")
}
if mask.Fields {
headers = append(headers, "fields")
}
if mask.Raw {
headers = append(headers, "raw")
}
if mask.ExternalID {
headers = append(headers, "external_id")
}
if mask.Metadata {
headers = append(headers, "metadata")
}
if mask.GroupID {
headers = append(headers, "group_id")
}
if mask.GroupName {
headers = append(headers, "group_name")
}
if mask.ActorID {
headers = append(headers, "actor_id")
}
if mask.ActorName {
headers = append(headers, "actor_name")
}
if mask.ActorHref {
headers = append(headers, "actor_href")
}
if mask.ActorFields {
headers = append(headers, "actor_fields")
}
if mask.TargetID {
headers = append(headers, "target_id")
}
if mask.TargetName {
headers = append(headers, "target_name")
}
if mask.TargetHref {
headers = append(headers, "target_href")
}
if mask.TargetType {
headers = append(headers, "target_type")
}
if mask.TargetFields {
headers = append(headers, "target_fields")
}
if mask.DisplayMarkdown {
headers = append(headers, "display_markdown")
}
return headers
}
// CSVRow formats an event as a csv row
func (mask *EventNodeMask) CSVRow(e *EventNode) []string {
var fields []string
if mask.ID {
fields = append(fields, e.ID)
}
if mask.Action {
fields = append(fields, e.Action)
}
if mask.CRUD {
fields = append(fields, e.CRUD)
}
if mask.Description {
fields = append(fields, e.Description)
}
if mask.IsFailure {
fields = append(fields, strconv.FormatBool(e.IsFailure))
}
if mask.IsAnonymous {
fields = append(fields, strconv.FormatBool(e.IsAnonymous))
}
if mask.SourceIP {
fields = append(fields, e.SourceIP)
}
if mask.Country {
fields = append(fields, e.Country)
}
if mask.LocSubdiv1 {
fields = append(fields, e.LocSubdiv1)
}
if mask.LocSubdiv2 {
fields = append(fields, e.LocSubdiv2)
}
if mask.Received {
fields = append(fields, e.Received.Format(time.RFC3339))
}
if mask.Created {
fields = append(fields, e.Created.Format(time.RFC3339))
}
if mask.CanonicalTime {
fields = append(fields, e.CanonicalTime.Format(time.RFC3339))
}
if mask.Component {
fields = append(fields, e.Component)
}
if mask.Version {
fields = append(fields, e.Version)
}
if mask.Fields {
fields = append(fields, e.Fields.String())
}
if mask.Raw {
fields = append(fields, e.Raw)
}
if mask.ExternalID {
fields = append(fields, e.ExternalID)
}
if mask.Metadata {
fields = append(fields, e.Metadata.String())
}
if mask.GroupID {
var groupID string
if e.Group != nil {
groupID = e.Group.ID
}
fields = append(fields, groupID)
}
if mask.GroupName {
var groupName string
if e.Group != nil {
groupName = e.Group.Name
}
fields = append(fields, groupName)
}
if mask.ActorID {
var actorID string
if e.Actor != nil {
actorID = e.Actor.ID
}
fields = append(fields, actorID)
}
if mask.ActorName {
var actorName string
if e.Actor != nil {
actorName = e.Actor.Name
}
fields = append(fields, actorName)
}
if mask.ActorHref {
var actorHref string
if e.Actor != nil {
actorHref = e.Actor.Href
}
fields = append(fields, actorHref)
}
if mask.ActorFields {
var actorFields string
if e.Actor != nil {
actorFields = e.Actor.Fields.String()
}
fields = append(fields, actorFields)
}
if mask.TargetID {
var targetID string
if e.Target != nil {
targetID = e.Target.ID
}
fields = append(fields, targetID)
}
if mask.TargetName {
var targetName string
if e.Target != nil {
targetName = e.Target.Name
}
fields = append(fields, targetName)
}
if mask.TargetHref {
var targetHref string
if e.Target != nil {
targetHref = e.Target.Href
}
fields = append(fields, targetHref)
}
if mask.TargetType {
var targetType string
if e.Target != nil {
targetType = e.Target.Type
}
fields = append(fields, targetType)
}
if mask.TargetFields {
var targetFields string
if e.Target != nil {
targetFields = e.Target.Fields.String()
}
fields = append(fields, targetFields)
}
if mask.DisplayMarkdown {
var displayMarkdown string
if e.Display != nil {
displayMarkdown = e.Display.Markdown
}
fields = append(fields, displayMarkdown)
}
return fields
}
type graphQLSearchVariables struct {
Last int `json:"last,omitempty"`
Before string `json:"before,omitempty"`
Query string `json:"query"`
}
type graphQLSearchBody struct {
Query string `json:"query"`
Variables *graphQLSearchVariables `json:"variables,omitempty"`
}
type pageInfo struct {
HasPreviousPage bool `json:"hasPreviousPage"`
}
type EventEdge struct {
Cursor string `json:"cursor"`
Node *EventNode `json:"node"`
}
type graphQLSearch struct {
TotalCount int `json:"totalCount"`
PageInfo pageInfo `json:"pageInfo"`
Edges []*EventEdge `json:"edges"`
}
type graphQLSearchData struct {
Search graphQLSearch `json:"search"`
}
type graphQLSearchRoot struct {
Data graphQLSearchData `json:"data"`
}
// EventsConnection handles cursor-based pagination over query results.
type EventsConnection struct {
url string
authorization string
structuredQuery *StructuredQuery
mask *EventNodeMask
cursors []string
pageSize int
httpClient *http.Client
// If this connection's mask specifies fields in a nested struct, then
// that struct will be non-nil for results. For example, if the mask
// specifies GroupID, then Group will be non-nil even on events reported
// without a group ID.
currentResults []*EventNode
totalCount int
// 1-indexed
currentPageNumber int
}
func (ec *EventsConnection) NextPage() error {
return ec.call()
}
func (ec *EventsConnection) TotalPages() int {
pages := ec.totalCount / ec.pageSize
if ec.totalCount%ec.pageSize != 0 {
pages++
}
return pages
}
func (ec *EventsConnection) HasNextPage() bool {
return ec.currentPageNumber < ec.TotalPages()
}
func (ec *EventsConnection) HasPreviousPage() bool {
return ec.currentPageNumber > 1
}
func (ec *EventsConnection) CurrentPageNumber() int {
return ec.currentPageNumber
}
func (ec *EventsConnection) CurrentResults() []*EventNode {
return ec.currentResults
}
func (ec *EventsConnection) TotalCount() int {
return ec.totalCount
}
// cursor returns the last item in EventsConnection.cursors
func (ec *EventsConnection) cursor() string {
n := len(ec.cursors)
if n == 0 {
ec.cursors = []string{""}
n = 1
}
return ec.cursors[n-1]
}
func (ec *EventsConnection) call() error {
graphQLQuery, err := ec.mask.SearchOpQuery()
if err != nil {
return err
}
eventQuery := ""
if ec.structuredQuery != nil {
eventQuery = ec.structuredQuery.String()
}
encoded, err := json.Marshal(&graphQLSearchBody{
Query: graphQLQuery,
Variables: &graphQLSearchVariables{
Last: ec.pageSize,
Before: ec.cursor(),
Query: eventQuery,
},
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", ec.url, bytes.NewBuffer(encoded))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", ec.authorization)
req.Header.Set("Accept", "application/json")
resp, err := ec.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if body, err := io.ReadAll(resp.Body); err == nil {
log.Printf("GraphQL error response: %s", body)
}
return fmt.Errorf("unexpected response from retraced api endpoint %s: %d", req.URL.String(), resp.StatusCode)
}
root := &graphQLSearchRoot{}
err = json.NewDecoder(resp.Body).Decode(root)
if err != nil {
return err
}
ec.totalCount = root.Data.Search.TotalCount
ec.currentPageNumber = len(ec.cursors)
hits := len(root.Data.Search.Edges)
events := make([]*EventNode, 0, hits)
for i, edge := range root.Data.Search.Edges {
event := edge.Node
if ec.mask.AnyGroup() && event.Group == nil {
event.Group = &Group{}
}
if ec.mask.AnyActor() && event.Actor == nil {
event.Actor = &Actor{}
}
if ec.mask.AnyTarget() && event.Target == nil {
event.Target = &Target{}
}
if ec.mask.AnyDisplay() && event.Display == nil {
event.Display = &Display{}
}
events = append(events, edge.Node)
if root.Data.Search.PageInfo.HasPreviousPage && i == hits-1 {
ec.cursors = append(ec.cursors, edge.Cursor)
}
}
ec.currentResults = events
return nil
}