2021-09-29 09:05:26 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
|
2021-09-29 11:58:07 +00:00
|
|
|
use pbs_api_types::{Authid, Userid};
|
|
|
|
use pbs_client::{HttpClient, HttpClientOptions};
|
|
|
|
use pbs_tools::ticket::Ticket;
|
|
|
|
|
2021-09-29 09:05:26 +00:00
|
|
|
use crate::auth_helpers::private_auth_key;
|
|
|
|
|
2021-09-29 11:58:07 +00:00
|
|
|
/// Connect to localhost:8007 as root@pam
|
|
|
|
///
|
|
|
|
/// This automatically creates a ticket if run as 'root' user.
|
2021-09-29 09:05:26 +00:00
|
|
|
pub fn connect_to_localhost() -> Result<pbs_client::HttpClient, Error> {
|
2021-09-29 11:58:07 +00:00
|
|
|
let options = if nix::unistd::Uid::current().is_root() {
|
|
|
|
let auth_key = private_auth_key();
|
|
|
|
let ticket = Ticket::new("PBS", Userid::root_userid())?.sign(auth_key, None)?;
|
2021-09-29 12:01:38 +00:00
|
|
|
let fingerprint = crate::cert_info()?.fingerprint()?;
|
2021-09-29 11:58:07 +00:00
|
|
|
HttpClientOptions::new_non_interactive(ticket, Some(fingerprint))
|
2021-09-29 09:05:26 +00:00
|
|
|
} else {
|
2021-09-29 11:58:07 +00:00
|
|
|
HttpClientOptions::new_interactive(None, None)
|
|
|
|
};
|
|
|
|
|
|
|
|
HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)
|
2021-09-29 09:05:26 +00:00
|
|
|
}
|