mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-26 20:26:39 +00:00
[client] Fix profile config directory permissions (#5457)
* fix user profile dir perm * fix fileExists * revert return var change * fix anti-pattern
This commit is contained in:
@@ -198,7 +198,7 @@ func getConfigDirForUser(username string) (string, error) {
|
|||||||
|
|
||||||
configDir := filepath.Join(DefaultConfigPathDir, username)
|
configDir := filepath.Join(DefaultConfigPathDir, username)
|
||||||
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(configDir, 0600); err != nil {
|
if err := os.MkdirAll(configDir, 0700); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,9 +206,15 @@ func getConfigDirForUser(username string) (string, error) {
|
|||||||
return configDir, nil
|
return configDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileExists(path string) bool {
|
func fileExists(path string) (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
return !os.IsNotExist(err)
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// createNewConfig creates a new config generating a new Wireguard key and saving to file
|
// createNewConfig creates a new config generating a new Wireguard key and saving to file
|
||||||
@@ -635,7 +641,11 @@ func isPreSharedKeyHidden(preSharedKey *string) bool {
|
|||||||
|
|
||||||
// UpdateConfig update existing configuration according to input configuration and return with the configuration
|
// UpdateConfig update existing configuration according to input configuration and return with the configuration
|
||||||
func UpdateConfig(input ConfigInput) (*Config, error) {
|
func UpdateConfig(input ConfigInput) (*Config, error) {
|
||||||
if !fileExists(input.ConfigPath) {
|
configExists, err := fileExists(input.ConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
||||||
|
}
|
||||||
|
if !configExists {
|
||||||
return nil, fmt.Errorf("config file %s does not exist", input.ConfigPath)
|
return nil, fmt.Errorf("config file %s does not exist", input.ConfigPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -644,7 +654,11 @@ func UpdateConfig(input ConfigInput) (*Config, error) {
|
|||||||
|
|
||||||
// UpdateOrCreateConfig reads existing config or generates a new one
|
// UpdateOrCreateConfig reads existing config or generates a new one
|
||||||
func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
||||||
if !fileExists(input.ConfigPath) {
|
configExists, err := fileExists(input.ConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
||||||
|
}
|
||||||
|
if !configExists {
|
||||||
log.Infof("generating new config %s", input.ConfigPath)
|
log.Infof("generating new config %s", input.ConfigPath)
|
||||||
cfg, err := createNewConfig(input)
|
cfg, err := createNewConfig(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -657,7 +671,7 @@ func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
|||||||
if isPreSharedKeyHidden(input.PreSharedKey) {
|
if isPreSharedKeyHidden(input.PreSharedKey) {
|
||||||
input.PreSharedKey = nil
|
input.PreSharedKey = nil
|
||||||
}
|
}
|
||||||
err := util.EnforcePermission(input.ConfigPath)
|
err = util.EnforcePermission(input.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to enforce permission on config dir: %v", err)
|
log.Errorf("failed to enforce permission on config dir: %v", err)
|
||||||
}
|
}
|
||||||
@@ -784,7 +798,12 @@ func ReadConfig(configPath string) (*Config, error) {
|
|||||||
|
|
||||||
// ReadConfig read config file and return with Config. If it is not exists create a new with default values
|
// ReadConfig read config file and return with Config. If it is not exists create a new with default values
|
||||||
func readConfig(configPath string, createIfMissing bool) (*Config, error) {
|
func readConfig(configPath string, createIfMissing bool) (*Config, error) {
|
||||||
if fileExists(configPath) {
|
configExists, err := fileExists(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if configExists {
|
||||||
err := util.EnforcePermission(configPath)
|
err := util.EnforcePermission(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to enforce permission on config dir: %v", err)
|
log.Errorf("failed to enforce permission on config dir: %v", err)
|
||||||
@@ -831,7 +850,11 @@ func DirectWriteOutConfig(path string, config *Config) error {
|
|||||||
// DirectUpdateOrCreateConfig is like UpdateOrCreateConfig but uses direct (non-atomic) writes.
|
// DirectUpdateOrCreateConfig is like UpdateOrCreateConfig but uses direct (non-atomic) writes.
|
||||||
// Use this on platforms where atomic writes are blocked (e.g., tvOS sandbox).
|
// Use this on platforms where atomic writes are blocked (e.g., tvOS sandbox).
|
||||||
func DirectUpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
func DirectUpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
||||||
if !fileExists(input.ConfigPath) {
|
configExists, err := fileExists(input.ConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
||||||
|
}
|
||||||
|
if !configExists {
|
||||||
log.Infof("generating new config %s", input.ConfigPath)
|
log.Infof("generating new config %s", input.ConfigPath)
|
||||||
cfg, err := createNewConfig(input)
|
cfg, err := createNewConfig(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -256,7 +256,11 @@ func (s *ServiceManager) AddProfile(profileName, username string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
profPath := filepath.Join(configDir, profileName+".json")
|
profPath := filepath.Join(configDir, profileName+".json")
|
||||||
if fileExists(profPath) {
|
profileExists, err := fileExists(profPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to check if profile exists: %w", err)
|
||||||
|
}
|
||||||
|
if profileExists {
|
||||||
return ErrProfileAlreadyExists
|
return ErrProfileAlreadyExists
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +289,11 @@ func (s *ServiceManager) RemoveProfile(profileName, username string) error {
|
|||||||
return fmt.Errorf("cannot remove profile with reserved name: %s", defaultProfileName)
|
return fmt.Errorf("cannot remove profile with reserved name: %s", defaultProfileName)
|
||||||
}
|
}
|
||||||
profPath := filepath.Join(configDir, profileName+".json")
|
profPath := filepath.Join(configDir, profileName+".json")
|
||||||
if !fileExists(profPath) {
|
profileExists, err := fileExists(profPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to check if profile exists: %w", err)
|
||||||
|
}
|
||||||
|
if !profileExists {
|
||||||
return ErrProfileNotFound
|
return ErrProfileNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ func (pm *ProfileManager) GetProfileState(profileName string) (*ProfileState, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
stateFile := filepath.Join(configDir, profileName+".state.json")
|
stateFile := filepath.Join(configDir, profileName+".state.json")
|
||||||
if !fileExists(stateFile) {
|
stateFileExists, err := fileExists(stateFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check if profile state file exists: %w", err)
|
||||||
|
}
|
||||||
|
if !stateFileExists {
|
||||||
return nil, errors.New("profile state file does not exist")
|
return nil, errors.New("profile state file does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user