-
Notifications
You must be signed in to change notification settings - Fork 17
/
api.go
375 lines (318 loc) · 8.37 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
/*************************************************************************
* 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 (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
const (
//MAJOR API VERSIONS should always be compatible, there just may be
//additional features
API_VERSION_MAJOR uint32 = 0
API_VERSION_MINOR uint32 = 2
)
// Helpers for the marshaling functions
var (
emptyList = []byte(`[]`)
emptyObj = []byte(`{}`)
emptyRawObj = RawObject(`{}`)
emptyString = []byte(`""`)
jsonNull = []byte(`null`)
)
type RawObject json.RawMessage
type es struct{}
var empty es
// ErrorObject is a basic error object with the error value and an optional
// info structure that has more info about the error
type ErrorObject struct {
Err string `json:"error"`
Info string `json:"info,omitempty"`
}
type VersionInfo struct {
API ApiInfo
Build BuildInfo
}
type ApiInfo struct {
Major uint32
Minor uint32
}
type BuildInfo struct {
CanonicalVersion
BuildDate time.Time `json:",omitempty"`
BuildID string `json:",omitempty"`
GUIBuildID string `json:",omitempty"`
}
// The full version of Gravwell in this build - eg 4.1.2
type CanonicalVersion struct {
Major uint32
Minor uint32
Point uint32
}
func ApiVersion() ApiInfo {
return ApiInfo{
Major: API_VERSION_MAJOR,
Minor: API_VERSION_MINOR,
}
}
// Return the full build version of Gravwell eg "4.1.2"
func (bi BuildInfo) Version() string {
return bi.CanonicalVersion.String()
}
func (bi BuildInfo) String() string {
return fmt.Sprintf("%s (%s) %s [GUI: %s]",
bi.CanonicalVersion.String(),
bi.BuildDate.Format(`2006-01-02`), bi.BuildID, bi.GUIBuildID)
}
func (bi BuildInfo) NewerVersion(nbi BuildInfo) bool {
return bi.CanonicalVersion.NewerVersion(nbi.CanonicalVersion)
}
func CheckApiVersion(remote ApiInfo) error {
local := ApiVersion()
if local.Major == remote.Major {
return nil //we match
}
return fmt.Errorf("Version mismatch!\nLocal: %d.%d\nRemote %d.%d\n",
local.Major, local.Minor, remote.Major, remote.Minor)
}
func parseUint32(v string) (r uint32, err error) {
var x uint64
if x, err = strconv.ParseUint(v, 10, 32); err == nil {
r = uint32(x)
}
return
}
// Return a CanonicalVersion object containing the given version string. Must
// be in the form of "X.Y.Z".
func ParseCanonicalVersion(s string) (r CanonicalVersion, err error) {
var bits []string
if s = strings.TrimSpace(s); len(s) == 0 {
//return, this is the zero value
return
} else if bits = strings.Split(s, `.`); len(bits) != 3 {
err = errors.New("Malformed version string")
return
}
//at this point we know we have 3 bits
if r.Major, err = parseUint32(bits[0]); err == nil {
if r.Minor, err = parseUint32(bits[1]); err == nil {
r.Point, err = parseUint32(bits[2])
}
}
return
}
// NewerVersion returns true if the incoming version is newer than coming
func (cv CanonicalVersion) NewerVersion(ncv CanonicalVersion) bool {
return cv.Compare(ncv) > 0
}
// Compare returns the following:
//
// 0 - equal versions
// <0 - incoming is older than existing
// >0 - incoming is newer then existing
func (cv CanonicalVersion) Compare(ncv CanonicalVersion) int {
if ncv.Major > cv.Major {
return 1 //incoming newer
} else if cv.Major > ncv.Major {
return -1 //incoming older
}
//same major
if ncv.Minor > cv.Minor {
return 1 //incoming newer
} else if cv.Minor > ncv.Minor {
return -1 //incoming older
}
//same major and minor
if ncv.Point > cv.Point {
return 1 //incoming newer
} else if cv.Point > ncv.Point {
return -1 //incoming older
}
return 0 // same version
}
func (cv CanonicalVersion) String() string {
return fmt.Sprintf("%d.%d.%d", cv.Major, cv.Minor, cv.Point)
}
func (cv CanonicalVersion) Enabled() bool {
return cv.Major > 0 || cv.Minor > 0 || cv.Point > 0
}
func (cv CanonicalVersion) Compatible(min, max CanonicalVersion) bool {
if !cv.Enabled() {
//if we are zero, then we are always compatible
return true
}
//check if we are too low
if min.Enabled() && cv.Compare(min) > 0 {
return false // we are too old
}
if max.Enabled() && cv.Compare(max) < 0 {
return false // we are too new
}
return true // all good!
}
type LoginRequest struct {
User string
Pass string
}
type LoginResponse struct {
LoginStatus bool
Reason string `json:",omitempty"`
Admin bool `json:",omitempty"`
JWT string `json:",omitempty"`
}
type SSOStatus struct {
Enabled bool `json:"enabled"`
}
type WarnResp struct {
Name string
Err error `json:",omitempty"`
}
type IngestResponse struct {
Count int64
Size int64
Tags []string
}
func (wr WarnResp) MarshalJSON() ([]byte, error) {
var s string
if wr.Err != nil {
s = wr.Err.Error()
}
return json.Marshal(&struct {
Name string
Err string
}{
Name: wr.Name,
Err: s,
})
}
func (wr *WarnResp) UnmarshalJSON(buff []byte) error {
type alias struct {
Name string
Err string `json:",omitempty"`
}
var a alias
if err := json.Unmarshal(buff, &a); err != nil {
return err
}
wr.Name = a.Name
if len(a.Err) > 0 {
wr.Err = errors.New(a.Err)
}
return nil
}
type GUISettings struct {
DistributedWebservers bool
DisableMapTileProxy bool
MapTileUrl string
// If true, the UI shouldn't display any notifications about new features
DisableFeaturePopups bool
// Indicates that we're in cloud mode - changes some behaviors
CloudMode bool
ServerTime time.Time
ServerTimezone string
ServerTimezoneOffset int
MaxFileSize uint64 // the maximum size allowed for user file uploads
MaxResourceSize uint64 // the largest resource you're allowed to make
MaxJsonRequestSize uint64 // the largest object you're allowed to send in a JSON request body
IngestAllowed bool // set to true if the user is allowed to use the ingest APIs
NonCommercial bool // set to true if the license is a non-commercial license
}
type SearchAgentConfig struct {
Searchagent_UUID string
Webserver_Address []string
Insecure_Skip_TLS_Verify bool
Insecure_Use_HTTP bool
Search_Agent_Auth string
Scratch_Path string
Max_Script_Run_Time int64 // minutes!
Log_File string
Log_Level string
Disable_Network_Script_Functions bool // disables "risky" scripting functions (network stuff)
Disable_Self_Ingest bool // disables ingesting search agent logs to indexers
HTTP_Proxy string
}
type SearchAgentCheckin struct {
LastCheckin string
Warning bool
SearchAgentUUID string
SearchAgentIP string
}
type emptyInts []int32
func (ei emptyInts) MarshalJSON() ([]byte, error) {
if len(ei) == 0 {
return emptyList, nil
}
return json.Marshal([]int32(ei))
}
type emptyStrings []string
func (es emptyStrings) MarshalJSON() ([]byte, error) {
if len(es) == 0 {
return emptyList, nil
}
return json.Marshal([]string(es))
}
type emptyByteArrays [][]byte
func (eba emptyByteArrays) MarshalJSON() ([]byte, error) {
if len(eba) == 0 {
return emptyList, nil
}
return json.Marshal([][]byte(eba))
}
type emptyInt64s []int64
func (ei emptyInt64s) MarshalJSON() ([]byte, error) {
if len(ei) == 0 {
return emptyList, nil
}
return json.Marshal([]int64(ei))
}
type emptyFloat64s []float64
func (ei emptyFloat64s) MarshalJSON() ([]byte, error) {
if len(ei) == 0 {
return emptyList, nil
}
return json.Marshal([]float64(ei))
}
// marshalling and handlers for our raw object type
func (o RawObject) MarshalJSON() ([]byte, error) {
if len(o) == 0 || o == nil {
return emptyObj, nil
}
b := json.RawMessage(o)
return json.Marshal(&b)
}
func (o *RawObject) UnmarshalJSON(buff []byte) error {
var b json.RawMessage
if err := json.Unmarshal(buff, &b); err != nil {
return err
}
*o = RawObject(b)
return nil
}
func (o RawObject) String() string {
return string(o)
}
type LoggingLevels struct {
Levels []string
Current string
}
type LogLevel struct {
Level string
}
func (m *LoggingLevels) MarshalJSON() ([]byte, error) {
type alias LoggingLevels
return json.Marshal(&struct {
alias
Levels emptyStrings
}{
alias: alias(*m),
Levels: emptyStrings(m.Levels),
})
}