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

+9 -15
View File
@@ -55,11 +55,9 @@ func SetMany(appName string, entries map[string]string, replace bool, restart bo
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
fmt.Println(prettyPrintEnvEntries(" ", entries))
}
env.Write()
common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
})
if err := env.Write(); err != nil {
return fmt.Errorf("Unable to write config vars: %s", err.Error())
}
triggerUpdate(appName, "set", keys)
}
if !global && restart && shouldRestart(appName) {
@@ -91,11 +89,9 @@ func UnsetMany(appName string, keys []string, restart bool) (err error) {
}
}
if changed {
env.Write()
common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
})
if err := env.Write(); err != nil {
return fmt.Errorf("Unable to write config vars: %s", err.Error())
}
triggerUpdate(appName, "unset", keys)
}
if !global && restart && shouldRestart(appName) {
@@ -118,11 +114,9 @@ func UnsetAll(appName string, restart bool) (err error) {
changed = true
}
if changed {
env.Write()
common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
})
if err := env.Write(); err != nil {
return fmt.Errorf("Unable to write config vars: %s", err.Error())
}
triggerUpdate(appName, "clear", []string{})
}
if !global && restart && shouldRestart(appName) {
+64 -3
View File
@@ -164,7 +164,7 @@ testKey2=TESTING-updated2
env.Set("testKey", "TESTING-original1")
env.Set("testKey2", "TESTING-original2")
env.Set("testKey3", "TESTING-original3")
env.Write()
Expect(env.Write()).To(Succeed())
Expect(CommandImport(testAppName, false, false, true, "envfile", tempFile.Name())).To(Succeed())
expectValue(testAppName, "testKey", "TESTING-updated1")
@@ -201,7 +201,7 @@ func TestConfigImportJSON(t *testing.T) {
env.Set("testKey", "TESTING-original1")
env.Set("testKey2", "TESTING-original2")
env.Set("testKey3", "TESTING-original3")
env.Write()
Expect(env.Write()).To(Succeed())
Expect(CommandImport(testAppName, false, false, true, "json", tempFile.Name())).To(Succeed())
expectValue(testAppName, "testKey", "TESTING-updated1")
@@ -231,7 +231,7 @@ func TestEnvironmentLoading(t *testing.T) {
env, err = LoadAppEnv(testAppName)
env.Set("testKey", "TESTING-updated")
env.Set("testKey2", "TESTING-'\n'-updated")
env.Write()
Expect(env.Write()).To(Succeed())
expectValue(testAppName, "testKey", "TESTING-updated")
expectValue(testAppName, "testKey2", "TESTING-'\n'-updated")
@@ -284,6 +284,67 @@ func TestInvalidEnvOnDisk(t *testing.T) {
}
// TestConfigSetManyReportsWriteFailure covers the reported bug: a write that
// never lands used to print "Setting config vars", fire the update trigger and
// exit successfully. The app's config directory path is occupied by a regular
// file so the write fails with ENOTDIR, which root cannot bypass; the read
// treats the same path as an empty environment and succeeds.
func TestConfigSetManyReportsWriteFailure(t *testing.T) {
RegisterTestingT(t)
_, libRoot := setupIsolatedEnv(t)
Expect(os.MkdirAll(filepath.Join(libRoot, "config"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(libRoot, "config", "alpha"), []byte(""), 0600)).To(Succeed())
Expect(SetMany("alpha", map[string]string{"testKey": "value"}, false, false)).ToNot(Succeed())
expectNoValue("alpha", "testKey")
}
// TestConfigUnsetManyReportsWriteFailure and its UnsetAll counterpart cover the
// same bug for the removal paths. No filesystem state lets the read load a
// non-empty environment and still fail the write for root, because both go
// through one path resolved once at load, so the permission step inside Write
// is made to fail instead. That step runs against the staged temporary file, so
// the environment on disk is left untouched.
func TestConfigUnsetManyReportsWriteFailure(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
setupIsolatedApp(t, "alpha")
t.Setenv("DOKKU_SYSTEM_USER", "dokku-no-such-user")
Expect(UnsetMany("alpha", []string{"testKey"}, false)).ToNot(Succeed())
expectValue("alpha", "testKey", "TESTING")
}
func TestConfigUnsetAllReportsWriteFailure(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
setupIsolatedApp(t, "alpha")
t.Setenv("DOKKU_SYSTEM_USER", "dokku-no-such-user")
Expect(UnsetAll("alpha", false)).ToNot(Succeed())
expectValue("alpha", "testKey", "TESTING")
}
// TestTriggerConfigUnsetReportsFailure covers the config-unset plugin trigger,
// which discarded the error it was handed and always exited successfully. An
// invalid key fails validation before anything is read or written.
func TestTriggerConfigUnsetReportsFailure(t *testing.T) {
RegisterTestingT(t)
setupIsolatedEnv(t)
Expect(TriggerConfigUnset("alpha", "invalid-key", false)).ToNot(Succeed())
}
// setupIsolatedApp creates the config directory for an app inside an isolated
// environment and seeds it with a value to remove
func setupIsolatedApp(t *testing.T, appName string) {
t.Helper()
Expect(setupAppConfigDir(appName)).To(Succeed())
Expect(SetMany(appName, map[string]string{"testKey": "TESTING"}, false, false)).To(Succeed())
}
func expectValue(appName string, key string, expected string) {
v, ok := Get(appName, key)
Expect(ok).To(Equal(true))
+67 -2
View File
@@ -164,12 +164,77 @@ func (e *Env) Merge(other *Env) {
}
}
// Write an Env back to the file it was read from as an exportfile
// Write an Env back to the file it was read from as an exportfile. The contents
// are written to a temporary file in the same directory and renamed into place,
// so a failed write leaves the previous environment intact rather than
// truncating it. The temporary file is created at 0600, so the values are never
// on disk in a mode other users can read.
func (e *Env) Write() error {
if e.filename == "" {
return errors.New("this Env was created unbound to a file")
}
return godotenv.Write(e.Map(), e.filename)
contents, err := godotenv.Marshal(e.Map())
if err != nil {
return fmt.Errorf("Unable to serialize environment: %s", err.Error())
}
target := resolveEnvPath(e.filename)
file, err := os.CreateTemp(filepath.Dir(target), ".ENV-*")
if err != nil {
return fmt.Errorf("Unable to create temporary environment file: %s", err.Error())
}
defer os.Remove(file.Name())
if err := writeEnvFile(file, contents); err != nil {
return err
}
if err := common.SetPermissions(common.SetPermissionInput{
Filename: file.Name(),
Mode: os.FileMode(0600),
}); err != nil {
return fmt.Errorf("Unable to set permissions on environment file: %s", err.Error())
}
if err := os.Rename(file.Name(), target); err != nil {
return fmt.Errorf("Unable to move environment file into place: %s", err.Error())
}
return nil
}
// resolveEnvPath follows any symlink at filename so the environment is written
// through the symlink rather than replacing it with a regular file, and so the
// temporary file is created on the same filesystem as the real target. A path
// that cannot be resolved - a file that does not exist yet being the common
// case - is returned unchanged.
func resolveEnvPath(filename string) string {
resolved, err := filepath.EvalSymlinks(filename)
if err != nil {
return filename
}
return resolved
}
// writeEnvFile writes contents to file, flushes it to disk and closes it
func writeEnvFile(file *os.File, contents string) error {
if _, err := file.WriteString(contents + "\n"); err != nil {
file.Close()
return fmt.Errorf("Unable to write environment file: %s", err.Error())
}
if err := file.Sync(); err != nil {
file.Close()
return fmt.Errorf("Unable to flush environment file: %s", err.Error())
}
if err := file.Close(); err != nil {
return fmt.Errorf("Unable to close environment file: %s", err.Error())
}
return nil
}
// Export the Env in the given format
+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
-7
View File
@@ -115,13 +115,6 @@ func drainLegacyEnvFile(name string, oldEnvFile string, env *Env) error {
return fmt.Errorf("Unable to write environment: %s", err.Error())
}
if err := common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
}); err != nil {
return fmt.Errorf("Unable to set permissions on environment: %s", err.Error())
}
if err := writeEnvMigrated(name); err != nil {
return err
}
+14 -13
View File
@@ -9,11 +9,12 @@ import (
"github.com/dokku/dokku/plugins/common"
)
// setupMigrateEnv points the dokku env at temporary directories and tells the
// setupIsolatedEnv points the dokku env at temporary directories and tells the
// permission helpers to chown files to the current user (a no-op) so the test
// works without root. The package-level paths in config_test.go are captured at
// init against the real dokku directories, so these tests must not use them.
func setupMigrateEnv(t *testing.T) (dokkuRoot string, libRoot string) {
// init against the real dokku directories, so a test using this helper must not
// use them.
func setupIsolatedEnv(t *testing.T) (dokkuRoot string, libRoot string) {
t.Helper()
libRoot = t.TempDir()
@@ -79,7 +80,7 @@ func expectEnvValue(t *testing.T, appName, key, want string) {
}
func TestMigrateEnvFiles_DrainsAndRemovesAppFile(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
legacy := writeLegacyAppEnv(t, dokkuRoot, "alpha", "export DOKKU_CHECKS_SKIPPED=worker\nexport MY_VAR=value\n")
@@ -99,7 +100,7 @@ func TestMigrateEnvFiles_DrainsAndRemovesAppFile(t *testing.T) {
}
func TestMigrateEnvFiles_DrainsAndRemovesGlobalFile(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
legacy := filepath.Join(dokkuRoot, "ENV")
if err := os.WriteFile(legacy, []byte("export DOKKU_WAIT_TO_RETIRE=30\n"), 0600); err != nil {
@@ -124,7 +125,7 @@ func TestMigrateEnvFiles_DrainsAndRemovesGlobalFile(t *testing.T) {
// that is still there once the migration has been recorded. Nothing it holds is
// imported, and it is moved aside so the values remain recoverable.
func TestMigrateEnvFiles_PreservesStaleFileWithoutImporting(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
writeLegacyAppEnv(t, dokkuRoot, "alpha", "export FIRST=one\n")
if err := MigrateEnvFiles(); err != nil {
@@ -152,7 +153,7 @@ func TestMigrateEnvFiles_PreservesStaleFileWithoutImporting(t *testing.T) {
// left the legacy file in place, so draining it a second time replayed the
// environment as it stood at that upgrade over every config:set made since.
func TestMigrateEnvFiles_KeepsConfigSetAfterMigration(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
writeLegacyAppEnv(t, dokkuRoot, "alpha", "export DATABASE_URL=value-A\n")
if err := MigrateEnvFiles(); err != nil {
@@ -176,7 +177,7 @@ func TestMigrateEnvFiles_KeepsConfigSetAfterMigration(t *testing.T) {
// key unset after the migration was recorded came back when the legacy file was
// drained again, which for a rotated secret meant putting it back into service.
func TestMigrateEnvFiles_DoesNotResurrectUnsetKeys(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
writeLegacyAppEnv(t, dokkuRoot, "alpha", "export DOKKU_PROXY_PORT=80\n")
if err := MigrateEnvFiles(); err != nil {
@@ -201,7 +202,7 @@ func TestMigrateEnvFiles_DoesNotResurrectUnsetKeys(t *testing.T) {
// upgrade: the leftover file still agrees with the current config, so there is
// nothing to preserve and no reason to say anything about it.
func TestMigrateEnvFiles_RemovesStaleFileMatchingCurrentConfig(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
writeLegacyAppEnv(t, dokkuRoot, "alpha", "export MY_VAR=value\n")
if err := MigrateEnvFiles(); err != nil {
@@ -226,7 +227,7 @@ func TestMigrateEnvFiles_RemovesStaleFileMatchingCurrentConfig(t *testing.T) {
// releases 0.38.0 through 0.38.25 never removed at all, so every host upgraded
// through that range still has one.
func TestMigrateEnvFiles_PreservesStaleGlobalFile(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
legacy := filepath.Join(dokkuRoot, "ENV")
if err := os.WriteFile(legacy, []byte("export DOKKU_WAIT_TO_RETIRE=30\n"), 0600); err != nil {
@@ -255,7 +256,7 @@ func TestMigrateEnvFiles_PreservesStaleGlobalFile(t *testing.T) {
// that does import: before the migration is recorded the legacy file is the
// source of truth, so it overwrites the value already held at the config path.
func TestMigrateEnvFiles_LegacyValueWins(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
if err := os.MkdirAll(filepath.Join(dokkuRoot, "alpha"), 0755); err != nil {
t.Fatalf("MkdirAll: %v", err)
@@ -279,7 +280,7 @@ func TestMigrateEnvFiles_LegacyValueWins(t *testing.T) {
}
func TestMigrateEnvFiles_MarksAppsWithoutLegacyFile(t *testing.T) {
dokkuRoot, _ := setupMigrateEnv(t)
dokkuRoot, _ := setupIsolatedEnv(t)
if err := os.MkdirAll(filepath.Join(dokkuRoot, "alpha"), 0755); err != nil {
t.Fatalf("MkdirAll: %v", err)
@@ -297,7 +298,7 @@ func TestMigrateEnvFiles_MarksAppsWithoutLegacyFile(t *testing.T) {
// TestMigrateEnvFiles_KeepsLegacyFileWhenWriteFails verifies the legacy file
// survives a failure to write the merged environment, so nothing is lost.
func TestMigrateEnvFiles_KeepsLegacyFileWhenWriteFails(t *testing.T) {
dokkuRoot, libRoot := setupMigrateEnv(t)
dokkuRoot, libRoot := setupIsolatedEnv(t)
legacy := writeLegacyAppEnv(t, dokkuRoot, "alpha", "export MY_VAR=value\n")
+1 -2
View File
@@ -56,8 +56,7 @@ func TriggerConfigSet(appName string, noRestart bool, pairs ...string) error {
// TriggerConfigUnset unsets an app config value by key
func TriggerConfigUnset(appName string, key string, restart bool) error {
UnsetMany(appName, []string{key}, restart)
return nil
return UnsetMany(appName, []string{key}, restart)
}
func setupAppConfigDir(appName string) error {