public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: Proxmox Backup Server development discussion
	<pbs-devel@lists.proxmox.com>,
	Dietmar Maurer <dietmar@proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox-backup 1/2] support more ENV vars to get secret values
Date: Mon, 19 Jul 2021 15:40:25 +0200	[thread overview]
Message-ID: <d5cc878b-92b5-0215-b174-7071acf9cbce@proxmox.com> (raw)
In-Reply-To: <20210719092114.4073945-1-dietmar@proxmox.com>

On 19.07.21 11:21, Dietmar Maurer wrote:
> diff --git a/src/bin/proxmox_client_tools/mod.rs b/src/bin/proxmox_client_tools/mod.rs
> index 77c33ff0..e4dfb859 100644
> --- a/src/bin/proxmox_client_tools/mod.rs
> +++ b/src/bin/proxmox_client_tools/mod.rs

> @@ -34,6 +40,66 @@ pub const CHUNK_SIZE_SCHEMA: Schema = IntegerSchema::new("Chunk size in KB. Must
>      .default(4096)
>      .schema();
>  
> +/// Helper to read secrets from ENV vars. Reads the following VARs:
> +///

"VARs" is bad capitalization.

IMO it would be good to document the precedence order too, as if one environment
defines more that one of the variants below it may not be clear what one will "win"
to the user.

So for above using something like the following could help to avoid confusion when reading
about that helper in rustdoc:

/// Helper to read a secret through a environment variable (ENV).
///
/// Tries the following variable names in order and returns the value it will resolve
/// for the first defined one:



> +/// BASE_NAME => use value from ENV(BASE_NAME)

/// use value from ENV(BASE_NAME) directly as secret

> +/// BASE_NAME_FD => read from specified file descriptor

s/read from/read the secret from the/

> +/// BASE_NAME_FILE => read from specified file name

s/read from/read the secret from the/

> +/// BASE_NAME_CMD => read from specified file name

above comment should be:

/// read the secret from specified command first line of output on stdout 

what about newlines in secrets? I can have them in the "direct" use, where we may not
change that for backward compatibility, but not in the fd/file/command use?

I can get where this comes from, and while it feels a bit inconsistent it can be OK but must
be at least documented a bit more explicitly.

> +///
> +/// Only return the first line when reading from a file or command.
> +pub fn get_secret_from_env(base_name: &str) -> Result<Option<String>, Error> {
> +
> +    match std::env::var(base_name) {
> +        Ok(p) => return Ok(Some(p)),
> +        Err(NotUnicode(_)) => bail!(format!("{} contains bad characters", base_name)),
> +        Err(NotPresent) => {},
> +    };
> +
> +    let firstline = |data: String| -> String {
> +        match data.lines().next() {
> +            Some(line) => line.to_string(),
> +            None => String::new(),
> +        }
> +    };
> +
> +    let env_name = format!("{}_FD", base_name);
> +    match std::env::var(&env_name) {
> +        Ok(fd_str) => {
> +            let fd: i32 = fd_str.parse()
> +                .map_err(|err| format_err!("unable to parse file descriptor in ENV({}): {}", env_name, err))?;
> +            let mut file = unsafe { File::from_raw_fd(fd) };
> +            let mut buffer = String::new();
> +            let _ = file.read_to_string(&mut buffer)?;


Avoiding to read all of the file (which could be rather big in theory)
wouldn't be that much more code, i.e., above line would become

let mut reader = BufReader::new(file);
let _ = reader.read_line(&mut line)?;


> +            return Ok(Some(firstline(buffer)));
> +        }
> +        _ => {}
> +    }
> +
> +    let env_name = format!("{}_FILE", base_name);
> +    match std::env::var(&env_name) {
> +        Ok(filename) => {
> +            let data = proxmox::tools::fs::file_read_string(filename)?;

why not proxmox::tools::fs::file_read_firstline ?

looks OK besides that, and besides a more detailed rust-docs I have no hard feelings for the other things.




  parent reply	other threads:[~2021-07-19 13:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-19  9:21 Dietmar Maurer
2021-07-19  9:21 ` [pbs-devel] [PATCH proxmox-backup 2/2] doc: Document new environment vars to specify " Dietmar Maurer
2021-07-19 13:40 ` Thomas Lamprecht [this message]
2021-07-21  8:47 [pbs-devel] [PATCH proxmox-backup 1/2] support more ENV vars to get " Dietmar Maurer

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=d5cc878b-92b5-0215-b174-7071acf9cbce@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=dietmar@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