2016-09-05 20:03:58 +00:00
|
|
|
package server
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-09-06 21:28:07 +00:00
|
|
|
"net/http"
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
"github.com/kolide/kolide-ose/errors"
|
|
|
|
"github.com/kolide/kolide-ose/kolide"
|
2016-09-26 17:14:39 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/contexts/host"
|
2016-09-04 05:13:42 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-09-21 03:08:11 +00:00
|
|
|
type osqueryError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e osqueryError) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
func (svc service) EnrollAgent(ctx context.Context, enrollSecret, hostIdentifier string) (string, error) {
|
2016-09-14 16:11:06 +00:00
|
|
|
if enrollSecret != svc.config.Osquery.EnrollSecret {
|
2016-09-04 05:13:42 +00:00
|
|
|
return "", errors.New(
|
|
|
|
"Node key invalid",
|
|
|
|
fmt.Sprintf("Invalid node key provided: %s", enrollSecret),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:11:06 +00:00
|
|
|
host, err := svc.ds.EnrollHost(hostIdentifier, "", "", "", svc.config.Osquery.NodeKeySize)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return host.NodeKey, nil
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:11:39 +00:00
|
|
|
func (svc service) GetClientConfig(ctx context.Context, action string, data json.RawMessage) (*kolide.OsqueryConfig, error) {
|
2016-09-06 21:28:07 +00:00
|
|
|
var config kolide.OsqueryConfig
|
2016-09-19 23:11:39 +00:00
|
|
|
return &config, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 21:28:07 +00:00
|
|
|
func (svc service) SubmitStatusLogs(ctx context.Context, logs []kolide.OsqueryResultLog) error {
|
|
|
|
for _, log := range logs {
|
|
|
|
err := json.NewEncoder(svc.osqueryStatusLogWriter).Encode(log)
|
|
|
|
if err != nil {
|
|
|
|
return errors.NewFromError(err, http.StatusInternalServerError, "error writing status log")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) SubmitResultsLogs(ctx context.Context, logs []kolide.OsqueryStatusLog) error {
|
|
|
|
for _, log := range logs {
|
|
|
|
err := json.NewEncoder(svc.osqueryResultsLogWriter).Encode(log)
|
|
|
|
if err != nil {
|
|
|
|
return errors.NewFromError(err, http.StatusInternalServerError, "error writing result log")
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-21 03:08:11 +00:00
|
|
|
// hostLabelQueryPrefix is appended before the query name when a query is
|
|
|
|
// provided as a label query. This allows the results to be retrieved when
|
|
|
|
// osqueryd writes the distributed query results.
|
|
|
|
const hostLabelQueryPrefix = "kolide_label_query_"
|
|
|
|
|
|
|
|
// hostDetailQueryPrefix is appended before the query name when a query is
|
|
|
|
// provided as a detail query.
|
|
|
|
const hostDetailQueryPrefix = "kolide_detail_query_"
|
|
|
|
|
|
|
|
// hostDetailQueries returns the map of queries that should be executed by
|
|
|
|
// osqueryd to fill in the host details
|
|
|
|
func hostDetailQueries(host kolide.Host) map[string]string {
|
|
|
|
queries := make(map[string]string)
|
|
|
|
if host.Platform == "" {
|
|
|
|
queries[hostDetailQueryPrefix+"platform"] = "select build_platform from osquery_info;"
|
|
|
|
}
|
|
|
|
return queries
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
func (svc service) GetDistributedQueries(ctx context.Context) (map[string]string, error) {
|
2016-09-21 03:08:11 +00:00
|
|
|
queries := make(map[string]string)
|
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
host, ok := host.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errNoContext
|
2016-09-21 03:08:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
queries = hostDetailQueries(host)
|
2016-09-21 03:08:11 +00:00
|
|
|
if len(queries) > 0 {
|
|
|
|
// If the host details need to be updated, we should do so
|
|
|
|
// before checking for any other queries
|
|
|
|
return queries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the label queries that should be updated
|
|
|
|
cutoff := svc.clock.Now().Add(-svc.config.Osquery.LabelUpdateInterval)
|
2016-09-26 17:14:39 +00:00
|
|
|
labelQueries, err := svc.ds.LabelQueriesForHost(&host, cutoff)
|
2016-09-21 03:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, query := range labelQueries {
|
|
|
|
queries[hostLabelQueryPrefix+name] = query
|
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-21 03:08:11 +00:00
|
|
|
// TODO: retrieve the active distributed queries for this host
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
return queries, nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 21:28:07 +00:00
|
|
|
func (svc service) SubmitDistributedQueryResults(ctx context.Context, results kolide.OsqueryDistributedQueryResults) error {
|
2016-09-04 05:13:42 +00:00
|
|
|
return nil
|
|
|
|
}
|