use std::collections::HashSet;
use std::fmt;
use ::persistant::capability::provider::{EndpointSpec, ProviderNeeds};
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 {
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,
}
impl Default for TransferOptions {
fn default() -> Self {
TransferOptions {
allow_existing: false,
page_size: 256,
}
}
}
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
}
}
#[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
}
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> {
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).await
}
#[cfg(feature = "persistant-blocking")]
pub fn transfer_blocking(
&self,
from: &TransferEnd,
to: &TransferEnd,
needs: &Needs,
options: &TransferOptions,
) -> 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(from, to, needs, options))
}
}
pub async fn copy_between<S: KvBackend, T: KvBackend>(
source: &S,
target: &T,
options: &TransferOptions,
) -> Result<TransferReport, TransferError> {
copy_and_verify(source, target, options, &[]).await
}
async fn read_page<S: KvBackend>(
source: &S,
after: Option<&str>,
limit: usize,
hide: &[String],
) -> Result<(Vec<(String, Vec<u8>)>, Option<String>), TransferError> {
match source.ordered_scan_values("", after, limit).await {
Ok(page) => return Ok((page.entries, page.next)),
Err(KvError::Unsupported(_)) => {}
Err(e) => return Err(kv_err("source", "ordered_scan_values", e, hide)),
}
let keys = source
.ordered_scan("", after, limit)
.await
.map_err(|e| kv_err("source", "ordered_scan", e, hide))?;
let mut entries = Vec::with_capacity(keys.keys.len());
for key in keys.keys {
let got = source
.get(&key)
.await
.map_err(|e| kv_err("source", "get", e, hide))?;
if let Some(value) = got {
entries.push((key, value));
}
}
Ok((entries, keys.next))
}
async fn target_has_any<T: KvBackend>(target: &T, hide: &[String]) -> Result<bool, TransferError> {
match target.ordered_scan("", None, 1).await {
Ok(page) => Ok(!page.keys.is_empty()),
Err(KvError::Unsupported(_)) => target
.prefix_list("", None, 1)
.await
.map(|page| !page.keys.is_empty())
.map_err(|e| kv_err("target", "prefix_list", e, hide)),
Err(e) => Err(kv_err("target", "ordered_scan", e, hide)),
}
}
async fn target_count<T: KvBackend>(
target: &T,
limit: usize,
hide: &[String],
) -> Result<usize, TransferError> {
let (mut n, mut after) = (0, None::<String>);
loop {
let page = target
.ordered_scan("", after.as_deref(), limit)
.await
.map_err(|e| kv_err("target", "ordered_scan", e, hide))?;
n += page.keys.len();
match page.next {
Some(cursor) => after = Some(cursor),
None => return Ok(n),
}
}
}
async fn copy_and_verify<S: KvBackend, T: KvBackend>(
source: &S,
target: &T,
options: &TransferOptions,
hide: &[String],
) -> Result<TransferReport, TransferError> {
let limit = options.page_size.max(1);
if !options.allow_existing && target_has_any(target, hide).await? {
return Err(TransferError::TargetNotEmpty);
}
let (mut copied, mut with_ttl, mut skipped) = (0, 0, 0);
let mut kept: HashSet<String> = HashSet::new();
let mut after: Option<String> = None;
loop {
let (entries, next) = read_page(source, after.as_deref(), limit, hide).await?;
for (key, value) in entries {
let ttl = source
.ttl_remaining(&key)
.await
.map_err(|e| kv_err("source", "ttl_remaining", e, hide))?;
if options.allow_existing {
let stored = target
.put_if_absent(&key, value, ttl)
.await
.map_err(|e| kv_err("target", "put_if_absent", e, hide))?;
if !stored {
skipped += 1;
kept.insert(key);
continue;
}
} else {
target
.put(&key, value, ttl)
.await
.map_err(|e| kv_err("target", "put", e, hide))?;
}
copied += 1;
with_ttl += usize::from(ttl.is_some());
}
match next {
Some(cursor) => after = Some(cursor),
None => break,
}
}
let (mut verified, mut missing, mut differing) = (0, 0, 0);
let mut sample: Vec<String> = Vec::new();
let mut note = |key: String| {
if sample.len() < 10 {
sample.push(key);
}
};
let mut after: Option<String> = None;
loop {
let (entries, next) = read_page(source, after.as_deref(), limit, hide).await?;
for (key, value) in entries {
if kept.contains(&key) {
continue;
}
match target
.get(&key)
.await
.map_err(|e| kv_err("target", "get", e, hide))?
{
None => {
missing += 1;
note(key);
}
Some(stored) if stored != value => {
differing += 1;
note(key);
}
Some(_) => verified += 1,
}
}
match next {
Some(cursor) => after = Some(cursor),
None => break,
}
}
let extra = if options.allow_existing {
0
} else {
target_count(target, limit, hide)
.await?
.saturating_sub(verified + differing)
};
if missing + differing + extra > 0 {
return Err(TransferError::Mismatch {
missing,
differing,
extra,
sample,
});
}
Ok(TransferReport {
copied,
with_ttl,
skipped,
verified,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[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::Url(url.to_owned()));
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]
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,
}
#[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,
}
}
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> {
self.module
.transfer_blocking(&self.from, &self.to, &self.needs, &self.options)
}
}