Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 4eb7461

Browse files
committed
version 0.3.1 - see changelog
1 parent b3a13c4 commit 4eb7461

File tree

11 files changed

+193
-98
lines changed

11 files changed

+193
-98
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 0.3.1 2015-04-18
4+
5+
+ Fix table sort - respect query order by first.
6+
+ perf limit added to query response. Limit by 10k.
7+
+ better query debug output for node scope queries.
8+
+ minor UI fixes.
9+
+ rename osquery.go to query.go
10+
311
## 0.3.0 - 2015-04-16
412

513
+ fix bug with node template not showing os icon

envdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
const (
1616
Name = "envdb"
17-
Version = "0.3.0"
17+
Version = "0.3.1"
1818

1919
DefaultServerPort = 3636
2020
DefaultWebServerPort = 8080

http.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,24 @@ func NewWebServer(webPort int, server *Server) {
6666
Format: "json",
6767
})
6868

69-
if len(data) != 1 {
69+
if len(data.Results) != 1 {
7070
d := QueryResults{}
7171

7272
err := errors.New(fmt.Sprintf("Node not found for id (%s).", sql.Id))
7373
return d, err
7474
}
7575

76-
tables = strings.Split(data[0].Results.(string), "\n")
76+
tables = strings.Split(data.Results[0].Results.(string), "\n")
7777

7878
for i, t := range tables {
7979
newT := strings.Replace(t, " => ", "", -1)
8080
tables[i] = newT
8181
}
8282

8383
tables = tables[:len(tables)-1]
84-
data[0].Results = tables
84+
data.Results[0].Results = tables
8585

86-
return data[0], nil
86+
return data.Results[0], data.Error
8787
})
8888

8989
gotalk.Handle("table-info", func(sql SqlRequest) (QueryResults, error) {
@@ -92,14 +92,14 @@ func NewWebServer(webPort int, server *Server) {
9292
Format: "json",
9393
})
9494

95-
if len(data) != 1 {
95+
if len(data.Results) != 1 {
9696
d := QueryResults{}
9797

9898
err := errors.New(fmt.Sprintf("Node not found for id (%s).", sql.Id))
9999
return d, err
100100
}
101101

102-
return data[0], nil
102+
return data.Results[0], data.Error
103103
})
104104

105105
gotalk.Handle("query", func(sql SqlRequest) ([]QueryResults, error) {
@@ -108,7 +108,7 @@ func NewWebServer(webPort int, server *Server) {
108108
Format: "json",
109109
})
110110

111-
return data, nil
111+
return data.Results, data.Error
112112
})
113113

