derive/impl and use Default for some structs
and revamp HttpClientOptions with two constructors for the common use cases Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
		
				
					committed by
					
						
						Wolfgang Bumiller
					
				
			
			
				
	
			
			
			
						parent
						
							f4e52bb27d
						
					
				
				
					commit
					93e3581ce7
				
			@ -28,7 +28,7 @@ async fn run() -> Result<(), Error> {
 | 
			
		||||
 | 
			
		||||
    let auth_id = Authid::root_auth_id();
 | 
			
		||||
 | 
			
		||||
    let options = HttpClientOptions::new()
 | 
			
		||||
    let options = HttpClientOptions::default()
 | 
			
		||||
        .interactive(true)
 | 
			
		||||
        .ticket_cache(true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ async fn upload_speed() -> Result<f64, Error> {
 | 
			
		||||
 | 
			
		||||
    let auth_id = Authid::root_auth_id();
 | 
			
		||||
 | 
			
		||||
    let options = HttpClientOptions::new()
 | 
			
		||||
    let options = HttpClientOptions::default()
 | 
			
		||||
        .interactive(true)
 | 
			
		||||
        .ticket_cache(true);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -310,9 +310,7 @@ pub fn delete_remote(name: String, digest: Option<String>) -> Result<(), Error>
 | 
			
		||||
 | 
			
		||||
/// Helper to get client for remote.cfg entry
 | 
			
		||||
pub async fn remote_client(remote: remote::Remote) -> Result<HttpClient, Error> {
 | 
			
		||||
    let options = HttpClientOptions::new()
 | 
			
		||||
        .password(Some(remote.password.clone()))
 | 
			
		||||
        .fingerprint(remote.fingerprint.clone());
 | 
			
		||||
    let options = HttpClientOptions::new_non_interactive(remote.password.clone(), remote.fingerprint.clone());
 | 
			
		||||
 | 
			
		||||
    let client = HttpClient::new(
 | 
			
		||||
        &remote.host,
 | 
			
		||||
 | 
			
		||||
@ -67,6 +67,7 @@ fn remove_incomplete_snapshots(
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default)]
 | 
			
		||||
pub struct PruneOptions {
 | 
			
		||||
    pub keep_last: Option<u64>,
 | 
			
		||||
    pub keep_hourly: Option<u64>,
 | 
			
		||||
 | 
			
		||||
@ -211,13 +211,7 @@ fn connect_do(server: &str, port: u16, auth_id: &Authid) -> Result<HttpClient, E
 | 
			
		||||
        Err(NotPresent) => None,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let options = HttpClientOptions::new()
 | 
			
		||||
        .prefix(Some("proxmox-backup".to_string()))
 | 
			
		||||
        .password(password)
 | 
			
		||||
        .interactive(true)
 | 
			
		||||
        .fingerprint(fingerprint)
 | 
			
		||||
        .fingerprint_cache(true)
 | 
			
		||||
        .ticket_cache(true);
 | 
			
		||||
    let options = HttpClientOptions::new_interactive(password, fingerprint);
 | 
			
		||||
 | 
			
		||||
    HttpClient::new(server, port, auth_id, options)
 | 
			
		||||
}
 | 
			
		||||
@ -1565,13 +1559,9 @@ async fn try_get(repo: &BackupRepository, url: &str) -> Value {
 | 
			
		||||
    let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok();
 | 
			
		||||
    let password = std::env::var(ENV_VAR_PBS_PASSWORD).ok();
 | 
			
		||||
 | 
			
		||||
    let options = HttpClientOptions::new()
 | 
			
		||||
        .prefix(Some("proxmox-backup".to_string()))
 | 
			
		||||
        .password(password)
 | 
			
		||||
        .interactive(false)
 | 
			
		||||
        .fingerprint(fingerprint)
 | 
			
		||||
        .fingerprint_cache(true)
 | 
			
		||||
        .ticket_cache(true);
 | 
			
		||||
    // ticket cache, but no questions asked
 | 
			
		||||
    let options = HttpClientOptions::new_interactive(password, fingerprint)
 | 
			
		||||
        .interactive(false);
 | 
			
		||||
 | 
			
		||||
    let client = match HttpClient::new(repo.host(), repo.port(), repo.auth_id(), options) {
 | 
			
		||||
        Ok(v) => v,
 | 
			
		||||
 | 
			
		||||
@ -49,17 +49,16 @@ pub fn connect_to_localhost() -> Result<HttpClient, Error> {
 | 
			
		||||
 | 
			
		||||
    let uid = nix::unistd::Uid::current();
 | 
			
		||||
 | 
			
		||||
    let mut options = HttpClientOptions::new()
 | 
			
		||||
        .prefix(Some("proxmox-backup".to_string()))
 | 
			
		||||
        .verify_cert(false); // not required for connection to localhost
 | 
			
		||||
 | 
			
		||||
    let client = if uid.is_root()  {
 | 
			
		||||
        let ticket = Ticket::new("PBS", Userid::root_userid())?
 | 
			
		||||
            .sign(private_auth_key(), None)?;
 | 
			
		||||
        options = options.password(Some(ticket));
 | 
			
		||||
        let fingerprint = crate::tools::cert::CertInfo::new()?.fingerprint()?;
 | 
			
		||||
        let options = HttpClientOptions::new_non_interactive(ticket, Some(fingerprint));
 | 
			
		||||
 | 
			
		||||
        HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)?
 | 
			
		||||
    } else {
 | 
			
		||||
        options = options.ticket_cache(true).interactive(true);
 | 
			
		||||
        let options = HttpClientOptions::new_interactive(None, None);
 | 
			
		||||
 | 
			
		||||
        HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)?
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -52,15 +52,23 @@ pub struct HttpClientOptions {
 | 
			
		||||
 | 
			
		||||
impl HttpClientOptions {
 | 
			
		||||
 | 
			
		||||
    pub fn new() -> Self {
 | 
			
		||||
    pub fn new_interactive(password: Option<String>, fingerprint: Option<String>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            prefix: None,
 | 
			
		||||
            password: None,
 | 
			
		||||
            fingerprint: None,
 | 
			
		||||
            interactive: false,
 | 
			
		||||
            ticket_cache: false,
 | 
			
		||||
            fingerprint_cache: false,
 | 
			
		||||
            verify_cert: true,
 | 
			
		||||
            password,
 | 
			
		||||
            fingerprint,
 | 
			
		||||
            fingerprint_cache: true,
 | 
			
		||||
            ticket_cache: true,
 | 
			
		||||
            interactive: true,
 | 
			
		||||
            prefix: Some("proxmox-backup".to_string()),
 | 
			
		||||
            ..Self::default()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn new_non_interactive(password: String, fingerprint: Option<String>) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            password: Some(password),
 | 
			
		||||
            fingerprint,
 | 
			
		||||
            ..Self::default()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -100,6 +108,20 @@ impl HttpClientOptions {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for HttpClientOptions {
 | 
			
		||||
    fn default() -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            prefix: None,
 | 
			
		||||
            password: None,
 | 
			
		||||
            fingerprint: None,
 | 
			
		||||
            interactive: false,
 | 
			
		||||
            ticket_cache: false,
 | 
			
		||||
            fingerprint_cache: false,
 | 
			
		||||
            verify_cert: true,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// HTTP(S) API client
 | 
			
		||||
pub struct HttpClient {
 | 
			
		||||
    client: Client<HttpsConnector>,
 | 
			
		||||
 | 
			
		||||
@ -502,9 +502,7 @@ pub async fn pull_group(
 | 
			
		||||
        // get updated auth_info (new tickets)
 | 
			
		||||
        let auth_info = client.login().await?;
 | 
			
		||||
 | 
			
		||||
        let options = HttpClientOptions::new()
 | 
			
		||||
            .password(Some(auth_info.ticket.clone()))
 | 
			
		||||
            .fingerprint(fingerprint.clone());
 | 
			
		||||
        let options = HttpClientOptions::new_non_interactive(auth_info.ticket.clone(), fingerprint.clone());
 | 
			
		||||
 | 
			
		||||
        let new_client = HttpClient::new(
 | 
			
		||||
            src_repo.host(),
 | 
			
		||||
 | 
			
		||||
@ -299,6 +299,7 @@ pub fn check_acl_path(path: &str) -> Result<(), Error> {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Tree representing a parsed acl.cfg
 | 
			
		||||
#[derive(Default)]
 | 
			
		||||
pub struct AclTree {
 | 
			
		||||
    /// Root node of the tree.
 | 
			
		||||
    ///
 | 
			
		||||
@ -308,6 +309,7 @@ pub struct AclTree {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Node representing ACLs for a certain ACL path.
 | 
			
		||||
#[derive(Default)]
 | 
			
		||||
pub struct AclTreeNode {
 | 
			
		||||
    /// [User](crate::config::user::User) or
 | 
			
		||||
    /// [Token](crate::config::user::ApiToken) ACLs for this node.
 | 
			
		||||
@ -412,7 +414,7 @@ impl AclTreeNode {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn insert_group_role(&mut self, group: String, role: String, propagate: bool) {
 | 
			
		||||
        let map = self.groups.entry(group).or_insert_with(HashMap::new);
 | 
			
		||||
        let map = self.groups.entry(group).or_default();
 | 
			
		||||
        if role == ROLE_NAME_NO_ACCESS {
 | 
			
		||||
            map.clear();
 | 
			
		||||
            map.insert(role, propagate);
 | 
			
		||||
@ -423,7 +425,7 @@ impl AclTreeNode {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn insert_user_role(&mut self, auth_id: Authid, role: String, propagate: bool) {
 | 
			
		||||
        let map = self.users.entry(auth_id).or_insert_with(HashMap::new);
 | 
			
		||||
        let map = self.users.entry(auth_id).or_default();
 | 
			
		||||
        if role == ROLE_NAME_NO_ACCESS {
 | 
			
		||||
            map.clear();
 | 
			
		||||
            map.insert(role, propagate);
 | 
			
		||||
@ -465,7 +467,7 @@ impl AclTree {
 | 
			
		||||
            node = node
 | 
			
		||||
                .children
 | 
			
		||||
                .entry(String::from(*comp))
 | 
			
		||||
                .or_insert_with(AclTreeNode::new);
 | 
			
		||||
                .or_default();
 | 
			
		||||
        }
 | 
			
		||||
        node
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -318,7 +318,7 @@ enum NetworkOrderEntry {
 | 
			
		||||
    Option(String),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, Default)]
 | 
			
		||||
pub struct NetworkConfig {
 | 
			
		||||
    pub interfaces: BTreeMap<String, Interface>,
 | 
			
		||||
    order: Vec<NetworkOrderEntry>,
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user