refactor: allow setting more options when changing permissions on a file

Rather than hardcoding paths, just allow them to be options.
This commit is contained in:
Jose Diaz-Gonzalez committed 2024-01-30 07:31:02 -05:00
1 parent 6622de5998
commit a277c5da1b
5 files changed
+82 -44

No files matched your search

+8 -10
View File
@@ -13,11 +13,10 @@ func CreateAppDataDirectory(pluginName, appName string) error {
return err return err
} }
if err := SetPermissions(directory, 0755); err != nil { return SetPermissions(SetPermissionInput{
return err Filename: directory,
} Mode: os.FileMode(0755),
})
return nil
} }
// CreateDataDirectory creates a data directory for the given plugin/app combination with the correct permissions // CreateDataDirectory creates a data directory for the given plugin/app combination with the correct permissions
@@ -27,11 +26,10 @@ func CreateDataDirectory(pluginName string) error {
return err return err
} }
if err := SetPermissions(directory, 0755); err != nil { return SetPermissions(SetPermissionInput{
return err Filename: directory,
} Mode: os.FileMode(0755),
})
return nil
} }
// GetAppDataDirectory returns the path to the data directory for the given plugin/app combination // GetAppDataDirectory returns the path to the data directory for the given plugin/app combination
+27 -19
View File
@@ -151,24 +151,33 @@ func ReadFirstLine(filename string) (text string) {
return return
} }
// SetPermissionsInput is the input struct for SetPermissions
type SetPermissionInput struct {
Filename string
Mode os.FileMode
GroupName string
Username string
}
// SetPermissions sets the proper owner and filemode for a given file // SetPermissions sets the proper owner and filemode for a given file
func SetPermissions(path string, fileMode os.FileMode) error { func SetPermissions(input SetPermissionInput) error {
if err := os.Chmod(path, fileMode); err != nil { if err := os.Chmod(input.Filename, input.Mode); err != nil {
return err return err
} }
systemGroup := GetenvWithDefault("DOKKU_SYSTEM_GROUP", "dokku") if input.GroupName == "" {
systemUser := GetenvWithDefault("DOKKU_SYSTEM_USER", "dokku") input.GroupName = GetenvWithDefault("DOKKU_SYSTEM_GROUP", "dokku")
if strings.HasPrefix(path, "/etc/sudoers.d/") {
systemGroup = "root"
systemUser = "root"
} }
group, err := user.LookupGroup(systemGroup) if input.Username == "" {
input.Username = GetenvWithDefault("DOKKU_SYSTEM_USER", "dokku")
}
group, err := user.LookupGroup(input.GroupName)
if err != nil { if err != nil {
return err return err
} }
user, err := user.Lookup(systemUser) user, err := user.Lookup(input.Username)
if err != nil { if err != nil {
return err return err
} }
@@ -182,7 +191,7 @@ func SetPermissions(path string, fileMode os.FileMode) error {
if err != nil { if err != nil {
return err return err
} }
return os.Chown(path, uid, gid) return os.Chown(input.Filename, uid, gid)
} }
// TouchFile creates an empty file at the specified path // TouchFile creates an empty file at the specified path
@@ -198,10 +207,10 @@ func TouchFile(filename string) error {
return err return err
} }
if err := SetPermissions(filename, mode); err != nil { return SetPermissions(SetPermissionInput{
return err Filename: filename,
} Mode: mode,
return nil })
} }
type WriteSliceToFileInput struct { type WriteSliceToFileInput struct {
@@ -230,9 +239,8 @@ func WriteSliceToFile(input WriteSliceToFileInput) error {
return err return err
} }
if err := SetPermissions(input.Filename, input.Mode); err != nil { return SetPermissions(SetPermissionInput{
return err Filename: input.Filename,
} Mode: input.Mode,
})
return nil
} }
+31 -7
View File
@@ -202,7 +202,10 @@ func PropertyListWrite(pluginName string, appName string, property string, value
} }
file.Chmod(0600) file.Chmod(0600)
SetPermissions(propertyPath, 0600) SetPermissions(SetPermissionInput{
Filename: propertyPath,
Mode: os.FileMode(0600),
})
return nil return nil
} }
@@ -329,7 +332,10 @@ func PropertyListRemove(pluginName string, appName string, property string, valu
} }
file.Chmod(0600) file.Chmod(0600)
SetPermissions(propertyPath, 0600) SetPermissions(SetPermissionInput{
Filename: propertyPath,
Mode: os.FileMode(0600),
})
if !found { if !found {
return errors.New("Property not found, nothing was removed") return errors.New("Property not found, nothing was removed")
@@ -365,7 +371,10 @@ func PropertyListRemoveByPrefix(pluginName string, appName string, property stri
} }
file.Chmod(0600) file.Chmod(0600)
SetPermissions(propertyPath, 0600) SetPermissions(SetPermissionInput{
Filename: propertyPath,
Mode: os.FileMode(0600),
})
if !found { if !found {
return errors.New("Property not found, nothing was removed") return errors.New("Property not found, nothing was removed")
@@ -441,7 +450,10 @@ func PropertyWrite(pluginName string, appName string, property string, value str
fmt.Fprint(file, value) fmt.Fprint(file, value)
file.Chmod(0600) file.Chmod(0600)
SetPermissions(propertyPath, 0600) SetPermissions(SetPermissionInput{
Filename: propertyPath,
Mode: os.FileMode(0600),
})
return nil return nil
} }
@@ -451,10 +463,19 @@ 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(filepath.Join(MustGetEnv("DOKKU_LIB_ROOT"), "config"), 0755); err != nil {
input := SetPermissionInput{
Filename: filepath.Join(MustGetEnv("DOKKU_LIB_ROOT"), "config"),
Mode: os.FileMode(0755),
}
if err := SetPermissions(input); err != nil {
return err return err
} }
return SetPermissions(pluginConfigRoot, 0755) return SetPermissions(SetPermissionInput{
Filename: pluginConfigRoot,
Mode: os.FileMode(0755),
})
} }
func getPropertyPath(pluginName string, appName string, property string) string { func getPropertyPath(pluginName string, appName string, property string) string {
@@ -478,5 +499,8 @@ 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(SetPermissionInput{
Filename: pluginAppConfigRoot,
Mode: os.FileMode(0755),
})
} }
+12 -3
View File
@@ -52,7 +52,10 @@ func SetMany(appName string, entries map[string]string, restart bool) (err error
fmt.Println(prettyPrintEnvEntries(" ", entries)) fmt.Println(prettyPrintEnvEntries(" ", entries))
} }
env.Write() env.Write()
common.SetPermissions(env.Filename(), 0600) common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
})
triggerUpdate(appName, "set", keys) triggerUpdate(appName, "set", keys)
} }
if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) { if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
@@ -85,7 +88,10 @@ func UnsetMany(appName string, keys []string, restart bool) (err error) {
} }
if changed { if changed {
env.Write() env.Write()
common.SetPermissions(env.Filename(), 0600) common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
})
triggerUpdate(appName, "unset", keys) triggerUpdate(appName, "unset", keys)
} }
if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) { if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
@@ -109,7 +115,10 @@ func UnsetAll(appName string, restart bool) (err error) {
} }
if changed { if changed {
env.Write() env.Write()
common.SetPermissions(env.Filename(), 0600) common.SetPermissions(common.SetPermissionInput{
Filename: env.Filename(),
Mode: os.FileMode(0600),
})
triggerUpdate(appName, "clear", []string{}) triggerUpdate(appName, "clear", []string{})
} }
if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) { if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
+4 -5
View File
@@ -84,11 +84,10 @@ func TriggerInstall() error {
return err return err
} }
if err := common.SetPermissions(logDirectory, 0755); err != nil { return common.SetPermissions(common.SetPermissionInput{
return err Filename: logDirectory,
} Mode: os.FileMode(0755),
})
return nil
} }
// TriggerLogsGetProperty writes the logs key to stdout for a given app container // TriggerLogsGetProperty writes the logs key to stdout for a given app container