From 14f389d563ed20c7655dc1513e128b36d4d04d17 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 23 Nov 2021 17:57:00 +0100 Subject: [PATCH] update to proxmox-sys 0.2 crate - imported pbs-api-types/src/common_regex.rs from old proxmox crate - use hex crate to generate/parse hex digest - remove all reference to proxmox crate (use proxmox-sys and proxmox-serde instead) Signed-off-by: Dietmar Maurer --- pbs-api-types/Cargo.toml | 4 +- pbs-api-types/src/common_regex.rs | 78 ++++++++++++++++++++++++ pbs-api-types/src/crypto.rs | 3 +- pbs-api-types/src/human_byte.rs | 4 +- pbs-api-types/src/jobs.rs | 4 +- pbs-api-types/src/lib.rs | 5 +- pbs-api-types/src/remote.rs | 2 +- pbs-api-types/src/tape/media_location.rs | 4 +- pbs-api-types/src/userid.rs | 8 +-- 9 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 pbs-api-types/src/common_regex.rs diff --git a/pbs-api-types/Cargo.toml b/pbs-api-types/Cargo.toml index 925207e8..3bee23ba 100644 --- a/pbs-api-types/Cargo.toml +++ b/pbs-api-types/Cargo.toml @@ -15,8 +15,10 @@ openssl = "0.10" regex = "1.2" serde = { version = "1.0", features = ["derive"] } -proxmox = "0.15.3" proxmox-lang = "1.0.0" proxmox-schema = { version = "1.0.1", features = [ "api-macro" ] } +proxmox-serde = "0.1" proxmox-time = "1.1" proxmox-uuid = { version = "1.0.0", features = [ "serde" ] } + +proxmox-sys = "0.2" # only needed foör nodename()?? \ No newline at end of file diff --git a/pbs-api-types/src/common_regex.rs b/pbs-api-types/src/common_regex.rs new file mode 100644 index 00000000..8fe30673 --- /dev/null +++ b/pbs-api-types/src/common_regex.rs @@ -0,0 +1,78 @@ +//! Predefined Regular Expressions +//! +//! This is a collection of useful regular expressions + +use lazy_static::lazy_static; +use regex::Regex; + +#[rustfmt::skip] +#[macro_export] +macro_rules! IPV4OCTET { () => (r"(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])") } +#[rustfmt::skip] +#[macro_export] +macro_rules! IPV6H16 { () => (r"(?:[0-9a-fA-F]{1,4})") } +#[rustfmt::skip] +#[macro_export] +macro_rules! IPV6LS32 { () => (concat!(r"(?:(?:", IPV4RE!(), "|", IPV6H16!(), ":", IPV6H16!(), "))" )) } + +/// Returns the regular expression string to match IPv4 addresses +#[rustfmt::skip] +#[macro_export] +macro_rules! IPV4RE { () => (concat!(r"(?:(?:", IPV4OCTET!(), r"\.){3}", IPV4OCTET!(), ")")) } + +/// Returns the regular expression string to match IPv6 addresses +#[rustfmt::skip] +#[macro_export] +macro_rules! IPV6RE { () => (concat!(r"(?:", + r"(?:(?:", r"(?:", IPV6H16!(), r":){6})", IPV6LS32!(), r")|", + r"(?:(?:", r"::(?:", IPV6H16!(), r":){5})", IPV6LS32!(), r")|", + r"(?:(?:(?:", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){4})", IPV6LS32!(), r")|", + r"(?:(?:(?:(?:", IPV6H16!(), r":){0,1}", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){3})", IPV6LS32!(), r")|", + r"(?:(?:(?:(?:", IPV6H16!(), r":){0,2}", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){2})", IPV6LS32!(), r")|", + r"(?:(?:(?:(?:", IPV6H16!(), r":){0,3}", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){1})", IPV6LS32!(), r")|", + r"(?:(?:(?:(?:", IPV6H16!(), r":){0,4}", IPV6H16!(), r")?::", ")", IPV6LS32!(), r")|", + r"(?:(?:(?:(?:", IPV6H16!(), r":){0,5}", IPV6H16!(), r")?::", ")", IPV6H16!(), r")|", + r"(?:(?:(?:(?:", IPV6H16!(), r":){0,6}", IPV6H16!(), r")?::", ")))")) +} + +/// Returns the regular expression string to match IP addresses (v4 or v6) +#[rustfmt::skip] +#[macro_export] +macro_rules! IPRE { () => (concat!(r"(?:", IPV4RE!(), "|", IPV6RE!(), ")")) } + +/// Regular expression string to match IP addresses where IPv6 addresses require brackets around +/// them, while for IPv4 they are forbidden. +#[rustfmt::skip] +#[macro_export] +macro_rules! IPRE_BRACKET { () => ( + concat!(r"(?:", + IPV4RE!(), + r"|\[(?:", + IPV6RE!(), + r")\]", + r")")) +} + +lazy_static! { + pub static ref IP_REGEX: Regex = Regex::new(concat!(r"^", IPRE!(), r"$")).unwrap(); + pub static ref IP_BRACKET_REGEX: Regex = + Regex::new(concat!(r"^", IPRE_BRACKET!(), r"$")).unwrap(); + pub static ref SHA256_HEX_REGEX: Regex = Regex::new(r"^[a-f0-9]{64}$").unwrap(); + pub static ref SYSTEMD_DATETIME_REGEX: Regex = + Regex::new(r"^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$").unwrap(); +} + +#[test] +fn test_regexes() { + assert!(IP_REGEX.is_match("127.0.0.1")); + assert!(IP_REGEX.is_match("::1")); + assert!(IP_REGEX.is_match("2014:b3a::27")); + assert!(IP_REGEX.is_match("2014:b3a::192.168.0.1")); + assert!(IP_REGEX.is_match("2014:b3a:0102:adf1:1234:4321:4afA:BCDF")); + + assert!(IP_BRACKET_REGEX.is_match("127.0.0.1")); + assert!(IP_BRACKET_REGEX.is_match("[::1]")); + assert!(IP_BRACKET_REGEX.is_match("[2014:b3a::27]")); + assert!(IP_BRACKET_REGEX.is_match("[2014:b3a::192.168.0.1]")); + assert!(IP_BRACKET_REGEX.is_match("[2014:b3a:0102:adf1:1234:4321:4afA:BCDF]")); +} diff --git a/pbs-api-types/src/crypto.rs b/pbs-api-types/src/crypto.rs index 6f931f8f..bd817034 100644 --- a/pbs-api-types/src/crypto.rs +++ b/pbs-api-types/src/crypto.rs @@ -51,7 +51,8 @@ impl std::str::FromStr for Fingerprint { fn from_str(s: &str) -> Result { let mut tmp = s.to_string(); tmp.retain(|c| c != ':'); - let bytes = proxmox::tools::hex_to_digest(&tmp)?; + let mut bytes = [0u8; 32]; + hex::decode_to_slice(&tmp, &mut bytes)?; Ok(Fingerprint::new(bytes)) } } diff --git a/pbs-api-types/src/human_byte.rs b/pbs-api-types/src/human_byte.rs index 895f029b..e5969875 100644 --- a/pbs-api-types/src/human_byte.rs +++ b/pbs-api-types/src/human_byte.rs @@ -216,8 +216,8 @@ impl std::str::FromStr for HumanByte { } } -proxmox::forward_deserialize_to_from_str!(HumanByte); -proxmox::forward_serialize_to_display!(HumanByte); +proxmox_serde::forward_deserialize_to_from_str!(HumanByte); +proxmox_serde::forward_serialize_to_display!(HumanByte); #[test] fn test_human_byte_parser() -> Result<(), Error> { diff --git a/pbs-api-types/src/jobs.rs b/pbs-api-types/src/jobs.rs index fe2383a5..d6479a98 100644 --- a/pbs-api-types/src/jobs.rs +++ b/pbs-api-types/src/jobs.rs @@ -365,8 +365,8 @@ impl std::fmt::Display for GroupFilter { } } -proxmox::forward_deserialize_to_from_str!(GroupFilter); -proxmox::forward_serialize_to_display!(GroupFilter); +proxmox_serde::forward_deserialize_to_from_str!(GroupFilter); +proxmox_serde::forward_serialize_to_display!(GroupFilter); fn verify_group_filter(input: &str) -> Result<(), anyhow::Error> { GroupFilter::from_str(input).map(|_| ()) diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index a28ddafd..bb5d152f 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -3,10 +3,11 @@ use serde::{Deserialize, Serialize}; use anyhow::bail; +pub mod common_regex; + use proxmox_schema::{ api, const_regex, ApiStringFormat, ApiType, ArraySchema, Schema, StringSchema, ReturnType, }; -use proxmox::{IPRE, IPRE_BRACKET, IPV4OCTET, IPV4RE, IPV6H16, IPV6LS32, IPV6RE}; use proxmox_time::parse_daily_duration; #[rustfmt::skip] @@ -199,7 +200,7 @@ pub const DNS_NAME_OR_IP_SCHEMA: Schema = StringSchema::new("DNS name or IP addr pub const NODE_SCHEMA: Schema = StringSchema::new("Node name (or 'localhost')") .format(&ApiStringFormat::VerifyFn(|node| { - if node == "localhost" || node == proxmox::tools::nodename() { + if node == "localhost" || node == proxmox_sys::nodename() { Ok(()) } else { bail!("no such node '{}'", node); diff --git a/pbs-api-types/src/remote.rs b/pbs-api-types/src/remote.rs index 15c336b7..b7cee5d3 100644 --- a/pbs-api-types/src/remote.rs +++ b/pbs-api-types/src/remote.rs @@ -79,7 +79,7 @@ pub struct Remote { pub name: String, // Note: The stored password is base64 encoded #[serde(skip_serializing_if="String::is_empty")] - #[serde(with = "proxmox::tools::serde::string_as_base64")] + #[serde(with = "proxmox_serde::string_as_base64")] pub password: String, #[serde(flatten)] pub config: RemoteConfig, diff --git a/pbs-api-types/src/tape/media_location.rs b/pbs-api-types/src/tape/media_location.rs index a05f1156..496bd000 100644 --- a/pbs-api-types/src/tape/media_location.rs +++ b/pbs-api-types/src/tape/media_location.rs @@ -22,8 +22,8 @@ pub enum MediaLocation { Vault(String), } -proxmox::forward_deserialize_to_from_str!(MediaLocation); -proxmox::forward_serialize_to_display!(MediaLocation); +proxmox_serde::forward_deserialize_to_from_str!(MediaLocation); +proxmox_serde::forward_serialize_to_display!(MediaLocation); impl proxmox_schema::ApiType for MediaLocation { const API_SCHEMA: Schema = StringSchema::new( diff --git a/pbs-api-types/src/userid.rs b/pbs-api-types/src/userid.rs index 8c58da2e..60137d2a 100644 --- a/pbs-api-types/src/userid.rs +++ b/pbs-api-types/src/userid.rs @@ -685,8 +685,8 @@ fn test_token_id() { assert_eq!(auth_id.to_string(), "test@pam!bar".to_string()); } -proxmox::forward_deserialize_to_from_str!(Userid); -proxmox::forward_serialize_to_display!(Userid); +proxmox_serde::forward_deserialize_to_from_str!(Userid); +proxmox_serde::forward_serialize_to_display!(Userid); -proxmox::forward_deserialize_to_from_str!(Authid); -proxmox::forward_serialize_to_display!(Authid); +proxmox_serde::forward_deserialize_to_from_str!(Authid); +proxmox_serde::forward_serialize_to_display!(Authid);