Skip to content

Commit

Permalink
extension opens writer to write new config, feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Pickett committed Apr 24, 2024
1 parent bcec485 commit 487a772
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 1 addition & 2 deletions ee/agent/startupsettings/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions ee/agent/startupsettings/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions pkg/osquery/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/osquery/interactive/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 487a772

Please sign in to comment.