From 50cc9669c55feb95265d543c4f5549fb25f8bf6a Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 25 Jul 2019 10:00:36 -0700 Subject: [PATCH] Explicitly set the default config file's mode to 0600 --- cfg/config_files.go | 63 +++++++++++++++++-------------------------- cfg/error_messages.go | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 38 deletions(-) create mode 100644 cfg/error_messages.go diff --git a/cfg/config_files.go b/cfg/config_files.go index d044011a..2fa6ca34 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -20,6 +20,9 @@ const ( // WtfConfigDirV2 defines the path to the second version of the configuration. Use this. WtfConfigDirV2 = "~/.config/wtf/" + + // WtfConfigFile defines the name of the default config file + WtfConfigFile = "config.yml" ) /* -------------------- Exported Functions -------------------- */ @@ -59,6 +62,7 @@ func Initialize() { createXdgConfigDir() createWtfConfigDir() createWtfConfigFile() + chmodConfigFile() } // WtfConfigDir returns the absolute path to the configuration directory @@ -71,7 +75,7 @@ func WtfConfigDir() (string, error) { return configDir, nil } -// LoadWtfConfigFile loads the config.yml file to configure the app +// LoadWtfConfigFile loads the specified config file func LoadWtfConfigFile(filePath string, isCustomConfig bool) *config.Config { absPath, _ := expandHomeDir(filePath) @@ -91,6 +95,22 @@ func LoadWtfConfigFile(filePath string, isCustomConfig bool) *config.Config { /* -------------------- Unexported Functions -------------------- */ +// chmodConfigFile sets the mode of the config file to r+w for the owner only +func chmodConfigFile() { + relPath := fmt.Sprintf("%s%s", WtfConfigDirV2, WtfConfigFile) + absPath, _ := expandHomeDir(relPath) + + _, err := os.Stat(absPath) + if err != nil && os.IsNotExist(err) { + return + } + + err = os.Chmod(absPath, 0600) + if err != nil { + return + } +} + // createXdgConfigDir creates the necessary base directory for storing the config file // If ~/.config is missing, it will try to create it func createXdgConfigDir() { @@ -122,7 +142,7 @@ func createWtfConfigDir() { // createWtfConfigFile creates a simple config file in the config directory if // one does not already exist func createWtfConfigFile() { - filePath, err := CreateFile("config.yml") + filePath, err := CreateFile(WtfConfigFile) if err != nil { panic(err) } @@ -131,46 +151,13 @@ func createWtfConfigFile() { file, _ := os.Stat(filePath) if file.Size() == 0 { - if ioutil.WriteFile(filePath, []byte(defaultConfigFile), 0644) != nil { - panic(err) + if ioutil.WriteFile(filePath, []byte(defaultConfigFile), 0600) != nil { + displayDefaultConfigWriteError(err) + os.Exit(1) } } } -func displayXdgConfigDirCreateError(err error) { - fmt.Printf("\n\033[1mERROR:\033[0m Could not create the '\033[0;33m%s\033[0m' directory.\n", XdgConfigDir) - fmt.Println() - fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) -} - -func displayWtfConfigDirCreateError(err error) { - fmt.Printf("\n\033[1mERROR:\033[0m Could not create the '\033[0;33m%s\033[0m' directory.\n", WtfConfigDirV2) - fmt.Println() - fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) -} - -func displayWtfConfigFileLoadError(err error) { - fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.") - fmt.Println() - fmt.Println("This could mean one of two things:") - fmt.Println() - fmt.Println(" 1. Your \033[0;33mconfig.yml\033[0m file is missing. Check in \033[0;33m~/.config/wtf\033[0m to see if \033[0;33mconfig.yml\033[0m is there.") - fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a syntax error. Try running it through http://www.yamllint.com to check for errors.") - fmt.Println() - fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) -} - -func displayWtfCustomConfigFileLoadError(err error) { - fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.") - fmt.Println() - fmt.Println("This could mean one of two things:") - fmt.Println() - fmt.Println(" 1. That file doesn't exist.") - fmt.Println(" 2. That file has a YAML syntax error. Try running it through http://www.yamllint.com to check for errors.") - fmt.Println() - fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) -} - // Expand expands the path to include the home directory if the path // is prefixed with `~`. If it isn't prefixed with `~`, the path is // returned as-is. diff --git a/cfg/error_messages.go b/cfg/error_messages.go new file mode 100644 index 00000000..b0c5c7d8 --- /dev/null +++ b/cfg/error_messages.go @@ -0,0 +1,59 @@ +package cfg + +// This file contains the error messages that get written to the terminal when +// something goes wrong with the configuration process. +// +// As a general rule, if one of these has to be shown the app should then die +// via os.Exit(1) + +import ( + "fmt" + + "github.com/logrusorgru/aurora" +) + +/* -------------------- Unexported Functions -------------------- */ + +func displayError(err error) { + fmt.Printf("%s %s\n\n", aurora.Red("Error:"), err.Error()) +} + +func displayDefaultConfigWriteError(err error) { + fmt.Printf("\n%s Could not write the default configuration.\n", aurora.Red("ERROR:")) + fmt.Println() + displayError(err) +} + +func displayXdgConfigDirCreateError(err error) { + fmt.Printf("\n%s Could not create the '%s' directory.\n", aurora.Red("ERROR:"), aurora.Yellow(XdgConfigDir)) + fmt.Println() + displayError(err) +} + +func displayWtfConfigDirCreateError(err error) { + fmt.Printf("\n%s Could not create the '%s' directory.\n", aurora.Red("ERROR:"), aurora.Yellow(WtfConfigDirV2)) + fmt.Println() + displayError(err) +} + +func displayWtfConfigFileLoadError(err error) { + fmt.Printf("\n%s Could not load '%s'.\n", aurora.Red("ERROR:"), aurora.Yellow(WtfConfigFile)) + fmt.Println() + fmt.Println("This could mean one of two things:") + fmt.Println() + fmt.Printf(" 1. Your %s file is missing. Check in %s to see if %s is there.\n", aurora.Yellow(WtfConfigFile), aurora.Yellow("~/.config/wtf/"), aurora.Yellow(WtfConfigFile)) + fmt.Printf(" 2. Your %s file has a syntax error. Try running it through http://www.yamllint.com to check for errors.\n", aurora.Yellow(WtfConfigFile)) + fmt.Println() + displayError(err) +} + +func displayWtfCustomConfigFileLoadError(err error) { + fmt.Printf("\n%s Could not load '%s'.\n", aurora.Red("ERROR:"), aurora.Yellow(WtfConfigFile)) + fmt.Println() + fmt.Println("This could mean one of two things:") + fmt.Println() + fmt.Println(" 1. That file doesn't exist.") + fmt.Println(" 2. That file has a YAML syntax error. Try running it through http://www.yamllint.com to check for errors.") + fmt.Println() + displayError(err) +}