Skip to content

Commit

Permalink
feat(bin) : refactor auth_jwt_secret function (#5451)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoTheBestToGetTheBest authored Nov 15, 2023
1 parent 187f6fa commit 8207401
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
11 changes: 2 additions & 9 deletions bin/reth/src/args/rpc_server_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
config::RethRpcConfig,
ext::RethNodeCommandConfig,
},
utils::get_or_create_jwt_secret_from_path,
};
use clap::{
builder::{PossibleValue, RangedU64ValueParser, TypedValueParser},
Expand Down Expand Up @@ -449,15 +450,7 @@ impl RethRpcConfig for RpcServerArgs {
debug!(target: "reth::cli", user_path=?fpath, "Reading JWT auth secret file");
JwtSecret::from_file(fpath)
}
None => {
if default_jwt_path.exists() {
debug!(target: "reth::cli", ?default_jwt_path, "Reading JWT auth secret file");
JwtSecret::from_file(&default_jwt_path)
} else {
info!(target: "reth::cli", ?default_jwt_path, "Creating JWT auth secret file");
JwtSecret::try_create(&default_jwt_path)
}
}
None => get_or_create_jwt_secret_from_path(&default_jwt_path),
}
}

Expand Down
14 changes: 13 additions & 1 deletion bin/reth/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use reth_interfaces::p2p::{
use reth_primitives::{
fs, BlockHashOrNumber, ChainSpec, HeadersDirection, SealedBlock, SealedHeader,
};
use reth_rpc::{JwtError, JwtSecret};
use std::{
env::VarError,
path::{Path, PathBuf},
rc::Rc,
sync::Arc,
};
use tracing::info;
use tracing::{debug, info};

/// Exposing `open_db_read_only` function
pub mod db {
Expand Down Expand Up @@ -247,3 +248,14 @@ impl ListFilter {
self.len = len;
}
}
/// Attempts to retrieve or create a JWT secret from the specified path.
pub fn get_or_create_jwt_secret_from_path(path: &Path) -> Result<JwtSecret, JwtError> {
if path.exists() {
debug!(target: "reth::cli", ?path, "Reading JWT auth secret file");
JwtSecret::from_file(path)
} else {
info!(target: "reth::cli", ?path, "Creating JWT auth secret file");
JwtSecret::try_create(path)
}
}

0 comments on commit 8207401

Please sign in to comment.