Skip to content

Commit

Permalink
Merge pull request #487 from Security-Onion-Solutions/jertel/det
Browse files Browse the repository at this point in the history
remove custom files when resetting to default
  • Loading branch information
jertel committed May 14, 2024
2 parents 116deba + 20819ff commit 7e70360
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
39 changes: 24 additions & 15 deletions server/modules/salt/saltstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,23 +661,32 @@ func (store *Saltstore) UpdateSetting(ctx context.Context, setting *model.Settin
}

log.WithFields(log.Fields{
"settingId": setting.Id,
"minionId": setting.NodeId,
"path": path,
"length": len(setting.Value),
"settingId": setting.Id,
"minionId": setting.NodeId,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Updating advanced settings to new value")
os.WriteFile(path, []byte(setting.Value), 0600)

} else if setting.File {
path := fmt.Sprintf("%s/local/salt/%s", store.saltstackDir, store.relPathFromId(setting.Id))
if !remove {
log.WithFields(log.Fields{
"settingId": setting.Id,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Updating custom file setting to new value")

log.WithFields(log.Fields{
"settingId": setting.Id,
"path": path,
"length": len(setting.Value),
}).Info("Updating custom file setting to new value")
err = os.WriteFile(path, []byte(setting.Value), 0600)
} else {
log.WithFields(log.Fields{
"settingId": setting.Id,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Deleting custom file")

err = os.WriteFile(path, []byte(setting.Value), 0600)
err = os.Remove(path)
}
} else {
var path string
if setting.NodeId == "" {
Expand All @@ -691,15 +700,15 @@ func (store *Saltstore) UpdateSetting(ctx context.Context, setting *model.Settin
if err == nil {
if !remove {
log.WithFields(log.Fields{
"settingId": setting.Id,
"path": path,
"length": len(setting.Value),
"settingId": setting.Id,
"settingPath": path,
"settingLength": len(setting.Value),
}).Info("Updating setting to new value")
err = store.updateSetting(mapped, sections, setting)
} else {
log.WithFields(log.Fields{
"settingId": setting.Id,
"path": path,
"settingId": setting.Id,
"settingPath": path,
}).Info("Deleting setting")
_, err = store.deleteSetting(mapped, sections)
}
Expand Down
10 changes: 10 additions & 0 deletions server/modules/salt/saltstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ func TestUpdateSetting_UpdateFile(tester *testing.T) {
updated_setting := findSetting(settings, "myapp.foo__txt", "")
assert.Equal(tester, "anything", updated_setting.Default)
assert.Equal(tester, "something", updated_setting.Value)

// Delete setting
err = salt.UpdateSetting(ctx(), setting, true)
assert.NoError(tester, err)

settings, get_err = salt.GetSettings(ctx())
assert.NoError(tester, get_err)
updated_setting = findSetting(settings, "myapp.foo__txt", "")
assert.Equal(tester, "anything", updated_setting.Default)
assert.Equal(tester, "anything", updated_setting.Value)
}

func TestUpdateSetting_UpdateAdvancedFailToParse(tester *testing.T) {
Expand Down

0 comments on commit 7e70360

Please sign in to comment.