Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clearing protected settings #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions main/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func enablePre(ctx *log.Context, hEnv HandlerEnvironment, seqNum int) error {
// but last status file is still is "transitioning"
ctx.Log("event", "check status", "message", "transitioning status detected but no process to handle it - set to success status")
reportStatus(ctx, hEnv, seqNum, StatusSuccess, cmd{enable, "Enable", true, enablePre, 3}, "Last script execution didn't finish.")
} else { //agent restarts but execution is done and not transitioning
err = cleanUpSettings(hEnv.HandlerEnvironment.ConfigFolder)
if err != nil {
ctx.Log("message", "error clearing config folder")
} else {
ctx.Log("message", "config folder cleared successfully")
}
}
}
os.Exit(0)
Expand Down Expand Up @@ -189,6 +196,13 @@ func enable(ctx *log.Context, h HandlerEnvironment, seqNum int) (string, error)
ctx.Log("event", "enable script failed")
}

err = cleanUpSettings(h.HandlerEnvironment.ConfigFolder)
if err != nil {
ctx.Log("message", "error clearing config folder")
} else {
ctx.Log("message", "config folder cleared successfully")
}

msg := fmt.Sprintf("\n[stdout]\n%s\n[stderr]\n%s", string(stdoutTail), string(stderrTail))
// Always report nil for error because extension should not fail if script throws error
// Error still will be reported in the message
Expand Down
56 changes: 54 additions & 2 deletions main/handlersettings_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package main

import "testing"
import "github.com/stretchr/testify/require"
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/stretchr/testify/require"
)

func Test_handlerSettingsValidate(t *testing.T) {
// commandToExecute not specified
Expand Down Expand Up @@ -98,3 +105,48 @@ func Test_toJSON(t *testing.T) {
require.Nil(t, err)
require.Equal(t, `{"a":3}`, s)
}

func Test_protectedSettingsTest(t *testing.T) {
//set up test direcotry + test files
testFolderPath := "/config"
settingsExtensionName := ".settings"

err := createTestFiles(testFolderPath, settingsExtensionName)
require.NoError(t, err)

err = cleanUpSettings(testFolderPath)
require.NoError(t, err)

fileName := ""
for i := 0; i < 3; i++ {
fileName = filepath.Join(testFolderPath, strconv.FormatInt(int64(i), 10)+settingsExtensionName)
content, err := ioutil.ReadFile(fileName)
require.NoError(t, err)
require.Equal(t, len(content), 0)
}

// cleanup
defer os.RemoveAll(testFolderPath)
}

func createTestFiles(folderPath, settingsExtensionName string) error {
err := os.MkdirAll(folderPath, os.ModeDir)
if err != nil {
return err
}
fileName := ""
//create test directories
testContent := []byte("beep boop")
for i := 0; i < 3; i++ {
fileName = filepath.Join(folderPath, strconv.FormatInt(int64(i), 10)+settingsExtensionName)
file, err := os.Create(fileName)
if err != nil {
return err
}
size, err := file.Write(testContent)
if err != nil || size == 0 {
return err
}
}
return nil
}
21 changes: 21 additions & 0 deletions main/handlersettingscommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
)

const (
Expand Down Expand Up @@ -140,3 +141,23 @@ func unmarshalProtectedSettings(configFolder string, hs handlerSettingsCommon, v
}
return nil
}

// cleanUpSettings clears out the settings file [ex: 0.settings] to ensure no
// protected settings are logged in VM
func cleanUpSettings(configFolder string) error {
configDir, err := ioutil.ReadDir(configFolder)
if err != nil {
return err
}
content := []byte("")
for _, file := range configDir {
if strings.Compare(filepath.Ext(file.Name()), settingsFileSuffix) == 0 { //checking if its a settings file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save file.Name() and reuse

filePath := filepath.Join(configFolder, file.Name())
err = ioutil.WriteFile(filePath, content, 0644)
if err != nil {
return err
}
}
}
return nil
}