-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from jmpsec/test-handlers-tls
Refactor for TLS handlers
- Loading branch information
Showing
31 changed files
with
1,253 additions
and
541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/javuto/osctrl/admin/handlers | ||
|
||
go 1.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// Refresh last logging request | ||
if logType == types.StatusLog { | ||
err := l.Nodes.RefreshLastStatus(uuid) | ||
if err != nil { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
// 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) | ||
} | ||
} | ||
} |
Oops, something went wrong.