refactor: export SetPermissions function

This commit is contained in:
Jose Diaz-Gonzalez authored and Jose Diaz-Gonzalez committed 2020-11-21 20:57:33 -05:00
1 parent 943e032d12
commit 66696d0b46
2 files changed
+39 -40

No files matched your search

+31
View File
@@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/user"
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
@@ -804,6 +805,36 @@ func PlugnTriggerSetup(triggerName string, args ...string) *sh.Session {
return sh.Command("plugn", shellArgs...) return sh.Command("plugn", shellArgs...)
} }
// SetPermissions sets the proper owner and filemode for a given file
func SetPermissions(path string, fileMode os.FileMode) error {
if err := os.Chmod(path, fileMode); err != nil {
return err
}
systemGroup := GetenvWithDefault("DOKKU_SYSTEM_GROUP", "dokku")
systemUser := GetenvWithDefault("DOKKU_SYSTEM_USER", "dokku")
group, err := user.LookupGroup(systemGroup)
if err != nil {
return err
}
user, err := user.Lookup(systemUser)
if err != nil {
return err
}
uid, err := strconv.Atoi(user.Uid)
if err != nil {
return err
}
gid, err := strconv.Atoi(group.Gid)
if err != nil {
return err
}
return os.Chown(path, uid, gid)
}
func times(str string, n int) (out string) { func times(str string, n int) (out string) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
out += str out += str
+8 -40
View File
@@ -6,10 +6,8 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strconv"
"strings" "strings"
) )
@@ -174,7 +172,7 @@ func PropertyListAdd(pluginName string, appName string, property string, value s
} }
file.Chmod(0600) file.Chmod(0600)
setPermissions(propertyPath, 0600) SetPermissions(propertyPath, 0600)
return nil return nil
} }
@@ -301,7 +299,7 @@ func PropertyListRemove(pluginName string, appName string, property string, valu
} }
file.Chmod(0600) file.Chmod(0600)
setPermissions(propertyPath, 0600) SetPermissions(propertyPath, 0600)
if !found { if !found {
return errors.New("Property not found, nothing was removed") return errors.New("Property not found, nothing was removed")
@@ -337,7 +335,7 @@ func PropertyListRemoveByPrefix(pluginName string, appName string, property stri
} }
file.Chmod(0600) file.Chmod(0600)
setPermissions(propertyPath, 0600) SetPermissions(propertyPath, 0600)
if !found { if !found {
return errors.New("Property not found, nothing was removed") return errors.New("Property not found, nothing was removed")
@@ -390,7 +388,7 @@ func PropertyListSet(pluginName string, appName string, property string, value s
} }
file.Chmod(0600) file.Chmod(0600)
setPermissions(propertyPath, 0600) SetPermissions(propertyPath, 0600)
return nil return nil
} }
@@ -429,7 +427,7 @@ func PropertyWrite(pluginName string, appName string, property string, value str
fmt.Fprintf(file, value) fmt.Fprintf(file, value)
file.Chmod(0600) file.Chmod(0600)
setPermissions(propertyPath, 0600) SetPermissions(propertyPath, 0600)
return nil return nil
} }
@@ -439,10 +437,10 @@ func PropertySetup(pluginName string) error {
if err := os.MkdirAll(pluginConfigRoot, 0755); err != nil { if err := os.MkdirAll(pluginConfigRoot, 0755); err != nil {
return err return err
} }
if err := setPermissions(path.Join(MustGetEnv("DOKKU_LIB_ROOT"), "config"), 0755); err != nil { if err := SetPermissions(filepath.Join(MustGetEnv("DOKKU_LIB_ROOT"), "config"), 0755); err != nil {
return err return err
} }
return setPermissions(pluginConfigRoot, 0755) return SetPermissions(pluginConfigRoot, 0755)
} }
func getPropertyPath(pluginName string, appName string, property string) string { func getPropertyPath(pluginName string, appName string, property string) string {
@@ -466,35 +464,5 @@ func makePluginAppPropertyPath(pluginName string, appName string) error {
if err := os.MkdirAll(pluginAppConfigRoot, 0755); err != nil { if err := os.MkdirAll(pluginAppConfigRoot, 0755); err != nil {
return err return err
} }
return setPermissions(pluginAppConfigRoot, 0755) return SetPermissions(pluginAppConfigRoot, 0755)
}
// setPermissions sets the proper owner and filemode for a given file
func setPermissions(path string, fileMode os.FileMode) error {
if err := os.Chmod(path, fileMode); err != nil {
return err
}
systemGroup := GetenvWithDefault("DOKKU_SYSTEM_GROUP", "dokku")
systemUser := GetenvWithDefault("DOKKU_SYSTEM_USER", "dokku")
group, err := user.LookupGroup(systemGroup)
if err != nil {
return err
}
user, err := user.Lookup(systemUser)
if err != nil {
return err
}
uid, err := strconv.Atoi(user.Uid)
if err != nil {
return err
}
gid, err := strconv.Atoi(group.Gid)
if err != nil {
return err
}
return os.Chown(path, uid, gid)
} }