Add guide: a browser walk-through that writes and runs a plan #41
No files matched your search
@@ -7,6 +7,17 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- `guide` walks through a move into inbuxa in the browser. It serves a page
|
||||
on 127.0.0.1 that asks for the source (IMAP with a master user, JMAP with
|
||||
an administrator, or Maildir folders), the accounts (pasted, from a CSV,
|
||||
or read from the source) and the inbuxa server. It can create the missing
|
||||
accounts, previews with a dry run, then moves the mail with live progress
|
||||
and runs a final pass. It writes an ordinary plan and runs it with
|
||||
`migrate`, so the same move can be finished from a terminal. Secrets stay
|
||||
in memory, and only the browser given the address, with its random
|
||||
token, gets an answer.
|
||||
|
||||
## [27.1-rc.4]
|
||||
|
||||
The fourth release candidate of the 27.1 series: a whole domain moves with
|
||||
|
||||
@@ -25,6 +25,7 @@ contacts, identities, Sieve scripts and files.
|
||||
inbuxa-migrate export-directory … out a server's accounts, no secrets
|
||||
inbuxa-migrate import-directory … out those accounts, set up on inbuxa
|
||||
inbuxa-migrate migrate --plan plan.json work many accounts, by a plan
|
||||
inbuxa-migrate guide work the same, walked through in the browser
|
||||
|
||||
Between the two sides sits an archive: one SQLite file holding one account.
|
||||
`import` fills it from the old server; `export` writes it into the new one.
|
||||
@@ -52,7 +53,10 @@ list of domains, accounts, groups, lists and aliases to set up again
|
||||
elsewhere, without passwords, and `import-directory` sets them up on an
|
||||
inbuxa server, with a new password for each user. `migrate` moves many
|
||||
accounts with one plan file: the same steps for each account, several at
|
||||
once, resuming where it stopped, with one report at the end. The full
|
||||
once, resuming where it stopped, with one report at the end. `guide`
|
||||
walks through the same move in the browser: it asks where the mail is,
|
||||
which accounts and which inbuxa server, previews, and runs the plan it
|
||||
writes. The full
|
||||
command reference is in [docs/usage.md](docs/usage.md).
|
||||
|
||||
## Sources
|
||||
|
||||
+45
-1
@@ -13,7 +13,7 @@ and how to install it.
|
||||
## Commands and global flags
|
||||
|
||||
```
|
||||
inbuxa-migrate <import|export|export-imap|export-managesieve|export-caldav|export-carddav|export-files|export-directory|import-directory|migrate|inspect> [args...]
|
||||
inbuxa-migrate <import|export|export-imap|export-managesieve|export-caldav|export-carddav|export-files|export-directory|import-directory|migrate|guide|inspect> [args...]
|
||||
```
|
||||
|
||||
Every command takes a few global flags -- verbosity, worker pool size, retry
|
||||
@@ -630,6 +630,50 @@ logs. With `--dry-run`, each step runs with `--dry-run`, nothing is
|
||||
recorded, and the report goes to `dry-run-report.csv`. An export step
|
||||
reads the archive as it is, so on a first dry run it has nothing to show.
|
||||
|
||||
## The guide
|
||||
|
||||
```
|
||||
inbuxa-migrate guide [--port <PORT>] [--no-open] <DIR>
|
||||
```
|
||||
|
||||
Walks through moving a domain, or a few accounts, into inbuxa in the
|
||||
browser. `guide` serves a page on 127.0.0.1 from the binary itself and
|
||||
opens it. The page asks five things in turn:
|
||||
|
||||
1. **Where the mail is now:** an IMAP server, a JMAP server (Stalwart or
|
||||
inbuxa), or Maildir folders on this machine. It also asks for a login
|
||||
that can act for every account. On IMAP that's a master user, such as
|
||||
Dovecot's `{email}*master`. On JMAP it's an administrator, with a
|
||||
password or an API key.
|
||||
2. **Which accounts:** pasted one per line or as a CSV, or read from a
|
||||
JMAP server's account list. The list can be edited before anything runs.
|
||||
3. **Where to:** the inbuxa server and an administrator's API key, and
|
||||
whether to create the accounts inbuxa doesn't have yet. Each new account
|
||||
gets a generated password, written to a file only its owner can read.
|
||||
4. **Preview:** a dry run. It signs in for each account, counts what would
|
||||
move, and creates nothing.
|
||||
5. **Move,** with progress, a report, and a final pass for when the old
|
||||
server stops taking mail.
|
||||
|
||||
The guide writes an ordinary plan, `<DIR>/plan.json`, and runs it with
|
||||
`migrate`, so a move started in the browser can be finished from a
|
||||
terminal. The page shows the command. Everything else goes into `<DIR>`
|
||||
too: the archives, the logs, `work/report.csv`, the passwords file, and the
|
||||
answers so far, in `guide.json`. A guide started again on the same
|
||||
directory picks up where it was.
|
||||
|
||||
- **Only this browser.** The page is served on 127.0.0.1 alone, at an
|
||||
address with a random token that changes each start. A request without
|
||||
the token, for another host name, or from another site's page is
|
||||
refused.
|
||||
- **Secrets stay in memory.** Passwords and API keys never go into
|
||||
`guide.json` or the plan. The runs get them through environment
|
||||
variables, `MIGRATE_SOURCE_SECRET` and `MIGRATE_INBUXA_TOKEN`, as a plan
|
||||
run from a terminal does. After the guide restarts, the page asks for
|
||||
them again.
|
||||
- `--no-open` prints the address instead of opening a browser.
|
||||
`--port` picks the port; by default any free one is used.
|
||||
|
||||
## Inspect
|
||||
|
||||
```
|
||||
|
||||
+32
@@ -16,6 +16,7 @@ use crate::exchange_ews::types::MailboxKind;
|
||||
use crate::exchange_graph::types::{EventBodyFormat, MailboxKind as GraphMailboxKind, Surfaces};
|
||||
use crate::export_directory::ExportDirectoryConfig;
|
||||
use crate::export_files::ExportFilesConfig;
|
||||
use crate::guide::GuideConfig;
|
||||
use crate::import_directory::ImportDirectoryConfig;
|
||||
use crate::inspect::InspectConfig;
|
||||
use crate::jmap::account::AccountSelector;
|
||||
@@ -95,6 +96,10 @@ enum Command {
|
||||
about = "Move many accounts by a plan file: the same steps for each, several at once, resumable"
|
||||
)]
|
||||
Migrate(Box<MigrateArgs>),
|
||||
#[command(
|
||||
about = "Walk through a move to inbuxa in the browser: a page on this machine writes the plan and runs it"
|
||||
)]
|
||||
Guide(Box<GuideArgs>),
|
||||
#[command(about = "Show the contents of a local archive (read-only)")]
|
||||
Inspect(Box<InspectArgs>),
|
||||
}
|
||||
@@ -733,6 +738,26 @@ struct MigrateArgs {
|
||||
work: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
struct GuideArgs {
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "PORT",
|
||||
default_value_t = 0,
|
||||
help = "Port on 127.0.0.1 (default: any free one)"
|
||||
)]
|
||||
port: u16,
|
||||
|
||||
#[arg(long, help = "Print the page's address instead of opening a browser")]
|
||||
no_open: bool,
|
||||
|
||||
#[arg(
|
||||
value_name = "DIR",
|
||||
help = "Where the plan, the archives, the logs and the report go"
|
||||
)]
|
||||
dir: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
struct InspectArgs {
|
||||
#[arg(value_name = "ARCHIVE", help = "Local SQLite archive")]
|
||||
@@ -774,6 +799,7 @@ pub enum Action {
|
||||
ExportDirectory(ExportDirectoryConfig),
|
||||
ImportDirectory(ImportDirectoryConfig),
|
||||
Migrate(MigrateConfig),
|
||||
Guide(GuideConfig),
|
||||
ExportImap(CommonConfig, ExportImapConfig),
|
||||
ExportManageSieve(CommonConfig, ExportManageSieveConfig),
|
||||
ExportDav(CommonConfig, ExportDavConfig),
|
||||
@@ -906,6 +932,12 @@ impl Cli {
|
||||
Command::ExportDirectory(args) => resolve_export_directory(*args),
|
||||
Command::ImportDirectory(args) => resolve_import_directory(*args),
|
||||
Command::Migrate(args) => resolve_migrate(*args),
|
||||
Command::Guide(args) => Ok(Action::Guide(GuideConfig {
|
||||
dir: args.dir,
|
||||
port: args.port,
|
||||
open: !args.no_open,
|
||||
program: std::env::current_exe()?,
|
||||
})),
|
||||
Command::ExportImap(args) => resolve_export_imap(*args),
|
||||
Command::ExportManageSieve(args) => resolve_export_managesieve(*args),
|
||||
Command::ExportCaldav(args) => resolve_export_dav(*args, DavKindArg::Caldav),
|
||||
|
||||
@@ -0,0 +1,459 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! What the page sends, and the plan and directory written from it. The
|
||||
//! plan is an ordinary one: `migrate` runs it, and so can the administrator
|
||||
//! from the command line. Secrets stay out of it, named by the variables
|
||||
//! below, which the guide sets for the runs it starts.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::export_directory::FORMAT_VERSION as DIRECTORY_VERSION;
|
||||
use crate::migrate::plan::{self, FORMAT, FORMAT_VERSION};
|
||||
|
||||
/// The source's password or API key.
|
||||
pub const SOURCE_SECRET_VAR: &str = "MIGRATE_SOURCE_SECRET";
|
||||
/// The inbuxa administrator's API key.
|
||||
pub const TARGET_TOKEN_VAR: &str = "MIGRATE_INBUXA_TOKEN";
|
||||
/// The directory the setup step reads, relative to the guide's directory.
|
||||
pub const DIRECTORY: &str = "directory";
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Default)]
|
||||
#[serde(rename_all = "camelCase", default)]
|
||||
pub struct Form {
|
||||
pub source: Source,
|
||||
pub accounts: Vec<String>,
|
||||
/// Create on inbuxa the accounts it doesn't have yet.
|
||||
pub create: bool,
|
||||
pub target: Target,
|
||||
pub parallel: usize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Copy, Default, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum SourceKind {
|
||||
#[default]
|
||||
Imap,
|
||||
Jmap,
|
||||
Maildir,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Default)]
|
||||
#[serde(rename_all = "camelCase", default)]
|
||||
pub struct Source {
|
||||
pub kind: SourceKind,
|
||||
pub url: String,
|
||||
/// IMAP: the login for each account, such as `{email}*master`. JMAP:
|
||||
/// the administrator, or empty for an API key.
|
||||
pub login: String,
|
||||
/// Maildir: where each account's folder is, such as
|
||||
/// `/var/vmail/{domain}/{local}/Maildir`.
|
||||
pub path: String,
|
||||
/// IMAP: credentials on `imap://` without STARTTLS.
|
||||
pub cleartext: bool,
|
||||
#[serde(skip_serializing)]
|
||||
pub secret: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Default)]
|
||||
#[serde(rename_all = "camelCase", default)]
|
||||
pub struct Target {
|
||||
pub url: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
impl Form {
|
||||
/// The secrets a run needs, by variable.
|
||||
pub fn env(&self) -> Vec<(&'static str, String)> {
|
||||
let mut env = vec![(TARGET_TOKEN_VAR, self.target.token.clone())];
|
||||
if self.source.kind != SourceKind::Maildir {
|
||||
env.push((SOURCE_SECRET_VAR, self.source.secret.clone()));
|
||||
}
|
||||
env
|
||||
}
|
||||
|
||||
/// Fields left empty keep what an earlier form had: the page doesn't
|
||||
/// hold on to secrets across a reload, the guide does.
|
||||
pub fn keep_secrets_from(&mut self, earlier: &Form) {
|
||||
if self.source.secret.is_empty() {
|
||||
self.source.secret.clone_from(&earlier.source.secret);
|
||||
}
|
||||
if self.target.token.is_empty() {
|
||||
self.target.token.clone_from(&earlier.target.token);
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything a plan needs, or what's missing, in the page's words.
|
||||
pub fn check(&self) -> Result<(), String> {
|
||||
let s = &self.source;
|
||||
match s.kind {
|
||||
SourceKind::Imap | SourceKind::Jmap if s.url.trim().is_empty() => {
|
||||
return Err("Give the old server's address.".into());
|
||||
}
|
||||
SourceKind::Imap if !names_account(&s.login) => {
|
||||
return Err(
|
||||
"The login for each account has to name it: {email} or {local}.".into(),
|
||||
);
|
||||
}
|
||||
SourceKind::Maildir if !names_account(&s.path) => {
|
||||
return Err("The Maildir path has to name the account: {email} or {local}.".into());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if s.kind != SourceKind::Maildir && s.secret.is_empty() {
|
||||
return Err("Give the old server's password or API key.".into());
|
||||
}
|
||||
if self.accounts.is_empty() {
|
||||
return Err("List the accounts to move.".into());
|
||||
}
|
||||
if self.target.url.trim().is_empty() || self.target.token.is_empty() {
|
||||
return Err("Give the inbuxa server's address and an administrator's API key.".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The step that reads each account from the source into its archive.
|
||||
fn import_step(&self) -> Value {
|
||||
let s = &self.source;
|
||||
let mut args: Vec<&str> = Vec::new();
|
||||
let mut secrets = json!({});
|
||||
match s.kind {
|
||||
SourceKind::Imap => {
|
||||
args.extend(["import", "imap", "--url", s.url.trim()]);
|
||||
args.extend(["--auth-basic", s.login.trim()]);
|
||||
if s.cleartext {
|
||||
args.push("--allow-cleartext");
|
||||
}
|
||||
secrets = json!({"password": SOURCE_SECRET_VAR});
|
||||
}
|
||||
SourceKind::Jmap => {
|
||||
args.extend(["import", "jmap", "--url", s.url.trim()]);
|
||||
if s.login.trim().is_empty() {
|
||||
args.push("--auth-bearer");
|
||||
secrets = json!({"token": SOURCE_SECRET_VAR});
|
||||
} else {
|
||||
args.extend(["--auth-basic", s.login.trim()]);
|
||||
secrets = json!({"password": SOURCE_SECRET_VAR});
|
||||
}
|
||||
args.extend(["--account-name", "{email}"]);
|
||||
}
|
||||
SourceKind::Maildir => args.extend(["import", "maildir", s.path.trim()]),
|
||||
}
|
||||
args.push("{archive}");
|
||||
json!({"name": "import", "args": args, "secrets": secrets})
|
||||
}
|
||||
|
||||
fn export_step(&self) -> Value {
|
||||
json!({
|
||||
"name": "export",
|
||||
"args": ["export", "--url", self.target.url.trim(), "--auth-bearer",
|
||||
"--account-name", "{email}", "{archive}"],
|
||||
"secrets": {"token": TARGET_TOKEN_VAR},
|
||||
})
|
||||
}
|
||||
|
||||
fn setup(&self, passwords: &str) -> Vec<Value> {
|
||||
if !self.create {
|
||||
return Vec::new();
|
||||
}
|
||||
vec![json!({
|
||||
"name": "accounts",
|
||||
"args": ["import-directory", "--url", self.target.url.trim(), "--auth-bearer",
|
||||
"--passwords", passwords, DIRECTORY],
|
||||
"secrets": {"token": TARGET_TOKEN_VAR},
|
||||
})]
|
||||
}
|
||||
|
||||
/// The plan for the move: set up the accounts if asked, then read each
|
||||
/// one and write it into inbuxa. New passwords go to `passwords`.
|
||||
pub fn plan(&self, passwords: &str) -> Value {
|
||||
json!({
|
||||
"format": FORMAT, "formatVersion": FORMAT_VERSION,
|
||||
"setup": self.setup(passwords),
|
||||
"accounts": self.accounts,
|
||||
"steps": [self.import_step(), self.export_step()],
|
||||
})
|
||||
}
|
||||
|
||||
/// The plan the preview dry-runs. An export reads the archive, which a
|
||||
/// dry run leaves empty, so it would show nothing; the import is where
|
||||
/// a wrong login or a missing folder shows up.
|
||||
pub fn preview_plan(&self) -> Value {
|
||||
json!({
|
||||
"format": FORMAT, "formatVersion": FORMAT_VERSION,
|
||||
"setup": self.setup("passwords.csv"),
|
||||
"accounts": self.accounts,
|
||||
"steps": [self.import_step()],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn names_account(template: &str) -> bool {
|
||||
template.contains("{email}") || template.contains("{local}")
|
||||
}
|
||||
|
||||
/// The addresses in pasted text or a CSV: the first field on each line
|
||||
/// that is one. Blank lines, `#` comments and a header are skipped; a line
|
||||
/// with no usable address is handed back, so the page can show it.
|
||||
pub fn parse_accounts(text: &str) -> (Vec<String>, Vec<String>) {
|
||||
let mut accounts = Vec::new();
|
||||
let mut rejected = Vec::new();
|
||||
for (n, line) in text.lines().enumerate() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.starts_with('#') {
|
||||
continue;
|
||||
}
|
||||
let address = line
|
||||
.split([',', ';', '\t', ' '])
|
||||
.map(|f| f.trim().trim_matches('"'))
|
||||
.find(|f| f.contains('@'));
|
||||
match address.map(plan::account_email) {
|
||||
Some(Ok(email)) => {
|
||||
if !accounts.contains(&email) {
|
||||
accounts.push(email);
|
||||
}
|
||||
}
|
||||
None if n == 0 => {}
|
||||
_ => rejected.push(line.to_owned()),
|
||||
}
|
||||
}
|
||||
(accounts, rejected)
|
||||
}
|
||||
|
||||
/// The users in a directory `export-directory` wrote.
|
||||
pub fn directory_accounts(doc: &Value) -> Vec<String> {
|
||||
doc.get("users")
|
||||
.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|u| u.get("email").and_then(Value::as_str))
|
||||
.filter_map(|e| plan::account_email(e).ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The directory `import-directory` sets up: the source's, when it was
|
||||
/// read, with only the chosen users; and a plain user, and its domain, for
|
||||
/// each chosen account it doesn't have.
|
||||
pub fn directory(source: Option<&Value>, accounts: &[String]) -> Value {
|
||||
let mut doc = source.cloned().unwrap_or_else(|| {
|
||||
json!({
|
||||
"format": "inbuxa-migrate-directory", "formatVersion": DIRECTORY_VERSION,
|
||||
"domains": [], "tenants": [], "roles": [], "groups": [], "lists": [], "users": [],
|
||||
})
|
||||
});
|
||||
let email_of = |v: &Value| {
|
||||
v.get("email")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_ascii_lowercase)
|
||||
};
|
||||
let mut users: Vec<Value> = doc
|
||||
.get("users")
|
||||
.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|u| email_of(u).is_some_and(|e| accounts.contains(&e)))
|
||||
.cloned()
|
||||
.collect();
|
||||
let mut domains: Vec<Value> = doc
|
||||
.get("domains")
|
||||
.and_then(Value::as_array)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
for email in accounts {
|
||||
let Some((local, domain)) = email.split_once('@') else {
|
||||
continue;
|
||||
};
|
||||
if !users.iter().any(|u| email_of(u).as_ref() == Some(email)) {
|
||||
users.push(json!({
|
||||
"email": email, "name": local, "domain": domain, "description": null,
|
||||
"roles": "user", "permissions": null, "aliases": [], "quotas": {},
|
||||
"tenant": null, "locale": null, "timeZone": null, "externalId": null,
|
||||
"groups": [],
|
||||
}));
|
||||
}
|
||||
let known = domains.iter().any(|d| {
|
||||
d.get("name")
|
||||
.and_then(Value::as_str)
|
||||
.is_some_and(|n| n.eq_ignore_ascii_case(domain))
|
||||
});
|
||||
if !known {
|
||||
domains.push(json!({
|
||||
"name": domain, "enabled": true, "description": null, "aliases": [],
|
||||
"catchAllAddress": null, "subAddressing": "enabled", "tenant": null,
|
||||
}));
|
||||
}
|
||||
}
|
||||
doc["users"] = Value::Array(users);
|
||||
doc["domains"] = Value::Array(domains);
|
||||
doc
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn form(kind: SourceKind) -> Form {
|
||||
Form {
|
||||
source: Source {
|
||||
kind,
|
||||
url: "imaps://old.example.org".into(),
|
||||
login: "{email}*master".into(),
|
||||
path: "/var/vmail/{domain}/{local}/Maildir".into(),
|
||||
cleartext: false,
|
||||
secret: "s3cret".into(),
|
||||
},
|
||||
accounts: vec!["[email protected]".into()],
|
||||
create: true,
|
||||
target: Target {
|
||||
url: "https://mail.example.org".into(),
|
||||
token: "t0ken".into(),
|
||||
},
|
||||
parallel: 4,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_imap_plan_sets_up_reads_with_the_master_login_and_exports() {
|
||||
let plan = form(SourceKind::Imap).plan("passwords-1.csv");
|
||||
let text = plan.to_string();
|
||||
assert!(
|
||||
!text.contains("s3cret") && !text.contains("t0ken"),
|
||||
"{text}"
|
||||
);
|
||||
assert_eq!(
|
||||
plan["setup"][0]["args"],
|
||||
json!([
|
||||
"import-directory",
|
||||
"--url",
|
||||
"https://mail.example.org",
|
||||
"--auth-bearer",
|
||||
"--passwords",
|
||||
"passwords-1.csv",
|
||||
"directory"
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
plan["steps"][0]["args"],
|
||||
json!([
|
||||
"import",
|
||||
"imap",
|
||||
"--url",
|
||||
"imaps://old.example.org",
|
||||
"--auth-basic",
|
||||
"{email}*master",
|
||||
"{archive}"
|
||||
])
|
||||
);
|
||||
assert_eq!(plan["steps"][0]["secrets"]["password"], SOURCE_SECRET_VAR);
|
||||
assert_eq!(plan["steps"][1]["args"][5], "{email}");
|
||||
assert_eq!(plan["steps"][1]["secrets"]["token"], TARGET_TOKEN_VAR);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_jmap_source_acts_for_each_account_by_basic_or_by_key() {
|
||||
let mut f = form(SourceKind::Jmap);
|
||||
f.source.login = "admin".into();
|
||||
let step = &f.plan("p.csv")["steps"][0];
|
||||
assert_eq!(step["args"][4], "--auth-basic");
|
||||
assert_eq!(step["args"][6], "--account-name");
|
||||
f.source.login.clear();
|
||||
let step = &f.plan("p.csv")["steps"][0];
|
||||
assert_eq!(step["args"][4], "--auth-bearer");
|
||||
assert_eq!(step["secrets"]["token"], SOURCE_SECRET_VAR);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_preview_has_no_export_and_maildir_needs_no_secret() {
|
||||
let mut f = form(SourceKind::Maildir);
|
||||
f.create = false;
|
||||
f.source.secret.clear();
|
||||
assert_eq!(f.check(), Ok(()));
|
||||
let plan = f.preview_plan();
|
||||
assert_eq!(plan["setup"], json!([]));
|
||||
assert_eq!(plan["steps"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(
|
||||
plan["steps"][0]["args"],
|
||||
json!([
|
||||
"import",
|
||||
"maildir",
|
||||
"/var/vmail/{domain}/{local}/Maildir",
|
||||
"{archive}"
|
||||
])
|
||||
);
|
||||
assert_eq!(f.env(), [(TARGET_TOKEN_VAR, "t0ken".to_owned())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_form_that_cant_make_a_plan_says_why() {
|
||||
let mut f = form(SourceKind::Imap);
|
||||
f.source.login = "master".into();
|
||||
assert!(f.check().unwrap_err().contains("{email}"));
|
||||
let mut f = form(SourceKind::Imap);
|
||||
f.accounts.clear();
|
||||
assert!(f.check().unwrap_err().contains("accounts"));
|
||||
let mut f = form(SourceKind::Jmap);
|
||||
f.source.secret.clear();
|
||||
assert!(f.check().unwrap_err().contains("password"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secrets_are_never_saved_and_an_empty_one_keeps_the_last() {
|
||||
let f = form(SourceKind::Imap);
|
||||
let saved = serde_json::to_string(&f).unwrap();
|
||||
assert!(
|
||||
!saved.contains("s3cret") && !saved.contains("t0ken"),
|
||||
"{saved}"
|
||||
);
|
||||
let mut again: Form = serde_json::from_str(&saved).unwrap();
|
||||
again.keep_secrets_from(&f);
|
||||
assert_eq!(
|
||||
(again.source.secret.as_str(), again.target.token.as_str()),
|
||||
("s3cret", "t0ken")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accounts_come_from_pasted_lines_or_a_csv() {
|
||||
let (ok, bad) = parse_accounts(
|
||||
"email,name\n[email protected],Ann\n\n# left out\n\"[email protected]\";Bob\n[email protected]\nno address here\n/x@y\n",
|
||||
);
|
||||
assert_eq!(ok, ["[email protected]", "[email protected]"]);
|
||||
assert_eq!(bad, ["no address here", "/x@y"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_directory_keeps_the_chosen_users_and_adds_the_rest() {
|
||||
let source = json!({
|
||||
"format": "inbuxa-migrate-directory", "formatVersion": 1,
|
||||
"domains": [{"name": "example.org"}],
|
||||
"groups": [{"email": "[email protected]"}],
|
||||
"users": [{"email": "[email protected]", "name": "ann", "domain": "example.org",
|
||||
"roles": "admin"},
|
||||
{"email": "[email protected]"}],
|
||||
});
|
||||
let accounts = ["[email protected]".to_owned(), "[email protected]".to_owned()];
|
||||
let doc = directory(Some(&source), &accounts);
|
||||
let emails: Vec<_> = doc["users"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|u| u["email"].as_str().unwrap())
|
||||
.collect();
|
||||
assert_eq!(emails, ["[email protected]", "[email protected]"]);
|
||||
assert_eq!(doc["users"][0]["roles"], "admin");
|
||||
assert_eq!(doc["users"][1]["roles"], "user");
|
||||
assert_eq!(doc["domains"][1]["name"], "other.test");
|
||||
assert_eq!(doc["domains"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(doc["groups"], source["groups"]);
|
||||
assert_eq!(directory_accounts(&doc), accounts);
|
||||
|
||||
let fresh = directory(None, &accounts[..1]);
|
||||
assert_eq!(fresh["format"], "inbuxa-migrate-directory");
|
||||
assert_eq!(fresh["domains"][0]["name"], "example.org");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! Just enough HTTP/1.1 for one page and its calls, from one browser on
|
||||
//! this machine: a request per connection, then the connection closes.
|
||||
|
||||
use std::io::{self, BufRead, BufReader, Read, Write};
|
||||
|
||||
const MAX_HEAD: usize = 16 * 1024;
|
||||
const MAX_BODY: usize = 1024 * 1024;
|
||||
|
||||
pub struct Request {
|
||||
pub method: String,
|
||||
pub path: String,
|
||||
headers: Vec<(String, String)>,
|
||||
pub body: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub fn header(&self, name: &str) -> Option<&str> {
|
||||
self.headers
|
||||
.iter()
|
||||
.find(|(n, _)| n.eq_ignore_ascii_case(name))
|
||||
.map(|(_, v)| v.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid(message: &str) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::InvalidData, message.to_owned())
|
||||
}
|
||||
|
||||
pub fn read(stream: impl Read) -> io::Result<Request> {
|
||||
let mut reader = BufReader::new(stream.take((MAX_HEAD + MAX_BODY) as u64));
|
||||
let mut head = 0;
|
||||
let mut line = String::new();
|
||||
let mut next_line = |reader: &mut BufReader<_>, line: &mut String| -> io::Result<()> {
|
||||
line.clear();
|
||||
head += reader.read_line(line)?;
|
||||
if head > MAX_HEAD {
|
||||
return Err(invalid("request head too large"));
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
next_line(&mut reader, &mut line)?;
|
||||
let mut parts = line.split_whitespace();
|
||||
let (Some(method), Some(path), Some(_version)) = (parts.next(), parts.next(), parts.next())
|
||||
else {
|
||||
return Err(invalid("bad request line"));
|
||||
};
|
||||
let (method, path) = (method.to_owned(), path.to_owned());
|
||||
let mut headers = Vec::new();
|
||||
loop {
|
||||
next_line(&mut reader, &mut line)?;
|
||||
let l = line.trim_end_matches(['\r', '\n']);
|
||||
if l.is_empty() {
|
||||
break;
|
||||
}
|
||||
let (name, value) = l.split_once(':').ok_or_else(|| invalid("bad header"))?;
|
||||
headers.push((name.trim().to_owned(), value.trim().to_owned()));
|
||||
}
|
||||
let mut request = Request {
|
||||
method,
|
||||
path,
|
||||
headers,
|
||||
body: Vec::new(),
|
||||
};
|
||||
let length = match request.header("content-length") {
|
||||
Some(v) => v.parse::<usize>().map_err(|_| invalid("bad length"))?,
|
||||
None => 0,
|
||||
};
|
||||
if length > MAX_BODY {
|
||||
return Err(invalid("request body too large"));
|
||||
}
|
||||
request.body = vec![0; length];
|
||||
reader.read_exact(&mut request.body)?;
|
||||
Ok(request)
|
||||
}
|
||||
|
||||
pub struct Response {
|
||||
pub status: u16,
|
||||
pub content_type: &'static str,
|
||||
pub body: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new(status: u16, content_type: &'static str, body: impl Into<Vec<u8>>) -> Self {
|
||||
Self {
|
||||
status,
|
||||
content_type,
|
||||
body: body.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn json(value: &serde_json::Value) -> Self {
|
||||
Self::new(200, "application/json", value.to_string())
|
||||
}
|
||||
|
||||
pub fn error(status: u16, message: &str) -> Self {
|
||||
Self::new(
|
||||
status,
|
||||
"application/json",
|
||||
serde_json::json!({ "error": message }).to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
/// The token is in the page's address, so nothing is cached, nothing
|
||||
/// is sent on as a referrer, and the page loads only from here.
|
||||
pub fn write(&self, mut stream: impl Write) -> io::Result<()> {
|
||||
let reason = match self.status {
|
||||
200 => "OK",
|
||||
400 => "Bad Request",
|
||||
403 => "Forbidden",
|
||||
404 => "Not Found",
|
||||
405 => "Method Not Allowed",
|
||||
409 => "Conflict",
|
||||
415 => "Unsupported Media Type",
|
||||
_ => "Error",
|
||||
};
|
||||
write!(
|
||||
stream,
|
||||
"HTTP/1.1 {} {reason}\r\nContent-Type: {}\r\nContent-Length: {}\r\n\
|
||||
Cache-Control: no-store\r\nReferrer-Policy: no-referrer\r\n\
|
||||
X-Content-Type-Options: nosniff\r\n\
|
||||
Content-Security-Policy: default-src 'self'; frame-ancestors 'none'; \
|
||||
form-action 'none'; base-uri 'none'\r\nConnection: close\r\n\r\n",
|
||||
self.status,
|
||||
self.content_type,
|
||||
self.body.len()
|
||||
)?;
|
||||
stream.write_all(&self.body)?;
|
||||
stream.flush()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_request_is_read_with_its_headers_and_body() {
|
||||
let raw = b"POST /t/api/job HTTP/1.1\r\nHost: 127.0.0.1:1\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n{}";
|
||||
let r = read(&raw[..]).unwrap();
|
||||
assert_eq!((r.method.as_str(), r.path.as_str()), ("POST", "/t/api/job"));
|
||||
assert_eq!(r.header("content-type"), Some("application/json"));
|
||||
assert_eq!(r.body, b"{}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_or_broken_requests_are_refused() {
|
||||
let big = format!(
|
||||
"POST / HTTP/1.1\r\nContent-Length: {}\r\n\r\n",
|
||||
MAX_BODY + 1
|
||||
);
|
||||
assert!(read(big.as_bytes()).is_err());
|
||||
assert!(read(&b"nonsense\r\n\r\n"[..]).is_err());
|
||||
let long = format!("GET / HTTP/1.1\r\nX: {}\r\n\r\n", "a".repeat(MAX_HEAD));
|
||||
assert!(read(long.as_bytes()).is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,637 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! `guide`: the walk-through. A page served from this binary on
|
||||
//! 127.0.0.1 asks where the mail is, which accounts, and which inbuxa
|
||||
//! server, then writes a plan into its directory and has `migrate` preview
|
||||
//! and run it, so it moves exactly what the same plan would from the
|
||||
//! command line. Only the browser given the address, with its random
|
||||
//! token, gets an answer, and secrets stay in memory: the runs get them
|
||||
//! the way a plan's steps do, through variables.
|
||||
|
||||
pub mod form;
|
||||
mod http;
|
||||
|
||||
use std::fs;
|
||||
use std::io::{BufRead, BufReader, Read};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::jmap::http::{Auth, HttpClient, RetryPolicy};
|
||||
use crate::jmap::request::URN_INBUXA;
|
||||
use crate::jmap::session::Session;
|
||||
use crate::migrate::plan::SECRET_VARS;
|
||||
use crate::migrate::private_dir;
|
||||
use crate::net::CertOverride;
|
||||
use form::{Form, SourceKind};
|
||||
use http::{Request, Response};
|
||||
|
||||
const PAGE: &str = include_str!("page/index.html");
|
||||
const SCRIPT: &str = include_str!("page/guide.js");
|
||||
const STYLE: &str = include_str!("page/guide.css");
|
||||
|
||||
/// The non-secret answers, so a guide started again picks up where it was.
|
||||
const SAVED: &str = "guide.json";
|
||||
/// The output kept from a run, newest last.
|
||||
const KEEP_LINES: usize = 400;
|
||||
|
||||
pub struct GuideConfig {
|
||||
/// Where the plan, the archives and the report go.
|
||||
pub dir: PathBuf,
|
||||
/// 0 for any free port.
|
||||
pub port: u16,
|
||||
pub open: bool,
|
||||
/// This binary, which runs each step.
|
||||
pub program: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
Directory,
|
||||
Preview,
|
||||
Run,
|
||||
FinalPass,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
fn parse(s: &str) -> Option<Self> {
|
||||
Some(match s {
|
||||
"directory" => Self::Directory,
|
||||
"preview" => Self::Preview,
|
||||
"run" => Self::Run,
|
||||
"final" => Self::FinalPass,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Directory => "directory",
|
||||
Self::Preview => "preview",
|
||||
Self::Run => "run",
|
||||
Self::FinalPass => "final",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Job {
|
||||
kind: Kind,
|
||||
/// Where a directory run writes.
|
||||
out: Option<PathBuf>,
|
||||
lines: Vec<String>,
|
||||
exit: Option<i32>,
|
||||
result: Value,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
form: Form,
|
||||
/// The source's directory, once read.
|
||||
directory: Option<PathBuf>,
|
||||
job: Option<Job>,
|
||||
}
|
||||
|
||||
struct Guide {
|
||||
dir: PathBuf,
|
||||
token: String,
|
||||
port: u16,
|
||||
program: PathBuf,
|
||||
state: Mutex<State>,
|
||||
}
|
||||
|
||||
pub fn run(config: GuideConfig) -> Result<(), Error> {
|
||||
let dir = std::path::absolute(&config.dir)?;
|
||||
private_dir(&dir)?;
|
||||
let listener = TcpListener::bind(("127.0.0.1", config.port))?;
|
||||
let port = listener.local_addr()?.port();
|
||||
let mut token = [0u8; 16];
|
||||
getrandom::fill(&mut token)
|
||||
.map_err(|e| Error::Io(std::io::Error::other(format!("no random token: {e}"))))?;
|
||||
let token: String = token.iter().map(|b| format!("{b:02x}")).collect();
|
||||
|
||||
let mut state = State::default();
|
||||
if let Ok(text) = fs::read_to_string(dir.join(SAVED))
|
||||
&& let Ok(saved) = serde_json::from_str::<Value>(&text)
|
||||
{
|
||||
state.form = serde_json::from_value(saved["form"].clone()).unwrap_or_default();
|
||||
state.directory = saved["directory"].as_str().map(PathBuf::from);
|
||||
}
|
||||
let guide = Arc::new(Guide {
|
||||
dir,
|
||||
token,
|
||||
port,
|
||||
program: config.program,
|
||||
state: Mutex::new(state),
|
||||
});
|
||||
|
||||
let url = format!("http://127.0.0.1:{port}/{}/", guide.token);
|
||||
println!("The guide is at {url}");
|
||||
println!("Keep this running while you use it. Ctrl-C ends it.");
|
||||
if config.open && !open_browser(&url) {
|
||||
println!("Open that address in a browser on this machine.");
|
||||
}
|
||||
for stream in listener.incoming() {
|
||||
let Ok(stream) = stream else { continue };
|
||||
let guide = Arc::clone(&guide);
|
||||
std::thread::spawn(move || guide.serve(stream));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn open_browser(url: &str) -> bool {
|
||||
#[cfg(target_os = "macos")]
|
||||
let mut command = Command::new("open");
|
||||
#[cfg(windows)]
|
||||
let mut command = {
|
||||
let mut c = Command::new("cmd");
|
||||
c.args(["/C", "start", ""]);
|
||||
c
|
||||
};
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let mut command = Command::new("xdg-open");
|
||||
command
|
||||
.arg(url)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.map(|mut child| std::thread::spawn(move || child.wait()))
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
|
||||
m.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
|
||||
/// Equal, in time that doesn't depend on where they differ.
|
||||
fn same(a: &str, b: &str) -> bool {
|
||||
a.len() == b.len() && a.bytes().zip(b.bytes()).fold(0, |d, (x, y)| d | (x ^ y)) == 0
|
||||
}
|
||||
|
||||
impl Guide {
|
||||
fn serve(self: Arc<Self>, mut stream: TcpStream) {
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(10)));
|
||||
let response = match http::read(&mut stream) {
|
||||
Ok(request) => self.answer(&request),
|
||||
Err(_) => Response::error(400, "bad request"),
|
||||
};
|
||||
let _ = response.write(&mut stream);
|
||||
}
|
||||
|
||||
fn answer(self: &Arc<Self>, r: &Request) -> Response {
|
||||
// A page from anywhere else can't reach this, even by a name that
|
||||
// resolves here: the Host has to be this address.
|
||||
let hosts = [
|
||||
format!("127.0.0.1:{}", self.port),
|
||||
format!("localhost:{}", self.port),
|
||||
];
|
||||
if !r
|
||||
.header("host")
|
||||
.is_some_and(|h| hosts.iter().any(|x| x == h))
|
||||
{
|
||||
return Response::error(403, "wrong host");
|
||||
}
|
||||
let Some(rest) = r
|
||||
.path
|
||||
.strip_prefix('/')
|
||||
.and_then(|p| p.split_once('/'))
|
||||
.filter(|(token, _)| same(token, &self.token))
|
||||
.map(|(_, rest)| rest)
|
||||
else {
|
||||
return Response::error(404, "not found");
|
||||
};
|
||||
if r.method == "POST" {
|
||||
if !r
|
||||
.header("content-type")
|
||||
.is_some_and(|t| t.starts_with("application/json"))
|
||||
{
|
||||
return Response::error(415, "send JSON");
|
||||
}
|
||||
if let Some(origin) = r.header("origin")
|
||||
&& !hosts.iter().any(|h| origin == format!("http://{h}"))
|
||||
{
|
||||
return Response::error(403, "wrong origin");
|
||||
}
|
||||
}
|
||||
let body = || serde_json::from_slice::<Value>(&r.body).unwrap_or(Value::Null);
|
||||
match (r.method.as_str(), rest) {
|
||||
("GET", "") => Response::new(200, "text/html; charset=utf-8", PAGE),
|
||||
("GET", "guide.js") => Response::new(200, "text/javascript; charset=utf-8", SCRIPT),
|
||||
("GET", "guide.css") => Response::new(200, "text/css; charset=utf-8", STYLE),
|
||||
("GET", "api/state") => Response::json(&self.state_json()),
|
||||
("POST", "api/accounts") => {
|
||||
let (accounts, rejected) =
|
||||
form::parse_accounts(body()["text"].as_str().unwrap_or_default());
|
||||
Response::json(&json!({ "accounts": accounts, "rejected": rejected }))
|
||||
}
|
||||
("POST", "api/check-source") => match self.take_form(&body()) {
|
||||
Ok(f) => Response::json(&self.check_source(&f)),
|
||||
Err(e) => Response::error(400, &e),
|
||||
},
|
||||
("POST", "api/check-target") => match self.take_form(&body()) {
|
||||
Ok(f) => Response::json(&check_target(&f)),
|
||||
Err(e) => Response::error(400, &e),
|
||||
},
|
||||
("POST", "api/job") => self.start(&body()),
|
||||
(_, "" | "guide.js" | "guide.css" | "api/state") => {
|
||||
Response::error(405, "method not allowed")
|
||||
}
|
||||
_ => Response::error(404, "not found"),
|
||||
}
|
||||
}
|
||||
|
||||
fn state_json(&self) -> Value {
|
||||
let state = lock(&self.state);
|
||||
let job = state.job.as_ref().map(|j| {
|
||||
json!({
|
||||
"kind": j.kind.name(), "running": j.exit.is_none(), "exit": j.exit,
|
||||
"lines": j.lines, "result": j.result,
|
||||
})
|
||||
});
|
||||
json!({
|
||||
"dir": self.dir.display().to_string(),
|
||||
"form": state.form,
|
||||
"hasSourceSecret": !state.form.source.secret.is_empty(),
|
||||
"hasTargetToken": !state.form.target.token.is_empty(),
|
||||
"directoryRead": state.directory.is_some(),
|
||||
"job": job,
|
||||
"command": self.command_line(&state.form),
|
||||
})
|
||||
}
|
||||
|
||||
/// The same run from a terminal, for the administrator to keep.
|
||||
fn command_line(&self, f: &Form) -> String {
|
||||
let mut vars: Vec<_> = f.env().into_iter().map(|(var, _)| var).collect();
|
||||
vars.sort_unstable();
|
||||
let mut text: String = vars.iter().map(|v| format!("export {v}=…\n")).collect();
|
||||
text += &format!(
|
||||
"inbuxa-migrate migrate --plan {} --parallel {} {}",
|
||||
self.dir.join("plan.json").display(),
|
||||
f.parallel.max(1),
|
||||
self.dir.join("work").display()
|
||||
);
|
||||
text
|
||||
}
|
||||
|
||||
/// The form the page sent, with the secrets it left out kept from the
|
||||
/// last one, and saved without them.
|
||||
fn take_form(&self, body: &Value) -> Result<Form, String> {
|
||||
let mut f: Form = serde_json::from_value(body["form"].clone())
|
||||
.map_err(|e| format!("the form didn't read: {e}"))?;
|
||||
let mut state = lock(&self.state);
|
||||
f.keep_secrets_from(&state.form);
|
||||
if f.parallel == 0 {
|
||||
f.parallel = 4;
|
||||
}
|
||||
state.form = f.clone();
|
||||
let saved = json!({
|
||||
"form": f,
|
||||
"directory": state.directory.as_ref().map(|d| d.display().to_string()),
|
||||
});
|
||||
drop(state);
|
||||
write_private(
|
||||
&self.dir.join(SAVED),
|
||||
&serde_json::to_vec_pretty(&saved).unwrap_or_default(),
|
||||
)
|
||||
.map_err(|e| format!("{SAVED} couldn't be written: {e}"))?;
|
||||
Ok(f)
|
||||
}
|
||||
|
||||
fn check_source(&self, f: &Form) -> Value {
|
||||
let s = &f.source;
|
||||
match s.kind {
|
||||
SourceKind::Jmap => {
|
||||
let auth = if s.login.trim().is_empty() {
|
||||
Auth::Bearer {
|
||||
token: s.secret.clone(),
|
||||
}
|
||||
} else {
|
||||
Auth::Basic {
|
||||
user: s.login.trim().to_owned(),
|
||||
password: s.secret.clone(),
|
||||
}
|
||||
};
|
||||
match discover(auth, &s.url) {
|
||||
Err(e) => json!({ "ok": false, "message": e }),
|
||||
Ok(session) if session.registry_account().is_none() => json!({
|
||||
"ok": false,
|
||||
"message": "This login reaches only its own account. Sign in as an \
|
||||
administrator, who can act for every account.",
|
||||
}),
|
||||
Ok(_) => json!({
|
||||
"ok": true, "directory": true,
|
||||
"message": "Signed in as an administrator: the guide can read the \
|
||||
account list and each account.",
|
||||
}),
|
||||
}
|
||||
}
|
||||
SourceKind::Maildir => {
|
||||
let found = f
|
||||
.accounts
|
||||
.iter()
|
||||
.filter(|a| self.dir.join(fill(&s.path, a)).is_dir())
|
||||
.count();
|
||||
let total = f.accounts.len();
|
||||
json!({
|
||||
"ok": total > 0 && found == total,
|
||||
"message": if total == 0 {
|
||||
"List the accounts, and the guide will look for each one's folder.".to_owned()
|
||||
} else {
|
||||
format!("Found the folder for {found} of {total} accounts.")
|
||||
},
|
||||
})
|
||||
}
|
||||
SourceKind::Imap => json!({
|
||||
"ok": true,
|
||||
"message": "The preview signs in for each account and shows any that fail.",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn start(self: &Arc<Self>, body: &Value) -> Response {
|
||||
let Some(kind) = body["kind"].as_str().and_then(Kind::parse) else {
|
||||
return Response::error(400, "no such run");
|
||||
};
|
||||
if lock(&self.state)
|
||||
.job
|
||||
.as_ref()
|
||||
.is_some_and(|j| j.exit.is_none())
|
||||
{
|
||||
return Response::error(409, "Something is already running; wait for it to end.");
|
||||
}
|
||||
let f = match self.take_form(body) {
|
||||
Ok(f) => f,
|
||||
Err(e) => return Response::error(400, &e),
|
||||
};
|
||||
let prepared = match kind {
|
||||
Kind::Directory => self.directory_run(&f),
|
||||
_ => self.plan_run(&f, kind),
|
||||
};
|
||||
let (args, env, out) = match prepared {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Response::error(400, &e),
|
||||
};
|
||||
let mut command = Command::new(&self.program);
|
||||
command
|
||||
.args(&args)
|
||||
.current_dir(&self.dir)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
for var in SECRET_VARS {
|
||||
command.env_remove(var);
|
||||
}
|
||||
command.envs(env);
|
||||
let mut child = match command.spawn() {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Response::error(400, &format!("couldn't start: {e}")),
|
||||
};
|
||||
lock(&self.state).job = Some(Job {
|
||||
kind,
|
||||
out,
|
||||
lines: vec![format!("inbuxa-migrate {}", args.join(" "))],
|
||||
exit: None,
|
||||
result: Value::Null,
|
||||
});
|
||||
let readers: Vec<_> = [
|
||||
child
|
||||
.stdout
|
||||
.take()
|
||||
.map(|o| Box::new(o) as Box<dyn Read + Send>),
|
||||
child
|
||||
.stderr
|
||||
.take()
|
||||
.map(|e| Box::new(e) as Box<dyn Read + Send>),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|out| {
|
||||
let me = Arc::clone(self);
|
||||
std::thread::spawn(move || {
|
||||
for line in BufReader::new(out).lines().map_while(Result::ok) {
|
||||
if let Some(job) = lock(&me.state).job.as_mut() {
|
||||
job.lines.push(line);
|
||||
let over = job.lines.len().saturating_sub(KEEP_LINES);
|
||||
job.lines.drain(..over);
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let me = Arc::clone(self);
|
||||
std::thread::spawn(move || {
|
||||
for r in readers {
|
||||
let _ = r.join();
|
||||
}
|
||||
let exit = child.wait().ok().and_then(|s| s.code()).unwrap_or(1);
|
||||
let result = me.result_of(kind, exit);
|
||||
if let Some(job) = lock(&me.state).job.as_mut() {
|
||||
job.result = result;
|
||||
job.exit = Some(exit);
|
||||
}
|
||||
});
|
||||
Response::json(&json!({ "started": kind.name() }))
|
||||
}
|
||||
|
||||
/// `export-directory` into a new directory beside the plan.
|
||||
fn directory_run(&self, f: &Form) -> Result<Prepared, String> {
|
||||
let s = &f.source;
|
||||
if s.kind != SourceKind::Jmap || s.url.trim().is_empty() || s.secret.is_empty() {
|
||||
return Err(
|
||||
"Only a JMAP server, signed in as an administrator, has a list to read.".into(),
|
||||
);
|
||||
}
|
||||
let out = format!("source-directory-{}", stamp());
|
||||
let mut args = vec![
|
||||
"export-directory".to_owned(),
|
||||
"--url".into(),
|
||||
s.url.trim().into(),
|
||||
];
|
||||
let var = if s.login.trim().is_empty() {
|
||||
args.push("--auth-bearer".into());
|
||||
"INBUXA_MIGRATE_API_KEY"
|
||||
} else {
|
||||
args.extend(["--auth-basic".into(), s.login.trim().into()]);
|
||||
"INBUXA_MIGRATE_PASSWORD"
|
||||
};
|
||||
args.push(out.clone());
|
||||
Ok((
|
||||
args,
|
||||
vec![(var, s.secret.clone())],
|
||||
Some(self.dir.join(out)),
|
||||
))
|
||||
}
|
||||
|
||||
/// Writes the plan, and the directory it sets up, for `migrate`.
|
||||
fn plan_run(&self, f: &Form, kind: Kind) -> Result<Prepared, String> {
|
||||
f.check()?;
|
||||
let write = |name: &str, value: &Value| {
|
||||
let path = self.dir.join(name);
|
||||
write_private(&path, &serde_json::to_vec_pretty(value).unwrap_or_default())
|
||||
.map_err(|e| format!("{} couldn't be written: {e}", path.display()))
|
||||
};
|
||||
if f.create {
|
||||
let source = lock(&self.state).directory.clone();
|
||||
let source = match source {
|
||||
Some(d) => {
|
||||
let text = fs::read_to_string(d.join("directory.json"))
|
||||
.map_err(|e| format!("{}: {e}", d.display()))?;
|
||||
Some(serde_json::from_str::<Value>(&text).map_err(|e| e.to_string())?)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
private_dir(&self.dir.join(form::DIRECTORY)).map_err(|e| e.to_string())?;
|
||||
write(
|
||||
&format!("{}/directory.json", form::DIRECTORY),
|
||||
&form::directory(source.as_ref(), &f.accounts),
|
||||
)?;
|
||||
}
|
||||
let plan = if kind == Kind::Preview {
|
||||
write("preview-plan.json", &f.preview_plan())?;
|
||||
"preview-plan.json"
|
||||
} else {
|
||||
write("plan.json", &f.plan(&format!("passwords-{}.csv", stamp())))?;
|
||||
"plan.json"
|
||||
};
|
||||
let mut args: Vec<String> = ["migrate", "--plan", plan, "--parallel"]
|
||||
.map(str::to_owned)
|
||||
.into();
|
||||
args.push(f.parallel.max(1).to_string());
|
||||
match kind {
|
||||
Kind::Preview => args.push("--dry-run".into()),
|
||||
Kind::FinalPass => args.push("--again".into()),
|
||||
_ => {}
|
||||
}
|
||||
args.push("work".into());
|
||||
Ok((args, f.env(), None))
|
||||
}
|
||||
|
||||
/// What the page shows once a run ends.
|
||||
fn result_of(&self, kind: Kind, exit: i32) -> Value {
|
||||
if kind == Kind::Directory {
|
||||
let dir = lock(&self.state).job.as_ref().and_then(|j| j.out.clone());
|
||||
let doc = dir.as_ref().and_then(|d| {
|
||||
let text = fs::read_to_string(d.join("directory.json")).ok()?;
|
||||
serde_json::from_str::<Value>(&text).ok()
|
||||
});
|
||||
return match (exit, dir, doc) {
|
||||
(0, Some(dir), Some(doc)) => {
|
||||
lock(&self.state).directory = Some(dir);
|
||||
json!({ "accounts": form::directory_accounts(&doc) })
|
||||
}
|
||||
_ => Value::Null,
|
||||
};
|
||||
}
|
||||
let report = if kind == Kind::Preview {
|
||||
"dry-run-report.csv"
|
||||
} else {
|
||||
"report.csv"
|
||||
};
|
||||
let passwords = if kind == Kind::Preview {
|
||||
None
|
||||
} else {
|
||||
newest_passwords(&self.dir)
|
||||
};
|
||||
json!({
|
||||
"report": fs::read_to_string(self.dir.join("work").join(report)).ok(),
|
||||
"reportPath": self.dir.join("work").join(report).display().to_string(),
|
||||
"passwords": passwords.map(|p| p.display().to_string()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A run's arguments, the secrets it gets, and where a directory run writes.
|
||||
type Prepared = (Vec<String>, Vec<(&'static str, String)>, Option<PathBuf>);
|
||||
|
||||
/// A template with the account's parts filled in.
|
||||
fn fill(template: &str, email: &str) -> String {
|
||||
let (local, domain) = email.split_once('@').unwrap_or((email, ""));
|
||||
template
|
||||
.replace("{email}", email)
|
||||
.replace("{local}", local)
|
||||
.replace("{domain}", domain)
|
||||
}
|
||||
|
||||
fn discover(auth: Auth, url: &str) -> Result<Session, String> {
|
||||
if url.trim().is_empty() {
|
||||
return Err("Give the server's address.".into());
|
||||
}
|
||||
let client = HttpClient::new(auth, RetryPolicy::new(1), CertOverride::none());
|
||||
Session::discover(&client, url.trim()).map_err(|e| format!("Couldn't sign in: {e}"))
|
||||
}
|
||||
|
||||
fn check_target(f: &Form) -> Value {
|
||||
let t = &f.target;
|
||||
if t.token.is_empty() {
|
||||
return json!({ "ok": false, "message": "Give an administrator's API key." });
|
||||
}
|
||||
let auth = Auth::Bearer {
|
||||
token: t.token.clone(),
|
||||
};
|
||||
match discover(auth, &t.url) {
|
||||
Err(e) => json!({ "ok": false, "message": e }),
|
||||
Ok(s) if !s.capabilities.contains_key(URN_INBUXA) => json!({
|
||||
"ok": false, "message": "That server isn't inbuxa.",
|
||||
}),
|
||||
Ok(s) => match s.registry_account() {
|
||||
None => json!({
|
||||
"ok": false,
|
||||
"message": "This key can't act for other accounts. Use an administrator's API key.",
|
||||
}),
|
||||
Some((id, _)) => json!({
|
||||
"ok": true,
|
||||
"message": format!(
|
||||
"Signed in to inbuxa as {}, an administrator.",
|
||||
s.accounts.get(id).map_or("?", |a| a.name.as_str())
|
||||
),
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// The passwords file the last setup wrote, if it wrote one.
|
||||
fn newest_passwords(dir: &Path) -> Option<PathBuf> {
|
||||
fs::read_dir(dir)
|
||||
.ok()?
|
||||
.filter_map(Result::ok)
|
||||
.map(|e| e.path())
|
||||
.filter(|p| {
|
||||
p.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.is_some_and(|n| n.starts_with("passwords-") && n.ends_with(".csv"))
|
||||
})
|
||||
.max()
|
||||
}
|
||||
|
||||
fn stamp() -> String {
|
||||
let now = time::OffsetDateTime::now_utc();
|
||||
format!(
|
||||
"{:04}{:02}{:02}-{:02}{:02}{:02}",
|
||||
now.year(),
|
||||
u8::from(now.month()),
|
||||
now.day(),
|
||||
now.hour(),
|
||||
now.minute(),
|
||||
now.second()
|
||||
)
|
||||
}
|
||||
|
||||
fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
||||
let mut options = fs::OpenOptions::new();
|
||||
options.write(true).create(true).truncate(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
options.mode(0o600);
|
||||
}
|
||||
std::io::Write::write_all(&mut options.open(path)?, bytes)
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
:root {
|
||||
--ink: #1d2330;
|
||||
--muted: #5b6475;
|
||||
--line: #d9dde5;
|
||||
--paper: #ffffff;
|
||||
--page: #f4f5f8;
|
||||
--accent: #3b5bdb;
|
||||
--ok: #2b8a3e;
|
||||
--bad: #c92a2a;
|
||||
--warn: #a35d00;
|
||||
color-scheme: light dark;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--ink: #e6e8ee;
|
||||
--muted: #9aa3b5;
|
||||
--line: #343b4a;
|
||||
--paper: #1b1f29;
|
||||
--page: #12151c;
|
||||
--accent: #7c95f5;
|
||||
--ok: #69db7c;
|
||||
--bad: #ff8787;
|
||||
--warn: #ffc078;
|
||||
}
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
background: var(--page);
|
||||
color: var(--ink);
|
||||
font: 16px/1.5 system-ui, sans-serif;
|
||||
}
|
||||
|
||||
header, main { max-width: 46rem; margin: 0 auto; padding: 0 1rem; }
|
||||
header { padding-top: 2rem; }
|
||||
h1 { margin: 0 0 .25rem; font-size: 1.75rem; }
|
||||
header p { color: var(--muted); margin-top: 0; }
|
||||
|
||||
section {
|
||||
background: var(--paper);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 10px;
|
||||
padding: 1.25rem 1.5rem;
|
||||
margin: 1rem 0;
|
||||
}
|
||||
|
||||
h2 { margin: 0 0 .75rem; font-size: 1.2rem; display: flex; align-items: center; gap: .6rem; }
|
||||
h3 { font-size: 1rem; margin: 1.5rem 0 .25rem; }
|
||||
.n {
|
||||
display: inline-grid; place-items: center;
|
||||
width: 1.7rem; height: 1.7rem; border-radius: 50%;
|
||||
background: var(--accent); color: var(--paper); font-size: .95rem;
|
||||
}
|
||||
|
||||
label { display: block; margin: .75rem 0 .25rem; font-weight: 600; }
|
||||
label.check { font-weight: normal; display: flex; gap: .5rem; align-items: center; }
|
||||
label.short input[type=number] { width: 6rem; }
|
||||
.choices { border: 0; padding: 0; margin: 0 0 .5rem; }
|
||||
.choices label { font-weight: normal; display: flex; gap: .5rem; margin: .3rem 0; }
|
||||
|
||||
input:not([type=radio]):not([type=checkbox]), textarea {
|
||||
display: block; width: 100%; margin-top: .25rem;
|
||||
padding: .5rem .6rem; font: inherit; font-weight: normal; color: inherit;
|
||||
background: var(--page); border: 1px solid var(--line); border-radius: 6px;
|
||||
}
|
||||
textarea { font-family: ui-monospace, monospace; font-size: .9rem; }
|
||||
|
||||
.note { color: var(--muted); font-size: .9rem; margin: .25rem 0 .5rem; }
|
||||
code { font-family: ui-monospace, monospace; font-size: .9em; }
|
||||
|
||||
button {
|
||||
font: inherit; font-weight: 600; cursor: pointer;
|
||||
margin-top: .75rem; padding: .5rem 1.1rem;
|
||||
color: var(--paper); background: var(--accent);
|
||||
border: 1px solid var(--accent); border-radius: 6px;
|
||||
}
|
||||
button.secondary { color: var(--accent); background: transparent; }
|
||||
button:disabled { opacity: .5; cursor: default; }
|
||||
.row { display: flex; gap: .75rem; flex-wrap: wrap; }
|
||||
|
||||
.status { min-height: 1.5em; margin: .5rem 0 0; }
|
||||
.status.ok { color: var(--ok); }
|
||||
.status.bad { color: var(--bad); }
|
||||
|
||||
.job pre, #command {
|
||||
max-height: 16rem; overflow: auto; white-space: pre-wrap;
|
||||
background: var(--page); border: 1px solid var(--line); border-radius: 6px;
|
||||
padding: .5rem .75rem; font-size: .8rem;
|
||||
}
|
||||
.job .summary { font-weight: 600; margin: .75rem 0 .25rem; }
|
||||
|
||||
table { border-collapse: collapse; width: 100%; font-size: .85rem; margin-top: .5rem; }
|
||||
th, td { text-align: left; padding: .3rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
|
||||
td:last-child { overflow-wrap: anywhere; }
|
||||
td.num { text-align: right; font-variant-numeric: tabular-nums; }
|
||||
tr.bad td { color: var(--bad); }
|
||||
tr.warn td { color: var(--warn); }
|
||||
|
||||
details { margin-top: 1rem; }
|
||||
summary { cursor: pointer; color: var(--muted); }
|
||||
.job .summary.bad { color: var(--bad); }
|
||||
@@ -0,0 +1,228 @@
|
||||
// SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
"use strict";
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
let polling = false;
|
||||
|
||||
function kind() {
|
||||
return document.querySelector("input[name=kind]:checked").value;
|
||||
}
|
||||
|
||||
function showKind() {
|
||||
const k = kind();
|
||||
for (const el of document.querySelectorAll("[data-kind]")) {
|
||||
el.hidden = !el.dataset.kind.split(" ").includes(k);
|
||||
}
|
||||
}
|
||||
|
||||
function lines(text) {
|
||||
return text.split("\n").map((l) => l.trim()).filter((l) => l);
|
||||
}
|
||||
|
||||
function form() {
|
||||
const k = kind();
|
||||
return {
|
||||
source: {
|
||||
kind: k,
|
||||
url: $("src-url").value.trim(),
|
||||
login: k === "imap" ? $("src-imap-login").value.trim() : $("src-jmap-login").value.trim(),
|
||||
path: $("src-path").value.trim(),
|
||||
cleartext: $("src-cleartext").checked,
|
||||
secret: $("src-secret").value,
|
||||
},
|
||||
accounts: lines($("accounts").value),
|
||||
create: $("create").checked,
|
||||
target: { url: $("dst-url").value.trim(), token: $("dst-token").value },
|
||||
parallel: Number($("parallel").value) || 4,
|
||||
};
|
||||
}
|
||||
|
||||
async function call(path, body) {
|
||||
const init = body === undefined ? {} : {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
const r = await fetch(path, init);
|
||||
const data = await r.json().catch(() => ({}));
|
||||
if (!r.ok) throw new Error(data.error || `The guide answered ${r.status}.`);
|
||||
return data;
|
||||
}
|
||||
|
||||
function status(id, ok, message) {
|
||||
const el = $(id);
|
||||
el.className = "status " + (ok ? "ok" : "bad");
|
||||
el.textContent = message;
|
||||
}
|
||||
|
||||
// The list as the server reads it: tidied in place, with what it couldn't use.
|
||||
async function checkAccounts() {
|
||||
const r = await call("api/accounts", { text: $("accounts").value });
|
||||
$("accounts").value = r.accounts.join("\n");
|
||||
let message = r.accounts.length === 1 ? "1 account." : `${r.accounts.length} accounts.`;
|
||||
if (r.rejected.length) message += ` Left out, no address: ${r.rejected.join("; ")}`;
|
||||
status("accounts-status", r.accounts.length > 0 && !r.rejected.length, message);
|
||||
return r.accounts;
|
||||
}
|
||||
|
||||
async function check(path, statusId) {
|
||||
try {
|
||||
if (statusId === "source-status" && kind() === "maildir") await checkAccounts();
|
||||
const r = await call(path, { form: form() });
|
||||
status(statusId, r.ok, r.message);
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
status(statusId, false, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function start(jobKind) {
|
||||
const box = jobKind === "directory" ? "accounts-status" : jobKind === "preview" ? "preview-job" : "run-job";
|
||||
try {
|
||||
if (jobKind !== "directory") await checkAccounts();
|
||||
await call("api/job", { kind: jobKind, form: form() });
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
if (box === "accounts-status") status(box, false, e.message);
|
||||
else { $(box).replaceChildren(); $(box).append(para("summary bad", e.message)); }
|
||||
}
|
||||
}
|
||||
|
||||
function para(cls, text) {
|
||||
const p = document.createElement("p");
|
||||
p.className = cls;
|
||||
p.textContent = text;
|
||||
return p;
|
||||
}
|
||||
|
||||
function parseCsv(text) {
|
||||
const rows = [];
|
||||
let row = [], field = "", quoted = false;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const c = text[i];
|
||||
if (quoted) {
|
||||
if (c === '"' && text[i + 1] === '"') { field += '"'; i++; }
|
||||
else if (c === '"') quoted = false;
|
||||
else field += c;
|
||||
} else if (c === '"') quoted = true;
|
||||
else if (c === ",") { row.push(field); field = ""; }
|
||||
else if (c === "\n") { row.push(field); rows.push(row); row = []; field = ""; }
|
||||
else if (c !== "\r") field += c;
|
||||
}
|
||||
if (field || row.length) { row.push(field); rows.push(row); }
|
||||
return rows;
|
||||
}
|
||||
|
||||
function reportTable(csv) {
|
||||
const [head, ...rows] = parseCsv(csv);
|
||||
const show = ["account", "step", "result", "fetched", "created", "updated", "skipped", "failed", "detail"];
|
||||
const at = show.map((name) => head.indexOf(name));
|
||||
const table = document.createElement("table");
|
||||
const tr = table.insertRow();
|
||||
for (const name of show) {
|
||||
const th = document.createElement("th");
|
||||
th.textContent = name;
|
||||
tr.append(th);
|
||||
}
|
||||
for (const r of rows) {
|
||||
const row = table.insertRow();
|
||||
const result = r[head.indexOf("result")];
|
||||
if (result === "failed") row.className = "bad";
|
||||
else if (result === "partial" || result === "not run") row.className = "warn";
|
||||
at.forEach((i, n) => {
|
||||
const td = row.insertCell();
|
||||
td.textContent = r[i] ?? "";
|
||||
if (n >= 3 && n <= 7) td.className = "num";
|
||||
});
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
function summary(job) {
|
||||
const progress = job.lines.map((l) => l.match(/^\[(\d+)\/(\d+)\]/)).filter((m) => m).pop();
|
||||
if (job.running) {
|
||||
return progress ? `Running: ${progress[1]} of ${progress[2]} accounts done.` : "Running…";
|
||||
}
|
||||
const preview = job.kind === "preview";
|
||||
const report = (job.result && job.result.report) || "";
|
||||
if (/^\(setup\),[^,]*,failed,/m.test(report)) {
|
||||
return (preview ? "Setting up the accounts would fail" : "Setting up the accounts failed") +
|
||||
", so no account was " + (preview ? "previewed" : "moved") + ". The table says why.";
|
||||
}
|
||||
if (job.exit === 0) return preview ? "Nothing in the preview failed." : "Every account moved.";
|
||||
if (job.exit === 5) {
|
||||
return preview
|
||||
? "Some accounts would fail. The table says why."
|
||||
: "Some accounts didn't finish. Fix what the table says and start again: it picks up where each stopped.";
|
||||
}
|
||||
return (preview ? "The preview" : "The run") + " stopped. The output says why.";
|
||||
}
|
||||
|
||||
function renderJob(job) {
|
||||
if (job.kind === "directory") {
|
||||
if (job.running) status("accounts-status", true, "Reading the list from the server…");
|
||||
else if (job.result && job.result.accounts) {
|
||||
$("accounts").value = job.result.accounts.join("\n");
|
||||
status("accounts-status", true, `Read ${job.result.accounts.length} accounts. Remove any that shouldn't move.`);
|
||||
} else status("accounts-status", false, "The list couldn't be read: " + (job.lines.slice(-1)[0] || ""));
|
||||
return;
|
||||
}
|
||||
const box = $(job.kind === "preview" ? "preview-job" : "run-job");
|
||||
const pre = document.createElement("pre");
|
||||
pre.textContent = job.lines.slice(-40).join("\n");
|
||||
const ok = job.running || job.exit === 0;
|
||||
box.replaceChildren(para("summary " + (ok ? "" : "bad"), summary(job)), pre);
|
||||
const result = job.result || {};
|
||||
if (result.report) box.append(reportTable(result.report), para("note", "Report: " + result.reportPath));
|
||||
if (result.passwords) box.append(para("note", "The new accounts' passwords are in " + result.passwords));
|
||||
pre.scrollTop = pre.scrollHeight;
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
const s = await call("api/state");
|
||||
$("dir").textContent = s.dir;
|
||||
$("command").textContent = s.command;
|
||||
$("src-secret-kept").hidden = !s.hasSourceSecret;
|
||||
$("dst-token-kept").hidden = !s.hasTargetToken;
|
||||
const running = s.job && s.job.running;
|
||||
for (const id of ["preview", "run", "final", "read-directory"]) $(id).disabled = running;
|
||||
if (s.job) renderJob(s.job);
|
||||
if (running && !polling) {
|
||||
polling = true;
|
||||
setTimeout(() => { polling = false; refresh(); }, 1000);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
const s = await refresh();
|
||||
const f = s.form;
|
||||
if (f.source) {
|
||||
document.querySelector(`input[name=kind][value=${f.source.kind}]`).checked = true;
|
||||
$("src-url").value = f.source.url || "";
|
||||
if (f.source.kind === "imap" && f.source.login) $("src-imap-login").value = f.source.login;
|
||||
if (f.source.kind === "jmap") $("src-jmap-login").value = f.source.login || "";
|
||||
if (f.source.path) $("src-path").value = f.source.path;
|
||||
$("src-cleartext").checked = !!f.source.cleartext;
|
||||
$("accounts").value = (f.accounts || []).join("\n");
|
||||
$("create").checked = f.create || !f.target.url;
|
||||
$("dst-url").value = f.target.url || "";
|
||||
if (f.parallel) $("parallel").value = f.parallel;
|
||||
}
|
||||
showKind();
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
for (const r of document.querySelectorAll("input[name=kind]")) r.addEventListener("change", showKind);
|
||||
$("check-source").addEventListener("click", () => check("api/check-source", "source-status"));
|
||||
$("check-target").addEventListener("click", () => check("api/check-target", "target-status"));
|
||||
$("read-accounts").addEventListener("click", () => checkAccounts().catch((e) => status("accounts-status", false, e.message)));
|
||||
$("read-directory").addEventListener("click", () => start("directory"));
|
||||
$("preview").addEventListener("click", () => start("preview"));
|
||||
$("run").addEventListener("click", () => start("run"));
|
||||
$("final").addEventListener("click", () => start("final"));
|
||||
showKind();
|
||||
load().catch((e) => document.body.prepend(para("status bad", e.message)));
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
-->
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="referrer" content="no-referrer">
|
||||
<title>Move to inbuxa</title>
|
||||
<link rel="stylesheet" href="guide.css">
|
||||
<script src="guide.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Move to inbuxa</h1>
|
||||
<p>Answer each step, preview, then run. Everything is saved in <code id="dir"></code>,
|
||||
and the guide can be closed and started again at any point.</p>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<section id="step-source">
|
||||
<h2><span class="n">1</span> Where is the mail now?</h2>
|
||||
<fieldset class="choices">
|
||||
<label><input type="radio" name="kind" value="imap" checked> An IMAP server</label>
|
||||
<label><input type="radio" name="kind" value="jmap"> A JMAP server (Stalwart or inbuxa)</label>
|
||||
<label><input type="radio" name="kind" value="maildir"> Maildir folders on this machine</label>
|
||||
</fieldset>
|
||||
|
||||
<div data-kind="imap jmap">
|
||||
<label>Server address <input id="src-url" placeholder="imaps://mail.example.org" autocomplete="off"></label>
|
||||
</div>
|
||||
<div data-kind="imap">
|
||||
<label>Login for each account <input id="src-imap-login" value="{email}*master" autocomplete="off"></label>
|
||||
<p class="note">One login that can open every mailbox. Dovecot calls this a master user:
|
||||
the account's address, then <code>*</code>, then the master user's name. Other servers
|
||||
have their own form; <code>{email}</code>, <code>{local}</code> and <code>{domain}</code>
|
||||
stand for each account.</p>
|
||||
<label class="check"><input type="checkbox" id="src-cleartext"> The server has no encryption (a test server)</label>
|
||||
</div>
|
||||
<div data-kind="jmap">
|
||||
<label>Administrator <input id="src-jmap-login" placeholder="admin" autocomplete="off"></label>
|
||||
<p class="note">An administrator can read the account list and act for each account.
|
||||
Leave this empty to sign in with an API key instead of a password.</p>
|
||||
</div>
|
||||
<div data-kind="imap jmap">
|
||||
<label>Password or API key <input id="src-secret" type="password" autocomplete="off"></label>
|
||||
<p class="note" id="src-secret-kept" hidden>Already given; leave empty to keep it.</p>
|
||||
</div>
|
||||
<div data-kind="maildir">
|
||||
<label>Each account's folder <input id="src-path" value="/var/vmail/{domain}/{local}/Maildir" autocomplete="off"></label>
|
||||
<p class="note">Dovecot keeps one Maildir per account. <code>{domain}</code> and
|
||||
<code>{local}</code> are the two halves of the address.</p>
|
||||
</div>
|
||||
<button id="check-source">Check</button>
|
||||
<p class="status" id="source-status"></p>
|
||||
</section>
|
||||
|
||||
<section id="step-accounts">
|
||||
<h2><span class="n">2</span> Which accounts?</h2>
|
||||
<p>One address per line, or paste a CSV: the first address on each line counts.</p>
|
||||
<textarea id="accounts" rows="8" spellcheck="false" placeholder="[email protected] [email protected]"></textarea>
|
||||
<div class="row">
|
||||
<button id="read-accounts">Check the list</button>
|
||||
<button id="read-directory" data-kind="jmap" class="secondary">Read the list from the server</button>
|
||||
</div>
|
||||
<p class="status" id="accounts-status"></p>
|
||||
</section>
|
||||
|
||||
<section id="step-target">
|
||||
<h2><span class="n">3</span> Where to?</h2>
|
||||
<label>inbuxa server <input id="dst-url" placeholder="https://mail.example.org" autocomplete="off"></label>
|
||||
<label>Administrator's API key <input id="dst-token" type="password" autocomplete="off"></label>
|
||||
<p class="note">Make one in INBUXA Admin under your account's API keys. With it the guide
|
||||
writes into each account, so no one has to hand over a password.</p>
|
||||
<p class="note" id="dst-token-kept" hidden>Already given; leave empty to keep it.</p>
|
||||
<label class="check"><input type="checkbox" id="create" checked> Create the accounts inbuxa doesn't have yet</label>
|
||||
<p class="note">Each new account gets a generated password, written to a file only you can read.</p>
|
||||
<label class="short">Accounts at once <input id="parallel" type="number" min="1" max="32" value="4"></label>
|
||||
<button id="check-target">Check</button>
|
||||
<p class="status" id="target-status"></p>
|
||||
</section>
|
||||
|
||||
<section id="step-preview">
|
||||
<h2><span class="n">4</span> Preview</h2>
|
||||
<p>Signs in for each account and counts what it would move, without moving anything.</p>
|
||||
<button id="preview">Preview</button>
|
||||
<div class="job" id="preview-job"></div>
|
||||
</section>
|
||||
|
||||
<section id="step-run">
|
||||
<h2><span class="n">5</span> Move</h2>
|
||||
<p>People can keep working on the old server while this runs. A second start picks up
|
||||
where it stopped.</p>
|
||||
<button id="run">Start</button>
|
||||
<div class="job" id="run-job"></div>
|
||||
<h3>The final pass</h3>
|
||||
<p>Once the old server stops taking mail, run this. It moves only what changed since.</p>
|
||||
<button id="final" class="secondary">Final pass</button>
|
||||
<details>
|
||||
<summary>The same from a terminal</summary>
|
||||
<pre id="command"></pre>
|
||||
</details>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -14,6 +14,7 @@ pub mod exchange_ews;
|
||||
pub mod exchange_graph;
|
||||
pub mod export_directory;
|
||||
pub mod export_files;
|
||||
pub mod guide;
|
||||
pub mod imap;
|
||||
pub mod import_directory;
|
||||
pub mod inspect;
|
||||
|
||||
+7
-1
@@ -10,7 +10,7 @@ use clap::Parser;
|
||||
use inbuxa_migrate::cli::{Action, Cli};
|
||||
use inbuxa_migrate::error::Error;
|
||||
use inbuxa_migrate::sync::{self, RunOutcome, Summary};
|
||||
use inbuxa_migrate::{export_directory, export_files, import_directory, inspect, migrate};
|
||||
use inbuxa_migrate::{export_directory, export_files, guide, import_directory, inspect, migrate};
|
||||
|
||||
fn main() {
|
||||
let code = run();
|
||||
@@ -147,6 +147,12 @@ fn run() -> i32 {
|
||||
Err(err) => fail(&err),
|
||||
};
|
||||
}
|
||||
Action::Guide(config) => {
|
||||
return match guide::run(config) {
|
||||
Ok(()) => 0,
|
||||
Err(err) => fail(&err),
|
||||
};
|
||||
}
|
||||
Action::Inspect(config) => {
|
||||
return match inspect::run(config) {
|
||||
Ok(()) => 0,
|
||||
|
||||
+1
-1
@@ -222,7 +222,7 @@ fn chosen(plan: &Plan, only: &[String]) -> Result<Vec<String>, Error> {
|
||||
}
|
||||
|
||||
/// The work directory holds whole mailboxes: readable by its owner only.
|
||||
fn private_dir(path: &Path) -> Result<(), Error> {
|
||||
pub(crate) fn private_dir(path: &Path) -> Result<(), Error> {
|
||||
if path.is_dir() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
+1
-1
@@ -305,7 +305,7 @@ fn directory_users(path: &Path) -> Result<Vec<String>, Error> {
|
||||
}
|
||||
|
||||
/// An address that is safe as a directory name, lowercased.
|
||||
fn account_email(email: &str) -> Result<String, Error> {
|
||||
pub(crate) fn account_email(email: &str) -> Result<String, Error> {
|
||||
let email = email.trim().to_ascii_lowercase();
|
||||
let ok = match email.split_once('@') {
|
||||
Some((local, domain)) => {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! A `guide` started from the real binary, and requests sent to it the way
|
||||
//! a browser would, raw, so a test can also send what a browser wouldn't.
|
||||
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::TcpStream;
|
||||
use std::path::Path;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
pub struct Guide {
|
||||
child: Child,
|
||||
pub host: String,
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
impl Drop for Guide {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
/// `bin` is the test's `CARGO_BIN_EXE_inbuxa-migrate`.
|
||||
pub fn start(bin: &str, dir: &Path) -> Guide {
|
||||
let mut child = Command::new(bin)
|
||||
.args(["guide", "--no-open"])
|
||||
.arg(dir)
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let mut line = String::new();
|
||||
BufReader::new(child.stdout.take().unwrap())
|
||||
.read_line(&mut line)
|
||||
.unwrap();
|
||||
let url = line
|
||||
.trim()
|
||||
.strip_prefix("The guide is at http://")
|
||||
.unwrap_or_else(|| panic!("{line}"));
|
||||
let (host, token) = url.trim_end_matches('/').split_once('/').unwrap();
|
||||
Guide {
|
||||
host: host.to_owned(),
|
||||
token: token.to_owned(),
|
||||
child,
|
||||
}
|
||||
}
|
||||
|
||||
/// Status, headers and body of one request, sent as given.
|
||||
pub fn send(guide: &Guide, head: &str, body: &str) -> (u16, String, String) {
|
||||
let mut s = TcpStream::connect(&guide.host).unwrap();
|
||||
write!(s, "{head}\r\nContent-Length: {}\r\n\r\n{body}", body.len()).unwrap();
|
||||
let mut out = String::new();
|
||||
s.read_to_string(&mut out).unwrap();
|
||||
let (headers, body) = out.split_once("\r\n\r\n").unwrap();
|
||||
let status = headers[9..12].parse().unwrap();
|
||||
(status, headers.to_owned(), body.to_owned())
|
||||
}
|
||||
|
||||
pub fn get(guide: &Guide, path: &str) -> (u16, String, String) {
|
||||
send(
|
||||
guide,
|
||||
&format!(
|
||||
"GET /{}/{path} HTTP/1.1\r\nHost: {}",
|
||||
guide.token, guide.host
|
||||
),
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
pub fn post(guide: &Guide, path: &str, body: &Value) -> (u16, Value) {
|
||||
let (status, _, text) = send(
|
||||
guide,
|
||||
&format!(
|
||||
"POST /{}/{path} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\nOrigin: http://{}",
|
||||
guide.token, guide.host, guide.host
|
||||
),
|
||||
&body.to_string(),
|
||||
);
|
||||
(status, serde_json::from_str(&text).unwrap())
|
||||
}
|
||||
|
||||
/// The state once the running job, if any, has ended.
|
||||
pub fn settled(guide: &Guide) -> Value {
|
||||
let deadline = Instant::now() + Duration::from_secs(120);
|
||||
loop {
|
||||
let state: Value = serde_json::from_str(&get(guide, "api/state").2).unwrap();
|
||||
if !state["job"]["running"].as_bool().unwrap_or(false) {
|
||||
return state;
|
||||
}
|
||||
assert!(Instant::now() < deadline, "{state}");
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ pub mod data;
|
||||
pub mod dav_client;
|
||||
pub mod dovecot;
|
||||
pub mod error;
|
||||
pub mod guide;
|
||||
pub mod imap_client;
|
||||
pub mod inbuxa;
|
||||
pub mod layouts;
|
||||
|
||||
@@ -14,6 +14,7 @@ use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
use inbuxa_migrate::db;
|
||||
use integration::guide::{post, settled, start};
|
||||
use integration::inbuxa::{ADMIN_EMAIL, ADMIN_PASSWORD, Inbuxa};
|
||||
use serde_json::{Value, json};
|
||||
use tempfile::TempDir;
|
||||
@@ -289,3 +290,80 @@ fn a_second_export_updates_flags_and_sieve_names_are_renamed() {
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires Docker"]
|
||||
fn the_guide_sets_up_the_accounts_previews_and_moves_them() {
|
||||
let server = Inbuxa::start().expect("start inbuxa");
|
||||
let dir = TempDir::new().unwrap();
|
||||
let root = dir.path();
|
||||
message(
|
||||
&root.join("mail/example.org/cat"),
|
||||
"1:2,S",
|
||||
"cat-1",
|
||||
"For Cat",
|
||||
);
|
||||
message(
|
||||
&root.join("mail/other.test/dan"),
|
||||
"1:2,",
|
||||
"dan-1",
|
||||
"For Dan",
|
||||
);
|
||||
let guide = start(BIN, root);
|
||||
let form = json!({
|
||||
"source": {"kind": "maildir", "path": "mail/{domain}/{local}"},
|
||||
"accounts": ["[email protected]", "[email protected]"],
|
||||
"create": true,
|
||||
"target": {"url": server.base_url, "token": server.token},
|
||||
"parallel": 2,
|
||||
});
|
||||
let (_, checked) = post(&guide, "api/check-target", &json!({"form": form}));
|
||||
assert_eq!(checked["ok"], true, "{checked}");
|
||||
let job = |kind: &str| {
|
||||
let (status, body) = post(&guide, "api/job", &json!({"kind": kind, "form": form}));
|
||||
assert_eq!(status, 200, "{body}");
|
||||
let state = settled(&guide);
|
||||
assert_eq!(state["job"]["exit"], 0, "{state}");
|
||||
state["job"]["result"].clone()
|
||||
};
|
||||
|
||||
// The preview creates nothing and reads each account's folder.
|
||||
let preview = job("preview");
|
||||
let report = preview["report"].as_str().unwrap();
|
||||
assert!(report.contains("(setup),accounts,done,"), "{report}");
|
||||
assert!(
|
||||
report.contains("[email protected],import,done,2,2"),
|
||||
"{report}"
|
||||
);
|
||||
assert!(server.account_of("[email protected]", "x").is_err());
|
||||
|
||||
let run = job("run");
|
||||
let passwords =
|
||||
fs::read_to_string(run["passwords"].as_str().expect("a passwords file")).unwrap();
|
||||
let password = |email: &str| {
|
||||
passwords
|
||||
.lines()
|
||||
.find_map(|l| l.strip_prefix(&format!("{email},")))
|
||||
.unwrap_or_else(|| panic!("{email} not in {passwords}"))
|
||||
.to_owned()
|
||||
};
|
||||
assert_eq!(
|
||||
emails(&server, "[email protected]", &password("[email protected]")),
|
||||
[("For Cat".to_owned(), 1, vec!["$seen".to_owned()])]
|
||||
);
|
||||
assert_eq!(
|
||||
emails(&server, "[email protected]", &password("[email protected]")),
|
||||
[("For Dan".to_owned(), 1, Vec::new())]
|
||||
);
|
||||
|
||||
// The final pass finds nothing new.
|
||||
let last = job("final");
|
||||
let report = last["report"].as_str().unwrap();
|
||||
assert!(
|
||||
report
|
||||
.lines()
|
||||
.filter(|r| r.contains(",export,"))
|
||||
.all(|r| r.contains(",export,done,0,0,0,0,")),
|
||||
"{report}"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! `guide` as the browser sees it: the real binary serving its page on
|
||||
//! 127.0.0.1, answering only its own address and token, and running
|
||||
//! `migrate` on the plan it writes. Maildir in, offline; the inbuxa side
|
||||
//! is a port nothing listens on, so the export fails as it should.
|
||||
|
||||
mod integration;
|
||||
|
||||
use std::fs;
|
||||
|
||||
use integration::guide::{get, post, send, settled, start};
|
||||
use serde_json::{Value, json};
|
||||
use tempfile::TempDir;
|
||||
|
||||
const BIN: &str = env!("CARGO_BIN_EXE_inbuxa-migrate");
|
||||
|
||||
#[test]
|
||||
fn the_page_answers_only_its_own_address_and_token() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let guide = start(BIN, dir.path());
|
||||
|
||||
let (status, headers, page) = get(&guide, "");
|
||||
assert_eq!(status, 200);
|
||||
assert!(page.contains("Move to inbuxa"), "{page}");
|
||||
assert!(headers.contains("Content-Security-Policy: default-src 'self'"));
|
||||
assert!(headers.contains("Referrer-Policy: no-referrer"));
|
||||
assert_eq!(get(&guide, "guide.js").0, 200);
|
||||
|
||||
let wrong_token = format!("GET /{}x/ HTTP/1.1\r\nHost: {}", guide.token, guide.host);
|
||||
assert_eq!(send(&guide, &wrong_token, "").0, 404);
|
||||
let port = guide.host.rsplit(':').next().unwrap();
|
||||
let rebound = format!(
|
||||
"GET /{}/ HTTP/1.1\r\nHost: attacker.example:{port}",
|
||||
guide.token
|
||||
);
|
||||
assert_eq!(send(&guide, &rebound, "").0, 403);
|
||||
let form_post = format!(
|
||||
"POST /{}/api/job HTTP/1.1\r\nHost: {}\r\nContent-Type: text/plain",
|
||||
guide.token, guide.host
|
||||
);
|
||||
assert_eq!(send(&guide, &form_post, "{}").0, 415);
|
||||
let elsewhere = format!(
|
||||
"POST /{}/api/job HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\nOrigin: https://attacker.example",
|
||||
guide.token, guide.host
|
||||
);
|
||||
assert_eq!(send(&guide, &elsewhere, "{}").0, 403);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_maildir_move_previews_runs_and_reports() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let root = dir.path();
|
||||
let mail = root.join("mail/ann");
|
||||
for sub in ["cur", "new", "tmp"] {
|
||||
fs::create_dir_all(mail.join(sub)).unwrap();
|
||||
}
|
||||
fs::write(
|
||||
mail.join("cur/1:2,S"),
|
||||
"From: [email protected]\r\nSubject: Hi\r\nMessage-ID: <[email protected]>\r\n\r\nHi\r\n",
|
||||
)
|
||||
.unwrap();
|
||||
let guide = start(BIN, root);
|
||||
|
||||
let (_, parsed) = post(
|
||||
&guide,
|
||||
"api/accounts",
|
||||
&json!({"text": "email\n[email protected]\nnot one\n"}),
|
||||
);
|
||||
assert_eq!(parsed["accounts"], json!(["[email protected]"]));
|
||||
assert_eq!(parsed["rejected"], json!(["not one"]));
|
||||
|
||||
let form = json!({
|
||||
"source": {"kind": "maildir", "path": "mail/{local}"},
|
||||
"accounts": ["[email protected]"],
|
||||
"create": false,
|
||||
"target": {"url": "http://127.0.0.1:9", "token": "t0ken-for-the-test"},
|
||||
"parallel": 2,
|
||||
});
|
||||
let (_, found) = post(&guide, "api/check-source", &json!({"form": form}));
|
||||
assert_eq!(found["ok"], true, "{found}");
|
||||
|
||||
let (status, _) = post(&guide, "api/job", &json!({"kind": "preview", "form": form}));
|
||||
assert_eq!(status, 200);
|
||||
let state = settled(&guide);
|
||||
let report = state["job"]["result"]["report"].as_str().unwrap();
|
||||
assert!(
|
||||
report.contains("[email protected],import,done,2,2"),
|
||||
"{report}"
|
||||
);
|
||||
assert_eq!(state["job"]["exit"], 0, "{state}");
|
||||
|
||||
// The token came once; an empty one keeps it.
|
||||
let mut again = form.clone();
|
||||
again["target"]["token"] = json!("");
|
||||
let (status, _) = post(&guide, "api/job", &json!({"kind": "run", "form": again}));
|
||||
assert_eq!(status, 200);
|
||||
let state = settled(&guide);
|
||||
assert_eq!(state["job"]["exit"], 5, "{state}");
|
||||
let report = state["job"]["result"]["report"].as_str().unwrap();
|
||||
assert!(
|
||||
report.contains("[email protected],import,done,2,2"),
|
||||
"{report}"
|
||||
);
|
||||
assert!(report.contains("[email protected],export,failed"), "{report}");
|
||||
assert!(
|
||||
state["command"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains("export MIGRATE_INBUXA_TOKEN=…"),
|
||||
"{state}"
|
||||
);
|
||||
|
||||
// The plan is an ordinary one, and nothing written holds the token.
|
||||
let plan: Value =
|
||||
serde_json::from_str(&fs::read_to_string(root.join("plan.json")).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
plan["steps"][0]["args"],
|
||||
json!(["import", "maildir", "mail/{local}", "{archive}"])
|
||||
);
|
||||
for name in [
|
||||
"plan.json",
|
||||
"preview-plan.json",
|
||||
"guide.json",
|
||||
"work/report.csv",
|
||||
] {
|
||||
let text = fs::read_to_string(root.join(name)).unwrap();
|
||||
assert!(!text.contains("t0ken-for-the-test"), "{name}: {text}");
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mode = fs::metadata(root.join("guide.json"))
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode();
|
||||
assert_eq!(mode & 0o777, 0o600);
|
||||
}
|
||||
let saved: Value =
|
||||
serde_json::from_str(&fs::read_to_string(root.join("guide.json")).unwrap()).unwrap();
|
||||
assert_eq!(saved["form"]["target"]["url"], "http://127.0.0.1:9");
|
||||
}
|
||||
Reference in new issue
Block a user