fix: report config write failures instead of exiting 0

`config:set`, `config:unset` and `config:clear` discarded the result of writing the app's environment, so a write that never landed still printed `Setting config vars`, still fired the `post-config-update` trigger, still restarted the app and still exited 0. The `config-set` and `config-unset` plugin triggers behaved the same way, which meant an `app.json` `env` block that could not be persisted deployed anyway with the variables missing. The environment is now staged in a temporary file and renamed into place, so a write that fails partway leaves the previous environment intact, and that file is created at `0600` rather than being created at `0644` and chmodded afterwards. `apps:clone` and `apps:rename` never applied that chmod at all and left the new app's environment world-readable.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-09-18 00:36:38 -04:00
1 parent 4fc19fe1bc
commit 0c3ae47299
11 files changed
+369 -43

No files matched your search

+92
View File
@@ -1,6 +1,8 @@
package config
import (
"os"
"path/filepath"
"testing"
. "github.com/onsi/gomega"
@@ -91,6 +93,96 @@ func TestGet(t *testing.T) {
Expect(b).To(Equal(true)) //anything but "0" is true
}
// TestWriteSetsRestrictivePermissions covers an Env written over a file left
// behind at a mode other users can read, which is what every app ENV file
// created before this was fixed looks like on disk.
func TestWriteSetsRestrictivePermissions(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
filename := filepath.Join(t.TempDir(), "ENV")
Expect(os.WriteFile(filename, []byte("export testKey=TESTING\n"), 0644)).To(Succeed())
env, err := loadFromFile("alpha", filename)
Expect(err).To(Succeed())
env.Set("testKey", "updated")
Expect(env.Write()).To(Succeed())
info, err := os.Stat(filename)
Expect(err).To(Succeed())
Expect(info.Mode().Perm()).To(Equal(os.FileMode(0600)))
expectFileValue(filename, "testKey", "updated")
}
func TestWriteFailsWhenDirectoryIsMissing(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
env, err := loadFromFile("alpha", filepath.Join(t.TempDir(), "missing", "ENV"))
Expect(err).To(Succeed())
env.Set("testKey", "value")
Expect(env.Write()).ToNot(Succeed())
}
// TestWriteLeavesFileIntactOnFailure covers the guarantee that makes a reported
// failure safe to act on: the environment is staged in a temporary file and
// renamed into place, so a failure never leaves a truncated ENV file behind.
// The permission step is what is made to fail here, because it is the last
// thing that happens before the rename.
func TestWriteLeavesFileIntactOnFailure(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
filename := filepath.Join(t.TempDir(), "ENV")
Expect(os.WriteFile(filename, []byte("export testKey=TESTING\n"), 0600)).To(Succeed())
original, err := os.ReadFile(filename)
Expect(err).To(Succeed())
env, err := loadFromFile("alpha", filename)
Expect(err).To(Succeed())
env.Set("testKey", "updated")
t.Setenv("DOKKU_SYSTEM_USER", "dokku-no-such-user")
Expect(env.Write()).ToNot(Succeed())
Expect(os.ReadFile(filename)).To(Equal(original))
}
// TestWriteFollowsSymlinks covers an ENV file an operator has pointed at
// storage of their own. Renaming the staged file over the symlink would replace
// it with a regular file and orphan the original.
func TestWriteFollowsSymlinks(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
dir := t.TempDir()
target := filepath.Join(dir, "elsewhere", "ENV")
Expect(os.MkdirAll(filepath.Dir(target), 0755)).To(Succeed())
Expect(os.WriteFile(target, []byte("export testKey=TESTING\n"), 0600)).To(Succeed())
filename := filepath.Join(dir, "ENV")
Expect(os.Symlink(target, filename)).To(Succeed())
env, err := loadFromFile("alpha", filename)
Expect(err).To(Succeed())
env.Set("testKey", "updated")
Expect(env.Write()).To(Succeed())
info, err := os.Lstat(filename)
Expect(err).To(Succeed())
Expect(info.Mode() & os.ModeSymlink).ToNot(Equal(os.FileMode(0)))
expectFileValue(target, "testKey", "updated")
}
// expectFileValue asserts the ENV file at filename holds the given value
func expectFileValue(filename string, key string, expected string) {
env, err := loadFromFile("expected", filename)
Expect(err).To(Succeed())
value, ok := env.Get(key)
Expect(ok).To(Equal(true))
Expect(value).To(Equal(expected))
}
func pairs(vars ...string) map[string]string {
res := map[string]string{}
var i = 0