Skip to content

Commit

Permalink
Add config to startup_settings so interactive can access it as well
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Jun 25, 2024
1 parent 1645c4c commit 7c4698a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
7 changes: 4 additions & 3 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
startupSpan.AddEvent("log_shipper_init_completed")
}

s, err := startupsettings.OpenWriter(ctx, k)
startupSettingsWriter, err := startupsettings.OpenWriter(ctx, k)
if err != nil {
return fmt.Errorf("creating startup db: %w", err)
}
defer s.Close()
defer startupSettingsWriter.Close()

if err := s.WriteSettings(); err != nil {
if err := startupSettingsWriter.WriteSettings(); err != nil {
slogger.Log(ctx, slog.LevelError,
"writing startup settings",
"err", err,
Expand Down Expand Up @@ -404,6 +404,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
// atcConfigConsumer handles updates to Kolide's custom ATC tables
controlService.RegisterConsumer(atcConfigSubsystemName, keyvalueconsumer.New(k.AtcConfigStore()))
controlService.RegisterSubscriber(atcConfigSubsystemName, osqueryRunner)
controlService.RegisterSubscriber(atcConfigSubsystemName, startupSettingsWriter)

runner, err = desktopRunner.New(
k,
Expand Down
37 changes: 37 additions & 0 deletions ee/agent/startupsettings/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ func OpenWriter(ctx context.Context, knapsack types.Knapsack) (*startupSettingsW
return s, nil
}

// Ping satisfies the control.subscriber interface -- the runner subscribes to changes to
// the kolide_atc_config subsystem.
func (s *startupSettingsWriter) Ping() {
if err := s.WriteSettings(); err != nil {
s.knapsack.Slogger().Log(context.TODO(), slog.LevelWarn,
"could not write updated settings",
"err", err,
)
}
}

// WriteSettings updates the flags with their values from the agent flag data store.
func (s *startupSettingsWriter) WriteSettings() error {
updatedFlags := make(map[string]string)
Expand All @@ -69,6 +80,15 @@ func (s *startupSettingsWriter) WriteSettings() error {
updatedFlags["auto_table_construction"] = atcConfig
}

if kolideAtcConfig, err := s.extractKolideAutoTableConstructionConfig(); err != nil {
s.knapsack.Slogger().Log(context.TODO(), slog.LevelDebug,
"extracting kolide_atc_config",
"err", err,
)
} else {
updatedFlags["kolide_atc_config"] = kolideAtcConfig
}

if _, err := s.kvStore.Update(updatedFlags); err != nil {
return fmt.Errorf("writing settings: %w", err)
}
Expand Down Expand Up @@ -119,3 +139,20 @@ func (s *startupSettingsWriter) extractAutoTableConstructionConfig() (string, er

return string(atcJson), nil
}

func (s *startupSettingsWriter) extractKolideAutoTableConstructionConfig() (string, error) {
kolideCfg := make(map[string]string)
if err := s.knapsack.AtcConfigStore().ForEach(func(k []byte, v []byte) error {
kolideCfg[string(k)] = string(v)
return nil
}); err != nil {
return "", fmt.Errorf("could not get Kolide ATC config from store: %w", err)
}

atcJson, err := json.Marshal(kolideCfg)
if err != nil {
return "", fmt.Errorf("could not marshal kolide_atc_config: %w", err)
}

return string(atcJson), nil
}
71 changes: 60 additions & 11 deletions pkg/osquery/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package table

import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"

"github.com/kolide/launcher/ee/agent/startupsettings"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/allowedcmd"
"github.com/kolide/launcher/ee/tables/cryptoinfotable"
Expand Down Expand Up @@ -72,24 +76,69 @@ func PlatformTables(k types.Knapsack, slogger *slog.Logger, currentOsquerydBinar
return tables
}

// kolideCustomAtcTables will handle indexeddb tables and others in the future. For now,
// it just logs the config.
// kolideCustomAtcTables retrieves Kolide ATC config from the appropriate data store(s).
// For now, it just logs the configuration. In the future, it will handle indexeddb tables
// and others.
func kolideCustomAtcTables(k types.Knapsack, slogger *slog.Logger) []osquery.OsqueryPlugin {
loggableConfig := make(map[string]string)
if err := k.AtcConfigStore().ForEach(func(k []byte, v []byte) error {
loggableConfig[string(k)] = string(v)
return nil
}); err != nil {
// Fetch tables from KVStore or from startup settings
config, err := kolideAtcConfigFromDb(k)
if err != nil {
slogger.Log(context.TODO(), slog.LevelDebug,
"could not retrieve contents of Kolide ATC config store",
"could not retrieve Kolide ATC config from store, may not have access -- falling back to startup settings",
"err", err,
)
return nil

config, err = kolideAtcConfigFromStartupSettings(k)
if err != nil {
slogger.Log(context.TODO(), slog.LevelWarn,
"could not retrieve Kolide ATC config from startup settings",
"err", err,
)
return nil
}
}

// In the future, we would construct the plugins from the configuration here.
// For now, we just log.
slogger.Log(context.TODO(), slog.LevelDebug,
"retrieved contents of Kolide ATC config store",
"config", loggableConfig,
"retrieved Kolide ATC config",
"config", config,
)

return nil
}

func kolideAtcConfigFromDb(k types.Knapsack) (map[string]string, error) {
if k == nil || k.AtcConfigStore() == nil {
return nil, errors.New("stores in knapsack not available")
}
loggableConfig := make(map[string]string)
if err := k.AtcConfigStore().ForEach(func(k []byte, v []byte) error {
loggableConfig[string(k)] = string(v)
return nil
}); err != nil {
return nil, fmt.Errorf("retrieving contents of Kolide ATC config store: %w", err)
}

return loggableConfig, nil
}

func kolideAtcConfigFromStartupSettings(k types.Knapsack) (map[string]string, error) {
r, err := startupsettings.OpenReader(context.TODO(), k.RootDirectory())
if err != nil {
return nil, fmt.Errorf("error opening startup settings reader: %w", err)
}
defer r.Close()

atcConfig, err := r.Get("kolide_atc_config")
if err != nil {
return nil, fmt.Errorf("error getting kolide_atc_config from startup settings: %w", err)
}

var loggableConfig map[string]string
if err := json.Unmarshal([]byte(atcConfig), &loggableConfig); err != nil {
return nil, fmt.Errorf("unmarshalling kolide_atc_config: %w", err)
}

return loggableConfig, nil
}

0 comments on commit 7c4698a

Please sign in to comment.