<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #38889 PLEASE READ BELOW before looking at file changes Before converting individual files/packages to slog, we generally need to make these 2 changes to make the conversion easier: - Replace uses of `kitlog.With` since they are not fully compatible with our kitlog adapter - Directly use the kitlog adapter logger type instead of the kitlog interface, which will let us have direct access to the underlying slog logger: `*logging.Logger` Note: that I did not replace absolutely all uses of `kitlog.Logger`, but I did remove all uses of `kitlog.With` except for these due to complexity: - server/logging/filesystem.go and the other log writers (webhook, firehose, kinesis, lambda, pubsub, nats) - server/datastore/mysql/nanomdm_storage.go (adapter pattern) - server/vulnerabilities/nvd/* (cascades to CLI tools) - server/service/osquery_utils/queries.go (callback type signatures cascade broadly) - cmd/maintained-apps/ (standalone, so can be transitioned later all at once) Most of the changes in this PR follow these patterns: - `kitlog.Logger` type → `*logging.Logger` - `kitlog.With(logger, ...)` → `logger.With(...)` - `kitlog.NewNopLogger() → logging.NewNopLogger()`, including similar variations such as `logging.NewLogfmtLogger(w)` and `logging.NewJSONLogger(w)` - removed many now-unused kitlog imports Unique changes that the PR review should focus on: - server/platform/logging/kitlog_adapter.go: Core adapter changes - server/platform/logging/logging.go: New convenience functions - server/service/integration_logger_test.go: Test changes for slog # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Was added in previous PR ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Migrated the codebase to a unified internal structured logging system for more consistent, reliable logs and observability. * No user-facing functionality changed; runtime behavior and APIs remain compatible. * **Tests** * Updated tests to use the new logging helpers to ensure consistent test logging and validation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
168 lines
4.3 KiB
Go
168 lines
4.3 KiB
Go
package launcher
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/health"
|
|
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
|
"github.com/fleetdm/fleet/v4/server/service/mock"
|
|
"github.com/kolide/launcher/pkg/service"
|
|
"github.com/osquery/osquery-go/plugin/distributed"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLauncherEnrollment(t *testing.T) {
|
|
launcher, tls := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
nodeKey, invalid, err := launcher.RequestEnrollment(ctx, "secret", "identifier", service.EnrollmentDetails{})
|
|
require.Nil(t, err)
|
|
assert.True(t, tls.EnrollOsqueryFuncInvoked)
|
|
assert.False(t, invalid)
|
|
assert.Equal(t, "noop", nodeKey)
|
|
}
|
|
|
|
func TestLauncherRequestConfig(t *testing.T) {
|
|
launcher, tls := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
config, invalid, err := launcher.RequestConfig(ctx, "noop")
|
|
require.Nil(t, err)
|
|
assert.True(t, tls.AuthenticateHostFuncInvoked)
|
|
assert.False(t, invalid)
|
|
assert.JSONEq(t, `{"options":{"key":"value"},"decorators":{"deco":"foobar"}}`, config)
|
|
}
|
|
|
|
func TestLauncherRequestQueries(t *testing.T) {
|
|
launcher, tls := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
result, invalid, err := launcher.RequestQueries(ctx, "noop")
|
|
require.Nil(t, err)
|
|
assert.True(t, tls.AuthenticateHostFuncInvoked)
|
|
assert.False(t, invalid)
|
|
assert.Equal(t, map[string]string{"noop": `{"key": "value"}`}, result.Queries)
|
|
}
|
|
|
|
func TestLauncherPublishResults(t *testing.T) {
|
|
launcher, tls := newTestService(t)
|
|
ctx := context.Background()
|
|
|
|
_, _, invalid, err := launcher.PublishResults(
|
|
ctx,
|
|
"noop",
|
|
[]distributed.Result{},
|
|
)
|
|
require.Nil(t, err)
|
|
assert.True(t, tls.AuthenticateHostFuncInvoked)
|
|
assert.False(t, invalid)
|
|
|
|
// test with result
|
|
result := map[string]string{"key": "value"}
|
|
tls.SubmitDistributedQueryResultsFunc = func(
|
|
ctx context.Context,
|
|
results fleet.OsqueryDistributedQueryResults,
|
|
statuses map[string]fleet.OsqueryStatus,
|
|
messages map[string]string,
|
|
stats map[string]*fleet.Stats,
|
|
) (err error) {
|
|
assert.Equal(t, results["query"][0], result)
|
|
return nil
|
|
}
|
|
|
|
_, _, invalid, err = launcher.PublishResults(
|
|
ctx,
|
|
"noop",
|
|
[]distributed.Result{
|
|
{
|
|
QueryName: "query",
|
|
Status: 1,
|
|
Rows: []map[string]string{result},
|
|
},
|
|
},
|
|
)
|
|
require.Nil(t, err)
|
|
assert.False(t, invalid)
|
|
}
|
|
|
|
func newTestService(t *testing.T) (*launcherWrapper, *mock.TLSService) {
|
|
tls := newTLSService(t)
|
|
launcher := &launcherWrapper{
|
|
tls: tls,
|
|
logger: logging.NewNopLogger(),
|
|
healthCheckers: map[string]health.Checker{
|
|
"noop": health.Nop(),
|
|
},
|
|
}
|
|
return launcher, tls
|
|
}
|
|
|
|
// NewTLS service returns a mock TLS service where all the methods have a noop implementation.
|
|
// To test additional behaviors, override the funcs on the TLSService struct.
|
|
func newTLSService(t *testing.T) *mock.TLSService {
|
|
return &mock.TLSService{
|
|
EnrollOsqueryFunc: func(
|
|
ctx context.Context,
|
|
enrollSecret string,
|
|
hostIdentifier string,
|
|
hostDetails map[string](map[string]string),
|
|
) (nodeKey string, err error) {
|
|
nodeKey = "noop"
|
|
return
|
|
},
|
|
|
|
AuthenticateHostFunc: func(
|
|
ctx context.Context,
|
|
nodeKey string,
|
|
) (host *fleet.Host, debug bool, err error) {
|
|
return &fleet.Host{
|
|
NodeKey: &nodeKey,
|
|
}, false, nil
|
|
},
|
|
GetClientConfigFunc: func(
|
|
ctx context.Context,
|
|
) (config map[string]interface{}, err error) {
|
|
return map[string]interface{}{
|
|
"options": map[string]interface{}{
|
|
"key": "value",
|
|
},
|
|
"decorators": map[string]interface{}{
|
|
"deco": "foobar",
|
|
},
|
|
}, nil
|
|
},
|
|
|
|
GetDistributedQueriesFunc: func(
|
|
ctx context.Context,
|
|
) (queries map[string]string, discovery map[string]string, accelerate uint, err error) {
|
|
queries = map[string]string{
|
|
"noop": `{"key": "value"}`,
|
|
}
|
|
discovery = map[string]string{
|
|
"noop": `select 1`,
|
|
}
|
|
return
|
|
},
|
|
SubmitDistributedQueryResultsFunc: func(
|
|
ctx context.Context,
|
|
results fleet.OsqueryDistributedQueryResults,
|
|
statuses map[string]fleet.OsqueryStatus,
|
|
messages map[string]string,
|
|
stats map[string]*fleet.Stats,
|
|
) (err error) {
|
|
return
|
|
},
|
|
|
|
SubmitStatusLogsFunc: func(ctx context.Context, logs []json.RawMessage) (err error) {
|
|
return
|
|
},
|
|
SubmitResultLogsFunc: func(ctx context.Context, logs []json.RawMessage) (err error) {
|
|
return
|
|
},
|
|
}
|
|
}
|