use std::future::Future;
use std::sync::{Arc, Mutex};
-use proxmox_async::broadcast_future::BroadcastFuture;
use crate::lru_cache::LruCache;
+use proxmox_async::broadcast_future::BroadcastFuture;
/// Interface for asynchronously getting values on cache misses.
pub trait AsyncCacher<K, V: Clone>: Sync + Send {
//! Deals with the server's current certificates (proxy.pem).
-use std::path::PathBuf;
use std::mem::MaybeUninit;
+use std::path::PathBuf;
use anyhow::{bail, format_err, Error};
use foreign_types::ForeignTypeRef;
-use openssl::x509::{X509, GeneralName};
+use openssl::pkey::{PKey, Public};
use openssl::stack::Stack;
-use openssl::pkey::{Public, PKey};
+use openssl::x509::{GeneralName, X509};
// C type:
#[allow(non_camel_case_types)]
fn x509name_to_string(name: &openssl::x509::X509NameRef) -> Result<String, Error> {
let mut parts = Vec::new();
for entry in name.entries() {
- parts.push(format!("{} = {}", entry.object().nid().short_name()?, entry.data().as_utf8()?));
+ parts.push(format!(
+ "{} = {}",
+ entry.object().nid().short_name()?,
+ entry.data().as_utf8()?
+ ));
}
Ok(parts.join(", "))
}
pub fn from_pem(cert_pem: &[u8]) -> Result<Self, Error> {
let x509 = openssl::x509::X509::from_pem(cert_pem)?;
- Ok(Self{
- x509
- })
+ Ok(Self { x509 })
}
pub fn subject_alt_names(&self) -> Option<Stack<GeneralName>> {
.as_bytes()
.chunks(2)
.map(|v| std::str::from_utf8(v).unwrap())
- .collect::<Vec<&str>>().join(":"))
+ .collect::<Vec<&str>>()
+ .join(":"))
}
pub fn public_key(&self) -> Result<PKey<Public>, Error> {
//! encryption](https://en.wikipedia.org/wiki/Authenticated_encryption)
//! for a short introduction.
-use anyhow::{Error};
+use anyhow::Error;
use openssl::hash::MessageDigest;
use openssl::pkcs5::pbkdf2_hmac;
use openssl::symm::{Cipher, Crypter, Mode};
// openssl::sha::sha256(b"Proxmox Backup Encryption Key Fingerprint")
/// This constant is used to compute fingerprints.
const FINGERPRINT_INPUT: [u8; 32] = [
- 110, 208, 239, 119, 71, 31, 255, 77,
- 85, 199, 168, 254, 74, 157, 182, 33,
- 97, 64, 127, 19, 76, 114, 93, 223,
- 48, 153, 45, 37, 236, 69, 237, 38,
+ 110, 208, 239, 119, 71, 31, 255, 77, 85, 199, 168, 254, 74, 157, 182, 33, 97, 64, 127, 19, 76,
+ 114, 93, 223, 48, 153, 45, 37, 236, 69, 237, 38,
];
/// Encryption Configuration with secret key
}
impl CryptConfig {
-
/// Create a new instance.
///
/// We compute a derived 32 byte key using pbkdf2_hmac. This second
/// key is used in compute_digest.
pub fn new(enc_key: [u8; 32]) -> Result<Self, Error> {
-
let mut id_key = [0u8; 32];
pbkdf2_hmac(
b"_id_key",
10,
MessageDigest::sha256(),
- &mut id_key)?;
+ &mut id_key,
+ )?;
let id_pkey = openssl::pkey::PKey::hmac(&id_key).unwrap();
- Ok(Self { id_key, id_pkey, enc_key, cipher: Cipher::aes_256_gcm() })
+ Ok(Self {
+ id_key,
+ id_pkey,
+ enc_key,
+ cipher: Cipher::aes_256_gcm(),
+ })
}
/// Expose Cipher (AES_256_GCM)
}
/// Returns an openssl Crypter using AES_256_GCM,
- pub fn data_crypter(&self, iv: &[u8; 16], mode: Mode) -> Result<Crypter, Error> {
+ pub fn data_crypter(&self, iv: &[u8; 16], mode: Mode) -> Result<Crypter, Error> {
let mut crypter = openssl::symm::Crypter::new(self.cipher, mode, &self.enc_key, Some(iv))?;
crypter.aad_update(b"")?; //??
Ok(crypter)
use std::borrow::Borrow;
-use anyhow::{Error};
+use anyhow::Error;
use serde_json::Value;
use pbs_api_types::HumanByte;
pub fn strip_server_file_extension(name: &str) -> &str {
if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") {
- &name[..name.len()-5]
+ &name[..name.len() - 5]
} else {
name // should not happen
}
}
pub fn render_backup_file_list<S: Borrow<str>>(files: &[S]) -> String {
- let mut files: Vec<&str> = files.iter()
+ let mut files: Vec<&str> = files
+ .iter()
.map(|v| strip_server_file_extension(v.borrow()))
.collect();
}
pub fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
- if value.is_null() { return Ok(String::new()); }
+ if value.is_null() {
+ return Ok(String::new());
+ }
let text = match value.as_i64() {
Some(epoch) => {
if let Ok(epoch_string) = proxmox_time::strftime_local("%c", epoch as i64) {
} else {
epoch.to_string()
}
- },
- None => {
- value.to_string()
}
+ None => value.to_string(),
};
Ok(text)
}
}
pub fn render_bytes_human_readable(value: &Value, _record: &Value) -> Result<String, Error> {
- if value.is_null() { return Ok(String::new()); }
+ if value.is_null() {
+ return Ok(String::new());
+ }
let text = match value.as_u64() {
- Some(bytes) => {
- HumanByte::from(bytes).to_string()
- }
- None => {
- value.to_string()
- }
+ Some(bytes) => HumanByte::from(bytes).to_string(),
+ None => value.to_string(),
};
Ok(text)
}
/// less erratic behavior in the overall's runtime RSS size.
pub fn setup_libc_malloc_opts() {
unsafe {
- libc::mallopt(libc::M_MMAP_THRESHOLD, 4096*32);
+ libc::mallopt(libc::M_MMAP_THRESHOLD, 4096 * 32);
}
}
//! A HashMap is used for fast access by a given key and a doubly linked list
//! is used to keep track of the cache access order.
-use std::collections::{HashMap, hash_map::Entry};
+use std::collections::{hash_map::Entry, HashMap};
use std::marker::PhantomData;
/// Interface for getting values on cache misses.
}
impl<K, V> Drop for LruCache<K, V> {
- fn drop (&mut self) {
+ fn drop(&mut self) {
self.clear();
}
}
/// value.
/// If fetch returns a value, it is inserted as the most recently used entry
/// in the cache.
- pub fn access<'a>(&'a mut self, key: K, cacher: &mut dyn Cacher<K, V>) -> Result<Option<&'a mut V>, anyhow::Error> {
+ pub fn access<'a>(
+ &'a mut self,
+ key: K,
+ cacher: &mut dyn Cacher<K, V>,
+ ) -> Result<Option<&'a mut V>, anyhow::Error> {
match self.map.entry(key) {
Entry::Occupied(mut o) => {
// Cache hit, birng node to front of list
use anyhow::{bail, Error};
use nom::{
- error::{ParseError, VerboseError},
bytes::complete::{take_while, take_while1},
- combinator::{map_res, all_consuming, recognize},
- character::complete::{digit1},
+ character::complete::digit1,
+ combinator::{all_consuming, map_res, recognize},
+ error::{ParseError, VerboseError},
};
pub type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
}
/// Recognizes zero or more spaces and tabs (but not carage returns or line feeds)
-pub fn multispace0(i: &str) -> IResult<&str, &str> {
+pub fn multispace0(i: &str) -> IResult<&str, &str> {
take_while(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more spaces and tabs (but not carage returns or line feeds)
-pub fn multispace1(i: &str) -> IResult<&str, &str> {
+pub fn multispace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| c == ' ' || c == '\t')(i)
}
/// Recognizes one or more non-whitespace-characters
-pub fn notspace1(i: &str) -> IResult<&str, &str> {
+pub fn notspace1(i: &str) -> IResult<&str, &str> {
take_while1(|c| !(c == ' ' || c == '\t' || c == '\n'))(i)
}
/// Parse complete input, generate verbose error message with line numbers
pub fn parse_complete<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
- where F: Fn(&'a str) -> IResult<&'a str, O>,
+where
+ F: Fn(&'a str) -> IResult<&'a str, O>,
{
match all_consuming(parser)(i) {
- Err(nom::Err::Error(err)) |
- Err(nom::Err::Failure(err)) => {
- bail!("unable to parse {} - {}", what, nom::error::convert_error(i, err));
+ Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => {
+ bail!(
+ "unable to parse {} - {}",
+ what,
+ nom::error::convert_error(i, err)
+ );
}
Err(err) => {
bail!("unable to parse {} - {}", what, err);
}
Ok((_, data)) => Ok(data),
}
-
}
/// Parse complete input, generate simple error message (use this for sinple line input).
pub fn parse_complete_line<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
- where F: Fn(&'a str) -> IResult<&'a str, O>,
+where
+ F: Fn(&'a str) -> IResult<&'a str, O>,
{
match all_consuming(parser)(i) {
- Err(nom::Err::Error(VerboseError { errors })) |
- Err(nom::Err::Failure(VerboseError { errors })) => {
+ Err(nom::Err::Error(VerboseError { errors }))
+ | Err(nom::Err::Failure(VerboseError { errors })) => {
if errors.is_empty() {
bail!("unable to parse {}", what);
} else {
- bail!("unable to parse {} at '{}' - {:?}", what, errors[0].0, errors[0].1);
+ bail!(
+ "unable to parse {} at '{}' - {:?}",
+ what,
+ errors[0].0,
+ errors[0].1
+ );
}
}
Err(err) => {