Skip to content

Commit

Permalink
Merge branch 'main' into becca/watchdog-enable-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Nov 15, 2023
2 parents 30c0efe + 5b9a7f4 commit a9ebb2c
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 86 deletions.
1 change: 0 additions & 1 deletion cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ func runLauncher(ctx context.Context, cancel func(), slogger, systemSlogger *mul
if runLocalServer {
ls, err := localserver.New(
k,
localserver.WithLogger(logger),
)

if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions ee/localserver/krypto-ec-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ func (e *kryptoEcMiddleware) sendCallback(req *http.Request, data *callbackDataS

func (e *kryptoEcMiddleware) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
spanCtx, span := traces.StartSpan(r.Context())
r = r.WithContext(spanCtx)
r, span := traces.StartHttpRequestSpan(r)

defer span.End()

Expand Down
6 changes: 3 additions & 3 deletions ee/localserver/logging-handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package localserver

import (
"log/slog"
"net/http"
"time"

"github.com/go-kit/kit/log/level"
)

type statusRecorder struct {
Expand All @@ -22,7 +21,8 @@ func (ls *localServer) requestLoggingHandler(next http.Handler) http.Handler {
recorder := &statusRecorder{ResponseWriter: w, Status: 200}

defer func(begin time.Time) {
level.Debug(ls.logger).Log(
ls.slogger.Log(r.Context(), slog.LevelInfo,
"request log",
"path", r.URL.Path,
"method", r.Method,
"status", recorder.Status,
Expand Down
2 changes: 1 addition & 1 deletion ee/localserver/request-controlservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (ls *localServer) requestAccelerateControlHandler() http.Handler {
}

func (ls *localServer) requestAccelerateControlFunc(w http.ResponseWriter, r *http.Request) {
_, span := traces.StartSpan(r.Context(), "path", r.URL.Path)
r, span := traces.StartHttpRequestSpan(r, "path", r.URL.Path)
defer span.End()

if r.Body == nil {
Expand Down
2 changes: 1 addition & 1 deletion ee/localserver/request-controlservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Test_localServer_requestAccelerateControlFunc(t *testing.T) {
}

var logBytes bytes.Buffer
server := testServer(t, k, &logBytes)
server := testServer(t, k)

req, err := http.NewRequest("", "", nil)
if tt.body != nil {
Expand Down
19 changes: 12 additions & 7 deletions ee/localserver/request-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"os/user"
"runtime"
"time"

"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/consoleuser"
"github.com/kolide/launcher/pkg/backoff"
Expand Down Expand Up @@ -68,8 +68,8 @@ func (ls *localServer) requestIdHandler() http.Handler {
return http.HandlerFunc(ls.requestIdHandlerFunc)
}

func (ls *localServer) requestIdHandlerFunc(res http.ResponseWriter, req *http.Request) {
_, span := traces.StartSpan(req.Context(), "path", req.URL.Path)
func (ls *localServer) requestIdHandlerFunc(w http.ResponseWriter, r *http.Request) {
r, span := traces.StartHttpRequestSpan(r, "path", r.URL.Path)
defer span.End()

response := requestIdsResponse{
Expand All @@ -81,10 +81,11 @@ func (ls *localServer) requestIdHandlerFunc(res http.ResponseWriter, req *http.R
consoleUsers, err := consoleUsers()
if err != nil {
traces.SetError(span, err)
level.Error(ls.logger).Log(
"msg", "getting console users",
ls.slogger.Log(r.Context(), slog.LevelError,
"getting console users",
"err", err,
)

response.ConsoleUsers = []*user.User{}
} else {
response.ConsoleUsers = consoleUsers
Expand All @@ -93,11 +94,15 @@ func (ls *localServer) requestIdHandlerFunc(res http.ResponseWriter, req *http.R
jsonBytes, err := json.Marshal(response)
if err != nil {
traces.SetError(span, err)
level.Info(ls.logger).Log("msg", "unable to marshal json", "err", err)
ls.slogger.Log(r.Context(), slog.LevelError,
"marshaling json",
"err", err,
)

jsonBytes = []byte(fmt.Sprintf("unable to marshal json: %v", err))
}

res.Write(jsonBytes)
w.Write(jsonBytes)
}

func consoleUsers() ([]*user.User, error) {
Expand Down
14 changes: 9 additions & 5 deletions ee/localserver/request-id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package localserver
import (
"bytes"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -15,7 +16,6 @@ import (
storageci "github.com/kolide/launcher/pkg/agent/storage/ci"
"github.com/kolide/launcher/pkg/agent/types"
typesMocks "github.com/kolide/launcher/pkg/agent/types/mocks"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/kolide/launcher/pkg/osquery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -27,10 +27,14 @@ func Test_localServer_requestIdHandler(t *testing.T) {
mockKnapsack := typesMocks.NewKnapsack(t)
mockKnapsack.On("ConfigStore").Return(storageci.NewStore(t, log.NewNopLogger(), storage.ConfigStore.String()))
mockKnapsack.On("KolideServerURL").Return("localhost")
mockKnapsack.On("Slogger").Return(multislogger.New().Logger)

var logBytes bytes.Buffer
server := testServer(t, mockKnapsack, &logBytes)
slogger := slog.New(slog.NewJSONHandler(&logBytes, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
mockKnapsack.On("Slogger").Return(slogger)

server := testServer(t, mockKnapsack)

req, err := http.NewRequest("", "", nil)
require.NoError(t, err)
Expand Down Expand Up @@ -61,10 +65,10 @@ func Test_localServer_requestIdHandler(t *testing.T) {
assert.GreaterOrEqual(t, len(response.ConsoleUsers), 1, "should have at least one console user")
}

func testServer(t *testing.T, k types.Knapsack, logBytes *bytes.Buffer) *localServer {
func testServer(t *testing.T, k types.Knapsack) *localServer {
require.NoError(t, osquery.SetupLauncherKeys(k.ConfigStore()))

server, err := New(k, WithLogger(log.NewLogfmtLogger(logBytes)))
server, err := New(k)
require.NoError(t, err)
return server
}
10 changes: 5 additions & 5 deletions ee/localserver/request-query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"time"

"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/pkg/backoff"
"github.com/kolide/launcher/pkg/traces"
"github.com/osquery/osquery-go/plugin/distributed"
Expand All @@ -19,7 +19,7 @@ func (ls *localServer) requestQueryHandler() http.Handler {
}

func (ls *localServer) requestQueryHanlderFunc(w http.ResponseWriter, r *http.Request) {
_, span := traces.StartSpan(r.Context(), "path", r.URL.Path)
r, span := traces.StartHttpRequestSpan(r, "path", r.URL.Path)
defer span.End()

if r.Body == nil {
Expand Down Expand Up @@ -61,7 +61,7 @@ func (ls *localServer) requestScheduledQueryHandler() http.Handler {
// requestScheduledQueryHandlerFunc uses the name field in the request body to look up
// an existing osquery scheduled query execute it, returning the results.
func (ls *localServer) requestScheduledQueryHandlerFunc(w http.ResponseWriter, r *http.Request) {
_, span := traces.StartSpan(r.Context(), "path", r.URL.Path)
r, span := traces.StartHttpRequestSpan(r, "path", r.URL.Path)
defer span.End()

// The driver behind this is that the JS bridge has to use GET requests passing the query (in a nacl box) as a URL parameter.
Expand Down Expand Up @@ -107,8 +107,8 @@ func (ls *localServer) requestScheduledQueryHandlerFunc(w http.ResponseWriter, r

scheduledQueryResult, err := queryWithRetries(ls.querier, scheduledQuery["query"])
if err != nil {
level.Error(ls.logger).Log(
"msg", "running scheduled query on demand",
ls.slogger.Log(r.Context(), slog.LevelError,
"running scheduled query on demand",
"err", err,
"query", scheduledQuery["query"],
"query_name", scheduledQuery["name"],
Expand Down
6 changes: 2 additions & 4 deletions ee/localserver/request-query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func Test_localServer_requestQueryHandler(t *testing.T) {
mockQuerier.On("Query", tt.query).Return(tt.mockQueryResult, nil).Once()
}

var logBytes bytes.Buffer
server := testServer(t, mockKnapsack, &logBytes)
server := testServer(t, mockKnapsack)
server.querier = mockQuerier

jsonBytes, err := json.Marshal(map[string]string{
Expand Down Expand Up @@ -239,8 +238,7 @@ func Test_localServer_requestRunScheduledQueryHandler(t *testing.T) {
}

// set up test server
var logBytes bytes.Buffer
server := testServer(t, mockKnapsack, &logBytes)
server := testServer(t, mockKnapsack)
server.querier = mockQuerier

// make request body
Expand Down
Loading

0 comments on commit a9ebb2c

Please sign in to comment.