fix #3067: api: add support for multi-line comments in node.cfg
add support for multi-line comments to node.cfg and the api, similar to how pve handles multi-line comments Signed-off-by: Stefan Sterz <s.sterz@proxmox.com> Acked-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
		
				
					committed by
					
						 Thomas Lamprecht
						Thomas Lamprecht
					
				
			
			
				
	
			
			
			
						parent
						
							9e2b423e27
						
					
				
				
					commit
					2b422b82fb
				
			| @ -137,6 +137,8 @@ const_regex! { | |||||||
|  |  | ||||||
|     pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$"; |     pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$"; | ||||||
|  |  | ||||||
|  |     pub MULTI_LINE_COMMENT_REGEX = r"(?m)^([[:^cntrl:]]*)$"; | ||||||
|  |  | ||||||
|     pub BACKUP_REPO_URL_REGEX = concat!( |     pub BACKUP_REPO_URL_REGEX = concat!( | ||||||
|         r"^^(?:(?:(", |         r"^^(?:(?:(", | ||||||
|         USER_ID_REGEX_STR!(), "|", APITOKEN_ID_REGEX_STR!(), |         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) |     .format(&SINGLE_LINE_COMMENT_FORMAT) | ||||||
|     .schema(); |     .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.") | pub const SUBSCRIPTION_KEY_SCHEMA: Schema = StringSchema::new("Proxmox Backup Server subscription key.") | ||||||
|     .format(&SUBSCRIPTION_KEY_FORMAT) |     .format(&SUBSCRIPTION_KEY_FORMAT) | ||||||
|     .min_length(15) |     .min_length(15) | ||||||
|  | |||||||
| @ -64,6 +64,8 @@ pub enum DeletableProperty { | |||||||
|     ciphers_tls_1_2, |     ciphers_tls_1_2, | ||||||
|     /// Delete the default-lang property. |     /// Delete the default-lang property. | ||||||
|     default_lang, |     default_lang, | ||||||
|  |     /// Delete any description | ||||||
|  |     description, | ||||||
| } | } | ||||||
|  |  | ||||||
| #[api( | #[api( | ||||||
| @ -124,6 +126,7 @@ pub fn update_node_config( | |||||||
|                 DeletableProperty::ciphers_tls_1_3 => { config.ciphers_tls_1_3 = None; }, |                 DeletableProperty::ciphers_tls_1_3 => { config.ciphers_tls_1_3 = None; }, | ||||||
|                 DeletableProperty::ciphers_tls_1_2 => { config.ciphers_tls_1_2 = None; }, |                 DeletableProperty::ciphers_tls_1_2 => { config.ciphers_tls_1_2 = None; }, | ||||||
|                 DeletableProperty::default_lang => { config.default_lang = None; }, |                 DeletableProperty::default_lang => { config.default_lang = None; }, | ||||||
|  |                 DeletableProperty::description => { config.description = None; }, | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -139,6 +142,7 @@ pub fn update_node_config( | |||||||
|     if update.ciphers_tls_1_3.is_some() { config.ciphers_tls_1_3 = update.ciphers_tls_1_3; } |     if update.ciphers_tls_1_3.is_some() { config.ciphers_tls_1_3 = update.ciphers_tls_1_3; } | ||||||
|     if update.ciphers_tls_1_2.is_some() { config.ciphers_tls_1_2 = update.ciphers_tls_1_2; } |     if update.ciphers_tls_1_2.is_some() { config.ciphers_tls_1_2 = update.ciphers_tls_1_2; } | ||||||
|     if update.default_lang.is_some() { config.default_lang = update.default_lang; } |     if update.default_lang.is_some() { config.default_lang = update.default_lang; } | ||||||
|  |     if update.description.is_some() { config.description = update.description; } | ||||||
|  |  | ||||||
|     crate::config::node::save_config(&config)?; |     crate::config::node::save_config(&config)?; | ||||||
|  |  | ||||||
|  | |||||||
| @ -8,7 +8,11 @@ use proxmox_schema::{api, ApiStringFormat, ApiType, Updater}; | |||||||
|  |  | ||||||
| use proxmox_http::ProxyConfig; | use proxmox_http::ProxyConfig; | ||||||
|  |  | ||||||
| use pbs_api_types::{EMAIL_SCHEMA, OPENSSL_CIPHERS_TLS_1_2_SCHEMA, OPENSSL_CIPHERS_TLS_1_3_SCHEMA}; | use pbs_api_types::{ | ||||||
|  |     EMAIL_SCHEMA, MULTI_LINE_COMMENT_SCHEMA, OPENSSL_CIPHERS_TLS_1_2_SCHEMA, | ||||||
|  |     OPENSSL_CIPHERS_TLS_1_3_SCHEMA, | ||||||
|  | }; | ||||||
|  |  | ||||||
| use pbs_buildcfg::configdir; | use pbs_buildcfg::configdir; | ||||||
| use pbs_config::{open_backup_lockfile, BackupLockGuard}; | use pbs_config::{open_backup_lockfile, BackupLockGuard}; | ||||||
|  |  | ||||||
| @ -167,6 +171,10 @@ pub enum Translation { | |||||||
|         "default-lang" : { |         "default-lang" : { | ||||||
|             schema: Translation::API_SCHEMA, |             schema: Translation::API_SCHEMA, | ||||||
|             optional: true, |             optional: true, | ||||||
|  |         }, | ||||||
|  |         "description" : { | ||||||
|  |             optional: true, | ||||||
|  |             schema: MULTI_LINE_COMMENT_SCHEMA, | ||||||
|         } |         } | ||||||
|     }, |     }, | ||||||
| )] | )] | ||||||
| @ -210,6 +218,10 @@ pub struct NodeConfig { | |||||||
|     /// Default language used in the GUI |     /// Default language used in the GUI | ||||||
|     #[serde(skip_serializing_if = "Option::is_none")] |     #[serde(skip_serializing_if = "Option::is_none")] | ||||||
|     pub default_lang: Option<String>, |     pub default_lang: Option<String>, | ||||||
|  |  | ||||||
|  |     /// Node description | ||||||
|  |     #[serde(skip_serializing_if = "Option::is_none")] | ||||||
|  |     pub description: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl NodeConfig { | impl NodeConfig { | ||||||
|  | |||||||
| @ -31,8 +31,19 @@ pub fn value_from_str(input: &str, schema: &'static Schema) -> Result<Value, Err | |||||||
|     let schema = object_schema(schema)?; |     let schema = object_schema(schema)?; | ||||||
|  |  | ||||||
|     let mut config = Object::new(); |     let mut config = Object::new(); | ||||||
|  |     let mut lines = input.lines().enumerate().peekable(); | ||||||
|  |     let mut description = String::new(); | ||||||
|  |  | ||||||
|     for (lineno, line) in input.lines().enumerate() { |     while let Some((_, line)) = lines.next_if(|(_, line)| line.starts_with('#')) { | ||||||
|  |         description.push_str(&line[1..]); | ||||||
|  |         description.push('\n'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if !description.is_empty() { | ||||||
|  |         config.insert("description".to_string(), Value::String(description)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     for (lineno, line) in lines { | ||||||
|         let line = line.trim(); |         let line = line.trim(); | ||||||
|         if line.starts_with('#') || line.is_empty() { |         if line.starts_with('#') || line.is_empty() { | ||||||
|             continue; |             continue; | ||||||
| @ -133,8 +144,16 @@ pub fn value_to_bytes(value: &Value, schema: &'static Schema) -> Result<Vec<u8>, | |||||||
|  |  | ||||||
| /// Note: the object must have already been verified at this point. | /// Note: the object must have already been verified at this point. | ||||||
| fn object_to_writer(output: &mut dyn Write, object: &Object) -> Result<(), Error> { | fn object_to_writer(output: &mut dyn Write, object: &Object) -> Result<(), Error> { | ||||||
|  |     // special key `description` for multi-line notes, must be written before everything else | ||||||
|  |     if let Some(Value::String(description)) = object.get("description") { | ||||||
|  |         for lines in description.lines() { | ||||||
|  |             writeln!(output, "#{}", lines)?; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     for (key, value) in object.iter() { |     for (key, value) in object.iter() { | ||||||
|         match value { |         match value { | ||||||
|  |             _ if key == "description" => continue, // skip description as we handle it above | ||||||
|             Value::Null => continue,           // delete this entry |             Value::Null => continue,           // delete this entry | ||||||
|             Value::Bool(v) => writeln!(output, "{}: {}", key, v)?, |             Value::Bool(v) => writeln!(output, "{}: {}", key, v)?, | ||||||
|             Value::String(v) => { |             Value::String(v) => { | ||||||
| @ -172,3 +191,36 @@ fn test() { | |||||||
|  |  | ||||||
|     assert_eq!(config, NODE_CONFIG.as_bytes()); |     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"); | ||||||
|  |  | ||||||
|  |     let config = to_bytes(&data, &NodeConfig::API_SCHEMA) | ||||||
|  |         .expect("failed to serialize multi-line notes node config"); | ||||||
|  |  | ||||||
|  |     assert_eq!(config, NODE_OUTPUT.as_bytes()); | ||||||
|  | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user