Skip to content

Commit

Permalink
Set unix cfg correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Aug 30, 2023
1 parent 5861077 commit ca4edc3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions aquadoggo_cli/src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::fs::{self, File};
use std::io::{Read, Write};
#[cfg(target_os = "unix")]
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;

use anyhow::Result;
Expand Down Expand Up @@ -33,6 +35,7 @@ pub fn generate_ephemeral_key_pair() -> KeyPair {
///
/// This method automatically creates the required directories on that path and fixes the
/// permissions of the file (0600, read and write permissions only for the owner).
#[cfg(target_os = "unix")]
fn save_key_pair_to_file(key_pair: &KeyPair, path: PathBuf) -> Result<()> {
let private_key_hex = hex::encode(key_pair.private_key().as_bytes());

Expand All @@ -43,12 +46,22 @@ fn save_key_pair_to_file(key_pair: &KeyPair, path: PathBuf) -> Result<()> {
file.sync_all()?;

// Set permission for sensitive information
if cfg!(unix) {
use std::os::unix::fs::PermissionsExt;
let mut permissions = file.metadata()?.permissions();
permissions.set_mode(0o600);
fs::set_permissions(path, permissions)?;
}
let mut permissions = file.metadata()?.permissions();
permissions.set_mode(0o600);
fs::set_permissions(path, permissions)?;

Ok(())
}

#[cfg(not(target_os = "unix"))]
fn save_key_pair_to_file(key_pair: &KeyPair, path: PathBuf) -> Result<()> {
let private_key_hex = hex::encode(key_pair.private_key().as_bytes());

// Make sure that directories exist and write file into it
fs::create_dir_all(path.parent().unwrap())?;
let mut file = File::create(&path)?;
file.write_all(private_key_hex.as_bytes())?;
file.sync_all()?;

Ok(())
}
Expand Down

0 comments on commit ca4edc3

Please sign in to comment.