From 487a7728b92a51ce2746a0677c7855e31d494fe3 Mon Sep 17 00:00:00 2001 From: James Pickett Date: Wed, 24 Apr 2024 09:10:26 -0700 Subject: [PATCH] extension opens writer to write new config, feedback --- ee/agent/startupsettings/writer.go | 3 +-- ee/agent/startupsettings/writer_test.go | 3 +-- pkg/osquery/extension.go | 21 +++++++++++++++++---- pkg/osquery/interactive/interactive.go | 4 ++-- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ee/agent/startupsettings/writer.go b/ee/agent/startupsettings/writer.go index 154d0d6b2..4c6105346 100644 --- a/ee/agent/startupsettings/writer.go +++ b/ee/agent/startupsettings/writer.go @@ -12,7 +12,6 @@ import ( "github.com/kolide/launcher/ee/agent/flags/keys" agentsqlite "github.com/kolide/launcher/ee/agent/storage/sqlite" "github.com/kolide/launcher/ee/agent/types" - "github.com/kolide/launcher/pkg/osquery" "github.com/kolide/launcher/pkg/traces" ) @@ -101,7 +100,7 @@ func (s *startupSettingsWriter) Close() error { } func (s *startupSettingsWriter) extractAutoTableConstructionConfig() (string, error) { - osqConfig, err := s.knapsack.ConfigStore().Get([]byte(osquery.ConfigKey)) + osqConfig, err := s.knapsack.ConfigStore().Get([]byte("config")) if err != nil { return "", fmt.Errorf("could not get osquery config from store: %w", err) } diff --git a/ee/agent/startupsettings/writer_test.go b/ee/agent/startupsettings/writer_test.go index 6fa7fe21a..cf3167a42 100644 --- a/ee/agent/startupsettings/writer_test.go +++ b/ee/agent/startupsettings/writer_test.go @@ -12,7 +12,6 @@ import ( agentsqlite "github.com/kolide/launcher/ee/agent/storage/sqlite" typesmocks "github.com/kolide/launcher/ee/agent/types/mocks" "github.com/kolide/launcher/pkg/log/multislogger" - "github.com/kolide/launcher/pkg/osquery" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" _ "modernc.org/sqlite" @@ -140,7 +139,7 @@ func TestFlagsChanged(t *testing.T) { configJson, err := json.Marshal(configMap) require.NoError(t, err, "marshalling config map") - configStore.Set([]byte(osquery.ConfigKey), configJson) + configStore.Set([]byte("config"), configJson) k.On("ConfigStore").Return(configStore) // Set up storage db, which should create the database and set all flags diff --git a/pkg/osquery/extension.go b/pkg/osquery/extension.go index 7e41d3143..631937a3c 100644 --- a/pkg/osquery/extension.go +++ b/pkg/osquery/extension.go @@ -15,6 +15,7 @@ import ( "github.com/google/uuid" "github.com/kolide/launcher/ee/agent" + "github.com/kolide/launcher/ee/agent/startupsettings" "github.com/kolide/launcher/ee/agent/types" "github.com/kolide/launcher/pkg/backoff" "github.com/kolide/launcher/pkg/osquery/runtime/history" @@ -51,7 +52,7 @@ const ( // DB key for node key nodeKeyKey = "nodeKey" // DB key for last retrieved config - ConfigKey = "config" + configKey = "config" // DB keys for the rsa keys privateKeyKey = "privateKey" @@ -332,7 +333,7 @@ func NodeKey(getter types.Getter) (string, error) { // Config returns the device config from the storage layer func Config(getter types.Getter) (string, error) { - key, err := getter.Get([]byte(ConfigKey)) + key, err := getter.Get([]byte(configKey)) if err != nil { return "", fmt.Errorf("error getting config key: %w", err) } @@ -504,7 +505,7 @@ func (e *Extension) GenerateConfigs(ctx context.Context) (map[string]string, err ) // Try to use cached config var confBytes []byte - confBytes, _ = e.knapsack.ConfigStore().Get([]byte(ConfigKey)) + confBytes, _ = e.knapsack.ConfigStore().Get([]byte(configKey)) if len(confBytes) == 0 { if !e.enrolled() { @@ -516,7 +517,19 @@ func (e *Extension) GenerateConfigs(ctx context.Context) (map[string]string, err config = string(confBytes) } else { // Store good config - e.knapsack.ConfigStore().Set([]byte(ConfigKey), []byte(config)) + e.knapsack.ConfigStore().Set([]byte(configKey), []byte(config)) + + // open the start up settings writer just to trigger a write of the config, + // then we can immediately close it + startupSettingsWriter, err := startupsettings.OpenWriter(ctx, e.knapsack) + if err != nil { + e.slogger.Log(ctx, slog.LevelWarn, + "could not get startup settings writer", + "err", err, + ) + } else { + startupSettingsWriter.Close() + } // TODO log or record metrics when caching config fails? We // would probably like to return the config and not an error in // this case. diff --git a/pkg/osquery/interactive/interactive.go b/pkg/osquery/interactive/interactive.go index ae2257ec7..7506d26a8 100644 --- a/pkg/osquery/interactive/interactive.go +++ b/pkg/osquery/interactive/interactive.go @@ -61,7 +61,7 @@ func StartProcess(slogger *slog.Logger, rootDir, osquerydPath string, osqueryFla // if we were not provided a config path flag, try to add default config if !haveConfigPathOsqFlag { // check to see if we can actually get a config plugin - configPlugin, err := configPlugin() + configPlugin, err := generateConfigPlugin() if err != nil { slogger.Log(context.TODO(), slog.LevelDebug, "error creating config plugin", @@ -189,7 +189,7 @@ func waitForFile(path string, interval, timeout time.Duration) error { } } -func configPlugin() (*config.Plugin, error) { +func generateConfigPlugin() (*config.Plugin, error) { r, err := startupsettings.OpenReader(context.TODO(), launcher.DefaultPath(launcher.RootDirectory)) if err != nil { return nil, fmt.Errorf("error opening startup settings reader: %w", err)