rust fmt for pbs src

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2022-04-14 14:03:46 +02:00
parent ee0ea73500
commit 9531d2c570
30 changed files with 644 additions and 572 deletions

View File

@ -11,7 +11,6 @@ use pbs_api_types::PRIVILEGES;
use proxmox_backup::api2;
fn get_args() -> (String, Vec<String>) {
let mut args = std::env::args();
let prefix = args.next().unwrap();
let prefix = prefix.rsplit('/').next().unwrap().to_string(); // without path
@ -21,7 +20,6 @@ fn get_args() -> (String, Vec<String>) {
}
fn main() -> Result<(), Error> {
let (_prefix, args) = get_args();
if args.is_empty() {
@ -49,10 +47,9 @@ fn main() -> Result<(), Error> {
}
fn generate_api_tree() -> String {
let mut tree = Vec::new();
let mut data = dump_api_schema(& api2::ROUTER, ".");
let mut data = dump_api_schema(&api2::ROUTER, ".");
data["path"] = "/".into();
// hack: add invisible space to sort as first entry
data["text"] = "&#x200b;Management API (HTTP)".into();
@ -70,11 +67,13 @@ fn generate_api_tree() -> String {
data["text"] = "Restore API (HTTP/2)".into();
tree.push(data);
format!("var apiSchema = {};", serde_json::to_string_pretty(&tree).unwrap())
format!(
"var apiSchema = {};",
serde_json::to_string_pretty(&tree).unwrap()
)
}
pub fn dump_schema(schema: &Schema) -> Value {
let mut data;
match schema {
@ -112,23 +111,18 @@ pub fn dump_schema(schema: &Schema) -> Value {
match string_schema.format {
None | Some(ApiStringFormat::VerifyFn(_)) => { /* do nothing */ }
Some(ApiStringFormat::Pattern(const_regex)) => {
data["pattern"] = format!("/{}/", const_regex.regex_string)
.into();
data["pattern"] = format!("/{}/", const_regex.regex_string).into();
}
Some(ApiStringFormat::Enum(variants)) => {
let variants: Vec<String> = variants
.iter()
.map(|e| e.value.to_string())
.collect();
let variants: Vec<String> =
variants.iter().map(|e| e.value.to_string()).collect();
data["enum"] = serde_json::to_value(variants).unwrap();
}
Some(ApiStringFormat::PropertyString(subschema)) => {
match subschema {
Schema::Object(_) | Schema::Array(_) => {
data["format"] = dump_schema(subschema);
data["typetext"] = get_property_string_type_text(subschema)
.into();
data["typetext"] = get_property_string_type_text(subschema).into();
}
_ => { /* do nothing - shouldnot happen */ }
};
@ -137,7 +131,7 @@ pub fn dump_schema(schema: &Schema) -> Value {
// fixme: dump format
}
Schema::Integer(integer_schema) => {
data = json!({
data = json!({
"type": "integer",
"description": integer_schema.description,
});
@ -162,7 +156,7 @@ pub fn dump_schema(schema: &Schema) -> Value {
if let Some(minimum) = number_schema.minimum {
data["minimum"] = minimum.into();
}
if let Some(maximum) = number_schema.maximum {
if let Some(maximum) = number_schema.maximum {
data["maximum"] = maximum.into();
}
}
@ -182,7 +176,7 @@ pub fn dump_schema(schema: &Schema) -> Value {
if let Some(min_length) = array_schema.min_length {
data["minLength"] = min_length.into();
}
if let Some(max_length) = array_schema.min_length {
if let Some(max_length) = array_schema.min_length {
data["maxLength"] = max_length.into();
}
}
@ -216,7 +210,6 @@ pub fn dump_property_schema(param: &dyn ObjectSchemaType) -> Value {
}
fn dump_api_permission(permission: &Permission) -> Value {
match permission {
Permission::Superuser => json!({ "user": "root@pam" }),
Permission::User(user) => json!({ "user": user }),
@ -233,7 +226,6 @@ fn dump_api_permission(permission: &Permission) -> Value {
})
}
Permission::Privilege(name, value, partial) => {
let mut privs = Vec::new();
for (name, v) in PRIVILEGES {
if (value & v) != 0 {
@ -260,10 +252,7 @@ fn dump_api_permission(permission: &Permission) -> Value {
}
}
fn dump_api_method_schema(
method: &str,
api_method: &ApiMethod,
) -> Value {
fn dump_api_method_schema(method: &str, api_method: &ApiMethod) -> Value {
let mut data = json!({
"description": api_method.parameters.description(),
});
@ -277,10 +266,16 @@ fn dump_api_method_schema(
data["returns"] = returns;
match api_method.access {
ApiAccess { description: None, permission: Permission::Superuser } => {
ApiAccess {
description: None,
permission: Permission::Superuser,
} => {
// no need to output default
}
ApiAccess { description, permission } => {
ApiAccess {
description,
permission,
} => {
let mut permissions = dump_api_permission(permission);
if let Some(description) = description {
permissions["description"] = description.into();
@ -301,11 +296,7 @@ fn dump_api_method_schema(
data
}
pub fn dump_api_schema(
router: &Router,
path: &str,
) -> Value {
pub fn dump_api_schema(router: &Router, path: &str) -> Value {
let mut data = json!({});
let mut info = json!({});
@ -327,7 +318,7 @@ pub fn dump_api_schema(
match &router.subroute {
None => {
data["leaf"] = 1.into();
},
}
Some(SubRoute::MatchAll { router, param_name }) => {
let sub_path = if path == "." {
format!("/{{{}}}", param_name)
@ -343,7 +334,6 @@ pub fn dump_api_schema(
data["leaf"] = 0.into();
}
Some(SubRoute::Map(dirmap)) => {
let mut children = Vec::new();
for (key, sub_router) in dirmap.iter() {

View File

@ -4,19 +4,21 @@ use std::pin::Pin;
use anyhow::{bail, Error};
use futures::*;
use http::request::Parts;
use http::HeaderMap;
use http::Response;
use hyper::{Body, Method, StatusCode};
use http::HeaderMap;
use proxmox_lang::try_block;
use proxmox_router::{RpcEnvironmentType, UserInformation};
use proxmox_sys::fs::CreateOptions;
use proxmox_rest_server::{daemon, AuthError, ApiConfig, RestServer, RestEnvironment, ServerAdapter};
use proxmox_rest_server::{
daemon, ApiConfig, AuthError, RestEnvironment, RestServer, ServerAdapter,
};
use proxmox_backup::server::auth::check_pbs_auth;
use proxmox_backup::auth_helpers::*;
use proxmox_backup::config;
use proxmox_backup::server::auth::check_pbs_auth;
fn main() {
pbs_tools::setup_libc_malloc_opts();
@ -32,14 +34,12 @@ fn main() {
struct ProxmoxBackupApiAdapter;
impl ServerAdapter for ProxmoxBackupApiAdapter {
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()
@ -54,10 +54,14 @@ impl ServerAdapter for ProxmoxBackupApiAdapter {
&'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
})
) -> 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 })
}
}
@ -65,7 +69,8 @@ async fn run() -> Result<(), Error> {
if let Err(err) = syslog::init(
syslog::Facility::LOG_DAEMON,
log::LevelFilter::Info,
Some("proxmox-backup-api")) {
Some("proxmox-backup-api"),
) {
bail!("unable to inititialize syslog - {}", err);
}
@ -100,10 +105,17 @@ async fn run() -> Result<(), Error> {
)?;
let backup_user = pbs_config::backup_user()?;
let mut commando_sock = proxmox_rest_server::CommandSocket::new(proxmox_rest_server::our_ctrl_sock(), backup_user.gid);
let mut commando_sock = proxmox_rest_server::CommandSocket::new(
proxmox_rest_server::our_ctrl_sock(),
backup_user.gid,
);
let dir_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
let file_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
let dir_opts = CreateOptions::new()
.owner(backup_user.uid)
.group(backup_user.gid);
let file_opts = CreateOptions::new()
.owner(backup_user.uid)
.group(backup_user.gid);
config.enable_access_log(
pbs_buildcfg::API_ACCESS_LOG_FN,
@ -119,27 +131,26 @@ async fn run() -> Result<(), Error> {
&mut commando_sock,
)?;
let rest_server = RestServer::new(config);
proxmox_rest_server::init_worker_tasks(pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR_M!().into(), file_opts.clone())?;
proxmox_rest_server::init_worker_tasks(
pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR_M!().into(),
file_opts.clone(),
)?;
// http server future:
let server = daemon::create_daemon(
([127,0,0,1], 82).into(),
move |listener| {
let incoming = hyper::server::conn::AddrIncoming::from_listener(listener)?;
let server = daemon::create_daemon(([127, 0, 0, 1], 82).into(), move |listener| {
let incoming = hyper::server::conn::AddrIncoming::from_listener(listener)?;
Ok(async {
daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
Ok(async {
daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
hyper::Server::builder(incoming)
.serve(rest_server)
.with_graceful_shutdown(proxmox_rest_server::shutdown_future())
.map_err(Error::from)
.await
})
},
);
hyper::Server::builder(incoming)
.serve(rest_server)
.with_graceful_shutdown(proxmox_rest_server::shutdown_future())
.map_err(Error::from)
.await
})
});
proxmox_rest_server::write_pid(pbs_buildcfg::PROXMOX_BACKUP_API_PID_FN)?;

View File

@ -20,5 +20,9 @@ fn main() {
let mut rpcenv = CliEnvironment::new();
rpcenv.set_auth_id(Some(format!("{}@pam", username)));
run_cli_command(cmd_def, rpcenv, Some(|future| proxmox_async::runtime::main(future)));
run_cli_command(
cmd_def,
rpcenv,
Some(|future| proxmox_async::runtime::main(future)),
);
}

View File

@ -47,8 +47,8 @@ use pbs_buildcfg::configdir;
use proxmox_time::CalendarEvent;
use pbs_api_types::{
Authid, DataStoreConfig, PruneOptions, SyncJobConfig, TapeBackupJobConfig,
VerificationJobConfig, Operation
Authid, DataStoreConfig, Operation, PruneOptions, SyncJobConfig, TapeBackupJobConfig,
VerificationJobConfig,
};
use proxmox_rest_server::daemon;
@ -101,10 +101,14 @@ impl ServerAdapter for ProxmoxBackupProxyAdapter {
&'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
})
) -> 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 })
}
}
@ -194,7 +198,11 @@ async fn run() -> Result<(), Error> {
if let Err(err) = syslog::init(
syslog::Facility::LOG_DAEMON,
if debug { log::LevelFilter::Debug } else { log::LevelFilter::Info },
if debug {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
},
Some("proxmox-backup-proxy"),
) {
bail!("unable to inititialize syslog - {}", err);

View File

@ -166,7 +166,6 @@ fn merge_parameters(
}));
}
let params = schema.parse_parameter_strings(&param_list, true)?;
Ok(params)

View File

@ -1,13 +1,13 @@
use std::collections::HashSet;
use std::fs::File;
use std::io::{stdout, Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::path::Path;
use anyhow::{bail, format_err, Error};
use hex::FromHex;
use serde_json::{json, Value};
use walkdir::WalkDir;
use hex::FromHex;
use proxmox_router::cli::{
format_and_print_result, get_output_format, CliCommand, CliCommandMap, CommandLineInterface,
@ -15,7 +15,8 @@ use proxmox_router::cli::{
};
use proxmox_schema::api;
use pbs_tools::crypt_config::CryptConfig;
use pbs_client::tools::key_source::get_encryption_key_password;
use pbs_config::key_config::load_and_decrypt_key;
use pbs_datastore::dynamic_index::DynamicIndexReader;
use pbs_datastore::file_formats::{
COMPRESSED_BLOB_MAGIC_1_0, DYNAMIC_SIZED_CHUNK_INDEX_1_0, ENCRYPTED_BLOB_MAGIC_1_0,
@ -24,8 +25,7 @@ use pbs_datastore::file_formats::{
use pbs_datastore::fixed_index::FixedIndexReader;
use pbs_datastore::index::IndexFile;
use pbs_datastore::DataBlob;
use pbs_config::key_config::load_and_decrypt_key;
use pbs_client::tools::key_source::get_encryption_key_password;
use pbs_tools::crypt_config::CryptConfig;
// Returns either a new file, if a path is given, or stdout, if no path is given.
fn outfile_or_stdout<P: AsRef<Path>>(
@ -128,8 +128,7 @@ fn inspect_chunk(
let digest_raw: Option<[u8; 32]> = digest
.map(|ref d| {
<[u8; 32]>::from_hex(d)
.map_err(|e| format_err!("could not parse chunk - {}", e))
<[u8; 32]>::from_hex(d).map_err(|e| format_err!("could not parse chunk - {}", e))
})
.map_or(Ok(None), |r| r.map(Some))?;

View File

@ -1,3 +1,3 @@
pub mod api;
pub mod inspect;
pub mod recover;
pub mod api;

View File

@ -8,14 +8,14 @@ use serde_json::Value;
use proxmox_router::cli::{CliCommand, CliCommandMap, CommandLineInterface};
use proxmox_schema::api;
use pbs_tools::crypt_config::CryptConfig;
use pbs_client::tools::key_source::get_encryption_key_password;
use pbs_config::key_config::load_and_decrypt_key;
use pbs_datastore::dynamic_index::DynamicIndexReader;
use pbs_datastore::file_formats::{DYNAMIC_SIZED_CHUNK_INDEX_1_0, FIXED_SIZED_CHUNK_INDEX_1_0};
use pbs_datastore::fixed_index::FixedIndexReader;
use pbs_datastore::index::IndexFile;
use pbs_datastore::DataBlob;
use pbs_config::key_config::load_and_decrypt_key;
use pbs_client::tools::key_source::get_encryption_key_password;
use pbs_tools::crypt_config::CryptConfig;
#[api(
input: {

View File

@ -2,7 +2,6 @@
/// to read and set the encryption key.
///
/// This command can use STDIN as tape device handle.
use std::fs::File;
use std::os::unix::io::{AsRawFd, FromRawFd};
@ -14,24 +13,15 @@ use proxmox_schema::api;
use proxmox_uuid::Uuid;
use pbs_api_types::{
Fingerprint, LTO_DRIVE_PATH_SCHEMA, DRIVE_NAME_SCHEMA, TAPE_ENCRYPTION_KEY_FINGERPRINT_SCHEMA,
MEDIA_SET_UUID_SCHEMA, LtoTapeDrive,
Fingerprint, LtoTapeDrive, DRIVE_NAME_SCHEMA, LTO_DRIVE_PATH_SCHEMA, MEDIA_SET_UUID_SCHEMA,
TAPE_ENCRYPTION_KEY_FINGERPRINT_SCHEMA,
};
use pbs_tape::linux_list_drives::{open_lto_tape_device, check_tape_is_lto_tape_device};
use pbs_tape::linux_list_drives::{check_tape_is_lto_tape_device, open_lto_tape_device};
use proxmox_backup::{
tape::{
drive::{
TapeDriver,
LtoTapeHandle,
open_lto_tape_drive,
},
},
};
use proxmox_backup::tape::drive::{open_lto_tape_drive, LtoTapeHandle, TapeDriver};
fn get_tape_handle(param: &Value) -> Result<LtoTapeHandle, Error> {
let handle = if let Some(name) = param["drive"].as_str() {
let (config, _digest) = pbs_config::drive::config()?;
let drive: LtoTapeDrive = config.lookup("lto", name)?;
@ -56,7 +46,9 @@ fn get_tape_handle(param: &Value) -> Result<LtoTapeHandle, Error> {
let mut drive_names = Vec::new();
for (name, (section_type, _)) in config.sections.iter() {
if section_type != "lto" { continue; }
if section_type != "lto" {
continue;
}
drive_names.push(name);
}
@ -106,7 +98,6 @@ fn set_encryption(
uuid: Option<Uuid>,
param: Value,
) -> Result<(), Error> {
let result = proxmox_lang::try_block!({
let mut handle = get_tape_handle(&param)?;
@ -123,7 +114,8 @@ fn set_encryption(
}
Ok(())
}).map_err(|err: Error| err.to_string());
})
.map_err(|err: Error| err.to_string());
println!("{}", serde_json::to_string_pretty(&result)?);
@ -131,7 +123,6 @@ fn set_encryption(
}
fn main() -> Result<(), Error> {
// check if we are user root or backup
let backup_uid = pbs_config::backup_user()?.uid;
let backup_gid = pbs_config::backup_group()?.gid;
@ -146,16 +137,13 @@ fn main() -> Result<(), Error> {
if !running_uid.is_root() && (running_uid != backup_uid || running_gid != backup_gid) {
bail!(
"Not running as backup user or group (got uid {} gid {})",
running_uid, running_gid,
running_uid,
running_gid,
);
}
let cmd_def = CliCommandMap::new()
.insert(
"encryption",
CliCommand::new(&API_METHOD_SET_ENCRYPTION)
)
;
let cmd_def =
CliCommandMap::new().insert("encryption", CliCommand::new(&API_METHOD_SET_ENCRYPTION));
let mut rpcenv = CliEnvironment::new();
rpcenv.set_auth_id(Some(String::from("root@pam")));