Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning to the Web5 Rust CLI if they're not running as root #385

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading