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 <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-05-22 14:51:45 +02:00 committed by Dietmar Maurer
parent bfa0146c00
commit 83fd4b3b1b
2 changed files with 15 additions and 27 deletions

View File

@ -16,27 +16,8 @@ use crate::config::acl::{PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY};
description: "The list of configured remotes (with config digest).", description: "The list of configured remotes (with config digest).",
type: Array, type: Array,
items: { items: {
type: Object, type: remote::Remote,
description: "Remote configuration (without password).", 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: { access: {
@ -47,14 +28,20 @@ use crate::config::acl::{PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY};
pub fn list_remotes( pub fn list_remotes(
_param: Value, _param: Value,
_info: &ApiMethod, _info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment, mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Vec<remote::Remote>, Error> {
let (config, digest) = remote::config()?; let (config, digest) = remote::config()?;
let value = config.convert_to_array("name", Some(&digest), &["password"]); let mut list: Vec<remote::Remote> = 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( #[api(
@ -128,10 +115,10 @@ pub fn read_remote(
name: String, name: String,
_info: &ApiMethod, _info: &ApiMethod,
mut rpcenv: &mut dyn RpcEnvironment, mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<remote::Remote, Error> {
let (config, digest) = remote::config()?; let (config, digest) = remote::config()?;
let mut data = config.lookup_json("remote", &name)?; let mut data: remote::Remote = config.lookup("remote", &name)?;
data.as_object_mut().unwrap().remove("password"); data.password = "".to_string(); // do not return password in api
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
Ok(data) Ok(data)
} }

View File

@ -59,6 +59,7 @@ pub struct Remote {
pub comment: Option<String>, pub comment: Option<String>,
pub host: String, pub host: String,
pub userid: String, pub userid: String,
#[serde(skip_serializing_if="String::is_empty")]
pub password: String, pub password: String,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub fingerprint: Option<String>, pub fingerprint: Option<String>,