Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ connections to hubs, the volumes in each bucket, and the volumes this
machine uses, each as a plain folder or a mount.

- Add a connection (`a`) with the hub's address and token, then pick one
of its volumes to add, or make a new volume from a folder (`n`).
of its volumes to add, or make a new volume from a folder (`n`). Give a
passphrase when making one to encrypt it, and when adding an encrypted
one; it is not stored (each machine keeps the volume's key instead).
- Each volume syncs in a background service of its own (a systemd user
service on Linux, a launchd agent on a Mac), so it keeps syncing after
the TUI quits and starts again at login. `s` starts or stops it, `l`
Expand Down
2 changes: 1 addition & 1 deletion crates/rustfuse-sync/src/crypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) fn save_key(root: &Path, key: &VolumeKey) -> Result<()> {
}

/// The key, if the directory's volume is encrypted.
pub(crate) fn load_key_if_any(root: &Path) -> Result<Option<Cipher>> {
pub fn load_key_if_any(root: &Path) -> Result<Option<Cipher>> {
if key_path(root).exists() {
load_key(root).map(Some)
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/rustfuse-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! same way (`docs/design.md` §7).

mod apply;
mod crypt;
pub mod crypt;
pub mod fsmonitor;
pub mod gc;
mod git;
Expand Down
48 changes: 42 additions & 6 deletions crates/rustfuse-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,23 @@ impl App {
&self,
args: Vec<String>,
c: Option<&Connection>,
) -> impl FnOnce() -> Result<String> + Send + 'static {
self.tool_job_with(args, c, "")
}

/// Like [`tool_job`](Self::tool_job), giving an encrypted volume's
/// passphrase (if not empty) to the command.
fn tool_job_with(
&self,
args: Vec<String>,
c: Option<&Connection>,
passphrase: &str,
) -> impl FnOnce() -> Result<String> + Send + 'static {
let exe = self.exe.clone();
let env = c.map(|c| self.env_for(c)).unwrap_or_default();
let mut env = c.map(|c| self.env_for(c)).unwrap_or_default();
if !passphrase.is_empty() {
env.insert("RUSTFUSE_PASSPHRASE".into(), passphrase.to_string());
}
move || tool(&exe, &args, &env)
}

Expand Down Expand Up @@ -551,7 +565,8 @@ impl App {
});
bucket.entries = Loaded::Loading;
let dir = bucket.dir();
let job = self.tool_job(library::ls_args(&v.name, &dir, &c), Some(&c));
let synced = library::synced_dir(&v, &self.places.data);
let job = self.tool_job(library::ls_args(&v.name, &dir, &synced, &c), Some(&c));
let key = key.to_string();
self.spawn(
"Reading the bucket",
Expand Down Expand Up @@ -983,6 +998,7 @@ impl App {
&format_size(self.lib.settings.default_cache_size),
"e.g. 20 GB",
),
Field::secret("Passphrase", "only for an encrypted volume"),
],
)
.with_note(MOUNT_NOTE);
Expand All @@ -1001,9 +1017,15 @@ impl App {
vec![
Field::text("Name", "", "letters, digits, . _ -"),
Field::text("Folder", "", "e.g. ~/src/proj"),
Field::secret("Passphrase", "empty: not encrypted"),
Field::secret("Again", ""),
],
)
.with_note("The folder is uploaded as a new volume and keeps syncing in place.");
.with_note(
"The folder is uploaded as a new volume and keeps syncing in place. With a passphrase, \
content and names are encrypted before they leave this machine; every machine needs \
the passphrase to add the volume, and nobody can recover it if it is lost.",
);
self.modal = Some(Modal::Form(
FormFor::CreateVolume {
connection: connection.to_string(),
Expand Down Expand Up @@ -1105,7 +1127,8 @@ impl App {
.connection(&v.connection)
.context("no connection")?
.clone();
let job = self.tool_job(library::get_args(&v.name, path, &dest, &c), Some(&c));
let synced = library::synced_dir(v, &self.places.data);
let job = self.tool_job(library::get_args(&v.name, path, &dest, &synced, &c), Some(&c));
self.spawn(&format!("Downloading {path}"), job, move |app, r| match r {
Ok(_) => app.say(format!("Saved {}", dest.display())),
Err(e) => app.fail(format!("{e:#}")),
Expand Down Expand Up @@ -1241,7 +1264,11 @@ impl App {
};
let dir = library::synced_dir(&v, &self.places.data);
self.places.create()?;
let job = self.tool_job(library::attach_args(&v, &c, &dir), Some(&c));
let job = self.tool_job_with(
library::attach_args(&v, &c, &dir),
Some(&c),
&form.get("Passphrase"),
);
let what = if mode == Mode::Folder {
"Downloading"
} else {
Expand Down Expand Up @@ -1277,7 +1304,16 @@ impl App {
path: Some(dir.to_string_lossy().into()),
cache_size: None,
};
let job = self.tool_job(library::init_args(&name, &c, &dir), Some(&c));
let passphrase = form.get("Passphrase");
if passphrase != form.get("Again") {
bail!("The passphrases differ.");
}
let encrypt = !passphrase.is_empty();
let job = self.tool_job_with(
library::init_args(&name, &c, &dir, encrypt),
Some(&c),
&passphrase,
);
self.spawn(&format!("Uploading {name}"), job, move |app, r| match r {
Ok(_) => app.added(v),
Err(e) => app.fail(format!("Creating {name}: {e:#}")),
Expand Down
45 changes: 35 additions & 10 deletions crates/rustfuse-tui/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,13 @@ pub fn attach_args(v: &VolumeConfig, c: &Connection, dir: &Path) -> Vec<String>
}

/// `rustfuse init`: a new volume `name` from the folder `dir`, which then
/// syncs in place.
pub fn init_args(name: &str, c: &Connection, dir: &Path) -> Vec<String> {
/// syncs in place; `encrypt` seals it with the passphrase given in
/// `RUSTFUSE_PASSPHRASE`.
pub fn init_args(name: &str, c: &Connection, dir: &Path, encrypt: bool) -> Vec<String> {
let mut args = strings(&["init", &dir.to_string_lossy(), "--volume", name]);
if encrypt {
args.push("--encrypt".into());
}
args.extend(connection_args(c));
args
}
Expand All @@ -285,19 +289,36 @@ pub fn volumes_args(c: &Connection) -> Vec<String> {
args
}

/// `rustfuse ls`: a folder of volume `name` as the bucket holds it.
pub fn ls_args(name: &str, path: &str, c: &Connection) -> Vec<String> {
let mut args = strings(&["ls", "--volume", name, "--json"]);
/// `rustfuse ls`: a folder of volume `name` as the bucket holds it. An
/// encrypted volume's key comes from its synced directory `key_from`.
pub fn ls_args(name: &str, path: &str, key_from: &Path, c: &Connection) -> Vec<String> {
let mut args = strings(&[
"ls",
"--volume",
name,
"--json",
"--key-from",
&key_from.to_string_lossy(),
]);
if !path.is_empty() {
args.push(path.to_string());
}
args.extend(connection_args(c));
args
}

/// `rustfuse get`: downloads `path` of volume `name` to `dest`.
pub fn get_args(name: &str, path: &str, dest: &Path, c: &Connection) -> Vec<String> {
let mut args = strings(&["get", "--volume", name, path, &dest.to_string_lossy()]);
/// `rustfuse get`: downloads `path` of volume `name` to `dest`, like
/// [`ls_args`].
pub fn get_args(name: &str, path: &str, dest: &Path, key_from: &Path, c: &Connection) -> Vec<String> {
let mut args = strings(&[
"get",
"--volume",
name,
path,
&dest.to_string_lossy(),
"--key-from",
&key_from.to_string_lossy(),
]);
args.extend(connection_args(c));
args
}
Expand Down Expand Up @@ -464,8 +485,12 @@ mod tests {
);
assert!(!attach_args(&folder, &c, &dir).contains(&"--lazy".to_string()));
assert_eq!(
ls_args("proj", "", &c)[..4],
strings(&["ls", "--volume", "proj", "--json"])
ls_args("proj", "", &dir, &c)[..6],
strings(&["ls", "--volume", "proj", "--json", "--key-from", "/home/me/proj"])
);
assert_eq!(
init_args("proj", &c, &dir, true)[..5],
strings(&["init", "/home/me/proj", "--volume", "proj", "--encrypt"])
);
let env = tool_env(&c, Some("tok".into()), None);
assert_eq!(env["RUSTFUSE_HUB_TOKEN"], "tok");
Expand Down
24 changes: 20 additions & 4 deletions crates/rustfuse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ enum Command {
/// Print JSON (`{"seq": .., "entries": [..]}`).
#[arg(long)]
json: bool,
/// For an encrypted volume: take the key from this synced directory
/// of it instead of asking for the passphrase.
#[arg(long, value_name = "DIR")]
key_from: Option<PathBuf>,
#[command(flatten)]
hub: HubArgs,
#[command(flatten)]
Expand All @@ -192,6 +196,10 @@ enum Command {
path: PathBuf,
/// Where to put it; must not exist yet.
dest: PathBuf,
/// For an encrypted volume: take the key from this synced directory
/// of it instead of asking for the passphrase.
#[arg(long, value_name = "DIR")]
key_from: Option<PathBuf>,
#[command(flatten)]
hub: HubArgs,
#[command(flatten)]
Expand Down Expand Up @@ -478,7 +486,9 @@ fn passphrase(confirm: bool) -> Result<String> {
return Ok(p);
}
if !std::io::IsTerminal::is_terminal(&std::io::stdin()) {
bail!("this volume is encrypted: set RUSTFUSE_PASSPHRASE");
bail!(
"this volume is encrypted: give its passphrase (in RUSTFUSE_PASSPHRASE when not run from a terminal)"
);
}
let p = rpassword::prompt_password("Volume passphrase: ")?;
if p.is_empty() {
Expand Down Expand Up @@ -569,11 +579,15 @@ async fn remote_tree(
hub: &HubArgs,
store: &StoreArgs,
creds: &Credentials,
key_from: Option<&Path>,
) -> Result<rustfuse_sync::remote::RemoteTree> {
let spec = hub.spec()?;
let config = store.config(volume, hub)?;
let s = volume_store(&config, creds, &spec).await?;
let cipher = volume_cipher(&s).await?;
let cipher = match key_from {
Some(dir) => rustfuse_sync::crypt::load_key_if_any(dir)?.map(Arc::new),
None => volume_cipher(&s).await?,
};
Ok(rustfuse_sync::remote::RemoteTree::load(s, cipher).await?)
}

Expand Down Expand Up @@ -1288,8 +1302,9 @@ async fn run(cli: Cli) -> Result<()> {
hub,
store,
creds,
key_from,
} => {
let tree = remote_tree(volume, &hub, &store, &creds).await?;
let tree = remote_tree(volume, &hub, &store, &creds, key_from.as_deref()).await?;
let entries = tree.list(&path.unwrap_or_default())?;
if json {
let out = serde_json::json!({ "seq": tree.seq(), "entries": entries });
Expand All @@ -1314,8 +1329,9 @@ async fn run(cli: Cli) -> Result<()> {
hub,
store,
creds,
key_from,
} => {
let tree = remote_tree(volume, &hub, &store, &creds).await?;
let tree = remote_tree(volume, &hub, &store, &creds, key_from.as_deref()).await?;
let d = tree.download(&path, &dest).await?;
println!(
"Downloaded {} files ({}) and {} folders to {}.",
Expand Down
25 changes: 18 additions & 7 deletions macos/App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ final class AppModel: ObservableObject {

/// Adds volume `name` from connection `c`: downloads it (everything in
/// folder mode, the tree otherwise) and starts syncing it.
func addVolume(name: String, connection c: Connection, mode: Mode, path: String?, cacheSize: UInt64?) async throws {
func addVolume(
name: String, connection c: Connection, mode: Mode, path: String?, cacheSize: UInt64?, passphrase: String = ""
) async throws {
var v = VolumeConfig(
key: library.newKey(for: name), name: name, connection: c.id, mode: mode,
path: path.map { ($0 as NSString).expandingTildeInPath },
Expand All @@ -247,7 +249,7 @@ final class AppModel: ObservableObject {
if mode != .folder {
try FileManager.default.createDirectory(at: dir.deletingLastPathComponent(), withIntermediateDirectories: true)
}
_ = try await runTool(attachArguments(v, c, dir: dir), connection: c)
_ = try await runTool(attachArguments(v, c, dir: dir), connection: c, passphrase: passphrase)
v.path = v.path.map { URL(fileURLWithPath: $0).standardizedFileURL.path }
library.volumes.append(v)
save()
Expand All @@ -256,13 +258,14 @@ final class AppModel: ObservableObject {
}

/// Creates volume `name` from the folder at `path`, which then syncs in
/// place (folder mode).
func createVolume(name: String, connection c: Connection, folder path: String) async throws {
/// place (folder mode); with a passphrase it is encrypted.
func createVolume(name: String, connection c: Connection, folder path: String, passphrase: String = "") async throws {
guard Shared.validName(name) else {
throw failure("Use up to 40 letters, digits, '.', '_' or '-' for the name, not starting with '.'.")
}
let dir = URL(fileURLWithPath: (path as NSString).expandingTildeInPath).standardizedFileURL
_ = try await runTool(initArguments(name: name, c, dir: dir), connection: c)
_ = try await runTool(
initArguments(name: name, c, dir: dir, encrypt: !passphrase.isEmpty), connection: c, passphrase: passphrase)
let v = VolumeConfig(key: library.newKey(for: name), name: name, connection: c.id, mode: .folder, path: dir.path)
library.volumes.append(v)
save()
Expand Down Expand Up @@ -361,13 +364,21 @@ final class AppModel: ObservableObject {
guard let listing = demo.bucket[path] else { throw failure("No folder \(path) in the sample.") }
return listing
}
let out = try await runTool(lsArguments(volume: volume, path: path, c), connection: c)
let out = try await runTool(lsArguments(volume: volume, path: path, keyFrom: try keyDir(c, volume), c), connection: c)
return try JSONDecoder().decode(RemoteListing.self, from: Data(out.utf8))
}

/// Downloads the file or folder `path` of `volume` to `dest`.
func download(_ c: Connection, volume: String, path: String, to dest: URL) async throws {
_ = try await runTool(getArguments(volume: volume, path: path, dest: dest, c), connection: c)
_ = try await runTool(
getArguments(volume: volume, path: path, dest: dest, keyFrom: try keyDir(c, volume), c), connection: c)
}

/// The synced directory of `volume` if it is added here: an encrypted
/// volume's key is read from it.
private func keyDir(_ c: Connection, _ volume: String) throws -> URL? {
guard let v = library.volumes.first(where: { $0.connection == c.id && $0.name == volume }) else { return nil }
return syncedDir(v, container: try Shared.container())
}

/// Asks each volume's daemon how much disk it uses. A daemon that
Expand Down
12 changes: 7 additions & 5 deletions macos/App/Daemon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ func macfuseAvailable() -> Bool {
&& FileManager.default.isExecutableFile(atPath: macfuseTool().path)
}

/// The environment carrying `connection`'s secrets.
func toolEnv(for connection: Connection?) -> [String: String] {
/// The environment carrying `connection`'s secrets (and an encrypted
/// volume's passphrase).
func toolEnv(for connection: Connection?, passphrase: String? = nil) -> [String: String] {
toolEnvironment(
base: ProcessInfo.processInfo.environment,
token: connection.flatMap { Secrets.get(.hubToken, for: $0.id) },
s3Secret: connection.flatMap { Secrets.get(.s3Secret, for: $0.id) },
connection)
connection,
passphrase: passphrase)
}

/// Runs `rustfuse` to completion with `connection`'s secrets; returns its
/// standard output, or throws its output if it failed.
func runTool(_ args: [String], connection: Connection?) async throws -> String {
try await runProcess(rustfuseTool(), args, env: toolEnv(for: connection))
func runTool(_ args: [String], connection: Connection?, passphrase: String? = nil) async throws -> String {
try await runProcess(rustfuseTool(), args, env: toolEnv(for: connection, passphrase: passphrase))
}

/// Runs `tool` to completion; returns its standard output, or throws its
Expand Down
Loading
Loading