-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor for TLS handlers #54
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/javuto/osctrl/admin/handlers | ||
|
||
go 1.12 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package handlers | ||
|
||
const ( | ||
metricJSONReq = "admin-json-req" | ||
metricJSONErr = "admin-json-err" | ||
metricJSONOK = "admin-json-ok" | ||
metricHealthReq = "health-req" | ||
metricHealthOK = "health-ok" | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package logging | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/jmpsec/osctrl/nodes" | ||
"github.com/jmpsec/osctrl/types" | ||
) | ||
|
||
// DispatchLogs - Helper to dispatch logs | ||
func (l *LoggerTLS) DispatchLogs(data []byte, uuid, logType, environment string, metadata nodes.NodeMetadata, debug bool) { | ||
// Use metadata to update record | ||
if err := l.Nodes.UpdateMetadataByUUID(uuid, metadata); err != nil { | ||
log.Printf("error updating metadata %s", err) | ||
} | ||
// Send data to storage | ||
// FIXME allow multiple types of logging | ||
if debug { | ||
log.Printf("dispatching logs to %s", l.Logging) | ||
} | ||
l.Log( | ||
logType, | ||
data, | ||
environment, | ||
uuid, | ||
debug) | ||
Comment on lines
+22
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, funny line splitting. |
||
// Refresh last logging request | ||
if logType == types.StatusLog { | ||
err := l.Nodes.RefreshLastStatus(uuid) | ||
if err != nil { | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
log.Printf("error refreshing last status %v", err) | ||
} | ||
} | ||
if logType == types.ResultLog { | ||
if err := l.Nodes.RefreshLastResult(uuid); err != nil { | ||
log.Printf("error refreshing last result %v", err) | ||
} | ||
} | ||
} | ||
|
||
// DispatchQueries - Helper to dispatch queries | ||
func (l *LoggerTLS) DispatchQueries(queryData types.QueryWriteData, node nodes.OsqueryNode, debug bool) { | ||
// Prepare data to send | ||
data, err := json.Marshal(queryData) | ||
if err != nil { | ||
log.Printf("error preparing data %v", err) | ||
} | ||
// Refresh last query write request | ||
if err := l.Nodes.RefreshLastQueryWrite(node.UUID); err != nil { | ||
log.Printf("error refreshing last query write %v", err) | ||
} | ||
// Send data to storage | ||
// FIXME allow multiple types of logging | ||
if debug { | ||
log.Printf("dispatching queries to %s", l.Logging) | ||
} | ||
l.QueryLog( | ||
types.QueryLog, | ||
data, | ||
node.Environment, | ||
node.UUID, | ||
queryData.Name, | ||
queryData.Status, | ||
debug) | ||
Comment on lines
+58
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same odd line-splitting comment. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package logging | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/jmpsec/osctrl/nodes" | ||
"github.com/jmpsec/osctrl/types" | ||
) | ||
|
||
// ProcessLogs - Helper to process logs | ||
func (l *LoggerTLS) ProcessLogs(data json.RawMessage, logType, environment, ipaddress string, debug bool) { | ||
// Parse log to extract metadata | ||
var logs []types.LogGenericData | ||
err := json.Unmarshal(data, &logs) | ||
if err != nil { | ||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// FIXME metrics for this | ||
log.Printf("error parsing log %s %v", string(data), err) | ||
} | ||
if debug { | ||
log.Printf("parsing logs for metadata in %s:%s", logType, environment) | ||
} | ||
// Iterate through received messages to extract metadata | ||
var uuids, hosts, names, users, osqueryusers, hashes, dhashes, osqueryversions []string | ||
for _, l := range logs { | ||
uuids = append(uuids, l.HostIdentifier) | ||
hosts = append(hosts, l.Decorations.Hostname) | ||
names = append(names, l.Decorations.LocalHostname) | ||
users = append(users, l.Decorations.Username) | ||
osqueryusers = append(osqueryusers, l.Decorations.OsqueryUser) | ||
hashes = append(hashes, l.Decorations.ConfigHash) | ||
dhashes = append(dhashes, l.Decorations.DaemonHash) | ||
osqueryversions = append(osqueryversions, l.Version) | ||
} | ||
if debug { | ||
log.Printf("metadata and dispatch for %s", uniq(uuids)[0]) | ||
} | ||
// FIXME it only uses the first element from the []string that uniq returns | ||
metadata := nodes.NodeMetadata{ | ||
IPAddress: ipaddress, | ||
Username: uniq(users)[0], | ||
OsqueryUser: uniq(osqueryusers)[0], | ||
Hostname: uniq(hosts)[0], | ||
Localname: uniq(names)[0], | ||
ConfigHash: uniq(hashes)[0], | ||
DaemonHash: uniq(dhashes)[0], | ||
OsqueryVersion: uniq(osqueryversions)[0], | ||
} | ||
// Dispatch logs and update metadata | ||
l.DispatchLogs(data, uniq(uuids)[0], logType, environment, metadata, debug) | ||
} | ||
|
||
// ProcessLogQueryResult - Helper to process on-demand query result logs | ||
func (l *LoggerTLS) ProcessLogQueryResult(queries types.QueryWriteQueries, statuses types.QueryWriteStatuses, nodeKey string, environment string, debug bool) { | ||
// Retrieve node | ||
node, err := l.Nodes.GetByKey(nodeKey) | ||
if err != nil { | ||
log.Printf("error retrieving node %s", err) | ||
} | ||
// Tap into results so we can update internal metrics | ||
for q, r := range queries { | ||
// Dispatch query name, result and status | ||
d := types.QueryWriteData{ | ||
Name: q, | ||
Result: r, | ||
Status: statuses[q], | ||
} | ||
go l.DispatchQueries(d, node, debug) | ||
// Update internal metrics per query | ||
var err error | ||
if statuses[q] != 0 { | ||
err = l.Queries.IncError(q) | ||
} else { | ||
err = l.Queries.IncExecution(q) | ||
} | ||
if err != nil { | ||
log.Printf("error updating query %s", err) | ||
} | ||
// Add a record for this query | ||
if err := l.Queries.TrackExecution(q, node.UUID, statuses[q]); err != nil { | ||
log.Printf("error adding query execution %s", err) | ||
} | ||
// Check if query is completed | ||
if err := l.Queries.VerifyComplete(q); err != nil { | ||
log.Printf("error verifying and completing query %s", err) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package logging | ||
|
||
// Helper to remove duplicates from array of strings | ||
func uniq(duplicated []string) []string { | ||
keys := make(map[string]bool) | ||
result := []string{} | ||
for _, entry := range duplicated { | ||
if _, value := keys[entry]; !value { | ||
keys[entry] = true | ||
result = append(result, entry) | ||
} | ||
} | ||
return result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm I had this from a long time ago when I was running all services in the same binary, but now I am not using it anymore. Remove!