Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): Add missing key subcommands #1385

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
62 changes: 57 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ rustdoc-args = ["--document-private-items", "--generate-link-to-definition"]

[dependencies]
abscissa_core = { version = "0.8.1", default-features = false, features = ["application"] }
rustic_backend = { version = "0.5.2", features = ["cli"] }
rustic_core = { version = "0.7.2", features = ["cli"] }
rustic_backend = { git = "https://github.com/rustic-rs/rustic_core.git", branch = "more-key-control", features = ["cli"] }
rustic_core = { git = "https://github.com/rustic-rs/rustic_core.git", branch = "more-key-control", features = ["cli"] }

# allocators
jemallocator-global = { version = "0.3.2", optional = true }
Expand Down
182 changes: 157 additions & 25 deletions src/commands/key.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! `key` subcommand

use crate::{repository::CliOpenRepo, status_err, Application, RUSTIC_APP};
use crate::{
helpers::table_with_titles, repository::CliOpenRepo, status_err, Application, RUSTIC_APP,
};

use std::path::PathBuf;

use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
use dialoguer::Password;
use log::info;
use log::{info, warn};

use rustic_core::{CommandInput, KeyOptions, RepositoryOptions};
use rustic_core::{repofile::KeyFile, CommandInput, KeyOptions, RepositoryOptions};

/// `key` subcommand
#[derive(clap::Parser, Command, Debug)]
Expand All @@ -19,14 +21,26 @@ pub(super) struct KeyCmd {
cmd: KeySubCmd,
}

impl Runnable for KeyCmd {
fn run(&self) {
self.cmd.run();
}
}

#[derive(clap::Subcommand, Debug, Runnable)]
enum KeySubCmd {
/// Add a new key to the repository
Add(AddCmd),
/// List all keys in the repository
List(ListCmd),
/// Remove a key from the repository
Remove(RemoveCmd),
/// Change the password of a key
Password(PasswordCmd),
}

