2016-09-29 04:21:39 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
Push distributed query errors over results websocket (#878)
As of recently, osquery will report when a distributed query fails. We now
expose errors over the results websocket. When a query errored on the host, the
`error` key in the result will be non-null. Note that osquery currently doesn't
provide any details so the error string will always be "failed". I anticipate
that we will fix this and the string is included for future-proofing.
Successful result:
```
{
"type": "result",
"data": {
"distributed_query_execution_id": 15,
"host": {
... omitted ...
},
"rows": [
{
"hour": "1"
}
],
"error": null
}
}
```
Failed result:
```
{
"type": "result",
"data": {
"distributed_query_execution_id": 14,
"host": {
... omitted ...
},
"rows": [
],
"error": "failed"
}
}
```
2017-01-11 03:34:32 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-29 04:21:39 +00:00
|
|
|
"github.com/gorilla/mux"
|
2017-02-01 17:20:50 +00:00
|
|
|
"github.com/kolide/kolide/server/kolide"
|
2016-09-29 04:21:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeEnrollAgentRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/osquery/enroll", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeEnrollAgentRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(enrollAgentRequest)
|
|
|
|
assert.Equal(t, "secret", params.EnrollSecret)
|
|
|
|
assert.Equal(t, "uuid", params.HostIdentifier)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"enroll_secret": "secret",
|
|
|
|
"host_identifier": "uuid"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("POST", "/api/v1/osquery/enroll", &body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGetClientConfigRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/osquery/enroll", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeGetClientConfigRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(getClientConfigRequest)
|
|
|
|
assert.Equal(t, "key", params.NodeKey)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"node_key": "key"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("POST", "/api/v1/osquery/enroll", &body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGetDistributedQueriesRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/osquery/enroll", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeGetDistributedQueriesRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(getDistributedQueriesRequest)
|
|
|
|
assert.Equal(t, "key", params.NodeKey)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"node_key": "key"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("POST", "/api/v1/osquery/enroll", &body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeSubmitDistributedQueryResultsRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/osquery/enroll", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeSubmitDistributedQueryResultsRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(submitDistributedQueryResultsRequest)
|
|
|
|
assert.Equal(t, "key", params.NodeKey)
|
|
|
|
assert.Equal(t, kolide.OsqueryDistributedQueryResults{
|
|
|
|
"id1": {
|
|
|
|
{"col1": "val1", "col2": "val2"},
|
|
|
|
{"col1": "val3", "col2": "val4"},
|
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
{"col3": "val5", "col4": "val6"},
|
|
|
|
},
|
2016-10-05 00:17:55 +00:00
|
|
|
"id3": {},
|
2016-09-29 04:21:39 +00:00
|
|
|
}, params.Results)
|
Push distributed query errors over results websocket (#878)
As of recently, osquery will report when a distributed query fails. We now
expose errors over the results websocket. When a query errored on the host, the
`error` key in the result will be non-null. Note that osquery currently doesn't
provide any details so the error string will always be "failed". I anticipate
that we will fix this and the string is included for future-proofing.
Successful result:
```
{
"type": "result",
"data": {
"distributed_query_execution_id": 15,
"host": {
... omitted ...
},
"rows": [
{
"hour": "1"
}
],
"error": null
}
}
```
Failed result:
```
{
"type": "result",
"data": {
"distributed_query_execution_id": 14,
"host": {
... omitted ...
},
"rows": [
],
"error": "failed"
}
}
```
2017-01-11 03:34:32 +00:00
|
|
|
assert.Equal(t, map[string]string{"id1": "0", "id3": "1"}, params.Statuses)
|
2016-09-29 04:21:39 +00:00
|
|
|
}).Methods("POST")
|
|
|
|
|
2016-10-05 00:17:55 +00:00
|
|
|
// Note we explicitly test the case that requires using the shim
|
|
|
|
// because of the inconsistent JSON schema
|
2016-09-29 04:21:39 +00:00
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"node_key": "key",
|
|
|
|
"queries": {
|
|
|
|
"id1": [
|
|
|
|
{"col1": "val1", "col2": "val2"},
|
|
|
|
{"col1": "val3", "col2": "val4"}
|
|
|
|
],
|
|
|
|
"id2": [
|
|
|
|
{"col3": "val5", "col4": "val6"}
|
2016-10-05 00:17:55 +00:00
|
|
|
],
|
|
|
|
"id3": ""
|
Push distributed query errors over results websocket (#878)
As of recently, osquery will report when a distributed query fails. We now
expose errors over the results websocket. When a query errored on the host, the
`error` key in the result will be non-null. Note that osquery currently doesn't
provide any details so the error string will always be "failed". I anticipate
that we will fix this and the string is included for future-proofing.
Successful result:
```
{
"type": "result",
"data": {
"distributed_query_execution_id": 15,
"host": {
... omitted ...
},
"rows": [
{
"hour": "1"
}
],
"error": null
}
}
```
Failed result:
```
{
"type": "result",
"data": {
"distributed_query_execution_id": 14,
"host": {
... omitted ...
},
"rows": [
],
"error": "failed"
}
}
```
2017-01-11 03:34:32 +00:00
|
|
|
},
|
|
|
|
"statuses": {"id1": "0", "id3": "1"}
|
2016-09-29 04:21:39 +00:00
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("POST", "/api/v1/osquery/enroll", &body),
|
|
|
|
)
|
|
|
|
}
|