-
Notifications
You must be signed in to change notification settings - Fork 16
/
api.go
3041 lines (2589 loc) · 94.2 KB
/
api.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package sigsci provides methods for interacting with the Signal Sciences API.
package sigsci
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// apiURL is the base URL for API requests
var apiURL = "https://dashboard.signalsciences.net/api"
// SetAPIUrl allows developer to point to a different endpoint in special circumstances (e.g. during testing)
func SetAPIUrl(theURL string) {
apiURL = theURL
}
// Client is the API client
type Client struct {
email string
token string
fastlyKey string
}
// NewClient authenticates and returns a Client API client
func NewClient(email, password string) (Client, error) {
sc := Client{}
err := sc.authenticate(email, password)
if err != nil {
return Client{}, err
}
return sc, nil
}
func (c *Client) SetFastlyKey(fastlyKey string) {
c.fastlyKey = fastlyKey
}
// NewTokenClient creates a Client using token authentication
func NewTokenClient(email, token string) Client {
return Client{
email: email,
token: token,
}
}
// authenticate takes email/password and authenticates, attaching the
// returned token to the API client.
func (sc *Client) authenticate(email, password string) error {
resp, err := http.PostForm(apiURL+"/v0/auth", url.Values{"email": {email}, "password": {password}})
if err != nil {
return err
}
defer resp.Body.Close()
var tr struct {
Token string
}
err = json.NewDecoder(resp.Body).Decode(&tr)
if err != nil {
return err
}
sc.token = tr.Token
return nil
}
func (sc *Client) doRequest(method, url, reqBody string) ([]byte, error) {
resp, err := sc.doRequestDetailed(method, url, reqBody)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
switch method {
case "GET":
if resp.StatusCode != http.StatusOK {
return body, errMsg(body)
}
case "POST":
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusNoContent:
default:
return body, errMsg(body)
}
case "DELETE":
switch resp.StatusCode {
case http.StatusOK, http.StatusNoContent:
default:
return body, errMsg(body)
}
case "PUT":
fallthrough
case "PATCH":
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return body, errMsg(body)
}
}
return body, nil
}
func errMsg(b []byte) error {
var errResp struct {
Message string
}
err := json.Unmarshal(b, &errResp)
if err != nil {
return err
}
return errors.New(errResp.Message)
}
// Corp contains details for a corp.
type Corp struct {
Name string
DisplayName string
SmallIconURI string
Created time.Time
SiteLimit int
Sites map[string]string
AuthType string
MFAEnforced bool
SessionMaxAgeDashboard int
}
// corpsResponse is the response for list corps
type corpsResponse struct {
Data []Corp
}
// ListCorps lists corps.
func (sc *Client) ListCorps() ([]Corp, error) {
resp, err := sc.doRequest("GET", "/v0/corps", "")
if err != nil {
return []Corp{}, err
}
var cr corpsResponse
err = json.Unmarshal(resp, &cr)
if err != nil {
return []Corp{}, err
}
return cr.Data, nil
}
// GetCorp gets a corp by name.
func (sc *Client) GetCorp(corpName string) (Corp, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s", corpName), "")
if err != nil {
return Corp{}, err
}
var corp Corp
err = json.Unmarshal(resp, &corp)
if err != nil {
return Corp{}, err
}
return corp, nil
}
// UpdateCorpBody is the body for the UpdateCorp method.
type UpdateCorpBody struct {
DisplayName string `json:"displayName,omitempty"`
SmallIconURI string `json:"smallIconURI,omitempty"`
SessionMaxAgeDashboard int `json:"sessionMaxAgeDashboard,omitempty"`
}
// UpdateCorp updates a corp by name.
func (sc *Client) UpdateCorp(corpName string, body UpdateCorpBody) (Corp, error) {
b, err := json.Marshal(body)
if err != nil {
return Corp{}, err
}
resp, err := sc.doRequest("PATCH", fmt.Sprintf("/v0/corps/%s", corpName), string(b))
if err != nil {
return Corp{}, err
}
var corp Corp
err = json.Unmarshal(resp, &corp)
if err != nil {
return Corp{}, err
}
return corp, nil
}
// Role is a corp or site role
type Role string
// All available Roles
const (
RoleNoAccess = Role("none")
RoleUnknown = Role("unknown")
RoleOwner = Role("owner")
RoleAdmin = Role("admin")
RoleUser = Role("user")
RoleObserver = Role("observer")
// Deprecated corp/site roles
RoleSiteNoAccess = Role("none")
RoleSiteUnknown = Role("unknown")
RoleSiteOwner = Role("owner")
RoleSiteAdmin = Role("admin")
RoleSiteUser = Role("user")
RoleSiteObserver = Role("observer")
RoleCorpOwner = Role("corpOwner")
RoleCorpUser = Role("corpUser")
)
// CorpUser contains details for a corp user.
type CorpUser struct {
Name string
Email string
Memberships map[string]string
Role string
Status string
MFAEnabled bool
AuthStatus string
Created time.Time
}
// corpUsersResponse is the response for list corp users
type corpUsersResponse struct {
Data []CorpUser
}
// ListCorpUsers lists corp users.
func (sc *Client) ListCorpUsers(corpName string) ([]CorpUser, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/users", corpName), "")
if err != nil {
return []CorpUser{}, err
}
var cur corpUsersResponse
err = json.Unmarshal(resp, &cur)
if err != nil {
return []CorpUser{}, err
}
return cur.Data, nil
}
// GetCorpUser gets a corp user by email.
func (sc *Client) GetCorpUser(corpName, email string) (CorpUser, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/users/%s", corpName, email), "")
if err != nil {
return CorpUser{}, err
}
var cu CorpUser
err = json.Unmarshal(resp, &cu)
if err != nil {
return CorpUser{}, err
}
return cu, nil
}
// DeleteCorpUser deletes a user from the given corp.
func (sc *Client) DeleteCorpUser(corpName, email string) error {
_, err := sc.doRequest("DELETE", fmt.Sprintf("/v0/corps/%s/users/%s", corpName, email), "")
return err
}
type site struct {
Name string `json:"name"`
}
// SiteMembership contains the data needed for inviting a member to a site.
type SiteMembership struct {
Site site `json:"site"`
Role Role `json:"role"`
}
// NewSiteMembership returns a new site membership object for the given
// site name and role.
func NewSiteMembership(name string, role Role) SiteMembership {
return SiteMembership{
Site: site{Name: name},
Role: role,
}
}
type inviteMemberships struct {
Data []SiteMembership `json:"data"`
}
// CorpUserInvite is the request struct for inviting a user to a corp.
type CorpUserInvite struct {
Role Role `json:"role"`
Memberships inviteMemberships `json:"memberships"`
}
// NewCorpUserInvite creates a new invitation struct for inviting a user to a corp.
func NewCorpUserInvite(corpRole Role, memberships []SiteMembership) CorpUserInvite {
return CorpUserInvite{
Role: corpRole,
Memberships: inviteMemberships{
Data: memberships,
},
}
}
// InviteUser invites a user by email to a corp.
func (sc *Client) InviteUser(corpName, email string, invite CorpUserInvite) (CorpUser, error) {
body, err := json.Marshal(invite)
if err != nil {
return CorpUser{}, err
}
resp, err := sc.doRequest("POST", fmt.Sprintf("/v0/corps/%s/users/%s/invite", corpName, email), string(body))
if err != nil {
return CorpUser{}, err
}
var cu CorpUser
err = json.Unmarshal(resp, &cu)
if err != nil {
return CorpUser{}, err
}
return cu, nil
}
type topAttackType struct {
TagName string
TagCount int
TotalCount int
}
type topAttackSource struct {
CountryCode string
CountryName string
RequestCount int
TotalCount int
}
// OverviewSite is a site in the overview report.
type OverviewSite struct {
Name string
DisplayName string
TotalCount int
BlockedCount int
FlaggedCount int
AttackCount int
FlaggedIPCount int
TopAttackTypes []topAttackType
TopAttackSources []topAttackSource
}
// overviewResponse contains the overview report data.
type overviewResponse struct {
Data []OverviewSite
}
// GetOverviewReport gets the overview report data for a given corp.
func (sc *Client) GetOverviewReport(corpName string, query url.Values) ([]OverviewSite, error) {
url := fmt.Sprintf("/v0/corps/%s/reports/attacks", corpName)
if query.Encode() != "" {
url += "?" + query.Encode()
}
resp, err := sc.doRequest("GET", url, "")
if err != nil {
return []OverviewSite{}, err
}
var or overviewResponse
err = json.Unmarshal(resp, &or)
if err != nil {
return []OverviewSite{}, err
}
return or.Data, nil
}
// ActivityEvent contains the data for activity page responses.
type ActivityEvent struct {
ID string
EventType string
MsgData map[string]string
Message string
Attachments []struct{}
Created time.Time
}
// activityResponse is the response for the activity events endpoints.
type activityResponse struct {
TotalCount int
Next map[string]string
Data []ActivityEvent
}
// ListCorpActivity lists activity events for a given corp.
func (sc *Client) ListCorpActivity(corpName string, limit, page int) ([]ActivityEvent, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/activity?limit=%d&page=%d", corpName, limit, page), "")
if err != nil {
return []ActivityEvent{}, err
}
var ar activityResponse
err = json.Unmarshal(resp, &ar)
if err != nil {
return []ActivityEvent{}, err
}
return ar.Data, nil
}
type ClientIPRules []struct {
Header string `json:"header"`
}
// Site contains details for a site.
type Site struct {
Name string
DisplayName string
AgentLevel string
BlockHTTPCode int
BlockDurationSeconds int
BlockRedirectURL string
Created time.Time
Whitelist map[string]string
Blacklist map[string]string
Events map[string]string
Requests map[string]string
Redactions map[string]string
SuspiciousIPs map[string]string
Monitors map[string]string
Pathwhitelist map[string]string
Paramwhitelist map[string]string
Integrations map[string]string
HeaderLinks map[string]string
Agents map[string]string
Alerts map[string]string
AnalyticsEvents map[string]string
TopAttacks map[string]string
Members map[string]string
AgentAnonMode string
ClientIPRules ClientIPRules
AttackThresholds []AttackThreshold
ImmediateBlock bool
}
// sitesResponse is the response for list sites.
type sitesResponse struct {
Data []Site
}
// ListSites lists sites for a given corp.
func (sc *Client) ListSites(corpName string) ([]Site, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites", corpName), "")
if err != nil {
return []Site{}, err
}
var sr sitesResponse
err = json.Unmarshal(resp, &sr)
if err != nil {
return []Site{}, err
}
return sr.Data, nil
}
// GetSite gets a site by name.
func (sc *Client) GetSite(corpName, siteName string) (Site, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s", corpName, siteName), "")
if err != nil {
return Site{}, err
}
var site Site
err = json.Unmarshal(resp, &site)
if err != nil {
return Site{}, err
}
return site, nil
}
// UpdateSiteBody is the body for the update site method.
type UpdateSiteBody struct {
AgentLevel string `json:"agentLevel,omitempty"`
AgentAnonMode string `json:"agentAnonMode"`
AttackThresholds []AttackThreshold `json:"attackThresholds,omitempty"` // Slice of AttackThreshold
BlockDurationSeconds int `json:"blockDurationSeconds,omitempty"`
BlockHTTPCode int `json:"blockHTTPCode,omitempty"`
BlockRedirectURL string `json:"blockRedirectURL,omitempty"`
ClientIPRules ClientIPRules `json:"clientIPRules"`
DisplayName string `json:"displayName,omitempty"`
ImmediateBlock bool `json:"immediateBlock"` // Enable immediate blocking
}
// UpdateSite updates a site by name.
func (sc *Client) UpdateSite(corpName, siteName string, body UpdateSiteBody) (Site, error) {
b, err := json.Marshal(body)
if err != nil {
return Site{}, err
}
resp, err := sc.doRequest("PATCH", fmt.Sprintf("/v0/corps/%s/sites/%s", corpName, siteName), string(b))
if err != nil {
return Site{}, err
}
var site Site
err = json.Unmarshal(resp, &site)
if err != nil {
return Site{}, err
}
return site, nil
}
// CustomAlert is the body for creating a custom alert.
type CustomAlert struct {
ID string `json:"id,omitempty"` // Site-specific unique ID of the alert
SiteID string `json:"siteID,omitempty"`
TagName string `json:"tagName,omitempty"` // The name of the tag whose occurrences the alert is watching. Must match an existing tag
LongName string `json:"longName,omitempty"` // A human readable description of the alert. Must be between 3 and 25 characters.
Interval int `json:"interval"` // The number of minutes of past traffic to examine. Must be 1, 10 or 60.
Threshold int `json:"threshold"` // The number of occurrences of the tag in the interval needed to trigger the alert.
BlockDurationSeconds int `json:"blockDurationSeconds,omitempty"` // The number of seconds this alert is active. empty means default value
Enabled bool `json:"enabled"` // A flag to toggle this alert.
Action string `json:"action,omitempty"` // A flag that describes what happens when the alert is triggered. 'info' creates an incident in the dashboard. 'flagged' creates an incident and blocks traffic for 24 hours.
Type string `json:"type,omitempty"` // Type of alert (siteAlert, template, rateLimit, siteMetric)
SkipNotifications bool `json:"skipNotifications"` // A flag to disable external notifications - slack, webhooks, emails, etc.
FieldName string `json:"fieldName,omitempty"`
CreatedBy string `json:"createdBy,omitempty"` // The email of the user that created the alert
Created time.Time `json:"created,omitempty"` // RFC3339 date time
Operator string `json:"operator,omitempty"`
}
// customAlertsResponse is the response for the alerts endpoint
type customAlertsResponse struct {
Data []CustomAlert
}
// ListCustomAlerts lists custom alerts for a given corp and site.
func (sc *Client) ListCustomAlerts(corpName, siteName string) ([]CustomAlert, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/alerts", corpName, siteName), "")
if err != nil {
return []CustomAlert{}, err
}
var car customAlertsResponse
err = json.Unmarshal(resp, &car)
if err != nil {
return []CustomAlert{}, err
}
return car.Data, nil
}
// CustomAlertBody is the body for creating a custom alert.
type CustomAlertBody struct {
TagName string `json:"tagName"`
LongName string `json:"longName"`
Interval int `json:"interval"`
Threshold int `json:"threshold"`
Enabled bool `json:"enabled"`
Action string `json:"action"`
SkipNotifications bool `json:"skipNotifications"`
BlockDurationSeconds int `json:"blockDurationSeconds,omitempty"`
Operator string `json:"operator,omitempty"`
FieldName string `json:"fieldName,omitempty"`
}
// CreateCustomAlert creates a custom alert.
func (sc *Client) CreateCustomAlert(corpName, siteName string, body CustomAlertBody) (CustomAlert, error) {
b, err := json.Marshal(body)
if err != nil {
return CustomAlert{}, err
}
resp, err := sc.doRequest("POST", fmt.Sprintf("/v0/corps/%s/sites/%s/alerts", corpName, siteName), string(b))
if err != nil {
return CustomAlert{}, err
}
var c CustomAlert
err = json.Unmarshal(resp, &c)
if err != nil {
return CustomAlert{}, err
}
return c, nil
}
// GetCustomAlert gets a custom alert by ID
func (sc *Client) GetCustomAlert(corpName, siteName, id string) (CustomAlert, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/alerts/%s", corpName, siteName, id), "")
if err != nil {
return CustomAlert{}, err
}
var ca CustomAlert
err = json.Unmarshal(resp, &ca)
if err != nil {
return CustomAlert{}, err
}
return ca, nil
}
// UpdateCustomAlert updates a custom alert by id.
func (sc *Client) UpdateCustomAlert(corpName, siteName, id string, body CustomAlertBody) (CustomAlert, error) {
b, err := json.Marshal(body)
if err != nil {
return CustomAlert{}, err
}
resp, err := sc.doRequest("PATCH", fmt.Sprintf("/v0/corps/%s/sites/%s/alerts/%s", corpName, siteName, id), string(b))
if err != nil {
return CustomAlert{}, err
}
var c CustomAlert
err = json.Unmarshal(resp, &c)
if err != nil {
return CustomAlert{}, err
}
return c, err
}
// DeleteCustomAlert deletes a custom alert.
func (sc *Client) DeleteCustomAlert(corpName, siteName, id string) error {
_, err := sc.doRequest("DELETE", fmt.Sprintf("/v0/corps/%s/sites/%s/alerts/%s", corpName, siteName, id), "")
return err
}
// Event is a request event.
type Event struct {
ID string
Timestamp time.Time
Source string
RemoteCountryCode string
RemoteHostname string
UserAgents []string
Action string
Type string
Reasons map[string]int
RequestCount int
TagCount int
Window int
Expires time.Time
ExpiredBy string
}
type eventsResponse struct {
TotalCount int
Next map[string]string
Data []Event
}
// ListEvents lists events for a given site.
func (sc *Client) ListEvents(corpName, siteName string, query url.Values) ([]Event, error) {
url := fmt.Sprintf("/v0/corps/%s/sites/%s/events", corpName, siteName)
if query.Encode() != "" {
url += "?" + query.Encode()
}
resp, err := sc.doRequest("GET", url, "")
if err != nil {
return []Event{}, err
}
var er eventsResponse
err = json.Unmarshal(resp, &er)
if err != nil {
return []Event{}, err
}
return er.Data, nil
}
// GetEvent gets an event by ID.
func (sc *Client) GetEvent(corpName, siteName, id string) (Event, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/events/%s", corpName, siteName, id), "")
if err != nil {
return Event{}, err
}
var e Event
err = json.Unmarshal(resp, &e)
if err != nil {
return Event{}, err
}
return e, nil
}
// ExpireEvent expires an event by ID.
func (sc *Client) ExpireEvent(corpName, siteName, id string) (Event, error) {
resp, err := sc.doRequest("POST", fmt.Sprintf("/v0/corps/%s/sites/%s/events/%s/expire", corpName, siteName, id), "")
if err != nil {
return Event{}, err
}
var e Event
err = json.Unmarshal(resp, &e)
if err != nil {
return Event{}, err
}
return e, nil
}
// RequestTag is a tag in a request
type RequestTag struct {
Type string `json:"type"`
Location string `json:"location"`
Value string `json:"value"`
Detector string `json:"detector"`
}
// Request contains the data for a request
type Request struct {
ID string `json:"id"`
ServerHostname string `json:"serverHostname"`
RemoteIP string `json:"remoteIP"`
RemoteHostname string `json:"remoteHostname"`
RemoteCountryCode string `json:"remoteCountryCode"`
UserAgent string `json:"userAgent"`
Timestamp time.Time `json:"timestamp"`
Method string `json:"method"`
ServerName string `json:"serverName"`
Protocol string `json:"protocol"`
TLSProtocol string `json:"tlsProtocol"`
TLSCipher string `json:"tlsCipher"`
Scheme string `json:"scheme"`
HeadersIn [][]string `json:"headersIn"`
Path string `json:"path"`
URI string `json:"uri"`
ResponseCode int `json:"responseCode"`
ResponseSize int `json:"responseSize"`
ResponseMillis int `json:"responseMillis"`
HeadersOut [][]string `json:"headersOut"`
AgentResponseCode int `json:"agentResponseCode"`
Tags []RequestTag `json:"tags"`
}
// requestsResponse is the response for the search requests endpoint
type requestsResponse struct {
TotalCount int
Next map[string]string
Data []Request
}
// SearchRequests searches requests.
func (sc *Client) SearchRequests(corpName, siteName string, query url.Values) (totalCount int, next string, requests []Request, err error) {
url := fmt.Sprintf("/v0/corps/%s/sites/%s/requests", corpName, siteName)
if query.Encode() != "" {
url += "?" + query.Encode()
}
resp, err := sc.doRequest("GET", url, "")
if err != nil {
return 0, "", []Request{}, err
}
var r requestsResponse
err = json.Unmarshal(resp, &r)
if err != nil {
return 0, "", []Request{}, err
}
return r.TotalCount, r.Next["uri"], r.Data, nil
}
// GetRequest gets a request by id.
func (sc *Client) GetRequest(corpName, siteName, id string) (Request, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/requests/%s", corpName, siteName, id), "")
if err != nil {
return Request{}, err
}
var r Request
err = json.Unmarshal(resp, &r)
if err != nil {
return Request{}, err
}
return r, nil
}
// requestFeedResponse is the response for the requests feed endpoint.
type requestFeedResponse struct {
Next map[string]string
Data []Request
}
// GetRequestFeed gets the request feed for the site.
func (sc *Client) GetRequestFeed(corpName, siteName string, query url.Values) (next string, requests []Request, err error) {
url := fmt.Sprintf("/v0/corps/%s/sites/%s/feed/requests", corpName, siteName)
if query.Encode() != "" {
url += "?" + query.Encode()
}
resp, err := sc.doRequest("GET", url, "")
if err != nil {
return "", []Request{}, err
}
var r requestFeedResponse
err = json.Unmarshal(resp, &r)
if err != nil {
return "", []Request{}, err
}
return r.Next["uri"], r.Data, nil
}
// ListIP is a whitelisted or blacklisted IP address.
type ListIP struct {
ID string
Source string
Expires time.Time `json:"omitempty"`
Note string
CreatedBy string
Created time.Time
}
// whitelistResponse is the response for the whitelist endpoint.
type whitelistResponse struct {
Data []ListIP
}
// ListWhitelistIPs lists whitelisted IP addresses.
func (sc *Client) ListWhitelistIPs(corpName, siteName string) ([]ListIP, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/whitelist", corpName, siteName), "")
if err != nil {
return []ListIP{}, err
}
var wr whitelistResponse
err = json.Unmarshal(resp, &wr)
if err != nil {
return []ListIP{}, err
}
return wr.Data, nil
}
// ListIPBody is the body for adding an IP to the whitelist or blacklist.
type ListIPBody struct {
Source string `json:"source"`
Note string `json:"note"`
Expires time.Time `json:"expires,omitempty"`
}
// MarshalJSON is a custom JSON marshal method for ListIPBody
// so that Expires can be formatted as RFC3339
func (b ListIPBody) MarshalJSON() ([]byte, error) {
var expires string
if (b.Expires != time.Time{}) {
expires = b.Expires.Format(time.RFC3339)
}
return json.Marshal(struct {
Source string `json:"source"`
Note string `json:"note"`
Expires string `json:"expires,omitempty"`
}{
Source: b.Source,
Note: b.Note,
Expires: expires,
})
}
// AddWhitelistIP adds an IP address to the whitelist.
func (sc *Client) AddWhitelistIP(corpName, siteName string, body ListIPBody) (ListIP, error) {
b, err := json.Marshal(body)
if err != nil {
return ListIP{}, err
}
resp, err := sc.doRequest("POST", fmt.Sprintf("/v0/corps/%s/sites/%s/whitelist", corpName, siteName), string(b))
if err != nil {
return ListIP{}, err
}
var ip ListIP
err = json.Unmarshal(resp, &ip)
if err != nil {
return ListIP{}, err
}
return ip, nil
}
// DeleteWhitelistIP deletes a whitelisted IP by id.
func (sc *Client) DeleteWhitelistIP(corpName, siteName, id string) error {
_, err := sc.doRequest("DELETE", fmt.Sprintf("/v0/corps/%s/sites/%s/whitelist/%s", corpName, siteName, id), "")
return err
}
// blacklistResponse is the response for the blacklist endpoint.
type blacklistResponse struct {
Data []ListIP
}
// ListBlacklistIPs lists blacklisted IP addresses.
func (sc *Client) ListBlacklistIPs(corpName, siteName string) ([]ListIP, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/blacklist", corpName, siteName), "")
if err != nil {
return []ListIP{}, err
}
var br blacklistResponse
err = json.Unmarshal(resp, &br)
if err != nil {
return []ListIP{}, err
}
return br.Data, nil
}
// AddBlacklistIP adds an IP address to the blacklist.
func (sc *Client) AddBlacklistIP(corpName, siteName string, body ListIPBody) (ListIP, error) {
b, err := json.Marshal(body)
if err != nil {
return ListIP{}, err
}
resp, err := sc.doRequest("POST", fmt.Sprintf("/v0/corps/%s/sites/%s/blacklist", corpName, siteName), string(b))
if err != nil {
return ListIP{}, err
}
var ip ListIP
err = json.Unmarshal(resp, &ip)
if err != nil {
return ListIP{}, err
}
return ip, nil
}
// DeleteBlacklistIP deletes a blacklisted IP by id.
func (sc *Client) DeleteBlacklistIP(corpName, siteName, id string) error {
_, err := sc.doRequest("DELETE", fmt.Sprintf("/v0/corps/%s/sites/%s/blacklist/%s", corpName, siteName, id), "")
return err
}
// Redaction contains the data for a privacy redaction
type Redaction struct {
ID string
Field string
RedactionType int
CreatedBy string
Created time.Time
}
// redactionsResponse is the response for the list redactions endpoint
type redactionsResponse struct {
Data []Redaction
}
// ListRedactions lists redactions.
func (sc *Client) ListRedactions(corpName, siteName string) ([]Redaction, error) {
resp, err := sc.doRequest("GET", fmt.Sprintf("/v0/corps/%s/sites/%s/redactions", corpName, siteName), "")
if err != nil {
return []Redaction{}, err
}
var rr redactionsResponse
err = json.Unmarshal(resp, &rr)
if err != nil {
return []Redaction{}, err
}
return rr.Data, nil
}
// RedactionBody is the body for adding a redaction.
// Type of redaction (0: Request Parameter, 1: Request Header, 2: Response Header)
type RedactionBody struct {