-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonExport.go
209 lines (174 loc) · 7.95 KB
/
jsonExport.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
package ffs
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
)
type JsonFileEvent struct {
Actor string `json:"actor,omitempty"`
CloudDriveId string `json:"cloudDriveId,omitempty"`
CreateTimestamp string `json:"createTimestamp,omitempty"`
DestinationCategory string `json:"destinationCategory,omitempty"`
DestinationName string `json:"destinationName,omitempty"`
DetectionSourceAlias string `json:"detectionSourceAlias,omitempty"`
DeviceUid string `json:"deviceUid,omitempty"`
DeviceUserName string `json:"deviceUserName,omitempty"`
DirectoryId []string `json:"directoryId,omitempty"`
DomainName string `json:"domainName,omitempty"`
EmailDlpPolicyNames []string `json:"emailDlpPolicyNames,omitempty"`
EmailFrom string `json:"emailFrom,omitempty"`
EmailRecipients []string `json:"emailRecipients,omitempty"`
EmailSender string `json:"emailSender,omitempty"`
EmailSubject string `json:"emailSubject,omitempty"`
EventId string `json:"eventId"`
EventTimestamp string `json:"eventTimestamp,omitempty"`
EventType string `json:"eventType,omitempty"`
Exposure []string `json:"exposure,omitempty"`
FieldErrors []FieldError `json:"fieldErrors,omitempty"`
FileCategory string `json:"fileCategory,omitempty"`
FileCategoryByBytes string `json:"fileCategoryByBytes,omitempty"`
FileCategoryByExtension string `json:"fileCategoryByExtension,omitempty"`
FileId string `json:"fileId,omitempty"`
FileName string `json:"fileName,omitempty"`
FileOwner string `json:"fileOwner,omitempty"`
FilePath string `json:"filePath,omitempty"`
FileSize *int64 `json:"fileSize,omitempty"`
FileType string `json:"fileType,omitempty"`
InsertionTimestamp string `json:"insertionTimestamp,omitempty"`
Md5Checksum string `json:"md5Checksum,omitempty"`
MimeTypeByBytes string `json:"mimeTypeByBytes,omitempty"`
MimeTypeByExtension string `json:"mimeTypeByExtension,omitempty"`
MimeTypeMismatch *bool `json:"mimeTypeMismatch,omitempty"`
ModifyTimestamp string `json:"modifyTimestamp,omitempty"`
OperatingSystemUser string `json:"operatingSystemUser,omitempty"`
OsHostName string `json:"osHostName,omitempty"`
OutsideActiveHours *bool `json:"outsideActiveHours,omitempty"`
PrintJobName string `json:"printJobName,omitempty"`
PrinterName string `json:"printerName,omitempty"`
PrivateIpAddresses []string `json:"privateIpAddresses,omitempty"`
ProcessName string `json:"processName,omitempty"`
ProcessOwner string `json:"processOwner,omitempty"`
PublicIpAddress string `json:"publicIpAddress,omitempty"`
RemoteActivity string `json:"remoteActivity,omitempty"`
RemovableMediaBusType string `json:"removableMediaBusType,omitempty"`
RemovableMediaCapacity *int64 `json:"removableMediaCapacity,omitempty"`
RemovableMediaMediaName string `json:"removableMediaMediaName,omitempty"`
RemovableMediaName string `json:"removableMediaName,omitempty"`
RemovableMediaPartitionId []string `json:"removableMediaPartitionId,omitempty"`
RemovableMediaSerialNumber string `json:"removableMediaSerialNumber,omitempty"`
RemovableMediaVendor string `json:"removableMediaVendor,omitempty"`
RemovableMediaVolumeName []string `json:"removableMediaVolumeName,omitempty"`
Sha256Checksum string `json:"sha256Checksum,omitempty"`
Shared string `json:"shared,omitempty"`
SharedWith []SharedWith `json:"sharedWith,omitempty"`
SharingTypeAdded []string `json:"sharingTypeAdded,omitempty"`
Source string `json:"source,omitempty"`
SyncDestination string `json:"syncDestination,omitempty"`
SyncDestinationUsername []string `json:"syncDestinationUsername,omitempty"`
TabUrl string `json:"tabUrl,omitempty"`
Tabs []Tab `json:"tabs,omitempty"`
Trusted *bool `json:"trusted,omitempty"`
Url string `json:"url,omitempty"`
UserUid string `json:"userUid,omitempty"`
WindowTitle []string `json:"windowTitle,omitempty"`
}
type FieldError struct {
Error string `json:"error,omitempty"`
Field string `json:"field,omitempty"`
}
type SharedWith struct {
CloudUsername *string `json:"cloudUsername,omitempty"`
}
type Tab struct {
Title string `json:"title,omitempty"`
Url string `json:"url,omitempty"`
}
type JsonFileEventResponse struct {
FileEvents []JsonFileEvent `json:"fileEvents,omitempty"`
NextPgToken string `json:"nextPgToken,omitempty"`
Problems []QueryProblem `json:"problems,omitempty"`
TotalCount *int64 `json:"totalCount,omitempty"`
}
func GetJsonFileEventResponse(resp *http.Response) (*JsonFileEventResponse, error) {
var eventResponse JsonFileEventResponse
//Read Response Body as JSON
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &eventResponse)
if err != nil {
return nil, err
}
return &eventResponse, nil
}
func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken string, debugging bool) (*[]JsonFileEvent, string, error) {
var jsonFileEvents []JsonFileEvent
if pgToken != "" {
query.PgToken = pgToken
}
//Validate jsonQuery is valid JSON
ffsQuery, err := json.Marshal(query)
if err != nil {
return nil, "", errors.New("jsonQuery is not in a valid json format")
}
//Make sure authData token is not ""
if authData.AccessToken == "" {
return nil, "", errors.New("authData cannot be nil")
}
//Query ffsURI with authData API token and jsonQuery body
req, err := http.NewRequest("POST", ffsURI, bytes.NewReader(ffsQuery))
//Handle request errors
if err != nil {
return nil, "", err
}
//Set request headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+authData.AccessToken)
//Get Response
resp, err := http.DefaultClient.Do(req)
//Handle response errors
if err != nil {
return nil, "", err
}
//defer body close
defer resp.Body.Close()
//Make sure http status code is 200
if resp.StatusCode != http.StatusOK {
return nil, "", errors.New("Error with gathering file events POST: " + resp.Status)
}
fileEventResponse, err := GetJsonFileEventResponse(resp)
if err != nil {
return nil, "", err
}
if fileEventResponse.Problems != nil {
problems, err := json.Marshal(fileEventResponse.Problems)
if err != nil {
return nil, "", err
}
return nil, "", errors.New(string(problems))
}
if len(fileEventResponse.FileEvents) == 0 {
fileEventResponse.FileEvents = nil
} else {
jsonFileEvents = append(jsonFileEvents, fileEventResponse.FileEvents...)
}
var nextJsonFileEvents *[]JsonFileEvent
if fileEventResponse.NextPgToken != "" {
if debugging {
log.Print("Next Page Token: ")
log.Println(fileEventResponse.NextPgToken)
}
nextJsonFileEvents, _, err = GetJsonFileEvents(authData, ffsURI, query, fileEventResponse.NextPgToken, debugging)
if err != nil {
return nil, "", err
}
if nextJsonFileEvents != nil && len(*nextJsonFileEvents) != 0 {
jsonFileEvents = append(jsonFileEvents, *nextJsonFileEvents...)
}
}
return &jsonFileEvents, "", nil
}