Skip to content

Commit

Permalink
[slogger] Move Windows platform tables to use slogger partially or fu…
Browse files Browse the repository at this point in the history
…lly (#1610)
  • Loading branch information
RebeccaMahany authored Feb 16, 2024
1 parent 691a668 commit 33e4d68
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 47 deletions.
25 changes: 18 additions & 7 deletions ee/tables/dsim_default_associations/dsim_default_associations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"bytes"
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/allowedcmd"
"github.com/kolide/launcher/ee/dataflatten"
Expand All @@ -23,15 +23,17 @@ import (
)

type Table struct {
logger log.Logger
slogger *slog.Logger
logger log.Logger // preserved only for temporary use in dataflattentable and tablehelpers.Exec
}

func TablePlugin(logger log.Logger) *table.Plugin {
func TablePlugin(slogger *slog.Logger, logger log.Logger) *table.Plugin {

columns := dataflattentable.Columns()

t := &Table{
logger: logger,
slogger: slogger.With("table", "kolide_dsim_default_associations"),
logger: logger,
}

return table.NewPlugin("kolide_dsim_default_associations", columns, t.generate)
Expand All @@ -42,7 +44,10 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (

dismResults, err := t.execDism(ctx)
if err != nil {
level.Info(t.logger).Log("msg", "dism failed", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"dsim failed",
"err", err,
)
return results, err
}

Expand All @@ -54,7 +59,10 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (

rows, err := dataflatten.Xml(dismResults, flattenOpts...)
if err != nil {
level.Info(t.logger).Log("msg", "flatten failed", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"flatten failed",
"err", err,
)
continue
}

Expand Down Expand Up @@ -90,7 +98,10 @@ func (t *Table) execDism(ctx context.Context) ([]byte, error) {
cmd.Stdout = &stdout
cmd.Stderr = &stderr

level.Debug(t.logger).Log("msg", "calling dism", "args", cmd.Args)
t.slogger.Log(ctx, slog.LevelDebug,
"calling dsim",
"args", cmd.Args,
)

if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("calling dism. Got: %s: %w", stderr.String(), err)
Expand Down
25 changes: 18 additions & 7 deletions ee/tables/secedit/secedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/allowedcmd"
"github.com/kolide/launcher/ee/dataflatten"
Expand All @@ -26,16 +26,18 @@ import (
)

type Table struct {
logger log.Logger
slogger *slog.Logger
logger log.Logger // preserved only for temporary use in dataflattentable and tablehelpers.Exec
}

func TablePlugin(logger log.Logger) *table.Plugin {
func TablePlugin(slogger *slog.Logger, logger log.Logger) *table.Plugin {
columns := dataflattentable.Columns(
table.TextColumn("mergedpolicy"),
)

t := &Table{
logger: logger,
slogger: slogger.With("table", "kolide_secedit"),
logger: logger,
}

return table.NewPlugin("kolide_secedit", columns, t.generate)
Expand All @@ -47,20 +49,29 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (
for _, mergedpolicy := range tablehelpers.GetConstraints(queryContext, "mergedpolicy", tablehelpers.WithDefaults("false")) {
useMergedPolicy, err := strconv.ParseBool(mergedpolicy)
if err != nil {
level.Info(t.logger).Log("msg", "Cannot convert mergedpolicy constraint into a boolean value. Try passing \"true\"", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"cannot convert mergedpolicy constraint into a boolean value",
"err", err,
)
continue
}

for _, dataQuery := range tablehelpers.GetConstraints(queryContext, "query", tablehelpers.WithDefaults("*")) {
secEditResults, err := t.execSecedit(ctx, useMergedPolicy)
if err != nil {
level.Info(t.logger).Log("msg", "secedit failed", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"secedit failed",
"err", err,
)
continue
}

flatData, err := t.flattenOutput(dataQuery, secEditResults)
if err != nil {
level.Info(t.logger).Log("msg", "flatten failed", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"flatten failed",
"err", err,
)
continue
}

Expand Down
18 changes: 12 additions & 6 deletions ee/tables/wifi_networks/wifi_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"context"
_ "embed"
"fmt"
"log/slog"
"os"
"path/filepath"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/allowedcmd"
"github.com/kolide/launcher/ee/dataflatten"
Expand All @@ -37,16 +37,18 @@ var pwshScript []byte
type execer func(ctx context.Context, buf *bytes.Buffer) error

type Table struct {
logger log.Logger
slogger *slog.Logger
logger log.Logger // preserved only for use in dataflattentable temporarily
getBytes execer
}

func TablePlugin(logger log.Logger) *table.Plugin {
func TablePlugin(slogger *slog.Logger, logger log.Logger) *table.Plugin {
columns := dataflattentable.Columns()

t := &Table{
slogger: slogger.With("table", "kolide_wifi_networks"),
logger: logger,
getBytes: execPwsh(logger),
getBytes: execPwsh(slogger),
}

return table.NewPlugin("kolide_wifi_networks", columns, t.generate)
Expand All @@ -69,7 +71,7 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (
return append(results, dataflattentable.ToMap(rows, "", nil)...), nil
}

func execPwsh(logger log.Logger) execer {
func execPwsh(slogger *slog.Logger) execer {
return func(ctx context.Context, buf *bytes.Buffer) error {
// MS requires interfaces to complete network scans in <4 seconds, but
// that appears not to be consistent
Expand Down Expand Up @@ -106,7 +108,11 @@ func execPwsh(logger log.Logger) execer {
// successful execution code.
if err != nil || errOutput != "" {
// if there is an error, inspect the contents of stdout
level.Debug(logger).Log("msg", "error execing, inspecting stdout contents", "stdout", buf.String())
slogger.Log(ctx, slog.LevelDebug,
"error execing, inspecting stdout contents",
"stdout", buf.String(),
"err", err,
)

if err == nil {
err = fmt.Errorf("exec succeeded, but emitted to stderr")
Expand Down
24 changes: 17 additions & 7 deletions ee/tables/windowsupdatetable/windowsupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strconv"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/ee/dataflatten"
"github.com/kolide/launcher/ee/tables/dataflattentable"
"github.com/kolide/launcher/ee/tables/tablehelpers"
Expand All @@ -28,12 +28,13 @@ const (
)

type Table struct {
logger log.Logger
slogger *slog.Logger
logger log.Logger // preserved only for temporary use in dataflattentable
queryFunc queryFuncType
name string
}

func TablePlugin(mode tableMode, logger log.Logger) *table.Plugin {
func TablePlugin(mode tableMode, slogger *slog.Logger, logger log.Logger) *table.Plugin {

columns := dataflattentable.Columns(
table.TextColumn("locale"),
Expand All @@ -53,6 +54,8 @@ func TablePlugin(mode tableMode, logger log.Logger) *table.Plugin {
t.name = "kolide_windows_update_history"
}

t.slogger = slogger.With("name", t.name)

return table.NewPlugin(t.name, columns, t.generate)
}

Expand All @@ -70,9 +73,13 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (
var results []map[string]string

for _, locale := range tablehelpers.GetConstraints(queryContext, "locale", tablehelpers.WithDefaults("_default")) {
result, err := t.searchLocale(locale, queryContext)
result, err := t.searchLocale(ctx, locale, queryContext)
if err != nil {
level.Info(t.logger).Log("msg", "got error searching", "locale", locale, "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"got error searching",
"locale", locale,
"err", err,
)
continue
}
results = append(results, result...)
Expand All @@ -83,7 +90,7 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (

}

func (t *Table) searchLocale(locale string, queryContext table.QueryContext) ([]map[string]string, error) {
func (t *Table) searchLocale(ctx context.Context, locale string, queryContext table.QueryContext) ([]map[string]string, error) {
comshim.Add(1)
defer comshim.Done()

Expand All @@ -102,7 +109,10 @@ func (t *Table) searchLocale(locale string, queryContext table.QueryContext) ([]
for _, dataQuery := range tablehelpers.GetConstraints(queryContext, "query", tablehelpers.WithDefaults("*")) {
flatData, err := t.flattenOutput(dataQuery, searchResults)
if err != nil {
level.Info(t.logger).Log("msg", "flatten failed", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"flatten failed",
"err", err,
)
continue
}

Expand Down
29 changes: 17 additions & 12 deletions ee/tables/wmitable/wmitable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ package wmitable
import (
"context"
"errors"
"log/slog"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/ee/dataflatten"
"github.com/kolide/launcher/ee/tables/dataflattentable"
"github.com/kolide/launcher/ee/tables/tablehelpers"
"github.com/kolide/launcher/ee/wmi"
"github.com/kolide/launcher/pkg/contexts/ctxlog"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/osquery/osquery-go/plugin/table"
)

const allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"

type Table struct {
logger log.Logger
slogger *slog.Logger
logger log.Logger // preserved only for temporary use in dataflattentable and tablehelpers.Exec
}

func TablePlugin(logger log.Logger) *table.Plugin {
func TablePlugin(slogger *slog.Logger, logger log.Logger) *table.Plugin {

columns := dataflattentable.Columns(
table.TextColumn("namespace"),
Expand All @@ -36,7 +37,8 @@ func TablePlugin(logger log.Logger) *table.Plugin {
)

t := &Table{
logger: level.NewFilter(logger),
logger: level.NewFilter(logger),
slogger: slogger.With("table", "kolide_wmi"),
}

return table.NewPlugin("kolide_wmi", columns, t.generate)
Expand Down Expand Up @@ -97,19 +99,19 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (

wmiResults, err := wmi.Query(ctx, class, properties, wmi.ConnectUseMaxWait(), wmi.ConnectNamespace(ns), wmi.WithWhere(whereClause))
if err != nil {
level.Info(t.logger).Log(
"msg", "wmi query failure",
t.slogger.Log(ctx, slog.LevelInfo,
"wmi query failure",
"err", err,
"class", class,
"properties", rawProperties,
"namespace", ns,
"whereClause", whereClause,
"where_clause", whereClause,
)
continue
}

for _, dataQuery := range flattenQueries {
results = append(results, t.flattenRowsFromWmi(dataQuery, wmiResults, class, rawProperties, ns, whereClause)...)
results = append(results, t.flattenRowsFromWmi(ctx, dataQuery, wmiResults, class, rawProperties, ns, whereClause)...)
}
}
}
Expand All @@ -119,7 +121,7 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (
return results, nil
}

func (t *Table) flattenRowsFromWmi(dataQuery string, wmiResults []map[string]interface{}, wmiClass, wmiProperties, wmiNamespace, whereClause string) []map[string]string {
func (t *Table) flattenRowsFromWmi(ctx context.Context, dataQuery string, wmiResults []map[string]interface{}, wmiClass, wmiProperties, wmiNamespace, whereClause string) []map[string]string {
flattenOpts := []dataflatten.FlattenOpts{
dataflatten.WithLogger(t.logger),
dataflatten.WithQuery(strings.Split(dataQuery, "/")),
Expand All @@ -134,7 +136,10 @@ func (t *Table) flattenRowsFromWmi(dataQuery string, wmiResults []map[string]int

flatData, err := dataflatten.Flatten(resultsCasted, flattenOpts...)
if err != nil {
level.Info(t.logger).Log("msg", "failure flattening output", "err", err)
t.slogger.Log(ctx, slog.LevelInfo,
"failure flattening output",
"err", err,
)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion ee/tables/wmitable/wmitable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/tables/tablehelpers"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestQueries(t *testing.T) {
t.Parallel()

wmiTable := Table{logger: log.NewNopLogger()}
wmiTable := Table{logger: log.NewNopLogger(), slogger: multislogger.New().Logger}

var tests = []struct {
name string
Expand Down
Loading

0 comments on commit 33e4d68

Please sign in to comment.