move id and single line comment format to pbs-api-types

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-07-07 09:49:35 +02:00
parent 155f657f6b
commit 3c430e9a55
4 changed files with 36 additions and 28 deletions

View File

@ -3,8 +3,26 @@
use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema}; use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema};
use proxmox::const_regex; use proxmox::const_regex;
#[macro_export]
macro_rules! PROXMOX_SAFE_ID_REGEX_STR {
() => {
r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)"
};
}
const_regex! { const_regex! {
pub FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$"; pub FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$";
/// Regex for safe identifiers.
///
/// This
/// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
/// contains further information why it is reasonable to restict
/// names this way. This is not only useful for filenames, but for
/// any identifier command line tools work with.
pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
} }
pub const FINGERPRINT_SHA256_FORMAT: ApiStringFormat = pub const FINGERPRINT_SHA256_FORMAT: ApiStringFormat =
@ -14,3 +32,13 @@ pub const CERT_FINGERPRINT_SHA256_SCHEMA: Schema =
StringSchema::new("X509 certificate fingerprint (sha256).") StringSchema::new("X509 certificate fingerprint (sha256).")
.format(&FINGERPRINT_SHA256_FORMAT) .format(&FINGERPRINT_SHA256_FORMAT)
.schema(); .schema();
pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX);
pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).")
.format(&SINGLE_LINE_COMMENT_FORMAT)
.schema();

View File

@ -1,4 +0,0 @@
//! Macros exported from api2::types.
#[macro_export]
macro_rules! PROXMOX_SAFE_ID_REGEX_STR { () => (r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)") }

View File

@ -19,9 +19,6 @@ use crate::{
config::acl::Role, config::acl::Role,
}; };
#[macro_use]
mod macros;
#[macro_use] #[macro_use]
mod userid; mod userid;
pub use userid::{Realm, RealmRef}; pub use userid::{Realm, RealmRef};
@ -44,6 +41,12 @@ pub use pbs_api_types::{
CERT_FINGERPRINT_SHA256_SCHEMA, CERT_FINGERPRINT_SHA256_SCHEMA,
FINGERPRINT_SHA256_FORMAT, FINGERPRINT_SHA256_FORMAT,
FINGERPRINT_SHA256_REGEX, FINGERPRINT_SHA256_REGEX,
PROXMOX_SAFE_ID_FORMAT,
PROXMOX_SAFE_ID_REGEX,
PROXMOX_SAFE_ID_REGEX_STR,
SINGLE_LINE_COMMENT_FORMAT,
SINGLE_LINE_COMMENT_REGEX,
SINGLE_LINE_COMMENT_SCHEMA,
}; };
// File names: may not contain slashes, may not start with "." // File names: may not contain slashes, may not start with "."
@ -92,22 +95,11 @@ const_regex!{
pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
/// Regex for safe identifiers.
///
/// This
/// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
/// contains further information why it is reasonable to restict
/// names this way. This is not only useful for filenames, but for
/// any identifier command line tools work with.
pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
/// Regex for verification jobs 'DATASTORE:ACTUAL_JOB_ID' /// Regex for verification jobs 'DATASTORE:ACTUAL_JOB_ID'
pub VERIFICATION_JOB_WORKER_ID_REGEX = concat!(r"^(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):"); pub VERIFICATION_JOB_WORKER_ID_REGEX = concat!(r"^(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):");
/// Regex for sync jobs 'REMOTE:REMOTE_DATASTORE:LOCAL_DATASTORE:ACTUAL_JOB_ID' /// Regex for sync jobs 'REMOTE:REMOTE_DATASTORE:LOCAL_DATASTORE:ACTUAL_JOB_ID'
pub SYNC_JOB_WORKER_ID_REGEX = concat!(r"^(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):"); pub SYNC_JOB_WORKER_ID_REGEX = concat!(r"^(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):(", PROXMOX_SAFE_ID_REGEX_STR!(), r"):");
pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
pub HOSTNAME_REGEX = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)$"; pub HOSTNAME_REGEX = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)$";
pub DNS_NAME_REGEX = concat!(r"^", DNS_NAME!(), r"$"); pub DNS_NAME_REGEX = concat!(r"^", DNS_NAME!(), r"$");
@ -160,18 +152,12 @@ pub const IP_FORMAT: ApiStringFormat =
pub const PVE_CONFIG_DIGEST_FORMAT: ApiStringFormat = pub const PVE_CONFIG_DIGEST_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&SHA256_HEX_REGEX); ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
pub const BACKUP_ID_FORMAT: ApiStringFormat = pub const BACKUP_ID_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&BACKUP_ID_REGEX); ApiStringFormat::Pattern(&BACKUP_ID_REGEX);
pub const UUID_FORMAT: ApiStringFormat = pub const UUID_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&UUID_REGEX); ApiStringFormat::Pattern(&UUID_REGEX);
pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX);
pub const HOSTNAME_FORMAT: ApiStringFormat = pub const HOSTNAME_FORMAT: ApiStringFormat =
ApiStringFormat::Pattern(&HOSTNAME_REGEX); ApiStringFormat::Pattern(&HOSTNAME_REGEX);
@ -486,10 +472,6 @@ pub const VERIFICATION_OUTDATED_AFTER_SCHEMA: Schema = IntegerSchema::new(
.minimum(1) .minimum(1)
.schema(); .schema();
pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).")
.format(&SINGLE_LINE_COMMENT_FORMAT)
.schema();
pub const HOSTNAME_SCHEMA: Schema = StringSchema::new("Hostname (as defined in RFC1123).") pub const HOSTNAME_SCHEMA: Schema = StringSchema::new("Hostname (as defined in RFC1123).")
.format(&HOSTNAME_FORMAT) .format(&HOSTNAME_FORMAT)
.schema(); .schema();

View File

@ -33,6 +33,8 @@ use proxmox::api::api;
use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema}; use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema};
use proxmox::const_regex; use proxmox::const_regex;
use super::PROXMOX_SAFE_ID_REGEX_STR;
// we only allow a limited set of characters // we only allow a limited set of characters
// colon is not allowed, because we store usernames in // colon is not allowed, because we store usernames in
// colon separated lists)! // colon separated lists)!