Skip to content

Commit

Permalink
fixup! feat: Add support for --save-to-file for account new
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphexion committed Nov 19, 2024
1 parent fbde50f commit 35a1b4e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
13 changes: 10 additions & 3 deletions cli/src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use rayon::prelude::*;
use std::{
fs::File,
io::{Read, Write},
os::unix::fs::PermissionsExt,
path::PathBuf,
};

Expand Down Expand Up @@ -261,7 +260,7 @@ impl Account {
crate::check_parent_permissions(&path)?;
let mut file = File::create_new(path)?;
file.write_all(account.private_key().to_string().as_bytes())?;
file.set_permissions(PermissionsExt::from_mode(0o400))?;
crate::set_user_read_only(&file)?;
}
// Print the new Aleo account.
if !discreet {
Expand Down Expand Up @@ -355,7 +354,6 @@ mod tests {
use std::fs;
use std::fs::Permissions;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use tempfile::NamedTempFile;
use tempfile::TempDir;

Expand Down Expand Up @@ -421,8 +419,11 @@ mod tests {
assert_eq!(expected, actual);
}

#[cfg(unix)]
#[test]
fn test_new_save_to_file() {
use std::os::unix::fs::PermissionsExt;

let dir = TempDir::new().expect("Failed to create temp folder");
let dir_path = dir.path();
fs::set_permissions(dir_path, Permissions::from_mode(0o700)).expect("Failed to set permissions");
Expand Down Expand Up @@ -450,8 +451,11 @@ mod tests {
assert_eq!(permissions.mode() & 0o777, 0o400, "File permissions are not 0o400");
}

#[cfg(unix)]
#[test]
fn test_new_prevent_save_to_file_in_non_protected_folder() {
use std::os::unix::fs::PermissionsExt;

let dir = TempDir::new().expect("Failed to create temp folder");
let dir_path = dir.path();
fs::set_permissions(dir_path, Permissions::from_mode(0o444)).expect("Failed to set permissions");
Expand Down Expand Up @@ -548,8 +552,11 @@ mod tests {
assert!(account.parse().is_ok());
}

#[cfg(unix)]
#[test]
fn test_signature_raw_using_private_key_file_from_account_new() {
use std::os::unix::fs::PermissionsExt;

let message = "Hello, world!".to_string();

let dir = TempDir::new().expect("Failed to create temp folder");
Expand Down
47 changes: 35 additions & 12 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,43 @@ pub mod commands;
pub mod helpers;

use anyhow::Result;
use std::fs::{File, Permissions};
use std::path::Path;

pub fn check_parent_permissions<T: AsRef<Path> + std::fmt::Debug>(path: T) -> Result<()> {
#[cfg(target_family = "unix")]
{
use anyhow::{bail, ensure};
use std::os::unix::fs::PermissionsExt;

if let Some(parent) = path.as_ref().parent() {
let permissions = parent.metadata()?.permissions().mode();
ensure!(permissions & 0o777 == 0o700, "The folder {:?} must be readable only by the owner (0700)", parent);
} else {
bail!("Parent does not exist for path={:?}", path);
}
#[cfg(unix)]
pub fn check_parent_permissions<T: AsRef<Path>>(path: T) -> Result<()> {
use anyhow::{bail, ensure};
use std::os::unix::fs::PermissionsExt;

if let Some(parent) = path.as_ref().parent() {
let permissions = parent.metadata()?.permissions().mode();
ensure!(permissions & 0o777 == 0o700, "The folder {:?} must be readable only by the owner (0700)", parent);
} else {
let path = path.as_ref();
bail!("Parent does not exist for path={}", path.display());
}

Ok(())
}

#[cfg(windows)]
pub fn check_parent_permissions<T: AsRef<Path>>(_path: T) -> Result<()> {
Ok(())
}

#[cfg(unix)]
fn set_user_read_only(file: &File) -> Result<()> {
use std::os::unix::fs::PermissionsExt;

let permissions = Permissions::from_mode(0o400);
file.set_permissions(permissions)?;
Ok(())
}

#[cfg(windows)]
fn set_user_read_only(file: &File) -> Result<()> {
let mut permissions = file.metadata()?.permissions();
permissions.set_readonly(true);
file.set_permissions(permissions)?;
Ok(())
}

0 comments on commit 35a1b4e

Please sign in to comment.