/
info.go
348 lines (317 loc) · 10 KB
/
info.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
/*************************************************************************
* 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 client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"github.com/gravwell/gravwell/v3/client/types"
)
var (
ErrInvalidLogLevel = errors.New("Invalid logging level")
Version = VersionStruct{
Major: 0,
Minor: 1,
Revision: 1,
}
)
type VersionStruct struct {
Major uint16
Minor uint16
Revision uint16
}
// String returns the version in the format <Major>.<Minor>.<Revision>, e.g. "4.1.0".
func (v VersionStruct) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Revision)
}
func (c *Client) GetGuiSettings() (types.GUISettings, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.getGuiSettings()
}
func (c *Client) getGuiSettings() (types.GUISettings, error) {
settings := types.GUISettings{}
err := c.getStaticURL(SETTINGS_URL, &settings)
return settings, err
}
// MySessions returns an array of the current user's sessions.
func (c *Client) MySessions() ([]types.Session, error) {
if c.userDetails.UID == 0 {
return nil, ErrNotSynced
}
return c.Sessions(c.userDetails.UID)
}
// MyInfo returns the current user's information.
func (c *Client) MyInfo() (types.UserDetails, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.getMyInfo()
}
// MyUID returns the current user's numeric user ID.
func (c *Client) MyUID() int32 {
return c.userDetails.UID
}
// MyAdminStatus returns true if the current user is marked as an administrator.
func (c *Client) MyAdminStatus() bool {
return c.userDetails.Admin
}
// Groups returns the current user's group memberships.
func (c *Client) Groups() (gps []types.GroupDetails, err error) {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.userDetails.UID == 0 {
if err = c.syncNoLock(); err != nil {
return
}
}
gps = c.userDetails.Groups
return
}
func (c *Client) getMyInfo() (types.UserDetails, error) {
dets := types.UserDetails{}
if err := c.getStaticURL(USER_INFO_URL, &dets); err != nil {
return dets, err
}
return dets, nil
}
// CheckApiVersion assert the REST API version of the webserver is compatible
// with the client.
func (c *Client) CheckApiVersion() (string, error) {
var version types.VersionInfo
if err := c.getStaticURL(API_VERSION_URL, &version); err != nil {
return "", err
}
if err := types.CheckApiVersion(version.API); err != nil {
return err.Error(), nil
}
return "", nil
}
// GetApiVersion returns the REST API version of the webserver.
func (c *Client) GetApiVersion() (types.ApiInfo, error) {
var version types.VersionInfo
err := c.getStaticURL(API_VERSION_URL, &version)
return version.API, err
}
func (c *Client) getLogLevelInfo() (types.LoggingLevels, error) {
ll := types.LoggingLevels{}
if err := c.methodStaticURL(http.MethodGet, LOGGING_PATH_URL, &ll); err != nil {
return ll, err
}
return ll, nil
}
// GetLogLevel is an admin-only function which returns the webserver's enabled log level.
//
// Valid levels: "Off", "Error", "Warn", "Info", "Web Access".
func (c *Client) GetLogLevel() (string, error) {
ll, err := c.getLogLevelInfo()
if err != nil {
return "", err
}
return ll.Current, nil
}
// SetLogLevel is an admin-only function which sets the webserver's logging level.
//
// Valid levels: "Off", "Error", "Warn", "Info", "Web Access".
func (c *Client) SetLogLevel(level string) error {
//get what is supported
ll, err := c.getLogLevelInfo()
if err != nil {
return err
}
ok := false
//check that what is requested is valid
for i := range ll.Levels {
if strings.ToLower(level) == strings.ToLower(ll.Levels[i]) {
level = ll.Levels[i]
ok = true
break
}
}
if !ok {
return ErrInvalidLogLevel
}
l := types.LogLevel{
Level: level,
}
return c.methodStaticPushURL(http.MethodPut, LOGGING_PATH_URL, l, nil)
}
// GetTags returns an array of strings representing the tags on the Gravwell system.
func (c *Client) GetTags() ([]string, error) {
var tags []string
err := c.methodStaticURL(http.MethodGet, TAGS_URL, &tags)
return tags, err
}
// GetLicenseDistributionState checks the distribution status of a newly-uploaded license
// during the initial setup of a Gravwell cluster. This function MUST be called after
// calling InitLicense; when the status returned is "done", Gravwell is ready for use.
func (c *Client) GetLicenseDistributionState() (ds types.LicenseDistributionStatus, err error) {
uri := fmt.Sprintf("%s://%s%s", c.httpScheme, c.server, LICENSE_INIT_STATUS)
var req *http.Request
var resp *http.Response
if req, err = http.NewRequest(http.MethodGet, uri, nil); err != nil {
return
}
if resp, err = c.clnt.Do(req); err != nil {
c.objLog.Log("WEB "+req.Method+" Error "+err.Error(), req.URL.String(), nil)
return
}
if resp == nil {
err = errors.New("Invalid response")
return
}
defer drainResponse(resp)
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Invalid response %s", resp.Status)
return
}
if err = json.NewDecoder(resp.Body).Decode(&ds); err != nil {
return
}
return
}
// LicenseInitRequired returns true if the Gravwell cluster requires a license.
// If true, use InitLicense to upload a valid license file.
func (c *Client) LicenseInitRequired() bool {
if _, err := c.GetLicenseDistributionState(); err != nil {
return false
}
return true
}
// InitLicense uploads the contents of a Gravwell license. It will return nil
// if the license is valid and accepted by Gravwell. After calling InitLicense,
// you MUST use GetLicenseDistributionState to verify that Gravwell has distributed
// the license to the indexers and is ready to use.
func (c *Client) InitLicense(b []byte) error {
bb := bytes.NewBuffer(nil)
wtr := multipart.NewWriter(bb)
mp, err := wtr.CreateFormFile(`file`, `license`)
if err != nil {
return err
}
if n, err := mp.Write(b); err != nil {
return err
} else if n != len(b) {
return errors.New("Failed to create license upload package")
}
if err := wtr.Close(); err != nil {
return err
}
uri := fmt.Sprintf("%s://%s%s", c.httpScheme, c.server, LICENSE_INIT_UPLOAD)
req, err := http.NewRequest(http.MethodPost, uri, bb)
if err != nil {
return err
}
req.Header.Set(`Content-Type`, wtr.FormDataContentType())
resp, err := c.clnt.Do(req)
if err != nil {
c.objLog.Log("WEB "+req.Method+" Error "+err.Error(), req.URL.String(), nil)
return err
}
if resp == nil {
return errors.New("Invalid response")
}
defer drainResponse(resp)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Invalid response %s", resp.Status)
}
return nil
}
// SendMail sends an email with the specified parameters using the mail server configuration
// defined for the current user. Note that the email will be sent from the webserver, not the
// system running the client code.
func (c *Client) SendMail(from string, to []string, subject string, body string, attch []types.UserMailAttachment) error {
msg := types.UserMail{
From: from,
To: to,
Subject: subject,
Body: body,
Attachments: attch,
}
return c.postStaticURL(MAIL_URL, &msg, nil)
}
// SendPrebuiltMail operates as SendMail, but takes a pre-populated types.UserMail object
// as an argument instead of discrete arguments.
func (c *Client) SendPrebuiltMail(msg types.UserMail) error {
return c.postStaticURL(MAIL_URL, &msg, nil)
}
// ConfigureMail sets up mail server options for the current user.
// The user, pass, server, and port parameters specify the mail server and authentication
// options for the server. The useTLS flag enables TLS for SMTP, and the noVerify flag disables
// checking of TLS certs.
func (c *Client) ConfigureMail(user, pass, server string, port uint16, useTLS, noVerify bool) error {
msg := types.UserMailConfig{
Server: server,
Username: user,
Password: pass,
Port: int(port),
UseTLS: useTLS,
InsecureSkipVerify: noVerify,
}
return c.postStaticURL(MAIL_CONFIGURE_URL, &msg, nil)
}
// DeleteMailConfig removes a users mail configuration fom preferences
// this completely uninstalls any mail configs
func (c *Client) DeleteMailConfig() error {
return c.methodStaticPushURL(http.MethodDelete, MAIL_CONFIGURE_URL, nil, nil, http.StatusOK, http.StatusNotFound)
}
// MailConfig retrieves the current mail config
// if no mail config is set an empty UserMailConfig is returned
// Even on a valid mail config the Password portion is not present in the response
func (c *Client) MailConfig() (mc types.UserMailConfig, err error) {
err = c.getStaticURL(MAIL_CONFIGURE_URL, &mc)
return
}
// WellData returns information about the storage wells on the indexers.
// The return value is a map of indexer name strings to IndexerWellData objects.
func (c *Client) WellData() (mp map[string]types.IndexerWellData, err error) {
err = c.getStaticURL(wellDataUrl(), &mp)
return
}
// GetLibFile fetches the contents of a particular SOAR library file, as used in
// scheduled search scripts. The repo and commit arguments are optional.
// Examples:
//
// c.GetLibFile("https://github.com/gravwell/libs", "cd9d6c5", "alerts/email.ank")
// c.GetLibFile("", "", "utils/links.ank")
func (c *Client) GetLibFile(repo, commit, fn string) (bts []byte, err error) {
if fn == `` {
err = errors.New("Missing filename")
return
}
if _, err = url.Parse(repo); err != nil {
return
}
mp := make(map[string]string, 3)
if repo != `` {
mp[`repo`] = repo
}
if commit != `` {
mp[`commit`] = commit
}
mp[`path`] = fn
var resp *http.Response
if resp, err = c.methodParamRequestURL(http.MethodGet, LIBS_URL, mp, nil); err == nil {
if resp.StatusCode != 200 {
if err = decodeBodyError(resp.Body); err == nil {
err = fmt.Errorf("Invalid response code: %s(%d)", resp.Status, resp.StatusCode)
}
} else {
bb := bytes.NewBuffer(nil)
io.Copy(bb, resp.Body)
bts = bb.Bytes()
}
drainResponse(resp)
}
return
}