2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-01-10 12:28:15 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2020-04-17 12:57:26 +00:00
|
|
|
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-13 10:47:07 +00:00
|
|
|
use crate::api2::types::*;
|
2020-01-16 13:32:06 +00:00
|
|
|
use crate::config::remote;
|
2020-04-17 12:57:26 +00:00
|
|
|
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
|
2020-01-10 12:28:15 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
2020-01-14 13:55:58 +00:00
|
|
|
description: "The list of configured remotes (with config digest).",
|
2020-01-10 12:28:15 +00:00
|
|
|
type: Array,
|
|
|
|
items: {
|
2020-01-30 12:26:46 +00:00
|
|
|
type: Object,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
|
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
)]
|
|
|
|
/// List all remotes
|
|
|
|
pub fn list_remotes(
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let (config, digest) = remote::config()?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-30 12:26:46 +00:00
|
|
|
let value = config.convert_to_array("name", Some(&digest), &["password"]);
|
|
|
|
|
|
|
|
Ok(value.into())
|
2020-01-10 12:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
2020-01-11 09:42:09 +00:00
|
|
|
protected: true,
|
2020-01-10 12:28:15 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-01-13 10:47:07 +00:00
|
|
|
schema: REMOTE_ID_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
2020-01-13 11:02:13 +00:00
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
host: {
|
2020-01-13 13:17:22 +00:00
|
|
|
schema: DNS_NAME_OR_IP_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
userid: {
|
2020-01-13 13:17:22 +00:00
|
|
|
schema: PROXMOX_USER_ID_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
password: {
|
2020-01-16 13:32:06 +00:00
|
|
|
schema: remote::REMOTE_PASSWORD_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
2020-01-25 08:48:39 +00:00
|
|
|
fingerprint: {
|
|
|
|
optional: true,
|
|
|
|
schema: CERT_FINGERPRINT_SHA256_SCHEMA,
|
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
|
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
)]
|
|
|
|
/// Create new remote.
|
|
|
|
pub fn create_remote(name: String, param: Value) -> Result<(), Error> {
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let _lock = crate::tools::open_file_locked(remote::REMOTE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let remote: remote::Remote = serde_json::from_value(param.clone())?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let (mut config, _digest) = remote::config()?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
|
|
|
if let Some(_) = config.sections.get(&name) {
|
|
|
|
bail!("remote '{}' already exists.", name);
|
|
|
|
}
|
|
|
|
|
2020-01-11 10:09:27 +00:00
|
|
|
config.set_data(&name, "remote", &remote)?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
remote::save_config(&config)?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:20:16 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: REMOTE_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-14 13:55:58 +00:00
|
|
|
returns: {
|
|
|
|
description: "The remote configuration (with config digest).",
|
2020-01-16 13:32:06 +00:00
|
|
|
type: remote::Remote,
|
2020-01-14 13:55:58 +00:00
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
|
|
|
|
}
|
2020-01-14 13:20:16 +00:00
|
|
|
)]
|
|
|
|
/// Read remote configuration data.
|
|
|
|
pub fn read_remote(name: String) -> Result<Value, Error> {
|
2020-01-16 13:32:06 +00:00
|
|
|
let (config, digest) = remote::config()?;
|
2020-01-14 13:20:16 +00:00
|
|
|
let mut data = config.lookup_json("remote", &name)?;
|
|
|
|
data.as_object_mut().unwrap()
|
|
|
|
.insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into());
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: REMOTE_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
|
|
|
},
|
|
|
|
host: {
|
|
|
|
optional: true,
|
|
|
|
schema: DNS_NAME_OR_IP_SCHEMA,
|
|
|
|
},
|
|
|
|
userid: {
|
|
|
|
optional: true,
|
|
|
|
schema: PROXMOX_USER_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
optional: true,
|
2020-01-16 13:32:06 +00:00
|
|
|
schema: remote::REMOTE_PASSWORD_SCHEMA,
|
2020-01-14 13:20:16 +00:00
|
|
|
},
|
2020-01-25 08:48:39 +00:00
|
|
|
fingerprint: {
|
|
|
|
optional: true,
|
|
|
|
schema: CERT_FINGERPRINT_SHA256_SCHEMA,
|
|
|
|
},
|
2020-01-15 11:27:05 +00:00
|
|
|
digest: {
|
|
|
|
optional: true,
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
},
|
2020-01-14 13:20:16 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
|
|
|
|
},
|
2020-01-14 13:20:16 +00:00
|
|
|
)]
|
|
|
|
/// Update remote configuration.
|
|
|
|
pub fn update_remote(
|
|
|
|
name: String,
|
|
|
|
comment: Option<String>,
|
|
|
|
host: Option<String>,
|
|
|
|
userid: Option<String>,
|
|
|
|
password: Option<String>,
|
2020-01-25 08:48:39 +00:00
|
|
|
fingerprint: Option<String>,
|
2020-01-15 11:27:05 +00:00
|
|
|
digest: Option<String>,
|
2020-01-14 13:20:16 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let _lock = crate::tools::open_file_locked(remote::REMOTE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
|
2020-01-15 10:57:12 +00:00
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let (mut config, expected_digest) = remote::config()?;
|
2020-01-15 11:27:05 +00:00
|
|
|
|
|
|
|
if let Some(ref digest) = digest {
|
|
|
|
let digest = proxmox::tools::hex_to_digest(digest)?;
|
|
|
|
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
|
|
|
|
}
|
2020-01-14 13:20:16 +00:00
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let mut data: remote::Remote = config.lookup("remote", &name)?;
|
2020-01-14 13:20:16 +00:00
|
|
|
|
|
|
|
if let Some(comment) = comment {
|
|
|
|
let comment = comment.trim().to_string();
|
|
|
|
if comment.is_empty() {
|
|
|
|
data.comment = None;
|
|
|
|
} else {
|
|
|
|
data.comment = Some(comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(host) = host { data.host = host; }
|
|
|
|
if let Some(userid) = userid { data.userid = userid; }
|
|
|
|
if let Some(password) = password { data.password = password; }
|
|
|
|
|
2020-01-25 08:48:39 +00:00
|
|
|
// fixme: howto delete a fingeprint?
|
|
|
|
if let Some(fingerprint) = fingerprint { data.fingerprint = Some(fingerprint); }
|
|
|
|
|
2020-01-14 13:20:16 +00:00
|
|
|
config.set_data(&name, "remote", &data)?;
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
remote::save_config(&config)?;
|
2020-01-14 13:20:16 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:28:15 +00:00
|
|
|
#[api(
|
2020-01-11 09:42:09 +00:00
|
|
|
protected: true,
|
2020-01-10 12:28:15 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-01-13 10:47:07 +00:00
|
|
|
schema: REMOTE_ID_SCHEMA,
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
|
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
)]
|
|
|
|
/// Remove a remote from the configuration file.
|
|
|
|
pub fn delete_remote(name: String) -> Result<(), Error> {
|
|
|
|
|
|
|
|
// fixme: locking ?
|
|
|
|
// fixme: check digest ?
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let (mut config, _digest) = remote::config()?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
|
|
|
match config.sections.get(&name) {
|
|
|
|
Some(_) => { config.sections.remove(&name); },
|
|
|
|
None => bail!("remote '{}' does not exist.", name),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:20:16 +00:00
|
|
|
const ITEM_ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_READ_REMOTE)
|
|
|
|
.put(&API_METHOD_UPDATE_REMOTE)
|
|
|
|
.delete(&API_METHOD_DELETE_REMOTE);
|
|
|
|
|
2020-01-10 12:28:15 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_LIST_REMOTES)
|
|
|
|
.post(&API_METHOD_CREATE_REMOTE)
|
2020-01-14 13:20:16 +00:00
|
|
|
.match_all("name", &ITEM_ROUTER);
|