public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Max Carrara <m.carrara@proxmox.com>
To: Proxmox Backup Server development discussion
	<pbs-devel@lists.proxmox.com>,
	Filip Schauer <f.schauer@proxmox.com>
Subject: Re: [pbs-devel] [PATCH v4 vma-to-pbs 4/6] Add the ability to provide credentials via files
Date: Wed, 6 Mar 2024 16:49:33 +0100	[thread overview]
Message-ID: <3ba76061-4230-4251-98a0-d4c7c74ccdd7@proxmox.com> (raw)
In-Reply-To: <20240305135645.96347-5-f.schauer@proxmox.com>

On 3/5/24 14:56, Filip Schauer wrote:
> Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
> ---
>  src/main.rs | 31 +++++++++++++++++++++++++++++--
>  1 file changed, 29 insertions(+), 2 deletions(-)
> 
> diff --git a/src/main.rs b/src/main.rs
> index 8d95b11..b58387b 100644
> --- a/src/main.rs
> +++ b/src/main.rs
> @@ -280,6 +280,18 @@ fn main() -> Result<()> {
>                  .help("Encrypt the Backup")
>                  .action(ArgAction::SetTrue),
>          )
> +        .arg(
> +            Arg::new("password_file")
> +                .long("password_file")
> +                .value_name("PASSWORD_FILE")
> +                .help("Password file"),
> +        )
> +        .arg(
> +            Arg::new("key_password_file")
> +                .long("key_password_file")
> +                .value_name("KEY_PASSWORD_FILE")
> +                .help("Key password file"),
> +        )
>          .arg(Arg::new("vma_file"))
>          .get_matches();
>  
> @@ -296,10 +308,25 @@ fn main() -> Result<()> {
>      let encrypt = matches.get_flag("encrypt");
>  
>      let vma_file_path = matches.get_one::<String>("vma_file").unwrap().to_string();
> +    let password_file = matches.get_one::<String>("password_file");
> +
> +    let pbs_password = match password_file {
> +        Some(password_file) => {
> +            std::fs::read_to_string(password_file).expect("Could not read password file")

This here would be better handled by `anyhow::Context` [0].

> +        }
> +        None => String::from_utf8(tty::read_password("Password: ")?)?,
> +    };

The entire block could *maybe* be something like:

let pbs_password = if let Some(filename) = password_file {
    // ...
} else {
    // ...
};

.. as you're only really matching for one variant.

>  
> -    let pbs_password = String::from_utf8(tty::read_password(&"Password: ").unwrap()).unwrap();

The `unwrap()`s here should be replaced with a descriptive `anyhow::Context` [0]
again, especially since it's UI code.

>      let key_password = match keyfile {
> -        Some(_) => Some(String::from_utf8(tty::read_password(&"Key Password: ").unwrap()).unwrap()),

Here as well.

> +        Some(_) => {
> +            let key_password_file = matches.get_one::<String>("key_password_file");
> +
> +            Some(match key_password_file {
> +                Some(key_password_file) => std::fs::read_to_string(key_password_file)
> +                    .expect("Could not read key password file"),

The `expect()` here too.

> +                None => String::from_utf8(tty::read_password("Key Password: ")?)?,
> +            })
> +        }
>          None => None,
>      };

There probably isn't any prettier way to do this, so this whole nested match
is fine IMO.

>  

[0]: https://docs.rs/anyhow/latest/anyhow/trait.Context.html




  reply	other threads:[~2024-03-06 15:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-05 13:56 [pbs-devel] [PATCH v4 vma-to-pbs 0/6] Implement vma-to-pbs tool Filip Schauer
2024-03-05 13:56 ` [pbs-devel] [PATCH v4 vma-to-pbs 1/6] Initial commit Filip Schauer
2024-03-06 15:49   ` Max Carrara
2024-03-05 13:56 ` [pbs-devel] [PATCH v4 vma-to-pbs 2/6] cargo fmt Filip Schauer
2024-03-05 13:56 ` [pbs-devel] [PATCH v4 vma-to-pbs 3/6] bump proxmox-backup-qemu Filip Schauer
2024-03-05 13:56 ` [pbs-devel] [PATCH v4 vma-to-pbs 4/6] Add the ability to provide credentials via files Filip Schauer
2024-03-06 15:49   ` Max Carrara [this message]
2024-03-05 13:56 ` [pbs-devel] [PATCH v4 vma-to-pbs 5/6] Add support for streaming the VMA file via stdin Filip Schauer
2024-03-06 15:49   ` Max Carrara
2024-03-12 14:04     ` Filip Schauer
2024-03-05 13:56 ` [pbs-devel] [PATCH v4 vma-to-pbs 6/6] Add a fallback for the --fingerprint argument Filip Schauer
2024-03-06 15:49 ` [pbs-devel] [PATCH v4 vma-to-pbs 0/6] Implement vma-to-pbs tool Max Carrara
2024-03-06 15:57   ` Wolfgang Bumiller
2024-03-20 14:18 ` Filip Schauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ba76061-4230-4251-98a0-d4c7c74ccdd7@proxmox.com \
    --to=m.carrara@proxmox.com \
    --cc=f.schauer@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal