Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Complete replacement Rust CLI integration

Manav Rathi2e2871c3eabc
diff --git a/.github/scripts/ci/paths.json b/.github/scripts/ci/paths.json
index 3a4fafd15a..533dfda147 100644
--- a/.github/scripts/ci/paths.json
+++ b/.github/scripts/ci/paths.json
@@ -77,6 +77,10 @@
"mobile/**/pubspec.yaml"
],
"repo-lint": null,
+ "rust-cli-test": [
+ ".github/workflows/rust-cli-test.yml",
+ "rust/apps/cli-next/**"
+ ],
"rust-lint": [".github/workflows/rust-lint.yml", "rust/**"],
"rust-test": [".github/workflows/rust-test.yml", "rust/**", "server/**"],
"server-lint": [".github/workflows/server-lint.yml", "server/**"],
diff --git a/.github/scripts/ci/test.mjs b/.github/scripts/ci/test.mjs
index a691f79584..631f82c9b8 100644
--- a/.github/scripts/ci/test.mjs
+++ b/.github/scripts/ci/test.mjs
@@ -32,6 +32,12 @@ test("shared build inputs select their consumers", (t) => {
"web-lint",
]);
assert.ok(select(t, ["server/pkg/api.go"]).includes("rust-test"));
+ assert.deepEqual(select(t, ["rust/apps/cli-next/src/main.rs"]), [
+ "repo-lint",
+ "rust-cli-test",
+ "rust-lint",
+ "rust-test",
+ ]);
assert.ok(
select(t, ["rust/bindings/napi/src/lib.rs"]).includes("desktop-lint"),
);
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index ff7640da32..ab809d8cb3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -69,6 +69,11 @@ jobs:
if: ${{ contains(fromJSON(needs.select.outputs.jobs), 'repo-lint') }}
uses: ./.github/workflows/repo-lint.yml
+ rust-cli-test:
+ needs: select
+ if: ${{ contains(fromJSON(needs.select.outputs.jobs), 'rust-cli-test') }}
+ uses: ./.github/workflows/rust-cli-test.yml
+
rust-lint:
needs: select
if: ${{ contains(fromJSON(needs.select.outputs.jobs), 'rust-lint') }}
@@ -103,6 +108,7 @@ jobs:
- mobile-lint
- mobile-podfile-lock
- repo-lint
+ - rust-cli-test
- rust-lint
- rust-test
- server-lint
diff --git a/.github/workflows/rust-cli-test.yml b/.github/workflows/rust-cli-test.yml
index 38020bd81f..075c19647d 100644
--- a/.github/workflows/rust-cli-test.yml
+++ b/.github/workflows/rust-cli-test.yml
@@ -1,18 +1,11 @@
name: "Test (Rust CLI)"
on:
- pull_request:
- paths:
- - ".github/workflows/rust-cli-test.yml"
- - "rust/apps/cli-next/**"
+ workflow_call:
permissions:
contents: read
-concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
-
env:
RUSTFLAGS: -D warnings
diff --git a/rust/apps/cli-next/src/api.rs b/rust/apps/cli-next/src/api.rs
index d1f228a596..1d12add272 100644
--- a/rust/apps/cli-next/src/api.rs
+++ b/rust/apps/cli-next/src/api.rs
@@ -57,6 +57,7 @@ pub fn session(account: &Account, product: Product) -> Result<Session> {
config,
account.user_id,
Key::try_from_slice(&account.identity.master_key)?,
+ Key::try_from_slice(&account.identity.recovery_key)?,
SecretKey::try_from_slice(&account.identity.secret_key)?,
)
.map_err(Into::into)
diff --git a/rust/apps/cli-next/src/args.rs b/rust/apps/cli-next/src/args.rs
index 2575748681..2b7c1a382a 100644
--- a/rust/apps/cli-next/src/args.rs
+++ b/rust/apps/cli-next/src/args.rs
@@ -50,8 +50,8 @@ pub enum Command {
#[command(subcommand)]
command: SessionCommand,
},
- #[command(about = "Manage accounts on this device")]
- Account {
+ #[command(about = "Manage accounts on this device", alias = "account")]
+ Accounts {
#[command(subcommand)]
command: AccountCommand,
},
diff --git a/rust/apps/cli-next/src/login.rs b/rust/apps/cli-next/src/login.rs
index 6e4cc17136..cd733bece0 100644
--- a/rust/apps/cli-next/src/login.rs
+++ b/rust/apps/cli-next/src/login.rs
@@ -8,7 +8,7 @@ use anyhow::{Context, Result, ensure};
use dialoguer::{Input, Password, Select, console::Term};
use ente_accounts::{
AuthFlow, AuthFlowUi, DEFAULT_API_ORIGIN, LoginParams, OtpPurpose, SecondFactorMethod,
- TotpPurpose,
+ TotpPurpose, auth,
};
use ente_core::http::{Api, ApiConfig, Http};
use serde::Deserialize;
@@ -28,20 +28,25 @@ pub async fn login(
args: LoginArgs,
selected: Option<&str>,
) -> Result<(State, usize)> {
- let requested_origin = match selected {
- Some(_) => None,
- None => Some(normalize_host(
+ enum Target<'a> {
+ Existing(&'a str),
+ New(String),
+ }
+
+ let target = match selected {
+ Some(name) => Target::Existing(name),
+ None => Target::New(normalize_host(
args.host.as_deref().unwrap_or(DEFAULT_API_ORIGIN),
)?),
};
let credentials = args.input.as_deref().map(Credentials::read).transpose()?;
let vault = Vault::open()?;
- let (expected, origin) = match selected {
- Some(name) => {
+ let (expected, origin) = match target {
+ Target::Existing(name) => {
let account = &vault.state.accounts[vault.state.named(name)?];
(Some(account.storage_id), account.origin.clone())
}
- None => (None, requested_origin.unwrap()),
+ Target::New(origin) => (None, origin),
};
let (snapshot, access) = vault.release();
let mut config = ApiConfig::new(origin.clone());
@@ -107,8 +112,15 @@ pub async fn login(
"authenticated identity does not match --account"
);
}
+ let recovery_key = Zeroizing::new(
+ authenticated
+ .recovery_key
+ .take()
+ .context("account has no recovery key")?,
+ );
let identity = AccountKeys {
master_key: std::mem::take(&mut authenticated.secrets.master_key),
+ recovery_key: auth::recovery_key_from_mnemonic_or_hex(&recovery_key)?.into_vec(),
secret_key: std::mem::take(&mut authenticated.secrets.secret_key),
};
let session = StoredSession {
@@ -117,7 +129,7 @@ pub async fn login(
let index = if let Some(index) = existing {
ensure!(
!supplied_name,
- "account already exists; use account rename to change its local name"
+ "account already exists; use accounts rename to change its local name"
);
let account = &mut vault.state.accounts[index];
account.email = email;
@@ -179,7 +191,10 @@ fn normalize_host(host: &str) -> Result<String> {
.is_ok_and(|ip| ip.is_loopback())
})
{
- url.set_scheme("http").expect("HTTP is a valid URL scheme");
+ ensure!(
+ url.set_scheme("http").is_ok(),
+ "host must be an HTTP or HTTPS origin"
+ );
}
ensure!(
matches!(url.scheme(), "http" | "https") && url.host_str().is_some(),
diff --git a/rust/apps/cli-next/src/main.rs b/rust/apps/cli-next/src/main.rs
index b81b115187..8be29a3513 100644
--- a/rust/apps/cli-next/src/main.rs
+++ b/rust/apps/cli-next/src/main.rs
@@ -52,7 +52,7 @@ async fn run(cli: Cli) -> Result<()> {
command: AlbumCommand::List,
},
} => album_list(selector.account.as_deref(), json).await,
- Command::Account { command } => account_command(command, json).await,
+ Command::Accounts { command } => account_command(command, json).await,
Command::Vault {
command:
VaultCommand::Key {
diff --git a/rust/apps/cli-next/src/output.rs b/rust/apps/cli-next/src/output.rs
index 99a6ed0e81..3d55deec5e 100644
--- a/rust/apps/cli-next/src/output.rs
+++ b/rust/apps/cli-next/src/output.rs
@@ -122,7 +122,7 @@ pub fn account(account: &AccountView<'_>) -> Result<()> {
.iter()
.map(|(name, _)| measure_text_width(name))
.max()
- .unwrap();
+ .unwrap_or(0);
let mut stdout = std::io::stdout().lock();
for (name, value) in fields {
writeln!(
@@ -184,7 +184,7 @@ fn write_table<const N: usize>(
.chain(rows)
.map(|row| measure_text_width(&row[column]))
.max()
- .unwrap()
+ .unwrap_or(0)
});
for row in std::iter::once(&header).chain(rows) {
for (column, cell) in row.iter().enumerate() {
diff --git a/rust/apps/cli-next/src/vault.rs b/rust/apps/cli-next/src/vault.rs
index 68ac908c1b..843744f233 100644
--- a/rust/apps/cli-next/src/vault.rs
+++ b/rust/apps/cli-next/src/vault.rs
@@ -64,6 +64,7 @@ pub struct Account {
#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
pub struct AccountKeys {
pub master_key: Vec<u8>,
+ pub recovery_key: Vec<u8>,
pub secret_key: Vec<u8>,
}
@@ -170,7 +171,7 @@ impl State {
self.accounts
.iter()
.position(|a| a.name == name)
- .with_context(|| format!("no account named {name:?}; use account list"))
+ .with_context(|| format!("no account named {name:?}; use accounts list"))
}
pub fn resolve(&self, name: Option<&str>) -> Result<usize> {
@@ -187,7 +188,7 @@ impl State {
match self.accounts.len() {
0 => bail!("no accounts; use photos login, locker login, or auth login"),
1 => Ok(0),
- _ => bail!("no account selected; use account switch <name> or --account <name>"),
+ _ => bail!("no account selected; use accounts switch <name> or --account <name>"),
}
}
diff --git a/rust/apps/cli-next/tests/cli.rs b/rust/apps/cli-next/tests/cli.rs
index 97d928b24c..bc4e5d5c11 100644
--- a/rust/apps/cli-next/tests/cli.rs
+++ b/rust/apps/cli-next/tests/cli.rs
@@ -1,3 +1,5 @@
+#![cfg(test)]
+
use std::{
fs,
io::Write,
@@ -20,14 +22,14 @@ fn reads_do_not_initialize_storage() {
fs::create_dir(&path).unwrap();
}
let output = home
- .command(&["account", "list"])
+ .command(&["accounts", "list"])
.env("ENTE_CLI_HOME", &path)
.output()
.unwrap();
assert_eq!(success(output).stdout, b"No accounts on this device.\n");
for args in [
- vec!["account", "list", "--json"],
- vec!["account", "view", "missing"],
+ vec!["accounts", "list", "--json"],
+ vec!["accounts", "view", "missing"],
vec!["photos", "album", "list"],
vec!["photos", "api", "/users/details/v2"],
] {
@@ -662,7 +664,10 @@ impl TestHome {
"accounts": [{
"storage_id": id, "name": "fixture", "email": "[email protected]",
"origin": origin, "user_id": 9007199254740993i64,
- "identity": {"master_key": vec![0u8; 32], "secret_key": vec![0u8; 32]},
+ "identity": {
+ "master_key": vec![0u8; 32], "recovery_key": vec![0u8; 32],
+ "secret_key": vec![0u8; 32]
+ },
"sessions": {"photos": {"token": [0xfb, 0xef]}}
}]
}));
diff --git a/rust/apps/cli-next/tests/support/museum.rs b/rust/apps/cli-next/tests/support/museum.rs
index 19deb2df0d..15b478ab3b 100644
--- a/rust/apps/cli-next/tests/support/museum.rs
+++ b/rust/apps/cli-next/tests/support/museum.rs
@@ -139,11 +139,11 @@ async fn exercise(origin: String) -> TestResult {
let alice = create_account(&origin, &alice_email).await;
let alice_id = alice.user_id.to_string();
let bob = create_account(&origin, &bob_email).await;
- let (album, key) = create_album(&origin, &alice, "Monsoon 🌧", "folder", false).await;
- let (archive, archive_key) = create_album(&origin, &alice, "Archive", "album", true).await;
- let (hidden, hidden_key) = create_album(&origin, &alice, "Private album", "album", false).await;
+ let (album, key) = create_album(&origin, &alice, "Monsoon 🌧", "folder").await;
+ let (archive, archive_key) = create_album(&origin, &alice, "Archive", "album").await;
+ let (hidden, hidden_key) = create_album(&origin, &alice, "Private album", "album").await;
let (default_hidden, default_hidden_key) =
- create_album(&origin, &alice, "Default hidden", "album", false).await;
+ create_album(&origin, &alice, "Default hidden", "album").await;
set_album_metadata(
&origin,
&alice,
@@ -171,7 +171,7 @@ async fn exercise(origin: String) -> TestResult {
json!({"subType": 1}),
)
.await;
- let (bob_album, _) = create_album(&origin, &bob, "Bob's album", "album", false).await;
+ let (bob_album, _) = create_album(&origin, &bob, "Bob's album", "album").await;
let shared_key = sealed::seal(
key.as_bytes(),
&PublicKey::try_from_slice(&bob.secrets.public_key).unwrap(),
@@ -416,7 +416,6 @@ async fn create_album(
owner: &AuthenticatedAccount,
name: &str,
kind: &str,
- legacy: bool,
) -> (i64, Key) {
let key = Key::generate();
let wrapped = secretbox::encrypt(
@@ -428,7 +427,7 @@ async fn create_album(
.header("x-auth-token", b64::encode_url_safe(&owner.secrets.token)).header("x-client-package", "io.ente.photos")
.json(&json!({
"encryptedKey": b64::encode(&wrapped.encrypted_data), "keyDecryptionNonce": b64::encode(wrapped.nonce.as_bytes()),
- "name": if legacy { name } else { "" }, "encryptedName": b64::encode(&encrypted_name.encrypted_data),
+ "name": "", "encryptedName": b64::encode(&encrypted_name.encrypted_data),
"nameDecryptionNonce": b64::encode(encrypted_name.nonce.as_bytes()), "type": kind, "attributes": {"version": 1}
})).send().await.unwrap().error_for_status().unwrap().json().await.unwrap();
(response["collection"]["id"].as_i64().unwrap(), key)