From afeb521f079502cc310ced07779e27d9c945df23 Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Sun, 28 Jan 2018 16:57:55 -0500 Subject: [PATCH] feature: remove bad config vars from envfiles when loaded resolves: #3036 --- plugins/config/config_test.go | 26 ++++++++++++++++++++++++++ plugins/config/environment.go | 14 ++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/plugins/config/config_test.go b/plugins/config/config_test.go index c0e47969e..05f42faad 100644 --- a/plugins/config/config_test.go +++ b/plugins/config/config_test.go @@ -140,6 +140,32 @@ func TestInvalidKeys(t *testing.T) { } } +func TestInvalidEnvOnDisk(t *testing.T) { + RegisterTestingT(t) + Expect(setupTestApp()).To(Succeed()) + defer teardownTestApp() + + appConfigFile := strings.Join([]string{testAppDir, "/ENV"}, "") + b := []byte("export --invalid-key=TESTING\nexport valid_key=value\n") + if err := ioutil.WriteFile(appConfigFile, b, 0644); err != nil { + return + } + + env, err := LoadAppEnv(testAppName) + Expect(err).NotTo(HaveOccurred()) + _, ok := env.Get("--invalid-key") + Expect(ok).To(Equal(false)) + value, ok := env.Get("valid_key") + Expect(ok).To(Equal(true)) + Expect(value).To(Equal("value")) + + //LoadAppEnv eliminates it from the file + content, err := ioutil.ReadFile(appConfigFile) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.Contains(string(content), "--invalid-key")).To(BeFalse()) + +} + func expectValue(appName string, key string, expected string) { v, ok := Get(appName, key) Expect(ok).To(Equal(true)) diff --git a/plugins/config/environment.go b/plugins/config/environment.go index 36289cec4..9a9e9e77a 100644 --- a/plugins/config/environment.go +++ b/plugins/config/environment.go @@ -253,6 +253,20 @@ func loadFromFile(name string, filename string) (env *Env, err error) { envMap, err = godotenv.Read(filename) } + dirty := false + for k := range envMap { + if err := validateKey(k); err != nil { + common.LogInfo1(fmt.Sprintf("Deleting invalid key %s from config for %s", k, name)) + delete(envMap, k) + dirty = true + } + } + if dirty { + if err := godotenv.Write(envMap, filename); err != nil { + common.LogFail(fmt.Sprintf("Error writing back config for %s after removing invalid keys", name)) + } + } + env = &Env{ name: name, filename: filename,