2020-11-05 11:12:23 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2020-01-10 12:28:15 +00:00
|
|
|
use serde_json::Value;
|
2020-04-29 07:04:17 +00:00
|
|
|
use ::serde::{Deserialize, Serialize};
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-04-17 12:57:26 +00:00
|
|
|
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
|
2020-11-05 11:12:23 +00:00
|
|
|
use proxmox::http_err;
|
2020-08-04 09:33:02 +00:00
|
|
|
use proxmox::tools::fs::open_file_locked;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-13 10:47:07 +00:00
|
|
|
use crate::api2::types::*;
|
2020-11-05 11:12:23 +00:00
|
|
|
use crate::client::{HttpClient, HttpClientOptions};
|
2020-10-30 11:36:42 +00:00
|
|
|
use crate::config::cached_user_info::CachedUserInfo;
|
2020-01-16 13:32:06 +00:00
|
|
|
use crate::config::remote;
|
2020-04-29 05:03:44 +00:00
|
|
|
use crate::config::acl::{PRIV_REMOTE_AUDIT, PRIV_REMOTE_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,
|
2020-12-09 09:54:38 +00:00
|
|
|
items: { type: remote::Remote },
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
2020-10-30 11:36:42 +00:00
|
|
|
description: "List configured remotes filtered by Remote.Audit privileges",
|
|
|
|
permission: &Permission::Anybody,
|
2020-04-17 12:57:26 +00:00
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
)]
|
|
|
|
/// List all remotes
|
|
|
|
pub fn list_remotes(
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
2020-05-22 12:51:45 +00:00
|
|
|
mut rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Vec<remote::Remote>, Error> {
|
2020-10-30 11:36:42 +00:00
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let (config, digest) = remote::config()?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-05-22 12:51:45 +00:00
|
|
|
let mut list: Vec<remote::Remote> = config.convert_to_typed_array("remote")?;
|
|
|
|
// don't return password in api
|
|
|
|
for remote in &mut list {
|
|
|
|
remote.password = "".to_string();
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:36:42 +00:00
|
|
|
let list = list
|
|
|
|
.into_iter()
|
|
|
|
.filter(|remote| {
|
|
|
|
let privs = user_info.lookup_privs(&auth_id, &["remote", &remote.name]);
|
|
|
|
privs & PRIV_REMOTE_AUDIT != 0
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-05-22 12:51:45 +00:00
|
|
|
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
|
|
|
|
Ok(list)
|
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
|
|
|
},
|
2020-09-29 14:18:58 +00:00
|
|
|
port: {
|
|
|
|
description: "The (optional) port.",
|
|
|
|
type: u16,
|
|
|
|
optional: true,
|
|
|
|
default: 8007,
|
|
|
|
},
|
2020-11-05 11:12:25 +00:00
|
|
|
"auth-id": {
|
2020-10-23 11:33:21 +00:00
|
|
|
type: Authid,
|
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: {
|
2020-04-29 05:03:44 +00:00
|
|
|
permission: &Permission::Privilege(&["remote"], PRIV_REMOTE_MODIFY, false),
|
2020-04-17 12:57:26 +00:00
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
)]
|
|
|
|
/// Create new remote.
|
2020-05-26 10:23:24 +00:00
|
|
|
pub fn create_remote(password: String, param: Value) -> Result<(), Error> {
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-09-28 08:50:44 +00:00
|
|
|
let _lock = open_file_locked(remote::REMOTE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-05-26 10:23:24 +00:00
|
|
|
let mut data = param.clone();
|
|
|
|
data["password"] = Value::from(base64::encode(password.as_bytes()));
|
|
|
|
let remote: remote::Remote = serde_json::from_value(data)?;
|
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
|
|
|
|
2020-05-22 12:51:40 +00:00
|
|
|
if let Some(_) = config.sections.get(&remote.name) {
|
|
|
|
bail!("remote '{}' already exists.", remote.name);
|
2020-01-10 12:28:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 12:51:40 +00:00
|
|
|
config.set_data(&remote.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-12-09 09:54:38 +00:00
|
|
|
returns: { type: remote::Remote },
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
2020-04-29 05:03:44 +00:00
|
|
|
permission: &Permission::Privilege(&["remote", "{name}"], PRIV_REMOTE_AUDIT, false),
|
2020-04-17 12:57:26 +00:00
|
|
|
}
|
2020-01-14 13:20:16 +00:00
|
|
|
)]
|
|
|
|
/// Read remote configuration data.
|
2020-05-22 12:51:41 +00:00
|
|
|
pub fn read_remote(
|
|
|
|
name: String,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
mut rpcenv: &mut dyn RpcEnvironment,
|
2020-05-22 12:51:45 +00:00
|
|
|
) -> Result<remote::Remote, Error> {
|
2020-01-16 13:32:06 +00:00
|
|
|
let (config, digest) = remote::config()?;
|
2020-05-22 12:51:45 +00:00
|
|
|
let mut data: remote::Remote = config.lookup("remote", &name)?;
|
|
|
|
data.password = "".to_string(); // do not return password in api
|
2020-05-22 12:51:41 +00:00
|
|
|
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
|
2020-01-14 13:20:16 +00:00
|
|
|
Ok(data)
|
|
|
|
}
|
2020-04-29 07:09:39 +00:00
|
|
|
|
2020-04-29 07:04:17 +00:00
|
|
|
#[api()]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
/// Deletable property name
|
|
|
|
pub enum DeletableProperty {
|
|
|
|
/// Delete the comment property.
|
|
|
|
comment,
|
|
|
|
/// Delete the fingerprint property.
|
|
|
|
fingerprint,
|
2020-09-29 14:18:58 +00:00
|
|
|
/// Delete the port property.
|
|
|
|
port,
|
2020-04-29 07:04:17 +00:00
|
|
|
}
|
2020-01-14 13:20:16 +00:00
|
|
|
|
|
|
|
#[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,
|
|
|
|
},
|
2020-09-29 14:18:58 +00:00
|
|
|
port: {
|
|
|
|
description: "The (optional) port.",
|
|
|
|
type: u16,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-11-05 11:12:25 +00:00
|
|
|
"auth-id": {
|
2020-01-14 13:20:16 +00:00
|
|
|
optional: true,
|
2020-10-23 11:33:21 +00:00
|
|
|
type: Authid,
|
2020-01-14 13:20:16 +00:00
|
|
|
},
|
|
|
|
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-04-29 07:04:17 +00:00
|
|
|
delete: {
|
|
|
|
description: "List of properties to delete.",
|
|
|
|
type: Array,
|
|
|
|
optional: true,
|
|
|
|
items: {
|
|
|
|
type: DeletableProperty,
|
|
|
|
}
|
|
|
|
},
|
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: {
|
2020-04-29 05:03:44 +00:00
|
|
|
permission: &Permission::Privilege(&["remote", "{name}"], PRIV_REMOTE_MODIFY, false),
|
2020-04-17 12:57:26 +00:00
|
|
|
},
|
2020-01-14 13:20:16 +00:00
|
|
|
)]
|
|
|
|
/// Update remote configuration.
|
|
|
|
pub fn update_remote(
|
|
|
|
name: String,
|
|
|
|
comment: Option<String>,
|
|
|
|
host: Option<String>,
|
2020-09-29 14:18:58 +00:00
|
|
|
port: Option<u16>,
|
2020-11-05 11:12:25 +00:00
|
|
|
auth_id: Option<Authid>,
|
2020-01-14 13:20:16 +00:00
|
|
|
password: Option<String>,
|
2020-01-25 08:48:39 +00:00
|
|
|
fingerprint: Option<String>,
|
2020-04-29 07:04:17 +00:00
|
|
|
delete: Option<Vec<DeletableProperty>>,
|
2020-01-15 11:27:05 +00:00
|
|
|
digest: Option<String>,
|
2020-01-14 13:20:16 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2020-09-28 08:50:44 +00:00
|
|
|
let _lock = open_file_locked(remote::REMOTE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?;
|
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
|
|
|
|
2020-04-29 07:04:17 +00:00
|
|
|
if let Some(delete) = delete {
|
|
|
|
for delete_prop in delete {
|
|
|
|
match delete_prop {
|
|
|
|
DeletableProperty::comment => { data.comment = None; },
|
|
|
|
DeletableProperty::fingerprint => { data.fingerprint = None; },
|
2020-09-29 14:18:58 +00:00
|
|
|
DeletableProperty::port => { data.port = None; },
|
2020-04-29 07:04:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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; }
|
2020-09-29 14:18:58 +00:00
|
|
|
if port.is_some() { data.port = port; }
|
2020-11-05 11:12:25 +00:00
|
|
|
if let Some(auth_id) = auth_id { data.auth_id = auth_id; }
|
2020-01-14 13:20:16 +00:00
|
|
|
if let Some(password) = password { data.password = password; }
|
|
|
|
|
2020-01-25 08:48:39 +00:00
|
|
|
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-05-22 12:51:42 +00:00
|
|
|
digest: {
|
|
|
|
optional: true,
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:57:26 +00:00
|
|
|
access: {
|
2020-04-29 05:03:44 +00:00
|
|
|
permission: &Permission::Privilege(&["remote", "{name}"], PRIV_REMOTE_MODIFY, false),
|
2020-04-17 12:57:26 +00:00
|
|
|
},
|
2020-01-10 12:28:15 +00:00
|
|
|
)]
|
|
|
|
/// Remove a remote from the configuration file.
|
2020-05-22 12:51:42 +00:00
|
|
|
pub fn delete_remote(name: String, digest: Option<String>) -> Result<(), Error> {
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-11-09 11:48:29 +00:00
|
|
|
use crate::config::sync::{self, SyncJobConfig};
|
|
|
|
|
|
|
|
let (sync_jobs, _) = sync::config()?;
|
|
|
|
|
|
|
|
let job_list: Vec<SyncJobConfig> = sync_jobs.convert_to_typed_array("sync")?;
|
|
|
|
for job in job_list {
|
|
|
|
if job.remote == name {
|
|
|
|
bail!("remote '{}' is used by sync job '{}' (datastore '{}')", name, job.id, job.store);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 08:50:44 +00:00
|
|
|
let _lock = open_file_locked(remote::REMOTE_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?;
|
2020-01-10 12:28:15 +00:00
|
|
|
|
2020-05-22 12:51:42 +00:00
|
|
|
let (mut config, expected_digest) = remote::config()?;
|
|
|
|
|
|
|
|
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-10 12:28:15 +00:00
|
|
|
|
|
|
|
match config.sections.get(&name) {
|
|
|
|
Some(_) => { config.sections.remove(&name); },
|
|
|
|
None => bail!("remote '{}' does not exist.", name),
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:45:00 +00:00
|
|
|
remote::save_config(&config)?;
|
|
|
|
|
2020-01-10 12:28:15 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:12:23 +00:00
|
|
|
/// Helper to get client for remote.cfg entry
|
|
|
|
pub async fn remote_client(remote: remote::Remote) -> Result<HttpClient, Error> {
|
|
|
|
let options = HttpClientOptions::new()
|
|
|
|
.password(Some(remote.password.clone()))
|
|
|
|
.fingerprint(remote.fingerprint.clone());
|
|
|
|
|
|
|
|
let client = HttpClient::new(
|
|
|
|
&remote.host,
|
|
|
|
remote.port.unwrap_or(8007),
|
2020-11-05 11:12:25 +00:00
|
|
|
&remote.auth_id,
|
2020-11-05 11:12:23 +00:00
|
|
|
options)?;
|
|
|
|
let _auth_info = client.login() // make sure we can auth
|
|
|
|
.await
|
|
|
|
.map_err(|err| format_err!("remote connection to '{}' failed - {}", remote.host, err))?;
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: REMOTE_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["remote", "{name}"], PRIV_REMOTE_AUDIT, false),
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "List the accessible datastores.",
|
|
|
|
type: Array,
|
2020-12-09 09:54:38 +00:00
|
|
|
items: { type: DataStoreListItem },
|
2020-11-05 11:12:23 +00:00
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List datastores of a remote.cfg entry
|
|
|
|
pub async fn scan_remote_datastores(name: String) -> Result<Vec<DataStoreListItem>, Error> {
|
|
|
|
let (remote_config, _digest) = remote::config()?;
|
|
|
|
let remote: remote::Remote = remote_config.lookup("remote", &name)?;
|
|
|
|
|
|
|
|
let map_remote_err = |api_err| {
|
|
|
|
http_err!(INTERNAL_SERVER_ERROR,
|
|
|
|
"failed to scan remote '{}' - {}",
|
|
|
|
&name,
|
|
|
|
api_err)
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = remote_client(remote)
|
|
|
|
.await
|
|
|
|
.map_err(map_remote_err)?;
|
|
|
|
let api_res = client
|
|
|
|
.get("api2/json/admin/datastore", None)
|
|
|
|
.await
|
|
|
|
.map_err(map_remote_err)?;
|
|
|
|
let parse_res = match api_res.get("data") {
|
|
|
|
Some(data) => serde_json::from_value::<Vec<DataStoreListItem>>(data.to_owned()),
|
|
|
|
None => bail!("remote {} did not return any datastore list data", &name),
|
|
|
|
};
|
|
|
|
|
|
|
|
match parse_res {
|
|
|
|
Ok(parsed) => Ok(parsed),
|
|
|
|
Err(_) => bail!("Failed to parse remote scan api result."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCAN_ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_SCAN_REMOTE_DATASTORES);
|
|
|
|
|
2020-01-14 13:20:16 +00:00
|
|
|
const ITEM_ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_READ_REMOTE)
|
|
|
|
.put(&API_METHOD_UPDATE_REMOTE)
|
2020-11-05 11:12:23 +00:00
|
|
|
.delete(&API_METHOD_DELETE_REMOTE)
|
|
|
|
.subdirs(&[("scan", &SCAN_ROUTER)]);
|
2020-01-14 13:20:16 +00:00
|
|
|
|
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);
|