feat(bin) : refactor auth_jwt_secret function (#5451)

This commit is contained in:
DoTheBestToGetTheBest
2023-11-15 11:43:45 -08:00
committed by GitHub
parent 187f6faa09
commit 8207401359
2 changed files with 15 additions and 10 deletions

View File

@ -10,6 +10,7 @@ use crate::{
config::RethRpcConfig,
ext::RethNodeCommandConfig,
},
utils::get_or_create_jwt_secret_from_path,
};
use clap::{
builder::{PossibleValue, RangedU64ValueParser, TypedValueParser},
@ -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),
}
}

View File

@ -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 {
@ -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)
}
}