Change private key file permissions (#551)

* Import the libc crate for file permission management

* Tighten permissions on key file

* Fix code to match style guidelines
This commit is contained in:
Mehdi Zerouali 2019-10-02 11:16:34 +10:00 committed by Paul Hauner
parent 9aedb12bfc
commit 6ae62c952b
3 changed files with 6 additions and 0 deletions

View File

@ -38,3 +38,4 @@ bincode = "^1.1.2"
futures = "0.1.25" futures = "0.1.25"
dirs = "2.0.1" dirs = "2.0.1"
logging = { path = "../eth2/utils/logging" } logging = { path = "../eth2/utils/logging" }
libc = "0.2"

View File

@ -261,12 +261,16 @@ impl Config {
/// Saves a keypair to a file inside the appropriate validator directory. Returns the saved path filename. /// Saves a keypair to a file inside the appropriate validator directory. Returns the saved path filename.
#[allow(dead_code)] #[allow(dead_code)]
pub fn save_key(&self, key: &Keypair) -> Result<PathBuf, Error> { pub fn save_key(&self, key: &Keypair) -> Result<PathBuf, Error> {
use std::os::unix::fs::PermissionsExt;
let validator_config_path = self.data_dir.join(key.identifier()); let validator_config_path = self.data_dir.join(key.identifier());
let key_path = validator_config_path.join(DEFAULT_PRIVATE_KEY_FILENAME); let key_path = validator_config_path.join(DEFAULT_PRIVATE_KEY_FILENAME);
fs::create_dir_all(&validator_config_path)?; fs::create_dir_all(&validator_config_path)?;
let mut key_file = File::create(&key_path)?; let mut key_file = File::create(&key_path)?;
let mut perm = key_file.metadata()?.permissions();
perm.set_mode((libc::S_IWUSR | libc::S_IRUSR) as u32);
key_file.set_permissions(perm)?;
bincode::serialize_into(&mut key_file, &key) bincode::serialize_into(&mut key_file, &key)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?; .map_err(|e| Error::new(ErrorKind::InvalidData, e))?;

View File

@ -1,3 +1,4 @@
extern crate libc;
pub mod config; pub mod config;
pub use crate::config::Config; pub use crate::config::Config;