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

feat(bin) : refactor auth_jwt_secret function #5451

Merged
merged 3 commits into from
Nov 15, 2023
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
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)
}
}