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 2A8868ADB0; Fri, 21 Oct 2022 15:03:31 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 337F922366; Fri, 21 Oct 2022 15:03:00 +0200 (CEST) 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; Fri, 21 Oct 2022 15:02:57 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id CC99344B27; Fri, 21 Oct 2022 15:02:56 +0200 (CEST) From: Fiona Ebner To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Fri, 21 Oct 2022 15:02:44 +0200 Message-Id: <20221021130252.176316-2-f.ebner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221021130252.176316-1-f.ebner@proxmox.com> References: <20221021130252.176316-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.028 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 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] Subject: [pve-devel] [PATCH proxmox 1/1] section config: parse additional properties when schema allows it X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Oct 2022 13:03:31 -0000 Additional properties will be parsed according to the default string schema. This is relevant for use cases when the full schema is not known for some reason or another. In particular this allows support for parsing older/newer versions of configuration files. One example of this is the proposed proxmox-mail-forward helper binary, which currently doesn't have access to the PBS API types for dependency reasons and is only interested in the email field for the root user. If it can only use a minimal schema with additional_properties set to true, it will be robust against changes. Writing already works, because the ObjectSchema's verify_json() already handles additional_properties correctly and format_section_content() handles them like all other properties (method doesn't depend on the schema). Signed-off-by: Fiona Ebner --- proxmox-section-config/src/lib.rs | 79 ++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/proxmox-section-config/src/lib.rs b/proxmox-section-config/src/lib.rs index 154f56e..080f23c 100644 --- a/proxmox-section-config/src/lib.rs +++ b/proxmox-section-config/src/lib.rs @@ -31,6 +31,9 @@ use proxmox_lang::try_block; use proxmox_schema::format::{dump_properties, wrap_text, ParameterDisplayStyle}; use proxmox_schema::*; +/// Used for additional properties when the schema allows them. +const ADDITIONAL_PROPERTY_SCHEMA: Schema = StringSchema::new("Additional property").schema(); + /// Associates a section type name with a `Schema`. pub struct SectionConfigPlugin { type_name: String, @@ -446,7 +449,10 @@ impl SectionConfig { (true, items) } Some((_optional, ref prop_schema)) => (false, prop_schema), - None => bail!("unknown property '{}'", key), + None => match plugin.properties.additional_properties() { + true => (false, &&ADDITIONAL_PROPERTY_SCHEMA), + false => bail!("unknown property '{}'", key), + }, }; let value = match prop_schema.parse_simple_value(&value) { @@ -884,6 +890,77 @@ lvmthin: local-lvm2 assert_eq!(raw, created); } +#[test] +fn test_section_config_with_additional_properties() { + let filename = "user.cfg"; + + const ID_SCHEMA: Schema = StringSchema::new("default id schema.") + .min_length(3) + .schema(); + let mut config = SectionConfig::new(&ID_SCHEMA); + let mut config_with_additional = SectionConfig::new(&ID_SCHEMA); + + const PROPERTIES: [(&str, bool, &proxmox_schema::Schema); 2] = [ + ( + "email", + false, + &StringSchema::new("The e-mail of the user").schema(), + ), + ( + "userid", + true, + &StringSchema::new("The id of the user (name@realm).") + .min_length(3) + .schema(), + ), + ]; + + const USER_PROPERTIES: ObjectSchema = ObjectSchema { + description: "user properties", + properties: &PROPERTIES, + additional_properties: false, + default_key: None, + }; + + const USER_PROPERTIES_WITH_ADDTIONAL: ObjectSchema = ObjectSchema { + description: "user properties with additional", + properties: &PROPERTIES, + additional_properties: true, + default_key: None, + }; + + let plugin = SectionConfigPlugin::new( + "user".to_string(), + Some("userid".to_string()), + &USER_PROPERTIES, + ); + config.register_plugin(plugin); + + let plugin = SectionConfigPlugin::new( + "user".to_string(), + Some("userid".to_string()), + &USER_PROPERTIES_WITH_ADDTIONAL, + ); + config_with_additional.register_plugin(plugin); + + let raw = r" + +user: root@pam + email root@example.com + shinynewoption somevalue +"; + + let res = config_with_additional.parse(filename, raw); + println!("RES: {:?}", res); + let written = config_with_additional.write(filename, &res.unwrap()); + println!("CONFIG:\n{}", written.unwrap()); + + assert!(config.parse(filename, raw).is_err()); + // SectionConfigData doesn't have Clone and it would only be needed here currently. + let res = config_with_additional.parse(filename, raw); + assert!(config.write(filename, &res.unwrap()).is_err()); +} + /// Generate ReST Documentaion for ``SectionConfig`` pub fn dump_section_config(config: &SectionConfig) -> String { let mut res = String::new(); -- 2.30.2