From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 8FD0B62944 for ; Tue, 22 Feb 2022 12:26:42 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 869A723600 for ; Tue, 22 Feb 2022 12:26:12 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 9C03323592 for ; Tue, 22 Feb 2022 12:26:06 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5D8CF417B0 for ; Tue, 22 Feb 2022 12:26:06 +0100 (CET) From: Stefan Sterz To: pbs-devel@lists.proxmox.com Date: Tue, 22 Feb 2022 12:25:54 +0100 Message-Id: <20220222112556.3747239-4-s.sterz@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220222112556.3747239-1-s.sterz@proxmox.com> References: <20220222112556.3747239-1-s.sterz@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.000 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [lib.rs, config.rs, node.rs] Subject: [pbs-devel] [PATCH proxmox-backup 3/5] fix #3067: api: add multi-line comments to node.cfg X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Feb 2022 11:26:42 -0000 add support for multiline comments to node.cfg, similar to how pve handles multi-line comments Signed-off-by: Stefan Sterz --- pbs-api-types/src/lib.rs | 9 ++++++ src/config/node.rs | 4 +-- src/tools/config.rs | 62 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index 754e7b22..3892980d 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -137,6 +137,8 @@ const_regex! { pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$"; + pub MULTI_LINE_COMMENT_REGEX = r"(?m)^([[:^cntrl:]]*)\s*$"; + pub BACKUP_REPO_URL_REGEX = concat!( r"^^(?:(?:(", USER_ID_REGEX_STR!(), "|", APITOKEN_ID_REGEX_STR!(), @@ -273,6 +275,13 @@ pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (singl .format(&SINGLE_LINE_COMMENT_FORMAT) .schema(); +pub const MULTI_LINE_COMMENT_FORMAT: ApiStringFormat = + ApiStringFormat::Pattern(&MULTI_LINE_COMMENT_REGEX); + +pub const MULTI_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (multiple lines).") + .format(&MULTI_LINE_COMMENT_FORMAT) + .schema(); + pub const SUBSCRIPTION_KEY_SCHEMA: Schema = StringSchema::new("Proxmox Backup Server subscription key.") .format(&SUBSCRIPTION_KEY_FORMAT) .min_length(15) diff --git a/src/config/node.rs b/src/config/node.rs index 9ca44a52..bb915f94 100644 --- a/src/config/node.rs +++ b/src/config/node.rs @@ -9,7 +9,7 @@ use proxmox_schema::{api, ApiStringFormat, ApiType, Updater}; use proxmox_http::ProxyConfig; use pbs_api_types::{EMAIL_SCHEMA, OPENSSL_CIPHERS_TLS_1_2_SCHEMA, OPENSSL_CIPHERS_TLS_1_3_SCHEMA, - SINGLE_LINE_COMMENT_SCHEMA}; + MULTI_LINE_COMMENT_SCHEMA}; use pbs_buildcfg::configdir; use pbs_config::{open_backup_lockfile, BackupLockGuard}; @@ -171,7 +171,7 @@ pub enum Translation { }, "comment" : { optional: true, - schema: SINGLE_LINE_COMMENT_SCHEMA, + schema: MULTI_LINE_COMMENT_SCHEMA, } }, )] diff --git a/src/tools/config.rs b/src/tools/config.rs index f666a8ab..738ab541 100644 --- a/src/tools/config.rs +++ b/src/tools/config.rs @@ -32,6 +32,20 @@ pub fn value_from_str(input: &str, schema: &'static Schema) -> Result Result, /// Note: the object must have already been verified at this point. fn object_to_writer(output: &mut dyn Write, object: &Object) -> Result<(), Error> { + // special key `comment` for multi-line notes + if object.contains_key("comment") { + let comment = match object.get("comment") { + Some(Value::String(v)) => v, + _ => bail!("only strings can be comments"), + }; + + for lines in comment.lines() { + writeln!(output, "#{}", lines)?; + } + } + for (key, value) in object.iter() { match value { Value::Null => continue, // delete this entry Value::Bool(v) => writeln!(output, "{}: {}", key, v)?, + Value::String(_) if key == "comment" => continue, // skip comment as we handle it above Value::String(v) => { if v.as_bytes().contains(&b'\n') { bail!("value for {} contains newlines", key); @@ -172,3 +199,38 @@ fn test() { assert_eq!(config, NODE_CONFIG.as_bytes()); } + +#[test] +fn test_with_comment() { + use proxmox_schema::ApiType; + + // let's just reuse some schema we actually have available: + use crate::config::node::NodeConfig; + + const NODE_INPUT: &str = "\ + #this should\n\ + #be included\n\ + acme: account=pebble\n\ + # this should not\n\ + acmedomain0: test1.invalid.local,plugin=power\n\ + acmedomain1: test2.invalid.local\n\ + "; + + const NODE_OUTPUT: &str = "\ + #this should\n\ + #be included\n\ + acme: account=pebble\n\ + acmedomain0: test1.invalid.local,plugin=power\n\ + acmedomain1: test2.invalid.local\n\ + "; + + let data: NodeConfig = from_str(NODE_INPUT, &NodeConfig::API_SCHEMA) + .expect("failed to parse multi-line notes node config"); + + println!("{}", data.comment.as_ref().expect("no comment was parsed")); + + let config = to_bytes(&data, &NodeConfig::API_SCHEMA) + .expect("failed to serialize multi-line notes node config"); + + assert_eq!(config, NODE_OUTPUT.as_bytes()); +} -- 2.30.2