Skip to content

Commit

Permalink
Add warning to the Web5 Rust CLI if they're not running as root (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshil-Jani authored Oct 9, 2024
1 parent d4bf728 commit c391ec8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/web5_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions crates/web5_cli/src/dids/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use web5::{
},
};

use crate::utils::warn_if_not_root;

#[derive(Subcommand, Debug)]
pub enum Commands {
Jwk {
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions crates/web5_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod dids;
mod test;
mod utils;
mod vcs;

use clap::{Parser, Subcommand};
Expand Down
12 changes: 12 additions & 0 deletions crates/web5_cli/src/test.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
24 changes: 24 additions & 0 deletions crates/web5_cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c391ec8

Please sign in to comment.