proxmox-rest-server: use new ServerAdapter trait instead of callbacks
Async callbacks are a PITA, so we now pass a single trait object which implements check_auth and get_index.
This commit is contained in:
@ -7,23 +7,17 @@ use std::os::unix::{
|
||||
};
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use anyhow::{bail, format_err, Error};
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, info};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use http::request::Parts;
|
||||
use http::Response;
|
||||
use hyper::{Body, StatusCode};
|
||||
use hyper::header;
|
||||
|
||||
use proxmox::api::RpcEnvironmentType;
|
||||
|
||||
use pbs_client::DEFAULT_VSOCK_PORT;
|
||||
use proxmox_rest_server::{ApiConfig, RestServer, RestEnvironment};
|
||||
use proxmox_rest_server::{ApiConfig, RestServer};
|
||||
|
||||
mod proxmox_restore_daemon;
|
||||
use proxmox_restore_daemon::*;
|
||||
@ -93,29 +87,14 @@ fn setup_system_env() -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_index<'a>(
|
||||
_env: RestEnvironment,
|
||||
_parts: Parts,
|
||||
) -> Pin<Box<dyn Future<Output = http::Response<Body>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
|
||||
let index = "<center><h1>Proxmox Backup Restore Daemon/h1></center>";
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "text/html")
|
||||
.body(index.into())
|
||||
.unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Error> {
|
||||
watchdog_init();
|
||||
|
||||
let auth_config = Arc::new(
|
||||
auth::ticket_auth().map_err(|err| format_err!("reading ticket file failed: {}", err))?,
|
||||
);
|
||||
let config = ApiConfig::new("", &ROUTER, RpcEnvironmentType::PUBLIC, auth_config, &get_index)?;
|
||||
let adaptor = StaticAuthAdapter::new()
|
||||
.map_err(|err| format_err!("reading ticket file failed: {}", err))?;
|
||||
|
||||
let config = ApiConfig::new("", &ROUTER, RpcEnvironmentType::PUBLIC, adaptor)?;
|
||||
let rest_server = RestServer::new(config);
|
||||
|
||||
let vsock_fd = get_vsock_fd()?;
|
||||
|
@ -5,10 +5,13 @@ use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use anyhow::{bail, format_err, Error};
|
||||
use hyper::{Body, Response, Method, StatusCode};
|
||||
use http::request::Parts;
|
||||
use http::HeaderMap;
|
||||
|
||||
use proxmox::api::UserInformation;
|
||||
|
||||
use proxmox_rest_server::{ApiAuth, AuthError};
|
||||
use proxmox_rest_server::{ServerAdapter, AuthError, RestEnvironment};
|
||||
|
||||
const TICKET_FILE: &str = "/ticket";
|
||||
|
||||
@ -22,15 +25,30 @@ impl UserInformation for SimpleUserInformation {
|
||||
fn lookup_privs(&self, _userid: &str, _path: &[&str]) -> u64 { 0 }
|
||||
}
|
||||
|
||||
pub struct StaticAuth {
|
||||
pub struct StaticAuthAdapter {
|
||||
ticket: String,
|
||||
}
|
||||
|
||||
impl ApiAuth for StaticAuth {
|
||||
impl StaticAuthAdapter {
|
||||
|
||||
pub fn new() -> Result<Self, Error> {
|
||||
let mut ticket_file = File::open(TICKET_FILE)?;
|
||||
let mut ticket = String::new();
|
||||
let len = ticket_file.read_to_string(&mut ticket)?;
|
||||
if len <= 0 {
|
||||
bail!("invalid ticket: cannot be empty");
|
||||
}
|
||||
Ok(StaticAuthAdapter { ticket })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl ServerAdapter for StaticAuthAdapter {
|
||||
|
||||
fn check_auth<'a>(
|
||||
&'a self,
|
||||
headers: &'a http::HeaderMap,
|
||||
_method: &'a hyper::Method,
|
||||
headers: &'a HeaderMap,
|
||||
_method: &'a Method,
|
||||
) -> Pin<Box<dyn Future<Output = Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
|
||||
@ -47,14 +65,21 @@ impl ApiAuth for StaticAuth {
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ticket_auth() -> Result<StaticAuth, Error> {
|
||||
let mut ticket_file = File::open(TICKET_FILE)?;
|
||||
let mut ticket = String::new();
|
||||
let len = ticket_file.read_to_string(&mut ticket)?;
|
||||
if len <= 0 {
|
||||
bail!("invalid ticket: cannot be empty");
|
||||
fn get_index(
|
||||
&self,
|
||||
_env: RestEnvironment,
|
||||
_parts: Parts,
|
||||
) -> Pin<Box<dyn Future<Output = http::Response<Body>> + Send>> {
|
||||
Box::pin(async move {
|
||||
|
||||
let index = "<center><h1>Proxmox Backup Restore Daemon/h1></center>";
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(hyper::header::CONTENT_TYPE, "text/html")
|
||||
.body(index.into())
|
||||
.unwrap()
|
||||
})
|
||||
}
|
||||
Ok(StaticAuth { ticket })
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ mod api;
|
||||
pub use api::*;
|
||||
|
||||
pub mod auth;
|
||||
pub use auth::*;
|
||||
|
||||
mod watchdog;
|
||||
pub use watchdog::*;
|
||||
|
Reference in New Issue
Block a user