use std::fmt;
use std::sync::{Arc, Mutex};
use ::persistant::capability::provider::{EndpointSpec, ProviderNeeds};
use ::persistant::kv::copy::{self, CopyError, CopyOptions, Phase, Side};
use ::persistant::kv::{KvBackend, KvError};
use ::persistant::registry::{self, Endpoint};
use super::{Durability, PersistantError, PersistantModule};
use crate::module::Needs;
pub const NOT_COPIED: &str = "leases, blob and relational data are not copied";
pub fn parse_endpoint(text: &str, spec: &EndpointSpec) -> Result<Endpoint, String> {
let text = text.trim();
let looks_like_url = text.contains("://");
match spec {
EndpointSpec::None => match text {
"" | "none" => Ok(Endpoint::None),
_ => Err("this backend needs no endpoint: leave it empty".to_owned()),
},
EndpointSpec::Path => {
if looks_like_url {
Err("this backend opens from a path, not a URL".to_owned())
} else if text.is_empty() {
Err("give the path (a directory or file)".to_owned())
} else {
Ok(Endpoint::path(text))
}
}
EndpointSpec::Url { schemes } => {
if let Some(name) = text.strip_prefix("env:") {
if !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
Ok(Endpoint::url_env(name))
} else {
Err("after `env:` give a variable name (letters, digits, `_`)".to_owned())
}
} else if looks_like_url {
Err(format!(
"a connection URL is a secret and is never typed here; put it in an environment variable and answer `env:NAME` (schemes: {})",
schemes.join(", ")
))
} else {
Err(
"answer `env:NAME`, the environment variable that holds the connection URL"
.to_owned(),
)
}
}
_ => Err("this build does not know how to ask for that kind of endpoint".to_owned()),
}
}
pub struct TransferEnd {
pub id: String,
pub endpoint: Endpoint,
}
impl TransferEnd {
pub fn new(id: &str, endpoint: Endpoint) -> Self {
TransferEnd {
id: id.to_owned(),
endpoint,
}
}
}
impl fmt::Debug for TransferEnd {
#[allow(
deprecated,
reason = "a caller may still hand us `Endpoint::Url`; it must be shown as hidden, never printed"
)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = match &self.endpoint {
Endpoint::None => "none",
Endpoint::Path(_) => "path",
Endpoint::Url(_) | Endpoint::Secret(_) => "url (hidden)",
_ => "endpoint",
};
f.debug_struct("TransferEnd")
.field("id", &self.id)
.field("endpoint", &kind)
.finish()
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TransferOptions {
pub allow_existing: bool,
pub page_size: usize,
pub count_first: bool,
}
impl Default for TransferOptions {
fn default() -> Self {
TransferOptions {
allow_existing: false,
page_size: 256,
count_first: false,
}
}
}
impl TransferOptions {
#[must_use]
pub fn allow_existing(mut self, yes: bool) -> Self {
self.allow_existing = yes;
self
}
#[must_use]
pub fn page_size(mut self, n: usize) -> Self {
self.page_size = n.max(1);
self
}
#[must_use]
pub fn count_first(mut self, yes: bool) -> Self {
self.count_first = yes;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransferPhase {
Copy,
Verify,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct TransferProgress {
pub phase: TransferPhase,
pub done: u64,
pub total: Option<u64>,
}
impl fmt::Display for TransferProgress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let what = match self.phase {
TransferPhase::Copy => "copying",
TransferPhase::Verify => "checking the copy",
};
match self.total {
Some(total) => write!(f, "{what}: {} of {total} records", self.done),
None => write!(f, "{what}: {} records", self.done),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ProgressCell(Arc<Mutex<Option<TransferProgress>>>);
impl ProgressCell {
pub fn new() -> Self {
Self::default()
}
pub fn clear(&self) {
*self.0.lock().unwrap_or_else(|e| e.into_inner()) = None;
}
pub fn latest(&self) -> Option<TransferProgress> {
*self.0.lock().unwrap_or_else(|e| e.into_inner())
}
fn set(&self, p: TransferProgress) {
*self.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(p);
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TransferReport {
pub copied: usize,
pub with_ttl: usize,
pub skipped: usize,
pub verified: usize,
}
impl fmt::Display for TransferReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"copied {} records ({} with a time to live), skipped {} already present, verified {}; {NOT_COPIED}",
self.copied, self.with_ttl, self.skipped, self.verified
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum TransferError {
#[error("the project's needs cannot be turned into a backend gate: {0}")]
Needs(PersistantError),
#[error(
"the source and the target are both backend `{0}`; a transfer needs two different backends"
)]
SameBackend(String),
#[error("could not open the {side}: {reason}")]
Open { side: &'static str, reason: String },
#[error(
"the target already holds records; nothing was copied (allow_existing keeps what is there)"
)]
TargetNotEmpty,
#[error(
"the {side} backend does not support `{op}`, which the copy needs; nothing was changed"
)]
Unsupported { side: &'static str, op: String },
#[error("the {side} backend failed during `{op}`: {reason}")]
Backend {
side: &'static str,
op: &'static str,
reason: String,
},
#[error(
"the copy does not match the source: {missing} missing, {differing} different, {extra} extra \
(first keys: {sample:?}); the source is untouched"
)]
Mismatch {
missing: usize,
differing: usize,
extra: usize,
sample: Vec<String>,
},
#[error("could not start the runtime: {0}")]
Runtime(String),
}
fn kv_err(side: &'static str, op: &'static str, e: KvError, hide: &[String]) -> TransferError {
match e {
KvError::Unsupported(_) => TransferError::Unsupported {
side,
op: op.to_owned(),
},
other => TransferError::Backend {
side,
op,
reason: scrub(other.to_string(), hide),
},
}
}
fn scrub(mut text: String, hide: &[String]) -> String {
let mut sorted: Vec<&String> = hide.iter().filter(|s| !s.is_empty()).collect();
sorted.sort_by_key(|s| std::cmp::Reverse(s.len()));
for s in sorted {
text = text.replace(s.as_str(), "<hidden>");
}
text
}
#[allow(
deprecated,
reason = "a caller may still hand us `Endpoint::Url` (persistant 0.17 deprecates it): its URL must still be hidden"
)]
fn secrets_of(endpoint: &Endpoint) -> Vec<String> {
let url = match endpoint {
Endpoint::Url(u) => Some(u.clone()),
Endpoint::Secret(sec) => sec.resolve().ok().map(|r| r.expose().to_string()),
_ => None,
};
let Some(url) = url else { return Vec::new() };
let mut out = vec![url.clone()];
if let Some((_, rest)) = url.split_once("://")
&& let Some((userinfo, _)) = rest.rsplit_once('@')
{
out.push(userinfo.to_owned());
if let Some((_, password)) = userinfo.split_once(':') {
out.push(password.to_owned());
}
}
out
}
impl PersistantModule {
pub fn endpoint_spec(&self, id: &str) -> Option<EndpointSpec> {
let any = ProviderNeeds::new(Vec::new(), Durability::Volatile);
registry::providers(&any)
.ok()?
.into_iter()
.find(|l| l.id == id)
.map(|l| l.endpoint)
}
pub async fn transfer(
&self,
from: &TransferEnd,
to: &TransferEnd,
needs: &Needs,
options: &TransferOptions,
) -> Result<TransferReport, TransferError> {
self.transfer_with_progress(from, to, needs, options, &mut |_| {})
.await
}
pub async fn transfer_with_progress(
&self,
from: &TransferEnd,
to: &TransferEnd,
needs: &Needs,
options: &TransferOptions,
progress: &mut dyn FnMut(TransferProgress),
) -> Result<TransferReport, TransferError> {
if from.id == to.id {
return Err(TransferError::SameBackend(from.id.clone()));
}
let target_gate = self.provider_needs(needs).map_err(TransferError::Needs)?;
let source_gate = ProviderNeeds::new(Vec::new(), Durability::Volatile);
let mut hide = secrets_of(&from.endpoint);
hide.extend(secrets_of(&to.endpoint));
let open_err = |side, e: registry::OpenError| TransferError::Open {
side,
reason: scrub(e.to_string(), &hide),
};
let src = registry::open_named(&from.id, &from.endpoint, &source_gate)
.await
.map_err(|e| open_err("source", e))?;
let dst = registry::open_named(&to.id, &to.endpoint, &target_gate)
.await
.map_err(|e| open_err("target", e))?;
copy_and_verify(src.kv(), dst.kv(), options, &hide, progress).await
}
#[cfg(feature = "persistant-blocking")]
pub fn transfer_blocking(
&self,
from: &TransferEnd,
to: &TransferEnd,
needs: &Needs,
options: &TransferOptions,
) -> Result<TransferReport, TransferError> {
self.transfer_blocking_with_progress(from, to, needs, options, &mut |_| {})
}
#[cfg(feature = "persistant-blocking")]
pub fn transfer_blocking_with_progress(
&self,
from: &TransferEnd,
to: &TransferEnd,
needs: &Needs,
options: &TransferOptions,
progress: &mut dyn FnMut(TransferProgress),
) -> Result<TransferReport, TransferError> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| TransferError::Runtime(e.to_string()))?;
rt.block_on(self.transfer_with_progress(from, to, needs, options, progress))
}
}
pub async fn copy_between<S: KvBackend, T: KvBackend>(
source: &S,
target: &T,
options: &TransferOptions,
) -> Result<TransferReport, TransferError> {
copy_between_with_progress(source, target, options, &mut |_| {}).await
}
pub async fn copy_between_with_progress<S: KvBackend, T: KvBackend>(
source: &S,
target: &T,
options: &TransferOptions,
progress: &mut dyn FnMut(TransferProgress),
) -> Result<TransferReport, TransferError> {
copy_and_verify(source, target, options, &[], progress).await
}
async fn copy_and_verify<S: KvBackend, T: KvBackend>(
source: &S,
target: &T,
options: &TransferOptions,
hide: &[String],
progress: &mut dyn FnMut(TransferProgress),
) -> Result<TransferReport, TransferError> {
let opts = CopyOptions {
allow_existing: options.allow_existing,
page_size: options.page_size.max(1),
count_first: options.count_first,
};
let mut relay = |p: copy::Progress| {
progress(TransferProgress {
phase: match p.phase {
Phase::Copy => TransferPhase::Copy,
_ => TransferPhase::Verify,
},
done: p.done,
total: p.total,
});
};
match copy::copy_all_with_progress(source, target, &opts, &mut relay).await {
Ok(r) => Ok(TransferReport {
copied: to_usize(r.copied),
with_ttl: to_usize(r.with_ttl),
skipped: to_usize(r.skipped),
verified: to_usize(r.verified),
}),
Err(CopyError::TargetNotEmpty) => Err(TransferError::TargetNotEmpty),
Err(CopyError::Kv { side, op, error }) => Err(kv_err(
match side {
Side::Source => "source",
_ => "target",
},
op,
error,
hide,
)),
Err(CopyError::Mismatch {
missing,
differing,
extra,
sample,
}) => Err(TransferError::Mismatch {
missing: to_usize(missing),
differing: to_usize(differing),
extra: to_usize(extra),
sample,
}),
Err(_) => Err(TransferError::Backend {
side: "target",
op: "copy",
reason: "the copy failed".to_owned(),
}),
}
}
fn to_usize(n: u64) -> usize {
usize::try_from(n).unwrap_or(usize::MAX)
}
#[cfg(test)]
mod tests {
use super::*;
use ::persistant::relational::ConnectionSecret;
#[test]
fn a_url_and_the_password_inside_it_are_scrubbed_from_a_backend_message() {
let url = "postgres://appuser:hunter2@db.internal/prod";
let hide = secrets_of(&Endpoint::Secret(ConnectionSecret::literal(url)));
let said = format!("connect to {url} failed: password authentication failed for hunter2");
let clean = scrub(said, &hide);
assert!(
!clean.contains("hunter2") && !clean.contains("db.internal"),
"{clean}"
);
assert!(clean.contains("<hidden>"), "{clean}");
}
#[test]
#[allow(
deprecated,
reason = "this is the test that `Endpoint::Url` is still hidden"
)]
fn a_deprecated_url_endpoint_is_still_scrubbed() {
let url = "postgres://appuser:hunter2@db.internal/prod";
let hide = secrets_of(&Endpoint::Url(url.to_owned()));
let clean = scrub(format!("connect to {url} failed: hunter2"), &hide);
assert!(
!clean.contains("hunter2") && !clean.contains("db.internal"),
"{clean}"
);
assert!(
format!(
"{:?}",
TransferEnd::new("pg", Endpoint::Url(url.to_owned()))
)
.contains("url (hidden)")
);
}
#[test]
fn a_typed_url_is_refused_without_being_echoed_and_env_name_is_accepted() {
let spec = EndpointSpec::Url {
schemes: vec!["postgres".to_owned()],
};
let typed = "postgres://appuser:hunter2@db.internal/prod";
let err = parse_endpoint(typed, &spec).unwrap_err();
assert!(
!err.contains("hunter2") && !err.contains("db.internal"),
"{err}"
);
assert_eq!(
parse_endpoint("env:APP_DB_URL", &spec),
Ok(Endpoint::url_env("APP_DB_URL"))
);
assert!(parse_endpoint("env:", &spec).is_err());
assert!(parse_endpoint("env:a b", &spec).is_err());
assert!(parse_endpoint("just words", &spec).is_err());
}
#[test]
fn path_and_none_specs_take_a_path_and_nothing() {
assert_eq!(
parse_endpoint(" /data/x ", &EndpointSpec::Path),
Ok(Endpoint::path("/data/x"))
);
assert!(parse_endpoint("", &EndpointSpec::Path).is_err());
assert!(parse_endpoint("sqlite://x", &EndpointSpec::Path).is_err());
assert_eq!(parse_endpoint("", &EndpointSpec::None), Ok(Endpoint::None));
assert_eq!(
parse_endpoint("none", &EndpointSpec::None),
Ok(Endpoint::None)
);
assert!(parse_endpoint("/x", &EndpointSpec::None).is_err());
}
#[test]
fn a_path_endpoint_hides_nothing() {
assert!(secrets_of(&Endpoint::path("/data/x")).is_empty());
}
}
#[cfg(feature = "persistant-blocking")]
#[derive(Debug)]
pub struct TransferJob {
module: PersistantModule,
from: TransferEnd,
to: TransferEnd,
needs: Needs,
options: TransferOptions,
progress: Option<ProgressCell>,
}
#[cfg(feature = "persistant-blocking")]
impl TransferJob {
pub fn new(
module: PersistantModule,
from: TransferEnd,
to: TransferEnd,
needs: Needs,
options: TransferOptions,
) -> Self {
TransferJob {
module,
from,
to,
needs,
options,
progress: None,
}
}
#[must_use]
pub fn with_progress(mut self, cell: ProgressCell) -> Self {
self.progress = Some(cell);
self
}
pub fn from_id(&self) -> &str {
&self.from.id
}
pub fn to_id(&self) -> &str {
&self.to.id
}
pub fn run(self) -> Result<TransferReport, TransferError> {
match self.progress.clone() {
Some(cell) => self.run_with_progress(&mut |p| cell.set(p)),
None => self.run_with_progress(&mut |_| {}),
}
}
pub fn run_with_progress(
self,
progress: &mut dyn FnMut(TransferProgress),
) -> Result<TransferReport, TransferError> {
self.module.transfer_blocking_with_progress(
&self.from,
&self.to,
&self.needs,
&self.options,
progress,
)
}
}