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:
@ -5,16 +5,17 @@ use anyhow::{bail, Error};
|
||||
use futures::*;
|
||||
use http::request::Parts;
|
||||
use http::Response;
|
||||
use hyper::{Body, StatusCode};
|
||||
use hyper::header;
|
||||
use hyper::{Body, Method, StatusCode};
|
||||
use http::HeaderMap;
|
||||
|
||||
use proxmox::try_block;
|
||||
use proxmox::api::RpcEnvironmentType;
|
||||
use proxmox::tools::fs::CreateOptions;
|
||||
use proxmox::api::UserInformation;
|
||||
|
||||
use proxmox_rest_server::{daemon, ApiConfig, RestServer, RestEnvironment};
|
||||
use proxmox_rest_server::{daemon, AuthError, ApiConfig, RestServer, RestEnvironment, ServerAdapter};
|
||||
|
||||
use proxmox_backup::server::auth::default_api_auth;
|
||||
use proxmox_backup::server::auth::check_pbs_auth;
|
||||
use proxmox_backup::auth_helpers::*;
|
||||
use proxmox_backup::config;
|
||||
|
||||
@ -27,20 +28,36 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_index<'a>(
|
||||
_env: RestEnvironment,
|
||||
_parts: Parts,
|
||||
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
struct ProxmoxBackupApiAdapter;
|
||||
|
||||
let index = "<center><h1>Proxmox Backup API Server</h1></center>";
|
||||
impl ServerAdapter for ProxmoxBackupApiAdapter {
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "text/html")
|
||||
.body(index.into())
|
||||
.unwrap()
|
||||
})
|
||||
fn get_index(
|
||||
&self,
|
||||
_env: RestEnvironment,
|
||||
_parts: Parts,
|
||||
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send>> {
|
||||
Box::pin(async move {
|
||||
|
||||
let index = "<center><h1>Proxmox Backup API Server</h1></center>";
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(hyper::header::CONTENT_TYPE, "text/html")
|
||||
.body(index.into())
|
||||
.unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
fn check_auth<'a>(
|
||||
&'a self,
|
||||
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 {
|
||||
check_pbs_auth(headers, method).await
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Error> {
|
||||
@ -78,8 +95,7 @@ async fn run() -> Result<(), Error> {
|
||||
pbs_buildcfg::JS_DIR,
|
||||
&proxmox_backup::api2::ROUTER,
|
||||
RpcEnvironmentType::PRIVILEGED,
|
||||
default_api_auth(),
|
||||
&get_index,
|
||||
ProxmoxBackupApiAdapter,
|
||||
)?;
|
||||
|
||||
let backup_user = pbs_config::backup_user()?;
|
||||
|
@ -15,21 +15,23 @@ use url::form_urlencoded;
|
||||
use openssl::ssl::{SslMethod, SslAcceptor, SslFiletype};
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use serde_json::{json, Value};
|
||||
use http::{Method, HeaderMap};
|
||||
|
||||
use proxmox::try_block;
|
||||
use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
|
||||
use proxmox::api::{RpcEnvironment, RpcEnvironmentType, UserInformation};
|
||||
use proxmox::sys::linux::socket::set_tcp_keepalive;
|
||||
use proxmox::tools::fs::CreateOptions;
|
||||
|
||||
use pbs_tools::task_log;
|
||||
use pbs_datastore::DataStore;
|
||||
use proxmox_rest_server::{
|
||||
rotate_task_log_archive, extract_cookie , ApiConfig, RestServer, RestEnvironment, WorkerTask,
|
||||
rotate_task_log_archive, extract_cookie , AuthError, ApiConfig, RestServer, RestEnvironment,
|
||||
ServerAdapter, WorkerTask,
|
||||
};
|
||||
|
||||
use proxmox_backup::{
|
||||
server::{
|
||||
auth::default_api_auth,
|
||||
auth::check_pbs_auth,
|
||||
jobstate::{
|
||||
self,
|
||||
Job,
|
||||
@ -81,6 +83,29 @@ fn main() -> Result<(), Error> {
|
||||
}
|
||||
|
||||
|
||||
struct ProxmoxBackupProxyAdapter;
|
||||
|
||||
impl ServerAdapter for ProxmoxBackupProxyAdapter {
|
||||
|
||||
fn get_index(
|
||||
&self,
|
||||
env: RestEnvironment,
|
||||
parts: Parts,
|
||||
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send>> {
|
||||
Box::pin(get_index_future(env, parts))
|
||||
}
|
||||
|
||||
fn check_auth<'a>(
|
||||
&'a self,
|
||||
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 {
|
||||
check_pbs_auth(headers, method).await
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_lang_header(headers: &http::HeaderMap) -> Option<String> {
|
||||
if let Some(Ok(cookie)) = headers.get("COOKIE").map(|v| v.to_str()) {
|
||||
return extract_cookie(cookie, "PBSLangCookie");
|
||||
@ -88,13 +113,6 @@ fn extract_lang_header(headers: &http::HeaderMap) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn get_index<'a>(
|
||||
env: RestEnvironment,
|
||||
parts: Parts,
|
||||
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
|
||||
Box::pin(get_index_future(env, parts))
|
||||
}
|
||||
|
||||
async fn get_index_future(
|
||||
env: RestEnvironment,
|
||||
parts: Parts,
|
||||
@ -191,8 +209,7 @@ async fn run() -> Result<(), Error> {
|
||||
pbs_buildcfg::JS_DIR,
|
||||
&proxmox_backup::api2::ROUTER,
|
||||
RpcEnvironmentType::PUBLIC,
|
||||
default_api_auth(),
|
||||
&get_index,
|
||||
ProxmoxBackupProxyAdapter,
|
||||
)?;
|
||||
|
||||
config.add_alias("novnc", "/usr/share/novnc-pve");
|
||||
|
Reference in New Issue
Block a user