use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use rings_core::delegation::DelegateeKey;
use rings_core::dht::Did;
use rings_core::ecc::SecretKey;
use super::config::Config;
use crate::error::Error;
use crate::error::Result;
use crate::processor::DelegationHorizon;
use crate::processor::Processor;
use crate::processor::SigningState;
use crate::processor::SIGNING_HORIZON_LEAD_MS;
use crate::util::ensure_parent_dir;
use crate::util::expand_home;
pub const HORIZON_POLL: Duration = Duration::from_secs(60);
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DelegationRenewalPaths {
pub delegatee_key_path: PathBuf,
pub delegator_key_path: PathBuf,
}
impl DelegationRenewalPaths {
pub fn from_config(config: &Config) -> Result<Option<Self>> {
config
.delegator_key
.as_deref()
.map(|delegator_key| {
expand_home(&config.delegatee_key).and_then(|delegatee_key_path| {
expand_home(delegator_key).map(|delegator_key_path| Self {
delegatee_key_path,
delegator_key_path,
})
})
})
.transpose()
}
}
fn parse_delegator_secret_key(raw: &str, display_path: &Path) -> Result<SecretKey> {
raw.lines()
.map(str::trim)
.find(|line| !line.is_empty() && !line.starts_with('#'))
.ok_or_else(|| {
Error::InvalidConfig(format!(
"{}: ECDSA key file contains no key entries",
display_path.display()
))
})
.map(|line| line.strip_prefix("0x").unwrap_or(line))
.and_then(|key| {
SecretKey::from_str(key).map_err(|_| {
Error::InvalidConfig(format!(
"{}: ECDSA key file contains an invalid key",
display_path.display()
))
})
})
}
pub fn read_delegator_secret_key(path: impl AsRef<Path>) -> Result<SecretKey> {
expand_home(path.as_ref()).and_then(|path| {
fs::read_to_string(&path)
.map_err(|error| Error::OpenFileError(format!("{}: {error}", path.display())))
.and_then(|raw| parse_delegator_secret_key(&raw, &path))
})
}
pub fn write_delegatee_key_atomically(
path: impl AsRef<Path>,
delegatee_key: &DelegateeKey,
) -> Result<PathBuf> {
let target = expand_home(path.as_ref())?;
ensure_parent_dir(&target)?;
let parent = target.parent().ok_or_else(|| {
Error::CreateFileError(format!(
"{}: path has no parent directory",
target.display()
))
})?;
let file_name = target
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("delegatee_key");
let temp_path = parent.join(format!(
".{file_name}.tmp.{}.{}",
std::process::id(),
uuid::Uuid::new_v4()
));
let dumped = delegatee_key.dump().map_err(Error::CoreError)?;
let write_result = (|| -> std::io::Result<()> {
let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
options.mode(0o600);
let mut file = options.open(&temp_path)?;
file.write_all(dumped.as_bytes())?;
file.sync_all()?;
drop(file);
fs::rename(&temp_path, &target)?;
#[cfg(unix)]
fs::File::open(parent)?.sync_all()?;
Ok(())
})();
if let Err(error) = write_result {
let _ = fs::remove_file(&temp_path);
return Err(Error::CreateFileError(format!(
"{}: {error}",
target.display()
)));
}
Ok(target)
}
async fn try_renew_delegation(
processor: &Processor,
paths: &DelegationRenewalPaths,
now_ms: u128,
) -> Result<DelegationHorizon> {
let expected_did = processor.did();
let next_horizon = processor.renew_delegatee_key_with(
now_ms,
|current| {
read_delegator_secret_key(&paths.delegator_key_path)
.and_then(|delegator_key| {
(Did::from(delegator_key.address()) == expected_did)
.then_some(delegator_key)
.ok_or_else(|| {
Error::VerifyError(
"delegator key DID does not match node DID".to_string(),
)
})
})
.and_then(|delegator_key| {
DelegateeKey::new_with_seckey_at(
&delegator_key,
current.delegation().ttl_ms(),
now_ms,
)
.map_err(Error::CoreError)
})
},
|next| write_delegatee_key_atomically(&paths.delegatee_key_path, next).map(|_| ()),
)?;
processor.republish_registrations().await;
Ok(next_horizon)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum HorizonAction {
ExitMute,
AttemptRenewal {
remaining_ms: u128,
},
Sleep {
wait_ms: u128,
warn_remaining_ms: Option<u128>,
},
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct HorizonSupervisorState {
warned_horizon_ms: Option<u128>,
attempted_horizon_ms: Option<u128>,
}
impl HorizonSupervisorState {
fn step(
self,
horizon: DelegationHorizon,
created_at_ms: u128,
now_ms: u128,
can_renew: bool,
) -> (Self, HorizonAction) {
let h = horizon.horizon_ms();
match horizon.state_at(now_ms) {
SigningState::Mute => (self, HorizonAction::ExitMute),
SigningState::Signing { remaining_ms } => (self, HorizonAction::Sleep {
wait_ms: remaining_ms - SIGNING_HORIZON_LEAD_MS,
warn_remaining_ms: None,
}),
SigningState::Expiring { remaining_ms } => {
let renew_at_ms = horizon.renew_at_ms(created_at_ms);
if can_renew && now_ms < renew_at_ms {
(self, HorizonAction::Sleep {
wait_ms: renew_at_ms - now_ms,
warn_remaining_ms: None,
})
} else if can_renew && self.attempted_horizon_ms != Some(h) {
(
Self {
attempted_horizon_ms: Some(h),
..self
},
HorizonAction::AttemptRenewal { remaining_ms },
)
} else {
let (next, wait_ms, warn_remaining_ms) = self.fallback_sleep(h, remaining_ms);
(next, HorizonAction::Sleep {
wait_ms,
warn_remaining_ms,
})
}
}
}
}
fn fallback_sleep(self, horizon_ms: u128, remaining_ms: u128) -> (Self, u128, Option<u128>) {
let warn_remaining_ms =
(self.warned_horizon_ms != Some(horizon_ms)).then_some(remaining_ms);
(
Self {
warned_horizon_ms: Some(horizon_ms),
..self
},
remaining_ms,
warn_remaining_ms,
)
}
}
fn bounded_horizon_sleep(wait_ms: u128) -> Duration {
Duration::from_millis(u64::try_from(wait_ms).unwrap_or(u64::MAX)).min(HORIZON_POLL)
}
fn log_mute_and_reach(horizon: DelegationHorizon) -> Error {
tracing::error!(
expires_at_ms = %horizon.expires_at_ms(),
horizon_ms = %horizon.horizon_ms(),
"the node's delegation reached its signing horizon: no frame can leave, so \
the node exits for its supervisor to restart it with a fresh delegatee key"
);
horizon.reached()
}
fn log_expiring_warning(horizon_ms: u128, remaining_ms: u128) {
tracing::warn!(
remaining_ms = %remaining_ms,
horizon_ms = %horizon_ms,
"the node's delegation nears its signing horizon: the node exits then; \
rotate its delegatee key and restart it before"
);
}
pub async fn run_signing_horizon(
processor: Arc<Processor>,
renewal: Option<DelegationRenewalPaths>,
now_ms: impl Fn() -> u128,
) -> Error {
let mut supervisor = HorizonSupervisorState::default();
loop {
let now = now_ms();
let delegation = processor.delegatee_key().delegation();
let horizon = DelegationHorizon::of(&delegation);
let h = horizon.horizon_ms();
let (next_state, action) =
supervisor.step(horizon, delegation.created_at_ms(), now, renewal.is_some());
supervisor = next_state;
let (wait_ms, warn_remaining_ms) = match (action, renewal.as_ref()) {
(HorizonAction::ExitMute, _) => return log_mute_and_reach(horizon),
(HorizonAction::AttemptRenewal { remaining_ms }, Some(paths)) => {
match try_renew_delegation(&processor, paths, now).await {
Ok(next_horizon) => {
tracing::info!(
previous_horizon_ms = %h,
next_horizon_ms = %next_horizon.horizon_ms(),
expires_at_ms = %next_horizon.expires_at_ms(),
"renewed the node's delegation in process before its signing horizon"
);
continue;
}
Err(error) => {
tracing::warn!(
%error,
remaining_ms = %remaining_ms,
horizon_ms = %h,
"failed to renew the node's delegation in process; falling \
back to exiting at the signing horizon"
);
let (fallback_state, wait_ms, warn_remaining_ms) =
supervisor.fallback_sleep(h, remaining_ms);
supervisor = fallback_state;
(wait_ms, warn_remaining_ms)
}
}
}
(HorizonAction::AttemptRenewal { remaining_ms }, None) => {
let (fallback_state, wait_ms, warn_remaining_ms) =
supervisor.fallback_sleep(h, remaining_ms);
supervisor = fallback_state;
(wait_ms, warn_remaining_ms)
}
(
HorizonAction::Sleep {
wait_ms,
warn_remaining_ms,
},
_,
) => (wait_ms, warn_remaining_ms),
};
if let Some(remaining_ms) = warn_remaining_ms {
log_expiring_warning(h, remaining_ms);
}
tokio::time::sleep(bounded_horizon_sleep(wait_ms)).await;
}
}
pub async fn signing_horizon(horizon: DelegationHorizon, now_ms: impl Fn() -> u128) -> Error {
let mut warned = false;
let h = horizon.horizon_ms();
loop {
match horizon.state_at(now_ms()) {
SigningState::Mute => return log_mute_and_reach(horizon),
SigningState::Signing { remaining_ms } => {
tokio::time::sleep(bounded_horizon_sleep(
remaining_ms - SIGNING_HORIZON_LEAD_MS,
))
.await;
}
SigningState::Expiring { remaining_ms } => {
if !std::mem::replace(&mut warned, true) {
log_expiring_warning(h, remaining_ms);
}
tokio::time::sleep(bounded_horizon_sleep(remaining_ms)).await;
}
}
}
}
#[cfg(test)]
mod tests;