src/client/http_client.rs: new password_env option
This commit is contained in:
parent
5030b7cea4
commit
99d863d770
@ -167,6 +167,7 @@ fn connect(server: &str, userid: &str) -> Result<HttpClient, Error> {
|
|||||||
|
|
||||||
let options = HttpClientOptions::new()
|
let options = HttpClientOptions::new()
|
||||||
.prefix(Some("proxmox-backup".to_string()))
|
.prefix(Some("proxmox-backup".to_string()))
|
||||||
|
.password_env(Some("PBS_PASSWORD".to_string()))
|
||||||
.interactive(true)
|
.interactive(true)
|
||||||
.fingerprint_cache(true)
|
.fingerprint_cache(true)
|
||||||
.ticket_cache(true);
|
.ticket_cache(true);
|
||||||
@ -1476,6 +1477,7 @@ async fn try_get(repo: &BackupRepository, url: &str) -> Value {
|
|||||||
|
|
||||||
let options = HttpClientOptions::new()
|
let options = HttpClientOptions::new()
|
||||||
.prefix(Some("proxmox-backup".to_string()))
|
.prefix(Some("proxmox-backup".to_string()))
|
||||||
|
.password_env(Some("PBS_PASSWORD".to_string()))
|
||||||
.interactive(false)
|
.interactive(false)
|
||||||
.fingerprint_cache(true)
|
.fingerprint_cache(true)
|
||||||
.ticket_cache(true);
|
.ticket_cache(true);
|
||||||
|
@ -33,6 +33,7 @@ pub struct AuthInfo {
|
|||||||
pub struct HttpClientOptions {
|
pub struct HttpClientOptions {
|
||||||
prefix: Option<String>,
|
prefix: Option<String>,
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
|
password_env: Option<String>,
|
||||||
fingerprint: Option<String>,
|
fingerprint: Option<String>,
|
||||||
interactive: bool,
|
interactive: bool,
|
||||||
ticket_cache: bool,
|
ticket_cache: bool,
|
||||||
@ -46,6 +47,7 @@ impl HttpClientOptions {
|
|||||||
Self {
|
Self {
|
||||||
prefix: None,
|
prefix: None,
|
||||||
password: None,
|
password: None,
|
||||||
|
password_env: None,
|
||||||
fingerprint: None,
|
fingerprint: None,
|
||||||
interactive: false,
|
interactive: false,
|
||||||
ticket_cache: false,
|
ticket_cache: false,
|
||||||
@ -64,6 +66,11 @@ impl HttpClientOptions {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn password_env(mut self, password_env: Option<String>) -> Self {
|
||||||
|
self.password_env = password_env;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fingerprint(mut self, fingerprint: Option<String>) -> Self {
|
pub fn fingerprint(mut self, fingerprint: Option<String>) -> Self {
|
||||||
self.fingerprint = fingerprint;
|
self.fingerprint = fingerprint;
|
||||||
self
|
self
|
||||||
@ -303,7 +310,7 @@ impl HttpClient {
|
|||||||
if let Some((ticket, _token)) = ticket_info {
|
if let Some((ticket, _token)) = ticket_info {
|
||||||
ticket
|
ticket
|
||||||
} else {
|
} else {
|
||||||
Self::get_password(&username, options.interactive)?
|
Self::get_password(&username, options.interactive, options.password_env.clone())?
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -347,19 +354,22 @@ impl HttpClient {
|
|||||||
(*self.fingerprint.lock().unwrap()).clone()
|
(*self.fingerprint.lock().unwrap()).clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_password(_username: &str, interactive: bool) -> Result<String, Error> {
|
fn get_password(username: &str, interactive: bool, password_env: Option<String>) -> Result<String, Error> {
|
||||||
use std::env::VarError::*;
|
if let Some(password_env) = password_env {
|
||||||
match std::env::var("PBS_PASSWORD") {
|
use std::env::VarError::*;
|
||||||
Ok(p) => return Ok(p),
|
match std::env::var(&password_env) {
|
||||||
Err(NotUnicode(_)) => bail!("PBS_PASSWORD contains bad characters"),
|
Ok(p) => return Ok(p),
|
||||||
Err(NotPresent) => {
|
Err(NotUnicode(_)) => bail!(format!("{} contains bad characters", password_env)),
|
||||||
// Try another method
|
Err(NotPresent) => {
|
||||||
|
// Try another method
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're on a TTY, query the user for a password
|
// If we're on a TTY, query the user for a password
|
||||||
if interactive && tty::stdin_isatty() {
|
if interactive && tty::stdin_isatty() {
|
||||||
return Ok(String::from_utf8(tty::read_password("Password: ")?)?);
|
let msg = format!("Password for \"{}\": ", username);
|
||||||
|
return Ok(String::from_utf8(tty::read_password(&msg)?)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
bail!("no password input mechanism available");
|
bail!("no password input mechanism available");
|
||||||
|
Loading…
Reference in New Issue
Block a user