Skip to content

Commit

Permalink
Add function to hash the password from the base node
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Nov 9, 2023
1 parent bf7820d commit e446353
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use anyhow::{anyhow, Error};
use async_trait::async_trait;
use clap::Parser;
use minotari_app_grpc::authentication::salted_password::create_salted_hashed_password;

use super::{CommandContext, HandleCommand};

/// Hashes the GRPC authentication password from the config and returns an argon2 hash
#[derive(Debug, Parser)]
pub struct Args {}

#[async_trait]
impl HandleCommand<Args> for CommandContext {
async fn handle_command(&mut self, _: Args) -> Result<(), Error> {
self.hash_grpc_password().await
}
}

impl CommandContext {
pub async fn hash_grpc_password(&mut self) -> Result<(), Error> {
match self
.config
.base_node
.grpc_authentication
.username_password()
.ok_or_else(|| anyhow!("GRPC basic auth is not configured"))
{
Ok((username, password)) => {
match create_salted_hashed_password(password.reveal()).map_err(|e| anyhow!(e.to_string())) {
Ok(hashed_password) => {
println!("Your hashed password is:");
println!("{}", *hashed_password);
println!();
println!(
"Use HTTP basic auth with username '{}' and the hashed password to make GRPC requests",
username
);
},
Err(e) => eprintln!("HashGrpcPassword error! {}", e),
}
},
Err(e) => eprintln!("HashGrpcPassword error! {}", e),
}

Ok(())
}
}
4 changes: 4 additions & 0 deletions applications/minotari_node/src/commands/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod get_mempool_stats;
mod get_network_stats;
mod get_peer;
mod get_state_info;
mod hash_grpc_password;
mod header_stats;
mod list_banned_peers;
mod list_connections;
Expand Down Expand Up @@ -136,6 +137,7 @@ pub enum Command {
Quit(quit::Args),
Exit(quit::Args),
Watch(watch_command::Args),
HashGrpcPassword(hash_grpc_password::Args),
}

impl Command {
Expand Down Expand Up @@ -228,6 +230,7 @@ impl CommandContext {
Command::Status(_) |
Command::Watch(_) |
Command::ListValidatorNodes(_) |
Command::HashGrpcPassword(_) |
Command::Quit(_) |
Command::Exit(_) => 30,
// These commands involve intense blockchain db operations and needs a lot of time to complete
Expand Down Expand Up @@ -293,6 +296,7 @@ impl HandleCommand<Command> for CommandContext {
Command::Quit(args) | Command::Exit(args) => self.handle_command(args).await,
Command::Watch(args) => self.handle_command(args).await,
Command::ListValidatorNodes(args) => self.handle_command(args).await,
Command::HashGrpcPassword(args) => self.handle_command(args).await,
}
}
}
Expand Down

0 comments on commit e446353

Please sign in to comment.