Skip to content
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

include arch and os in local server headers #1877

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions ee/localserver/krypto-ec-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
)

const (
timestampValidityRange = 150
kolideKryptoEccHeader20230130Value = "2023-01-30"
kolideKryptoHeaderKey = "X-Kolide-Krypto"
kolideSessionIdHeaderKey = "X-Kolide-Session"
kolidePresenceDetectionInterval = "X-Kolide-Presence-Detection-Interval"
kolidePresenceDetectionReason = "X-Kolide-Presence-Detection-Reason"
kolideDurationSinceLastPresenceDetection = "X-Kolide-Duration-Since-Last-Presence-Detection"
timestampValidityRange = 150
kolideKryptoEccHeader20230130Value = "2023-01-30"
kolideKryptoHeaderKey = "X-Kolide-Krypto"
kolideSessionIdHeaderKey = "X-Kolide-Session"
kolidePresenceDetectionIntervalHeaderKey = "X-Kolide-Presence-Detection-Interval"
kolidePresenceDetectionReasonHeaderKey = "X-Kolide-Presence-Detection-Reason"
kolideDurationSinceLastPresenceDetectionHeaderKey = "X-Kolide-Duration-Since-Last-Presence-Detection"
kolideOsHeaderKey = "X-Kolide-Os"
kolideArchHeaderKey = "X-Kolide-Arch"
)

type v2CmdRequestType struct {
Expand Down Expand Up @@ -316,6 +318,9 @@ func (e *kryptoEcMiddleware) Wrap(next http.Handler) http.Handler {
bhr := &bufferedHttpResponse{}
next.ServeHTTP(bhr, newReq)

bhr.Header().Add(kolideOsHeaderKey, runtime.GOOS)
bhr.Header().Add(kolideArchHeaderKey, runtime.GOARCH)

// add headers to the response map
// this assumes that the response to `bhr` was a json encoded blob.
var responseMap map[string]interface{}
Expand Down
11 changes: 9 additions & 2 deletions ee/localserver/krypto-ec-middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kolide/krypto/pkg/echelper"
"github.com/kolide/launcher/ee/agent/keys"
"github.com/kolide/launcher/ee/localserver/mocks"
"github.com/kolide/launcher/ee/presencedetection"

"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/assert"
Expand All @@ -42,7 +43,7 @@ func TestKryptoEcMiddleware(t *testing.T) {

koldieSessionId := ulid.New()
cmdRequestHeaders := map[string][]string{
kolidePresenceDetectionInterval: {"0s"},
kolidePresenceDetectionIntervalHeaderKey: {"0s"},
}

cmdReqCallBackHeaders := map[string][]string{
Expand Down Expand Up @@ -240,10 +241,16 @@ func TestKryptoEcMiddleware(t *testing.T) {
responseHeaders, err := extractJsonProperty[map[string][]string](opened.ResponseData, "headers")
require.NoError(t, err)

require.Equal(t, runtime.GOOS, responseHeaders[kolideOsHeaderKey][0])

// check that the presence detection interval is present
if runtime.GOOS == "darwin" {
require.Equal(t, (0 * time.Second).String(), responseHeaders[kolideDurationSinceLastPresenceDetection][0])
require.Equal(t, (0 * time.Second).String(), responseHeaders[kolideDurationSinceLastPresenceDetectionHeaderKey][0])
return
}

// not darwin
require.Equal(t, presencedetection.DetectionFailedDurationValue.String(), responseHeaders[kolideDurationSinceLastPresenceDetectionHeaderKey][0])
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion ee/localserver/presence-detection-middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestPresenceDetectionHandler(t *testing.T) {
handlerToTest.ServeHTTP(rr, req)

if tt.shouldHavePresenceDetectionDurationResponseHeader {
require.NotEmpty(t, rr.Header().Get(kolideDurationSinceLastPresenceDetection))
require.NotEmpty(t, rr.Header().Get(kolideDurationSinceLastPresenceDetectionHeaderKey))
}
require.Equal(t, tt.expectedStatusCode, rr.Code)
})
Expand Down
8 changes: 5 additions & 3 deletions ee/localserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/kolide/krypto/pkg/echelper"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/presencedetection"
"github.com/kolide/launcher/pkg/osquery"
"github.com/kolide/launcher/pkg/traces"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand Down Expand Up @@ -415,13 +416,14 @@ func (ls *localServer) presenceDetectionHandler(next http.Handler) http.Handler

// presence detection is only supported on macos currently
if runtime.GOOS != "darwin" {
w.Header().Add(kolideDurationSinceLastPresenceDetectionHeaderKey, presencedetection.DetectionFailedDurationValue.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this, we'll need to figure out how to differentiate between "not supported" and "timeout" or "error"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted it so we only return the presence response header when presence detection is actually requested.

That might give us enough data for some logic like

if failure && os == "linux"
  // we no it's not supported

but maybe we need something more concrete like a failure_reason header

next.ServeHTTP(w, r)
return
}

// can test this by adding an unauthed endpoint to the mux and running, for example:
// curl -i -H "X-Kolide-Presence-Detection-Interval: 10s" -H "X-Kolide-Presence-Detection-Reason: my reason" localhost:12519/id
detectionIntervalStr := r.Header.Get(kolidePresenceDetectionInterval)
detectionIntervalStr := r.Header.Get(kolidePresenceDetectionIntervalHeaderKey)

// no presence detection requested
if detectionIntervalStr == "" {
Expand All @@ -439,7 +441,7 @@ func (ls *localServer) presenceDetectionHandler(next http.Handler) http.Handler

// set a default reason, on macos the popup will look like "Kolide is trying to authenticate."
reason := "authenticate"
reasonHeader := r.Header.Get(kolidePresenceDetectionReason)
reasonHeader := r.Header.Get(kolidePresenceDetectionReasonHeaderKey)
if reasonHeader != "" {
reason = reasonHeader
}
Expand All @@ -460,7 +462,7 @@ func (ls *localServer) presenceDetectionHandler(next http.Handler) http.Handler
// and send the request through
// allow the server to decide what to do based on last detection duration

w.Header().Add(kolideDurationSinceLastPresenceDetection, durationSinceLastDetection.String())
w.Header().Add(kolideDurationSinceLastPresenceDetectionHeaderKey, durationSinceLastDetection.String())
next.ServeHTTP(w, r)
})
}
Loading