From 83fd4b3b1b228d190ac19f22c78e366efcf485b1 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Fri, 22 May 2020 14:51:45 +0200 Subject: [PATCH] remote: try to use Struct for api with a catch: password is in the struct but we do not want it to return via the api, so we only 'serialize' it when the string is not empty (this can only happen when the format is not checked by us, iow. when its returned from the api) and setting it manually to "" when we return remotes from the api this way we can still use the type but do not return the password Signed-off-by: Dominik Csapak --- src/api2/config/remote.rs | 41 +++++++++++++-------------------------- src/config/remote.rs | 1 + 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/api2/config/remote.rs b/src/api2/config/remote.rs index 8f17455d..1a93c519 100644 --- a/src/api2/config/remote.rs +++ b/src/api2/config/remote.rs @@ -16,27 +16,8 @@ use crate::config::acl::{PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY}; description: "The list of configured remotes (with config digest).", type: Array, items: { - type: Object, + type: remote::Remote, description: "Remote configuration (without password).", - properties: { - name: { - schema: REMOTE_ID_SCHEMA, - }, - comment: { - optional: true, - schema: SINGLE_LINE_COMMENT_SCHEMA, - }, - host: { - schema: DNS_NAME_OR_IP_SCHEMA, - }, - userid: { - schema: PROXMOX_USER_ID_SCHEMA, - }, - fingerprint: { - optional: true, - schema: CERT_FINGERPRINT_SHA256_SCHEMA, - }, - }, }, }, access: { @@ -47,14 +28,20 @@ use crate::config::acl::{PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY}; pub fn list_remotes( _param: Value, _info: &ApiMethod, - _rpcenv: &mut dyn RpcEnvironment, -) -> Result { + mut rpcenv: &mut dyn RpcEnvironment, +) -> Result, Error> { let (config, digest) = remote::config()?; - let value = config.convert_to_array("name", Some(&digest), &["password"]); + let mut list: Vec = config.convert_to_typed_array("remote")?; - Ok(value.into()) + // don't return password in api + for remote in &mut list { + remote.password = "".to_string(); + } + + rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); + Ok(list) } #[api( @@ -128,10 +115,10 @@ pub fn read_remote( name: String, _info: &ApiMethod, mut rpcenv: &mut dyn RpcEnvironment, -) -> Result { +) -> Result { let (config, digest) = remote::config()?; - let mut data = config.lookup_json("remote", &name)?; - data.as_object_mut().unwrap().remove("password"); + let mut data: remote::Remote = config.lookup("remote", &name)?; + data.password = "".to_string(); // do not return password in api rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); Ok(data) } diff --git a/src/config/remote.rs b/src/config/remote.rs index 50c59c6f..653904d1 100644 --- a/src/config/remote.rs +++ b/src/config/remote.rs @@ -59,6 +59,7 @@ pub struct Remote { pub comment: Option, pub host: String, pub userid: String, + #[serde(skip_serializing_if="String::is_empty")] pub password: String, #[serde(skip_serializing_if="Option::is_none")] pub fingerprint: Option,