fix: handle os.chmod errors (#457)
This commit is contained in:
		
							parent
							
								
									13a4c05ff5
								
							
						
					
					
						commit
						585b55c2ee
					
				|  | @ -68,7 +68,10 @@ func (o *JsonDB) Init() error { | ||||||
| 		serverInterface.PostDown = util.LookupEnvOrString(util.ServerPostDownScriptEnvVar, "") | 		serverInterface.PostDown = util.LookupEnvOrString(util.ServerPostDownScriptEnvVar, "") | ||||||
| 		serverInterface.UpdatedAt = time.Now().UTC() | 		serverInterface.UpdatedAt = time.Now().UTC() | ||||||
| 		o.conn.Write("server", "interfaces", serverInterface) | 		o.conn.Write("server", "interfaces", serverInterface) | ||||||
| 		os.Chmod(serverInterfacePath, 0600) | 		err := util.ManagePerms(serverInterfacePath) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// server's key pair
 | 	// server's key pair
 | ||||||
|  | @ -83,7 +86,10 @@ func (o *JsonDB) Init() error { | ||||||
| 		serverKeyPair.PublicKey = key.PublicKey().String() | 		serverKeyPair.PublicKey = key.PublicKey().String() | ||||||
| 		serverKeyPair.UpdatedAt = time.Now().UTC() | 		serverKeyPair.UpdatedAt = time.Now().UTC() | ||||||
| 		o.conn.Write("server", "keypair", serverKeyPair) | 		o.conn.Write("server", "keypair", serverKeyPair) | ||||||
| 		os.Chmod(serverKeyPairPath, 0600) | 		err = util.ManagePerms(serverKeyPairPath) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// global settings
 | 	// global settings
 | ||||||
|  | @ -108,7 +114,10 @@ func (o *JsonDB) Init() error { | ||||||
| 		globalSetting.ConfigFilePath = util.LookupEnvOrString(util.ConfigFilePathEnvVar, util.DefaultConfigFilePath) | 		globalSetting.ConfigFilePath = util.LookupEnvOrString(util.ConfigFilePathEnvVar, util.DefaultConfigFilePath) | ||||||
| 		globalSetting.UpdatedAt = time.Now().UTC() | 		globalSetting.UpdatedAt = time.Now().UTC() | ||||||
| 		o.conn.Write("server", "global_settings", globalSetting) | 		o.conn.Write("server", "global_settings", globalSetting) | ||||||
| 		os.Chmod(globalSettingPath, 0600) | 		err := util.ManagePerms(globalSettingPath) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// hashes
 | 	// hashes
 | ||||||
|  | @ -117,7 +126,10 @@ func (o *JsonDB) Init() error { | ||||||
| 		clientServerHashes.Client = "none" | 		clientServerHashes.Client = "none" | ||||||
| 		clientServerHashes.Server = "none" | 		clientServerHashes.Server = "none" | ||||||
| 		o.conn.Write("server", "hashes", clientServerHashes) | 		o.conn.Write("server", "hashes", clientServerHashes) | ||||||
| 		os.Chmod(hashesPath, 0600) | 		err := util.ManagePerms(hashesPath) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// user info
 | 	// user info
 | ||||||
|  | @ -143,7 +155,10 @@ func (o *JsonDB) Init() error { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		o.conn.Write("users", user.Username, user) | 		o.conn.Write("users", user.Username, user) | ||||||
| 		os.Chmod(path.Join(path.Join(o.dbPath, "users"), user.Username+".json"), 0600) | 		err = util.ManagePerms(path.Join(path.Join(o.dbPath, "users"), user.Username+".json")) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return nil | 	return nil | ||||||
|  | @ -183,7 +198,10 @@ func (o *JsonDB) GetUserByName(username string) (model.User, error) { | ||||||
| func (o *JsonDB) SaveUser(user model.User) error { | func (o *JsonDB) SaveUser(user model.User) error { | ||||||
| 	userPath := path.Join(path.Join(o.dbPath, "users"), user.Username+".json") | 	userPath := path.Join(path.Join(o.dbPath, "users"), user.Username+".json") | ||||||
| 	output := o.conn.Write("users", user.Username, user) | 	output := o.conn.Write("users", user.Username, user) | ||||||
| 	os.Chmod(userPath, 0600) | 	err := util.ManagePerms(userPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	return output | 	return output | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -296,7 +314,10 @@ func (o *JsonDB) GetClientByID(clientID string, qrCodeSettings model.QRCodeSetti | ||||||
| func (o *JsonDB) SaveClient(client model.Client) error { | func (o *JsonDB) SaveClient(client model.Client) error { | ||||||
| 	clientPath := path.Join(path.Join(o.dbPath, "clients"), client.ID+".json") | 	clientPath := path.Join(path.Join(o.dbPath, "clients"), client.ID+".json") | ||||||
| 	output := o.conn.Write("clients", client.ID, client) | 	output := o.conn.Write("clients", client.ID, client) | ||||||
| 	os.Chmod(clientPath, 0600) | 	err := util.ManagePerms(clientPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	return output | 	return output | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -307,21 +328,30 @@ func (o *JsonDB) DeleteClient(clientID string) error { | ||||||
| func (o *JsonDB) SaveServerInterface(serverInterface model.ServerInterface) error { | func (o *JsonDB) SaveServerInterface(serverInterface model.ServerInterface) error { | ||||||
| 	serverInterfacePath := path.Join(path.Join(o.dbPath, "server"), "interfaces.json") | 	serverInterfacePath := path.Join(path.Join(o.dbPath, "server"), "interfaces.json") | ||||||
| 	output := o.conn.Write("server", "interfaces", serverInterface) | 	output := o.conn.Write("server", "interfaces", serverInterface) | ||||||
| 	os.Chmod(serverInterfacePath, 0600) | 	err := util.ManagePerms(serverInterfacePath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	return output | 	return output | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (o *JsonDB) SaveServerKeyPair(serverKeyPair model.ServerKeypair) error { | func (o *JsonDB) SaveServerKeyPair(serverKeyPair model.ServerKeypair) error { | ||||||
| 	serverKeyPairPath := path.Join(path.Join(o.dbPath, "server"), "keypair.json") | 	serverKeyPairPath := path.Join(path.Join(o.dbPath, "server"), "keypair.json") | ||||||
| 	output := o.conn.Write("server", "keypair", serverKeyPair) | 	output := o.conn.Write("server", "keypair", serverKeyPair) | ||||||
| 	os.Chmod(serverKeyPairPath, 0600) | 	err := util.ManagePerms(serverKeyPairPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	return output | 	return output | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (o *JsonDB) SaveGlobalSettings(globalSettings model.GlobalSetting) error { | func (o *JsonDB) SaveGlobalSettings(globalSettings model.GlobalSetting) error { | ||||||
| 	globalSettingsPath := path.Join(path.Join(o.dbPath, "server"), "global_settings.json") | 	globalSettingsPath := path.Join(path.Join(o.dbPath, "server"), "global_settings.json") | ||||||
| 	output := o.conn.Write("server", "global_settings", globalSettings) | 	output := o.conn.Write("server", "global_settings", globalSettings) | ||||||
| 	os.Chmod(globalSettingsPath, 0600) | 	err := util.ManagePerms(globalSettingsPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	return output | 	return output | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -337,6 +367,9 @@ func (o *JsonDB) GetHashes() (model.ClientServerHashes, error) { | ||||||
| func (o *JsonDB) SaveHashes(hashes model.ClientServerHashes) error { | func (o *JsonDB) SaveHashes(hashes model.ClientServerHashes) error { | ||||||
| 	hashesPath := path.Join(path.Join(o.dbPath, "server"), "hashes.json") | 	hashesPath := path.Join(path.Join(o.dbPath, "server"), "hashes.json") | ||||||
| 	output := o.conn.Write("server", "hashes", hashes) | 	output := o.conn.Write("server", "hashes", hashes) | ||||||
| 	os.Chmod(hashesPath, 0600) | 	err := util.ManagePerms(hashesPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	return output | 	return output | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3,10 +3,10 @@ package jsondb | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" |  | ||||||
| 	"path" | 	"path" | ||||||
| 
 | 
 | ||||||
| 	"github.com/ngoduykhanh/wireguard-ui/model" | 	"github.com/ngoduykhanh/wireguard-ui/model" | ||||||
|  | 	"github.com/ngoduykhanh/wireguard-ui/util" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func (o *JsonDB) GetWakeOnLanHosts() ([]model.WakeOnLanHost, error) { | func (o *JsonDB) GetWakeOnLanHosts() ([]model.WakeOnLanHost, error) { | ||||||
|  | @ -70,7 +70,11 @@ func (o *JsonDB) SaveWakeOnLanHost(host model.WakeOnLanHost) error { | ||||||
| 
 | 
 | ||||||
| 	wakeOnLanHostPath := path.Join(path.Join(o.dbPath, model.WakeOnLanHostCollectionName), resourceName+".json") | 	wakeOnLanHostPath := path.Join(path.Join(o.dbPath, model.WakeOnLanHostCollectionName), resourceName+".json") | ||||||
| 	output := o.conn.Write(model.WakeOnLanHostCollectionName, resourceName, host) | 	output := o.conn.Write(model.WakeOnLanHostCollectionName, resourceName, host) | ||||||
| 	os.Chmod(wakeOnLanHostPath, 0600) | 	err = util.ManagePerms(wakeOnLanHostPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return output | 	return output | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -556,3 +556,8 @@ func RandomString(length int) string { | ||||||
| 	} | 	} | ||||||
| 	return string(b) | 	return string(b) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func ManagePerms(path string) error { | ||||||
|  | 	err := os.Chmod(path, 0600) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue