move fingerprint helpers from pbs-tools to pbs-api-types

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2021-11-17 07:07:40 +01:00
parent 96ec3801a9
commit c42a54795d
8 changed files with 47 additions and 49 deletions

View File

@ -7,6 +7,7 @@ description = "general API type helpers for PBS"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
hex = "0.4.3"
lazy_static = "1.4" lazy_static = "1.4"
libc = "0.2" libc = "0.2"
nix = "0.19.1" nix = "0.19.1"
@ -21,4 +22,3 @@ proxmox-time = "1.0.0"
proxmox-uuid = { version = "1.0.0", features = [ "serde" ] } proxmox-uuid = { version = "1.0.0", features = [ "serde" ] }
proxmox-systemd = { path = "../proxmox-systemd" } proxmox-systemd = { path = "../proxmox-systemd" }
pbs-tools = { path = "../pbs-tools" }

View File

@ -5,8 +5,6 @@ use serde::{Deserialize, Serialize};
use proxmox_schema::api; use proxmox_schema::api;
use pbs_tools::format::{as_fingerprint, bytes_as_fingerprint};
#[api(default: "encrypt")] #[api(default: "encrypt")]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
@ -55,3 +53,43 @@ impl std::str::FromStr for Fingerprint {
} }
} }
fn as_fingerprint(bytes: &[u8]) -> String {
hex::encode(bytes)
.as_bytes()
.chunks(2)
.map(|v| unsafe { std::str::from_utf8_unchecked(v) }) // it's a hex string
.collect::<Vec<&str>>().join(":")
}
pub mod bytes_as_fingerprint {
use std::mem::MaybeUninit;
use serde::{Deserialize, Serializer, Deserializer};
pub fn serialize<S>(
bytes: &[u8; 32],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = super::as_fingerprint(bytes);
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<[u8; 32], D::Error>
where
D: Deserializer<'de>,
{
// TODO: more efficiently implement with a Visitor implementing visit_str using split() and
// hex::decode by-byte
let mut s = String::deserialize(deserializer)?;
s.retain(|c| c != ':');
let mut out = MaybeUninit::<[u8; 32]>::uninit();
hex::decode_to_slice(s.as_bytes(), unsafe { &mut (*out.as_mut_ptr())[..] })
.map_err(serde::de::Error::custom)?;
Ok(unsafe { out.assume_init() })
}
}

View File

@ -64,7 +64,7 @@ pub use user::*;
pub use proxmox_schema::upid::*; pub use proxmox_schema::upid::*;
mod crypto; mod crypto;
pub use crypto::{CryptMode, Fingerprint}; pub use crypto::{CryptMode, Fingerprint, bytes_as_fingerprint};
pub mod file_restore; pub mod file_restore;

View File

@ -100,7 +100,7 @@ impl From<&KeyConfig> for KeyInfo {
fingerprint: key_config fingerprint: key_config
.fingerprint .fingerprint
.as_ref() .as_ref()
.map(|fp| pbs_tools::format::as_fingerprint(fp.bytes())), .map(|fp| fp.to_string()),
hint: key_config.hint.clone(), hint: key_config.hint.clone(),
} }
} }

View File

@ -190,5 +190,5 @@ pub fn complete_key_fingerprint(_arg: &str, _param: &HashMap<String, String>) ->
Err(_) => return Vec::new(), Err(_) => return Vec::new(),
}; };
data.keys().map(|fp| pbs_tools::format::as_fingerprint(fp.bytes())).collect() data.keys().map(|fp| fp.to_string()).collect()
} }

View File

@ -103,47 +103,6 @@ impl From<u64> for HumanByte {
} }
} }
pub fn as_fingerprint(bytes: &[u8]) -> String {
hex::encode(bytes)
.as_bytes()
.chunks(2)
.map(|v| unsafe { std::str::from_utf8_unchecked(v) }) // it's a hex string
.collect::<Vec<&str>>().join(":")
}
pub mod bytes_as_fingerprint {
use std::mem::MaybeUninit;
use serde::{Deserialize, Serializer, Deserializer};
pub fn serialize<S>(
bytes: &[u8; 32],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = super::as_fingerprint(bytes);
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<[u8; 32], D::Error>
where
D: Deserializer<'de>,
{
// TODO: more efficiently implement with a Visitor implementing visit_str using split() and
// hex::decode by-byte
let mut s = String::deserialize(deserializer)?;
s.retain(|c| c != ':');
let mut out = MaybeUninit::<[u8; 32]>::uninit();
hex::decode_to_slice(s.as_bytes(), unsafe { &mut (*out.as_mut_ptr())[..] })
.map_err(serde::de::Error::custom)?;
Ok(unsafe { out.assume_init() })
}
}
#[test] #[test]
fn correct_byte_convert() { fn correct_byte_convert() {
fn convert(b: usize) -> String { fn convert(b: usize) -> String {

View File

@ -21,6 +21,7 @@ use pbs_api_types::{
}; };
use pbs_api_types::{PRIV_TAPE_AUDIT, PRIV_TAPE_READ, PRIV_TAPE_WRITE}; use pbs_api_types::{PRIV_TAPE_AUDIT, PRIV_TAPE_READ, PRIV_TAPE_WRITE};
use pbs_config::CachedUserInfo; use pbs_config::CachedUserInfo;
use pbs_tape::{ use pbs_tape::{
BlockReadError, BlockReadError,
@ -695,7 +696,7 @@ pub async fn read_label(
flat.encryption_key_fingerprint = set flat.encryption_key_fingerprint = set
.encryption_key_fingerprint .encryption_key_fingerprint
.as_ref() .as_ref()
.map(|fp| pbs_tools::format::as_fingerprint(fp.bytes())); .map(|fp| fp.to_string());
let encrypt_fingerprint = set.encryption_key_fingerprint.clone() let encrypt_fingerprint = set.encryption_key_fingerprint.clone()
.map(|fp| (fp, set.uuid.clone())); .map(|fp| (fp, set.uuid.clone()));

View File

@ -317,7 +317,7 @@ impl TapeDriver for LtoTapeHandle {
} }
let output = if let Some((fingerprint, uuid)) = key_fingerprint { let output = if let Some((fingerprint, uuid)) = key_fingerprint {
let fingerprint = pbs_tools::format::as_fingerprint(fingerprint.bytes()); let fingerprint = fingerprint.to_string();
run_sg_tape_cmd("encryption", &[ run_sg_tape_cmd("encryption", &[
"--fingerprint", &fingerprint, "--fingerprint", &fingerprint,
"--uuid", &uuid.to_string(), "--uuid", &uuid.to_string(),