From 93e77295fc84ac66f8f3afb0d45255d24a342d0b Mon Sep 17 00:00:00 2001 From: Harshil-Jani Date: Wed, 9 Oct 2024 03:03:44 +0530 Subject: [PATCH] Add warning to the Web5 Rust CLI if they're not running as root --- crates/web5_cli/README.md | 2 ++ crates/web5_cli/src/dids/create.rs | 8 ++++++++ crates/web5_cli/src/main.rs | 2 ++ crates/web5_cli/src/test.rs | 12 ++++++++++++ crates/web5_cli/src/utils.rs | 24 ++++++++++++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 crates/web5_cli/src/test.rs create mode 100644 crates/web5_cli/src/utils.rs diff --git a/crates/web5_cli/README.md b/crates/web5_cli/README.md index e1b855c5..e72c1544 100644 --- a/crates/web5_cli/README.md +++ b/crates/web5_cli/README.md @@ -16,6 +16,8 @@ web5 vc -h ## Examples +Certain operations, such as those that utilize the `InMemoryKeyManager`, may require root privileges. Ensure that you are running the `did create` command with the appropriate permissions. + ### Create a `did:dht` ```shell diff --git a/crates/web5_cli/src/dids/create.rs b/crates/web5_cli/src/dids/create.rs index a42cdb29..7b27bde9 100644 --- a/crates/web5_cli/src/dids/create.rs +++ b/crates/web5_cli/src/dids/create.rs @@ -12,6 +12,8 @@ use web5::{ }, }; +use crate::utils::warn_if_not_root; + #[derive(Subcommand, Debug)] pub enum Commands { Jwk { @@ -57,6 +59,8 @@ impl Commands { no_indent, json_escape, } => { + // Check if the current process has root privileges because the InMemoryKeyManager may require root privileges + warn_if_not_root(); let key_manager = Arc::new(InMemoryKeyManager::new()); let bearer_did = DidJwk::create(Some(DidJwkCreateOptions { @@ -74,6 +78,8 @@ impl Commands { no_indent, json_escape, } => { + // Check if the current process has root privileges because the InMemoryKeyManager may require root privileges + warn_if_not_root(); let key_manager = Arc::new(InMemoryKeyManager::new()); let bearer_did = DidWeb::create( @@ -94,6 +100,8 @@ impl Commands { no_indent, json_escape, } => { + // Check if the current process has root privileges because the InMemoryKeyManager may require root privileges + warn_if_not_root(); let key_manager = Arc::new(InMemoryKeyManager::new()); let bearer_did = DidDht::create(Some(DidDhtCreateOptions { diff --git a/crates/web5_cli/src/main.rs b/crates/web5_cli/src/main.rs index 86e94a11..a28a2215 100644 --- a/crates/web5_cli/src/main.rs +++ b/crates/web5_cli/src/main.rs @@ -1,4 +1,6 @@ mod dids; +mod test; +mod utils; mod vcs; use clap::{Parser, Subcommand}; diff --git a/crates/web5_cli/src/test.rs b/crates/web5_cli/src/test.rs new file mode 100644 index 00000000..2950cfde --- /dev/null +++ b/crates/web5_cli/src/test.rs @@ -0,0 +1,12 @@ +#[cfg(test)] +mod tests { + use crate::utils::is_root; + use std::env; + + #[test] + fn test_is_root() { + if cfg!(target_os = "linux") || cfg!(target_os = "macos") { + assert_eq!(is_root(), env::var("USER").unwrap() == "root"); + } + } +} diff --git a/crates/web5_cli/src/utils.rs b/crates/web5_cli/src/utils.rs new file mode 100644 index 00000000..a2cfe33c --- /dev/null +++ b/crates/web5_cli/src/utils.rs @@ -0,0 +1,24 @@ +use std::env; + +// ANSI color codes +const YELLOW_COLOR: &str = "\x1b[93m"; +const RESET_COLOR: &str = "\x1b[0m"; + +// Function to check if the current process has root privileges +pub fn is_root() -> bool { + if let Ok(user) = env::var("USER") { + user == "root" + } else { + false + } +} + +// Function to display a warning if not running as root +pub fn warn_if_not_root() { + const WARNING_MESSAGE: &str = + "Warning: This command may require root privileges to function properly."; + + if !is_root() { + eprintln!("\n{}{}{}\n", YELLOW_COLOR, WARNING_MESSAGE, RESET_COLOR); + } +}