#[derive(clap::Parser, Debug)]
pub(crate) struct AddCmd {
pub(crate) struct NewPasswordOptions {
/// New password
#[clap(long)]
pub(crate) new_password: Option<String>,
Expand All @@ -38,19 +52,69 @@ pub(crate) struct AddCmd {
/// Command to get the new password from
#[clap(long)]
pub(crate) new_password_command: Option<CommandInput>,
}

impl NewPasswordOptions {
fn pass(&self, text: &str) -> Result<String> {
// create new Repository options which just contain password information
let mut pass_opts = RepositoryOptions::default();
pass_opts.password = self.new_password.clone();
pass_opts.password_file = self.new_password_file.clone();
pass_opts.password_command = self.new_password_command.clone();

let pass = pass_opts
.evaluate_password()
.map_err(Into::into)
.transpose()
.unwrap_or_else(|| -> Result<_> {
Ok(Password::new()
.with_prompt(text)
.allow_empty_password(true)
.with_confirmation("confirm password", "passwords do not match")
.interact()?)
})?;
Ok(pass)
}
}

#[derive(clap::Parser, Debug)]
pub(crate) struct AddCmd {
/// New password options
#[clap(flatten)]
pub(crate) pass_opts: NewPasswordOptions,

/// Key options
#[clap(flatten)]
pub(crate) key_opts: KeyOptions,
}

impl Runnable for KeyCmd {
impl Runnable for AddCmd {
fn run(&self) {
self.cmd.run();
if let Err(err) = RUSTIC_APP
.config()
.repository
.run_open(|repo| self.inner_run(repo))
{
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}

impl Runnable for AddCmd {
impl AddCmd {
fn inner_run(&self, repo: CliOpenRepo) -> Result<()> {
let pass = self.pass_opts.pass("enter password for new key")?;
let id = repo.add_key(&pass, &self.key_opts)?;
info!("key {id} successfully added.");

Ok(())
}
}

#[derive(clap::Parser, Debug)]
pub(crate) struct ListCmd;

impl Runnable for ListCmd {
fn run(&self) {
if let Err(err) = RUSTIC_APP
.config()
Expand All @@ -63,29 +127,97 @@ impl Runnable for AddCmd {
}
}

impl AddCmd {
impl ListCmd {
fn inner_run(&self, repo: CliOpenRepo) -> Result<()> {
// create new Repository options which just contain password information
let mut pass_opts = RepositoryOptions::default();
pass_opts.password = self.new_password.clone();
pass_opts.password_file = self.new_password_file.clone();
pass_opts.password_command = self.new_password_command.clone();
let used_key = repo.key_id();
let keys = repo
.stream_files()?
.inspect(|f| {
if let Err(err) = f {
warn!("{err:?}");
}
})
.filter_map(Result::ok);

let mut table = table_with_titles(["ID", "User", "Host", "Created"]);
_ = table.add_rows(keys.map(|key: (_, KeyFile)| {
[
format!("{}{}", if used_key == &key.0 { "*" } else { "" }, key.0),
key.1.username.unwrap_or_default(),
key.1.hostname.unwrap_or_default(),
key.1
.created
.map_or(String::new(), |time| format!("{time}")),
]
}));
println!("{table}");
Ok(())
}
}

let pass = pass_opts
.evaluate_password()
.map_err(Into::into)
.transpose()
.unwrap_or_else(|| -> Result<_> {
Ok(Password::new()
.with_prompt("enter password for new key")
.allow_empty_password(true)
.with_confirmation("confirm password", "passwords do not match")
.interact()?)
})?;
#[derive(clap::Parser, Debug)]
pub(crate) struct RemoveCmd {
/// The key is to remove
id: String,
}

let id = repo.add_key(&pass, &self.key_opts)?;
impl Runnable for RemoveCmd {
fn run(&self) {
if let Err(err) = RUSTIC_APP
.config()
.repository
.run_open(|repo| self.inner_run(repo))
{
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}

impl RemoveCmd {
fn inner_run(&self, repo: CliOpenRepo) -> Result<()> {
repo.delete_key(&self.id)?;
info!("key {} successfully removed.", self.id);
Ok(())
}
Comment on lines +178 to +182
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no safe-guarding in place, e.g. a second question as in 'Key {key_id} exists, do you really want to irrecoverably delete this key? (y/N)' with defaulting to No.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - except that repo.delete_key won't delete the actual key used to open the repository. This ensures that always a key is left.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good, and some safe-guarding against locking oneself out. But I think it's still important to add a normal feedback loop for deleting something. We could add a --force delete, so that feedback loop is being jumped over.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with forget and prune we also don't have a feedback loop, so I would find it very irritating to have one here. forget and prune do have --dry-run, but as you specify a key id to remove, I don't see much value in adding a dry-run option here..

Copy link
Contributor

@simonsan simonsan Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, I think it's a bit different with forget and prune, as you also said, and they even have a dry-run option. I think it's just good design, to have a feedback loop when deleting something, and optionally make that circumventable. I would find it pretty weird to not have that, to be honest. The loop itself is also important to give feedback on if the key was found at all and what happens to it. I think the whole key subcommand(s) make a pretty good role model for a tui, actually. Where there would be probably also a feedback loop when deleting.

In general, I would find it counterproductive to let someone delete something important like a key without feedback, even if the key, that accesses the repo is not deletable.
But it can get quite annoying fast, if someone accidentally deletes keys from another device and needs to set that up again.

Copy link
Contributor

@simonsan simonsan Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a confirmation prompt is also as easy as:
https://docs.rs/dialoguer/0.11.0/dialoguer/struct.Confirm.html

    let confirmation = Confirm::new()
        .with_prompt(format!("Are you sure you want to delete key `{key}`?"))
        .default(false)
        .interact()?;

    if confirmation {
        .. delete ..
    } else {
       info!("Deletion aborted, nothing was deleted.")
    }

}
#[derive(clap::Parser, Debug)]
pub(crate) struct PasswordCmd {
/// New password options
#[clap(flatten)]
pub(crate) pass_opts: NewPasswordOptions,
}

impl Runnable for PasswordCmd {
fn run(&self) {
if let Err(err) = RUSTIC_APP
.config()
.repository
.run_open(|repo| self.inner_run(repo))
{
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}

impl PasswordCmd {
fn inner_run(&self, repo: CliOpenRepo) -> Result<()> {
let pass = self.pass_opts.pass("enter new password")?;
let old_key: KeyFile = repo.get_file(repo.key_id())?;
let key_opts = KeyOptions::default()
.hostname(old_key.hostname)
.username(old_key.username)
.with_created(old_key.created.is_some());
let id = repo.add_key(&pass, &key_opts)?;
info!("key {id} successfully added.");

let old_key = *repo.key_id();
// re-open repository using new password
let repo = repo.open_with_password(&pass)?;
repo.delete_key(&old_key.to_string())?;
info!("key {old_key} successfully removed.");

Ok(())
}
}
Loading