114114
gotalk.Handle("disconnect", func(id string) error {

node.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ func (self *Node) Handlers() {
6464
self.Id = self.Config.Cache.Id
6565
} else {
6666

67-
// dev - remove
68-
// TODO: test for cache file remove
69-
// Log.Fatalf("no cache file - exit for debug.")
70-
7167
id, uuerr := uuid.NewV4()
7268
err = uuerr
7369

osquery.go renamed to query.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os/exec"
66
"strconv"
77
"strings"
8+
9+
"github.com/nu7hatch/gouuid"
810
)
911

1012
const (
@@ -24,6 +26,27 @@ type QueryResults struct {
2426
Error string `json:"error"`
2527
}
2628

29+
type Response struct {
30+
Id string `json:"id"`
31+
Results []QueryResults `json:"results"`
32+
Total int `json:"total"`
33+
Error error `json:"error"`
34+
}
35+
36+
func NewResponse() *Response {
37+
var id string
38+
39+
if uuid, err := uuid.NewV4(); err == nil {
40+
id = uuid.String()
41+
}
42+
43+
return &Response{
44+
Id: id,
45+
Error: nil,
46+
Total: 0,
47+
}
48+
}
49+
2750
func CheckOsQueryVersion(version string) bool {
2851
if version == MinOsQueryVersion {
2952
return true

query_database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ AS listening ON process.pid = listening.pid;`,
7272

7373
q7 := QueryDb{
7474
Name: "Shell history",
75-
Query: `SELECT * FROM shell_history`,
75+
Query: `SELECT * FROM shell_history;`,
7676
Type: "all",
7777
}
7878

server.go

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"crypto/tls"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"os"
@@ -212,19 +213,51 @@ func (self *Server) Delete(id string) error {
212213
return nil
213214
}
214215

215-
func (self *Server) sendAll(in interface{}) []QueryResults {
216-
// go func() {
216+
func ProcessResults(data []byte) (bool, []map[string]interface{}, []byte) {
217+
var all = []map[string]interface{}{}
218+
var returnData = []map[string]interface{}{}
219+
220+
if err := json.Unmarshal(data, &all); err == nil {
221+
if len(all) > DefaultRowLimit {
222+
223+
Log.Debug("Results too large. sending first 2000.")
224+
225+
for i, d := range all {
226+
227+
if i >= DefaultRowLimit {
228+
break
229+
}
230+
231+
returnData = append(returnData, d)
232+
}
233+
234+
if data, err := json.Marshal(returnData); err == nil {
235+
return true, all, data
236+
}
237+
238+
} else {
239+
return false, all, data
240+
}
241+
} else {
242+
return false, all, data
243+
}
244+
245+
return false, all, data
246+
}
247+
248+
func (self *Server) sendAll(in interface{}) *Response {
217249
self.mu.RLock()
218250
defer self.mu.RUnlock()
219251

220252
var wg sync.WaitGroup
221253

222-
var results []QueryResults
254+
resp := NewResponse()
223255

224256
Log.Debug("Sending request to nodes.")
225-
226257
Log.Debugf("Request: %s", in.(Query).Sql)
227258

259+
var count int
260+
228261
for s, node := range self.Nodes {
229262
wg.Add(1)
230263

@@ -236,6 +269,8 @@ func (self *Server) sendAll(in interface{}) []QueryResults {
236269
var data []byte
237270
err := s.Request("query", in, &data)
238271

272+
_, all, _ := ProcessResults(data)
273+
239274
qr := QueryResults{
240275
Id: node.Id,
241276
Name: node.Name,
@@ -248,62 +283,78 @@ func (self *Server) sendAll(in interface{}) []QueryResults {
248283
}
249284

250285
elapsed := time.Since(start)
251-
Log.Debugf(" * %s (%s)", node.Name, elapsed)
252286

253-
results = append(results, qr)
287+
count += len(all)
288+
289+
Log.Debugf(" * %s (%s)", node.Name, elapsed)
290+
resp.Results = append(resp.Results, qr)
254291
}(s, node)
255292
}
256293

257294
Log.Debug("Waiting for all requests to return.")
258295

259296
wg.Wait()
260297

261-
Log.Debug("Sending results back to requester.")
298+
resp.Total = count
299+
300+
if resp.Total > DefaultRowLimit {
301+
resp.Error = errors.New(fmt.Sprintf("Results too large. (Limit: %d got %d)", DefaultRowLimit, resp.Total))
302+
}
262303

263-
return results
264-
// }()
304+
Log.Debug("Sending results back to requester.")
305+
return resp
265306
}
266307

267-
func (self *Server) sendTo(id string, in interface{}) []QueryResults {
308+
func (self *Server) sendTo(id string, in interface{}) *Response {
268309
self.mu.RLock()
269310
defer self.mu.RUnlock()
270311

271-
var results []QueryResults
312+
resp := NewResponse()
272313

273314
node, err := self.GetNodeById(id)
274315

275316
if err != nil {
276-
return results
277-
}
317+
resp.Total = 0
278318

279-
Log.Debugf("%s: sending request.", node.Name)
280-
Log.Debugf("%s: request: %s", node.Name, in.(Query).Sql)
319+
return resp
320+
}
281321

282322
start := time.Now()
283323

284324
var data []byte
285325
err = node.Socket.Request("query", in, &data)
286326

327+
var newData []byte
328+
over, all, cut := ProcessResults(data)
329+
330+
if over {
331+
newData = cut
332+
} else {
333+
newData = data
334+
}
335+
336+
resp.Total = len(all)
337+
287338
qr := QueryResults{
288339
Id: node.Id,
289340
Name: node.Name,
290341
Hostname: node.Hostname,
291-
Results: string(data),
342+
Results: string(newData),
292343
}
293344

294345
if err != nil {
295346
qr.Error = err.Error()
296347
}
297348

298-
results = append(results, qr)
349+
resp.Results = append(resp.Results, qr)
299350

300351
elapsed := time.Since(start)
301-
Log.Debugf("%s: done (%s)", node.Name, elapsed)
302352

303-
return results
353+
Log.Debugf("\n - Node: %s\n - Request: %s\n - Response Id: %s\n - Total: %d\n - Elapsed Time: %s", node.Name, in.(Query).Sql, resp.Id, resp.Total, elapsed)
354+
return resp
304355
}
305356

306-
func (self *Server) Send(id string, in interface{}) []QueryResults {
357+
func (self *Server) Send(id string, in interface{}) *Response {
307358

308359
if id == "all" {
309360
return self.sendAll(in)

server_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var (
2828
DefaultPublicKeyPath = ""
2929
DefaultPrivateKeyPath = ""
3030

31+
DefaultRowLimit = 10000
32+
3133
SSL = false
3234
)
3335

web/login.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838
</div>
3939

4040
<script>
41+
$("#user-email").focus();
4142
$("#login").show().stop().animate({
4243
opacity: 1,
43-
}, 2000, function() {
44-
$("#user-email").focus();
45-
});
44+
}, 2000);
4645
</script>
4746

4847
</body>

web/public/css/envdb.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ border-radius: 100px;
287287
}
288288

289289
#nodes li.current{
290-
background-color: #6E4FC5;
290+
/* background-color: #6E4FC5; */
291+
background-color: #0077FF;
291292
}
292293

293294
#nodes li.current .node-metadata{

0 commit comments

Comments
 (0)