mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-28 21:50:26 +08:00
Reasons for the rewrite:
- Migrate away from `ec25519` as our implementation of Ed25519,
because that crate is unmaintained.
Instead of `ec25519` we use `ed25519-dalek` which is the most popular
implementation of Ed25519 in Rust.
- Migrate away from `amplify`, because we make very little use of its
features. Here, we only use `amplify::Bytes32`, which is just a thin
wrapper of `[u8; 32]`.
Instead of `amplify::Bytes32` we use `type PublicKeyBytes = [u8; 32]`.
The realization that `ExtendedSignature` is not really tied to SSH, but
is merely a representation of a key together with a signature, allows to
relax the bounds on traits in `radicle-cob`.
Separating out uses of "public key" (a rather simple model of a key,
just 32 bytes) and "verifying key" (an actual point on the Edwards
curve, ready to verify signatures, using more memory than just 32 bytes)
allows a more sensible definition of `crypto::Signer`, which propagates
to `radicle-cob`, `radicle`, `radicle-cli`, `radicle-node` and
simplifies trait bounds.
In `radicle-node`, make the fact clearer that actually the secret key
itself is required (for our implementation of Diffie-Hellman) not
"just" signing. This results in removal of the generic argument
(usually called `G`) of multiple types.
Concretizing the types in `radicle-node` this way, i.e. by (morally)
setting `G = radicle_crypto::SigningKey`, leads to removal of
`MemorySigner` and `MockSigner`, as they really just wrap a signing
key.
Re-exporting `ed25519::Signature` (via re-export in `ed25519-dalek`)
is hopefully more future-proof than using our own type for signatures.
However, the fact that `ed25519::Signature` is not serializable and
does not implement `std::hash::Hash` means that we have to resort to
ad-hoc solutions in `radicle`:
1. The Identity COB requires serialization of signatures. These are
generally questionable, since the security of the Identity COB
should rely on the security of Signed References. In the medium
term we might even want to consider ignoring signatures in actions.
2. When reading Signed References, detection of Replay Attacks uses
a `HashMap` keyed by signatures. This detection only exists for
backwards compatibility.
These two use cases are exotic enough to warrant ad-hoc implementations.
By not wrapping `ed25519::Signature`, we encourage writing more widely
compatible code in the future.
Co-authored-by: Fintan Halpenny <fintan.halpenny@gmail.com>
476 lines
15 KiB
Rust
476 lines
15 KiB
Rust
use std::io;
|
|
use std::net::SocketAddr;
|
|
use std::path::PathBuf;
|
|
use std::process::exit;
|
|
use std::str::FromStr;
|
|
|
|
use thiserror::Error;
|
|
|
|
use radicle::crypto::Signer as _;
|
|
use radicle::node::NodeId;
|
|
use radicle::profile;
|
|
|
|
use radicle_node::crypto::ssh::keystore::Keystore;
|
|
use radicle_node::fingerprint::{Fingerprint, FingerprintVerification};
|
|
use radicle_node::{Runtime, VERSION};
|
|
use radicle_signals as signals;
|
|
|
|
const HELP_MSG: &str = r#"
|
|
Usage
|
|
|
|
radicle-node [<option>…]
|
|
|
|
If you're running a public seed node, make sure to use `--listen` to bind a listening socket to
|
|
eg. `0.0.0.0:8776`, and add your external addresses in your configuration.
|
|
|
|
Options
|
|
|
|
--config <path> Config file to use
|
|
(default: ~/.radicle/config.json)
|
|
--secret <path> Secret key to use
|
|
(default ~/.radicle/keys/radicle)
|
|
--force Force start even if an existing control socket
|
|
is found
|
|
--listen <address> Address to listen on
|
|
--log-level <level> Set log level
|
|
(default: info)
|
|
--log-logger (radicle | structured | systemd) Set logger implementation
|
|
(default: radicle)
|
|
--log-format json Set log format for logger implementation
|
|
--version Print program version
|
|
--help Print help
|
|
"#;
|
|
|
|
#[cfg(feature = "dhat-heap")]
|
|
#[global_allocator]
|
|
static ALLOC: dhat::Alloc = dhat::Alloc;
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum Logger {
|
|
Radicle,
|
|
#[cfg(feature = "structured-logger")]
|
|
Structured,
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
Systemd,
|
|
}
|
|
|
|
// Required for Mac and potentially Windows as clippy complains because of the OS specific
|
|
// guard below.
|
|
#[allow(clippy::derivable_impls)]
|
|
impl Default for Logger {
|
|
fn default() -> Self {
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
if radicle_systemd::journal::connected() {
|
|
return Logger::Systemd;
|
|
}
|
|
|
|
Logger::Radicle
|
|
}
|
|
}
|
|
|
|
impl FromStr for Logger {
|
|
type Err = &'static str;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
match s {
|
|
"radicle" => Ok(Logger::Radicle),
|
|
#[cfg(feature = "structured-logger")]
|
|
"structured" => Ok(Logger::Structured),
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
"systemd" => Ok(Logger::Systemd),
|
|
_ => Err("unknown logger"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum LogFormat {
|
|
#[cfg(feature = "structured-logger")]
|
|
Json,
|
|
}
|
|
|
|
impl FromStr for LogFormat {
|
|
type Err = &'static str;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
match s {
|
|
#[cfg(feature = "structured-logger")]
|
|
"json" => Ok(LogFormat::Json),
|
|
_ => Err("unknown log format"),
|
|
}
|
|
}
|
|
}
|
|
|
|
struct LogOptions {
|
|
level: Option<log::Level>,
|
|
logger: Logger,
|
|
format: Option<LogFormat>,
|
|
}
|
|
|
|
struct Options {
|
|
config: Option<PathBuf>,
|
|
secret: Option<PathBuf>,
|
|
listen: Vec<SocketAddr>,
|
|
log: LogOptions,
|
|
force: bool,
|
|
}
|
|
|
|
fn parse_options() -> Result<Options, lexopt::Error> {
|
|
use lexopt::prelude::*;
|
|
use std::str::FromStr as _;
|
|
|
|
let mut parser = lexopt::Parser::from_env();
|
|
let mut listen = Vec::new();
|
|
let mut config = None;
|
|
let mut secret = None;
|
|
let mut force = false;
|
|
let mut log_level = None;
|
|
let mut log_logger = Logger::default();
|
|
let mut log_format = None;
|
|
|
|
while let Some(arg) = parser.next()? {
|
|
match arg {
|
|
Long("force") => {
|
|
force = true;
|
|
}
|
|
Long("config") => {
|
|
config = Some(parser.value()?.parse_with(PathBuf::from_str)?);
|
|
}
|
|
Long("secret") => {
|
|
secret = Some(parser.value()?.parse()?);
|
|
}
|
|
Long("listen") => {
|
|
let addr = parser.value()?.parse_with(SocketAddr::from_str)?;
|
|
listen.push(addr);
|
|
}
|
|
Long("log") | Long("log-level") => {
|
|
if matches!(arg, Long("log")) {
|
|
eprintln!(
|
|
"Warning: The option `--log` is deprecated and will be removed. Please use `--log-level` instead."
|
|
);
|
|
}
|
|
log_level = Some(parser.value()?.parse_with(log::Level::from_str)?);
|
|
}
|
|
Long("log-logger") => {
|
|
let parsed = parser.value()?.parse_with(Logger::from_str)?;
|
|
if matches!(parsed, Logger::Radicle) {
|
|
return Err(lexopt::Error::Custom(
|
|
"explicitly choosing this logger is forbidden, because it is deprecated"
|
|
.into(),
|
|
));
|
|
}
|
|
log_logger = parsed;
|
|
}
|
|
Long("log-format") => {
|
|
log_format = Some(parser.value()?.parse_with(LogFormat::from_str)?);
|
|
}
|
|
Long("help") | Short('h') => {
|
|
println!("{HELP_MSG}");
|
|
exit(0);
|
|
}
|
|
Long("version") => {
|
|
let _ = VERSION.write(&mut io::stdout());
|
|
exit(0);
|
|
}
|
|
_ => {
|
|
return Err(arg.unexpected());
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(Options {
|
|
force,
|
|
secret,
|
|
listen,
|
|
config,
|
|
log: LogOptions {
|
|
level: log_level,
|
|
logger: log_logger,
|
|
format: log_format,
|
|
},
|
|
})
|
|
}
|
|
|
|
#[derive(Error, Debug)]
|
|
enum ExecutionError {
|
|
#[error(transparent)]
|
|
Io(#[from] io::Error),
|
|
#[error(transparent)]
|
|
ConfigurationLoading(#[from] profile::config::LoadError),
|
|
#[error(transparent)]
|
|
Runtime(#[from] radicle_node::runtime::Error),
|
|
#[error(transparent)]
|
|
Fingerprint(#[from] radicle_node::fingerprint::Error),
|
|
#[error("failed to load secret key '{path}': not found")]
|
|
SecretNotFound { path: PathBuf },
|
|
#[error("failed to load secret '{path}': {source}")]
|
|
SecretLoading {
|
|
path: PathBuf,
|
|
source: radicle::crypto::ssh::keystore::Error,
|
|
},
|
|
#[error(
|
|
"failed to load secret key '{secret}': fingerprint of corresponding public key is different from '{fingerprint}'"
|
|
)]
|
|
FingerprintMismatch {
|
|
secret: PathBuf,
|
|
fingerprint: Fingerprint,
|
|
},
|
|
}
|
|
|
|
/// Loads the path to a credential from systemd, if available.
|
|
///
|
|
/// The credential ID should only be given as a suffix, as this function will
|
|
/// try different prefixes for backwards compatibility reasons.
|
|
///
|
|
/// The prefix `dev.radicle.node` is the preferred prefix, and should be used
|
|
/// for new credentials, while the prefix `xyz.radicle.node` is deprecated and
|
|
/// should be migrated away from. If it is used, a warning is logged.
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
fn load_credential(id_suffix: &str) -> Option<PathBuf> {
|
|
const INFIX_NODE: &str = ".radicle.node.";
|
|
const PREFIX_DEV: &str = "dev";
|
|
const PREFIX_XYZ: &str = "xyz";
|
|
|
|
let id_dev = format!("{}{}{}", PREFIX_DEV, INFIX_NODE, id_suffix);
|
|
|
|
match radicle_systemd::credential::path(&id_dev) {
|
|
Ok(option @ Some(_)) => return option,
|
|
Ok(None) => {
|
|
// Fall through and try `PREFIX_XYZ` for backwards compatibility.
|
|
}
|
|
Err(err) => {
|
|
log::warn!(target: "node", "Failed to obtain of the systemd credential with ID '{id_dev}': {err}");
|
|
}
|
|
};
|
|
|
|
let id_xyz = format!("{}{}{}", PREFIX_XYZ, INFIX_NODE, id_suffix);
|
|
match radicle_systemd::credential::path(&id_xyz) {
|
|
Ok(option @ Some(_)) => {
|
|
log::warn!(target: "node", "Obtained path of the systemd credential with ID '{id_xyz}'. Using this credential ID is discouraged. Please change the ID to '{id_dev}'.");
|
|
option
|
|
}
|
|
Ok(None) => {
|
|
// No credential found with either ID.
|
|
None
|
|
}
|
|
Err(err) => {
|
|
log::warn!(target: "node", "Failed to obtain path of the systemd credential with ID '{id_xyz}': {err}");
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
fn execute(options: Options) -> Result<(), ExecutionError> {
|
|
let home = profile::home()?;
|
|
|
|
// Up to now, the active log level was `LOG_LEVEL_DEFAULT`.
|
|
// The first thing we do after reading command line options is
|
|
// to set the log level, as this influences logging during
|
|
// configuration loading.
|
|
if let Some(level) = options.log.level {
|
|
log::set_max_level(level.to_level_filter());
|
|
}
|
|
|
|
let config = options.config.unwrap_or_else(|| home.config());
|
|
let mut config = profile::Config::load(&config)?;
|
|
|
|
if options.log.level.is_none() {
|
|
log::set_max_level(log::Level::from(config.node.log).to_level_filter());
|
|
} else {
|
|
// It might seem counter-intuitive at first, as there
|
|
// always is a log level in the configuration, but the command
|
|
// line argument has precedence, and if it is present, the
|
|
// log level has been already set above. Thus, we have nothing
|
|
// to do in this case.
|
|
}
|
|
|
|
log::info!(target: "node", "Starting node..");
|
|
log::info!(target: "node", "Version {} ({})", env!("RADICLE_VERSION"), env!("GIT_HEAD"));
|
|
log::info!(target: "node", "Unlocking node keystore..");
|
|
|
|
let passphrase = None;
|
|
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
let passphrase = passphrase.or_else(|| load_credential("passphrase").and_then(|path| {
|
|
match std::fs::read_to_string(&path) {
|
|
Ok(passphrase) => Some(passphrase.into()),
|
|
Err(err) => {
|
|
log::warn!(target: "node", "Failed to read passphrase from '{}': {err}", path.display());
|
|
None
|
|
}
|
|
}
|
|
}));
|
|
|
|
let passphrase = passphrase.or_else(profile::env::passphrase);
|
|
|
|
let secret_path = options.secret;
|
|
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
let secret_path = secret_path.or_else(|| load_credential("secret"));
|
|
|
|
let secret_path = secret_path
|
|
.or_else(|| config.node.secret.clone())
|
|
.unwrap_or_else(|| home.keys().join("radicle"));
|
|
|
|
let keystore = Keystore::from_secret_path(&secret_path);
|
|
|
|
let signer = keystore
|
|
.secret_key(passphrase.clone())
|
|
.map_err(|err| ExecutionError::SecretLoading {
|
|
path: secret_path.clone(),
|
|
source: err,
|
|
})?
|
|
.ok_or_else(|| ExecutionError::SecretNotFound {
|
|
path: secret_path.clone(),
|
|
})?;
|
|
|
|
if let Some(fp) = Fingerprint::read(&home)? {
|
|
log::debug!(target: "node", "Verifying fingerprint..");
|
|
if fp.verify(&signer) != FingerprintVerification::Match {
|
|
return Err(ExecutionError::FingerprintMismatch {
|
|
secret: keystore.secret_key_path().to_path_buf(),
|
|
fingerprint: fp,
|
|
});
|
|
}
|
|
} else {
|
|
log::info!(target: "node", "Initializing fingerprint..");
|
|
Fingerprint::init(&home, &signer)?;
|
|
}
|
|
|
|
log::info!(target: "node", "Node ID is {}", NodeId::from(*signer.public_key()));
|
|
|
|
// Add the preferred seeds as persistent peers so that we reconnect to them automatically.
|
|
config.node.connect.extend(config.preferred_seeds);
|
|
|
|
let listen = if !options.listen.is_empty() {
|
|
options.listen.clone()
|
|
} else {
|
|
config.node.listen.clone()
|
|
};
|
|
|
|
if let Err(e) = radicle::io::set_file_limit::<usize>(config.node.limits.max_open_files.into()) {
|
|
log::warn!(target: "node", "Unable to set process open file limit: {e}");
|
|
}
|
|
|
|
let signals = {
|
|
let (notify, signals) = std::sync::mpsc::sync_channel(1);
|
|
signals::install(notify)?;
|
|
signals
|
|
};
|
|
|
|
let socket = home.socket_from_env();
|
|
if options.force {
|
|
log::debug!(target: "node", "Removing existing control socket..");
|
|
std::fs::remove_file(&socket).ok();
|
|
}
|
|
Runtime::init(home, config.node, socket, listen, signals, signer)?.run()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn initialize_logging(options: &LogOptions) -> Result<(), Box<dyn std::error::Error>> {
|
|
let level = options.level.unwrap_or(log::Level::Info);
|
|
|
|
let logger: Box<dyn log::Log> = {
|
|
match options.logger {
|
|
#[cfg(feature = "structured-logger")]
|
|
Logger::Structured => {
|
|
use structured_logger::{Builder, json};
|
|
|
|
let writer = match options.format.unwrap_or(LogFormat::Json) {
|
|
LogFormat::Json => json::new_writer(io::stdout()),
|
|
};
|
|
|
|
// Set to trace (via `Level::max`) and defer to log::set_max_level for filtering
|
|
Box::new(
|
|
Builder::with_level(log::Level::max().as_str())
|
|
.with_default_writer(writer)
|
|
.build(),
|
|
)
|
|
}
|
|
#[cfg(all(feature = "systemd", target_os = "linux"))]
|
|
Logger::Systemd => {
|
|
use radicle_systemd::journal::*;
|
|
use thiserror::Error;
|
|
|
|
#[derive(Error, Debug)]
|
|
enum JournalError {
|
|
#[error("journald not connected")]
|
|
NotConnected,
|
|
#[error("journald i/o: {0}")]
|
|
Io(#[from] io::Error),
|
|
}
|
|
|
|
if !connected() {
|
|
return Err(Box::new(JournalError::NotConnected));
|
|
}
|
|
|
|
const SYSLOG_IDENTIFIER: &str = "radicle-node";
|
|
logger::<&str, &str, _>(SYSLOG_IDENTIFIER.to_string(), []).map_err(Box::new)?
|
|
}
|
|
Logger::Radicle => Box::new(radicle_log::Logger::new()),
|
|
}
|
|
};
|
|
|
|
log::set_boxed_logger(logger).expect("no other logger should have been set already");
|
|
log::set_max_level(level.to_level_filter());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn panic_hook(info: &std::panic::PanicHookInfo) {
|
|
#[cfg(feature = "backtrace")]
|
|
let backtrace = format!("{:?}", backtrace::Backtrace::new());
|
|
|
|
#[cfg(not(feature = "backtrace"))]
|
|
let backtrace = " (no backtrace available)";
|
|
|
|
let thread = std::thread::current();
|
|
let thread = thread.name().unwrap_or("<unnamed>");
|
|
|
|
let msg = info
|
|
.payload()
|
|
.downcast_ref::<&'static str>()
|
|
.copied()
|
|
.or(info.payload().downcast_ref::<String>().map(|s| s.as_str()))
|
|
.unwrap_or("Box<Any>");
|
|
|
|
match info.location() {
|
|
Some(location) => {
|
|
log::error!(
|
|
target: "panic", "thread '{thread}' panicked at '{msg}': {}:{}{backtrace}",
|
|
location.file(),
|
|
location.line(),
|
|
);
|
|
}
|
|
None => log::error!(
|
|
target: "panic", "thread '{thread}' panicked at '{msg}'{backtrace}",
|
|
),
|
|
}
|
|
|
|
log::logger().flush();
|
|
}
|
|
|
|
fn main() {
|
|
#[cfg(feature = "dhat-heap")]
|
|
let _profiler = dhat::Profiler::new_heap();
|
|
|
|
let options = parse_options().unwrap_or_else(|err| {
|
|
// The lexopt errors read nicely with a comma.
|
|
eprintln!("Failed to parse options, {err:#}");
|
|
exit(2);
|
|
});
|
|
|
|
initialize_logging(&options.log).unwrap_or_else(|err| {
|
|
eprintln!("Failed to initialize logging: {err:#}");
|
|
exit(3);
|
|
});
|
|
|
|
std::panic::set_hook(Box::new(panic_hook));
|
|
|
|
if let Err(err) = execute(options) {
|
|
log::error!(target: "node", "{err:#}");
|
|
exit(1);
|
|
}
|
|
}
|