notify: use HttpError from proxmox-http-error

Also improve API documentation in terms of which HttpError is
returned when.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-07-26 16:18:23 +02:00 committed by Wolfgang Bumiller
parent add38769f8
commit 1a75668dc9
7 changed files with 232 additions and 251 deletions

View File

@ -14,6 +14,7 @@ log.workspace = true
once_cell.workspace = true once_cell.workspace = true
openssl.workspace = true openssl.workspace = true
proxmox-http = { workspace = true, features = ["client-sync"], optional = true } proxmox-http = { workspace = true, features = ["client-sync"], optional = true }
proxmox-http-error.workspace = true
proxmox-human-byte.workspace = true proxmox-human-byte.workspace = true
proxmox-schema = { workspace = true, features = ["api-macro", "api-types"]} proxmox-schema = { workspace = true, features = ["api-macro", "api-types"]}
proxmox-section-config = { workspace = true } proxmox-section-config = { workspace = true }

View File

@ -1,15 +1,17 @@
use crate::api::ApiError; use super::http_err;
use crate::{Bus, Config, Notification}; use crate::{Bus, Config, Notification};
use proxmox_http_error::HttpError;
/// Send a notification to a given target. /// Send a notification to a given target.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns an `ApiError` in case of an error. /// Returns an `anyhow::Error` in case of an error.
pub fn send(config: &Config, channel: &str, notification: &Notification) -> Result<(), ApiError> { pub fn send(config: &Config, channel: &str, notification: &Notification) -> Result<(), HttpError> {
let bus = Bus::from_config(config).map_err(|err| { let bus = Bus::from_config(config).map_err(|err| {
ApiError::internal_server_error( http_err!(
"Could not instantiate notification bus", INTERNAL_SERVER_ERROR,
Some(Box::new(err)), "Could not instantiate notification bus: {err}"
) )
})?; })?;
@ -21,23 +23,20 @@ pub fn send(config: &Config, channel: &str, notification: &Notification) -> Resu
/// Test target (group or single endpoint) identified by its `name`. /// Test target (group or single endpoint) identified by its `name`.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns an `ApiError` if sending via the endpoint failed. /// Returns an `anyhow::Error` if sending via the endpoint failed.
pub fn test_target(config: &Config, endpoint: &str) -> Result<(), ApiError> { pub fn test_target(config: &Config, endpoint: &str) -> Result<(), HttpError> {
let bus = Bus::from_config(config).map_err(|err| { let bus = Bus::from_config(config).map_err(|err| {
ApiError::internal_server_error( http_err!(
"Could not instantiate notification bus", INTERNAL_SERVER_ERROR,
Some(Box::new(err)), "Could not instantiate notification bus: {err}"
) )
})?; })?;
bus.test_target(endpoint).map_err(|err| match err { bus.test_target(endpoint).map_err(|err| match err {
crate::Error::TargetDoesNotExist(endpoint) => { crate::Error::TargetDoesNotExist(endpoint) => {
ApiError::not_found(format!("endpoint '{endpoint}' does not exist"), None) http_err!(NOT_FOUND, "endpoint '{endpoint}' does not exist")
} }
_ => ApiError::internal_server_error( _ => http_err!(INTERNAL_SERVER_ERROR, "Could not test target: {err}"),
format!("Could not test target: {err}"),
Some(Box::new(err)),
),
})?; })?;
Ok(()) Ok(())
@ -49,7 +48,7 @@ pub fn test_target(config: &Config, endpoint: &str) -> Result<(), ApiError> {
/// the result for 'grp1' would be [grp1, a, b, c, filter1, filter2]. /// the result for 'grp1' would be [grp1, a, b, c, filter1, filter2].
/// The result will always contain the entity that was passed as a parameter. /// The result will always contain the entity that was passed as a parameter.
/// If the entity does not exist, the result will only contain the entity. /// If the entity does not exist, the result will only contain the entity.
pub fn get_referenced_entities(config: &Config, entity: &str) -> Result<Vec<String>, ApiError> { pub fn get_referenced_entities(config: &Config, entity: &str) -> Result<Vec<String>, HttpError> {
let entities = super::get_referenced_entities(config, entity); let entities = super::get_referenced_entities(config, entity);
Ok(Vec::from_iter(entities.into_iter())) Ok(Vec::from_iter(entities.into_iter()))
} }

View File

@ -1,63 +1,69 @@
use crate::api::ApiError; use crate::api::http_err;
use crate::filter::{DeleteableFilterProperty, FilterConfig, FilterConfigUpdater, FILTER_TYPENAME}; use crate::filter::{DeleteableFilterProperty, FilterConfig, FilterConfigUpdater, FILTER_TYPENAME};
use crate::Config; use crate::Config;
use proxmox_http_error::HttpError;
/// Get a list of all filters /// Get a list of all filters
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns a list of all filters or an `ApiError` if the config is erroneous. /// Returns a list of all filters or a `HttpError` if the config is
pub fn get_filters(config: &Config) -> Result<Vec<FilterConfig>, ApiError> { /// (`500 Internal server error`).
pub fn get_filters(config: &Config) -> Result<Vec<FilterConfig>, HttpError> {
config config
.config .config
.convert_to_typed_array(FILTER_TYPENAME) .convert_to_typed_array(FILTER_TYPENAME)
.map_err(|e| ApiError::internal_server_error("Could not fetch filters", Some(e.into()))) .map_err(|e| http_err!(INTERNAL_SERVER_ERROR, "Could not fetch filters: {e}"))
} }
/// Get filter with given `name` /// Get filter with given `name`
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns the endpoint or an `ApiError` if the filter was not found. /// Returns the endpoint or a `HttpError` if the filter was not found (`404 Not found`).
pub fn get_filter(config: &Config, name: &str) -> Result<FilterConfig, ApiError> { pub fn get_filter(config: &Config, name: &str) -> Result<FilterConfig, HttpError> {
config config
.config .config
.lookup(FILTER_TYPENAME, name) .lookup(FILTER_TYPENAME, name)
.map_err(|_| ApiError::not_found(format!("filter '{name}' not found"), None)) .map_err(|_| http_err!(NOT_FOUND, "filter '{name}' not found"))
} }
/// Add new notification filter. /// Add new notification filter.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if a filter with the same name already exists or /// Returns a `HttpError` if:
/// if the filter could not be saved. /// - an entity with the same name already exists (`400 Bad request`)
pub fn add_filter(config: &mut Config, filter_config: &FilterConfig) -> Result<(), ApiError> { /// - the configuration could not be saved (`500 Internal server error`)
pub fn add_filter(config: &mut Config, filter_config: &FilterConfig) -> Result<(), HttpError> {
super::ensure_unique(config, &filter_config.name)?; super::ensure_unique(config, &filter_config.name)?;
config config
.config .config
.set_data(&filter_config.name, FILTER_TYPENAME, filter_config) .set_data(&filter_config.name, FILTER_TYPENAME, filter_config)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!("could not save filter '{}'", filter_config.name), INTERNAL_SERVER_ERROR,
Some(e.into()), "could not save filter '{}': {e}",
filter_config.name
) )
})?; })?;
Ok(()) Ok(())
} }
/// Update existing filter /// Update existing notification filter
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the config could not be saved. /// Returns a `HttpError` if:
/// - the configuration could not be saved (`500 Internal server error`)
/// - an invalid digest was passed (`400 Bad request`)
pub fn update_filter( pub fn update_filter(
config: &mut Config, config: &mut Config,
name: &str, name: &str,
filter_updater: &FilterConfigUpdater, filter_updater: &FilterConfigUpdater,
delete: Option<&[DeleteableFilterProperty]>, delete: Option<&[DeleteableFilterProperty]>,
digest: Option<&[u8]>, digest: Option<&[u8]>,
) -> Result<(), ApiError> { ) -> Result<(), HttpError> {
super::verify_digest(config, digest)?; super::verify_digest(config, digest)?;
let mut filter = get_filter(config, name)?; let mut filter = get_filter(config, name)?;
@ -92,12 +98,7 @@ pub fn update_filter(
config config
.config .config
.set_data(name, FILTER_TYPENAME, &filter) .set_data(name, FILTER_TYPENAME, &filter)
.map_err(|e| { .map_err(|e| http_err!(INTERNAL_SERVER_ERROR, "could not save filter '{name}': {e}"))?;
ApiError::internal_server_error(
format!("could not save filter '{name}'"),
Some(e.into()),
)
})?;
Ok(()) Ok(())
} }
@ -106,8 +107,10 @@ pub fn update_filter(
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the filter does not exist. /// Returns a `HttpError` if:
pub fn delete_filter(config: &mut Config, name: &str) -> Result<(), ApiError> { /// - the entity does not exist (`404 Not found`)
/// - the filter is still referenced by another entity (`400 Bad request`)
pub fn delete_filter(config: &mut Config, name: &str) -> Result<(), HttpError> {
// Check if the filter exists // Check if the filter exists
let _ = get_filter(config, name)?; let _ = get_filter(config, name)?;
super::ensure_unused(config, name)?; super::ensure_unused(config, name)?;
@ -142,14 +145,14 @@ filter: filter2
} }
#[test] #[test]
fn test_update_not_existing_returns_error() -> Result<(), ApiError> { fn test_update_not_existing_returns_error() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
assert!(update_filter(&mut config, "test", &Default::default(), None, None).is_err()); assert!(update_filter(&mut config, "test", &Default::default(), None, None).is_err());
Ok(()) Ok(())
} }
#[test] #[test]
fn test_update_invalid_digest_returns_error() -> Result<(), ApiError> { fn test_update_invalid_digest_returns_error() -> Result<(), HttpError> {
let mut config = config_with_two_filters(); let mut config = config_with_two_filters();
assert!(update_filter( assert!(update_filter(
&mut config, &mut config,
@ -164,7 +167,7 @@ filter: filter2
} }
#[test] #[test]
fn test_filter_update() -> Result<(), ApiError> { fn test_filter_update() -> Result<(), HttpError> {
let mut config = config_with_two_filters(); let mut config = config_with_two_filters();
let digest = config.digest; let digest = config.digest;
@ -215,7 +218,7 @@ filter: filter2
} }
#[test] #[test]
fn test_filter_delete() -> Result<(), ApiError> { fn test_filter_delete() -> Result<(), HttpError> {
let mut config = config_with_two_filters(); let mut config = config_with_two_filters();
delete_filter(&mut config, "filter1")?; delete_filter(&mut config, "filter1")?;

View File

@ -1,4 +1,6 @@
use crate::api::ApiError; use proxmox_http_error::HttpError;
use crate::api::http_err;
use crate::endpoints::gotify::{ use crate::endpoints::gotify::{
DeleteableGotifyProperty, GotifyConfig, GotifyConfigUpdater, GotifyPrivateConfig, DeleteableGotifyProperty, GotifyConfig, GotifyConfigUpdater, GotifyPrivateConfig,
GotifyPrivateConfigUpdater, GOTIFY_TYPENAME, GotifyPrivateConfigUpdater, GOTIFY_TYPENAME,
@ -8,36 +10,41 @@ use crate::Config;
/// Get a list of all gotify endpoints. /// Get a list of all gotify endpoints.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns a list of all gotify endpoints or an `ApiError` if the config is erroneous. /// Returns a list of all gotify endpoints or a `HttpError` if the config is
pub fn get_endpoints(config: &Config) -> Result<Vec<GotifyConfig>, ApiError> { /// erroneous (`500 Internal server error`).
pub fn get_endpoints(config: &Config) -> Result<Vec<GotifyConfig>, HttpError> {
config config
.config .config
.convert_to_typed_array(GOTIFY_TYPENAME) .convert_to_typed_array(GOTIFY_TYPENAME)
.map_err(|e| ApiError::internal_server_error("Could not fetch endpoints", Some(e.into()))) .map_err(|e| http_err!(NOT_FOUND, "Could not fetch endpoints: {e}"))
} }
/// Get gotify endpoint with given `name` /// Get gotify endpoint with given `name`
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns the endpoint or an `ApiError` if the endpoint was not found. /// Returns the endpoint or a `HttpError` if the endpoint was not found (`404 Not found`).
pub fn get_endpoint(config: &Config, name: &str) -> Result<GotifyConfig, ApiError> { pub fn get_endpoint(config: &Config, name: &str) -> Result<GotifyConfig, HttpError> {
config config
.config .config
.lookup(GOTIFY_TYPENAME, name) .lookup(GOTIFY_TYPENAME, name)
.map_err(|_| ApiError::not_found(format!("endpoint '{name}' not found"), None)) .map_err(|_| http_err!(NOT_FOUND, "endpoint '{name}' not found"))
} }
/// Add a new gotify endpoint. /// Add a new gotify endpoint.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if an endpoint with the same name already exists, /// Returns a `HttpError` if:
/// or if the endpoint could not be saved. /// - an entity with the same name already exists (`400 Bad request`)
/// - a referenced filter does not exist (`400 Bad request`)
/// - the configuration could not be saved (`500 Internal server error`)
///
/// Panics if the names of the private config and the public config do not match.
pub fn add_endpoint( pub fn add_endpoint(
config: &mut Config, config: &mut Config,
endpoint_config: &GotifyConfig, endpoint_config: &GotifyConfig,
private_endpoint_config: &GotifyPrivateConfig, private_endpoint_config: &GotifyPrivateConfig,
) -> Result<(), ApiError> { ) -> Result<(), HttpError> {
if endpoint_config.name != private_endpoint_config.name { if endpoint_config.name != private_endpoint_config.name {
// Programming error by the user of the crate, thus we panic // Programming error by the user of the crate, thus we panic
panic!("name for endpoint config and private config must be identical"); panic!("name for endpoint config and private config must be identical");
@ -56,20 +63,22 @@ pub fn add_endpoint(
.config .config
.set_data(&endpoint_config.name, GOTIFY_TYPENAME, endpoint_config) .set_data(&endpoint_config.name, GOTIFY_TYPENAME, endpoint_config)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!("could not save endpoint '{}'", endpoint_config.name), INTERNAL_SERVER_ERROR,
Some(e.into()), "could not save endpoint '{}': {e}",
endpoint_config.name
) )
})?; })
Ok(())
} }
/// Update existing gotify endpoint /// Update existing gotify endpoint
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the config could not be saved. /// Returns a `HttpError` if:
/// - an entity with the same name already exists (`400 Bad request`)
/// - a referenced filter does not exist (`400 Bad request`)
/// - the configuration could not be saved (`500 Internal server error`)
pub fn update_endpoint( pub fn update_endpoint(
config: &mut Config, config: &mut Config,
name: &str, name: &str,
@ -77,7 +86,7 @@ pub fn update_endpoint(
private_endpoint_config_updater: &GotifyPrivateConfigUpdater, private_endpoint_config_updater: &GotifyPrivateConfigUpdater,
delete: Option<&[DeleteableGotifyProperty]>, delete: Option<&[DeleteableGotifyProperty]>,
digest: Option<&[u8]>, digest: Option<&[u8]>,
) -> Result<(), ApiError> { ) -> Result<(), HttpError> {
super::verify_digest(config, digest)?; super::verify_digest(config, digest)?;
let mut endpoint = get_endpoint(config, name)?; let mut endpoint = get_endpoint(config, name)?;
@ -120,21 +129,21 @@ pub fn update_endpoint(
.config .config
.set_data(name, GOTIFY_TYPENAME, &endpoint) .set_data(name, GOTIFY_TYPENAME, &endpoint)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!("could not save endpoint '{name}'"), INTERNAL_SERVER_ERROR,
Some(e.into()), "could not save endpoint '{name}': {e}"
) )
})?; })
Ok(())
} }
/// Delete existing gotify endpoint /// Delete existing gotify endpoint
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the endpoint does not exist. /// Returns a `HttpError` if:
pub fn delete_gotify_endpoint(config: &mut Config, name: &str) -> Result<(), ApiError> { /// - the entity does not exist (`404 Not found`)
/// - the endpoint is still referenced by another entity (`400 Bad request`)
pub fn delete_gotify_endpoint(config: &mut Config, name: &str) -> Result<(), HttpError> {
// Check if the endpoint exists // Check if the endpoint exists
let _ = get_endpoint(config, name)?; let _ = get_endpoint(config, name)?;
super::ensure_unused(config, name)?; super::ensure_unused(config, name)?;
@ -148,22 +157,20 @@ pub fn delete_gotify_endpoint(config: &mut Config, name: &str) -> Result<(), Api
fn set_private_config_entry( fn set_private_config_entry(
config: &mut Config, config: &mut Config,
private_config: &GotifyPrivateConfig, private_config: &GotifyPrivateConfig,
) -> Result<(), ApiError> { ) -> Result<(), HttpError> {
config config
.private_config .private_config
.set_data(&private_config.name, GOTIFY_TYPENAME, private_config) .set_data(&private_config.name, GOTIFY_TYPENAME, private_config)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!( INTERNAL_SERVER_ERROR,
"could not save private config for endpoint '{}'", "could not save private config for endpoint '{}': {e}",
private_config.name private_config.name
),
Some(e.into()),
) )
}) })
} }
fn remove_private_config_entry(config: &mut Config, name: &str) -> Result<(), ApiError> { fn remove_private_config_entry(config: &mut Config, name: &str) -> Result<(), HttpError> {
config.private_config.sections.remove(name); config.private_config.sections.remove(name);
Ok(()) Ok(())
} }
@ -173,7 +180,7 @@ mod tests {
use super::*; use super::*;
use crate::api::test_helpers::empty_config; use crate::api::test_helpers::empty_config;
pub fn add_default_gotify_endpoint(config: &mut Config) -> Result<(), ApiError> { pub fn add_default_gotify_endpoint(config: &mut Config) -> Result<(), HttpError> {
add_endpoint( add_endpoint(
config, config,
&GotifyConfig { &GotifyConfig {
@ -193,7 +200,7 @@ mod tests {
} }
#[test] #[test]
fn test_update_not_existing_returns_error() -> Result<(), ApiError> { fn test_update_not_existing_returns_error() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
assert!(update_endpoint( assert!(update_endpoint(
@ -210,7 +217,7 @@ mod tests {
} }
#[test] #[test]
fn test_update_invalid_digest_returns_error() -> Result<(), ApiError> { fn test_update_invalid_digest_returns_error() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_gotify_endpoint(&mut config)?; add_default_gotify_endpoint(&mut config)?;
@ -228,7 +235,7 @@ mod tests {
} }
#[test] #[test]
fn test_gotify_update() -> Result<(), ApiError> { fn test_gotify_update() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_gotify_endpoint(&mut config)?; add_default_gotify_endpoint(&mut config)?;
@ -279,7 +286,7 @@ mod tests {
} }
#[test] #[test]
fn test_gotify_endpoint_delete() -> Result<(), ApiError> { fn test_gotify_endpoint_delete() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_gotify_endpoint(&mut config)?; add_default_gotify_endpoint(&mut config)?;

View File

@ -1,43 +1,47 @@
use crate::api::ApiError; use proxmox_http_error::HttpError;
use crate::api::{http_bail, http_err};
use crate::group::{DeleteableGroupProperty, GroupConfig, GroupConfigUpdater, GROUP_TYPENAME}; use crate::group::{DeleteableGroupProperty, GroupConfig, GroupConfigUpdater, GROUP_TYPENAME};
use crate::Config; use crate::Config;
/// Get all notification groups /// Get all notification groups
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns a list of all groups or an `ApiError` if the config is erroneous. /// Returns a list of all groups or a `HttpError` if the config is
pub fn get_groups(config: &Config) -> Result<Vec<GroupConfig>, ApiError> { /// erroneous (`500 Internal server error`).
pub fn get_groups(config: &Config) -> Result<Vec<GroupConfig>, HttpError> {
config config
.config .config
.convert_to_typed_array(GROUP_TYPENAME) .convert_to_typed_array(GROUP_TYPENAME)
.map_err(|e| ApiError::internal_server_error("Could not fetch groups", Some(e.into()))) .map_err(|e| http_err!(INTERNAL_SERVER_ERROR, "Could not fetch groups: {e}"))
} }
/// Get group with given `name` /// Get group with given `name`
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns the endpoint or an `ApiError` if the group was not found. /// Returns the endpoint or an `HttpError` if the group was not found (`404 Not found`).
pub fn get_group(config: &Config, name: &str) -> Result<GroupConfig, ApiError> { pub fn get_group(config: &Config, name: &str) -> Result<GroupConfig, HttpError> {
config config
.config .config
.lookup(GROUP_TYPENAME, name) .lookup(GROUP_TYPENAME, name)
.map_err(|_| ApiError::not_found(format!("group '{name}' not found"), None)) .map_err(|_| http_err!(NOT_FOUND, "group '{name}' not found"))
} }
/// Add a new group. /// Add a new group.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if a group with the same name already exists, or /// Returns a `HttpError` if:
/// if the group could not be saved /// - an entity with the same name already exists (`400 Bad request`)
pub fn add_group(config: &mut Config, group_config: &GroupConfig) -> Result<(), ApiError> { /// - a referenced filter does not exist (`400 Bad request`)
/// - no endpoints were passed (`400 Bad request`)
/// - referenced endpoints do not exist (`404 Not found`)
/// - the configuration could not be saved (`500 Internal server error`)
pub fn add_group(config: &mut Config, group_config: &GroupConfig) -> Result<(), HttpError> {
super::ensure_unique(config, &group_config.name)?; super::ensure_unique(config, &group_config.name)?;
if group_config.endpoint.is_empty() { if group_config.endpoint.is_empty() {
return Err(ApiError::bad_request( http_bail!(BAD_REQUEST, "group must contain at least one endpoint",);
"group must contain at least one endpoint",
None,
));
} }
if let Some(filter) = &group_config.filter { if let Some(filter) = &group_config.filter {
@ -51,27 +55,31 @@ pub fn add_group(config: &mut Config, group_config: &GroupConfig) -> Result<(),
.config .config
.set_data(&group_config.name, GROUP_TYPENAME, group_config) .set_data(&group_config.name, GROUP_TYPENAME, group_config)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!("could not save group '{}'", group_config.name), INTERNAL_SERVER_ERROR,
Some(e.into()), "could not save group '{}': {e}",
group_config.name
) )
})?; })
Ok(())
} }
/// Update existing group /// Update existing group
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the config could not be saved. /// Returns a `HttpError` if:
/// - a referenced filter does not exist (`400 Bad request`)
/// - an invalid digest was passed (`400 Bad request`)
/// - no endpoints were passed (`400 Bad request`)
/// - referenced endpoints do not exist (`404 Not found`)
/// - the configuration could not be saved (`500 Internal server error`)
pub fn update_group( pub fn update_group(
config: &mut Config, config: &mut Config,
name: &str, name: &str,
updater: &GroupConfigUpdater, updater: &GroupConfigUpdater,
delete: Option<&[DeleteableGroupProperty]>, delete: Option<&[DeleteableGroupProperty]>,
digest: Option<&[u8]>, digest: Option<&[u8]>,
) -> Result<(), ApiError> { ) -> Result<(), HttpError> {
super::verify_digest(config, digest)?; super::verify_digest(config, digest)?;
let mut group = get_group(config, name)?; let mut group = get_group(config, name)?;
@ -88,10 +96,7 @@ pub fn update_group(
if let Some(endpoints) = &updater.endpoint { if let Some(endpoints) = &updater.endpoint {
super::ensure_endpoints_exist(config, endpoints)?; super::ensure_endpoints_exist(config, endpoints)?;
if endpoints.is_empty() { if endpoints.is_empty() {
return Err(ApiError::bad_request( http_bail!(BAD_REQUEST, "group must contain at least one endpoint",);
"group must contain at least one endpoint",
None,
));
} }
group.endpoint = endpoints.iter().map(Into::into).collect() group.endpoint = endpoints.iter().map(Into::into).collect()
} }
@ -109,22 +114,15 @@ pub fn update_group(
config config
.config .config
.set_data(name, GROUP_TYPENAME, &group) .set_data(name, GROUP_TYPENAME, &group)
.map_err(|e| { .map_err(|e| http_err!(INTERNAL_SERVER_ERROR, "could not save group '{name}': {e}"))
ApiError::internal_server_error(
format!("could not save group '{name}'"),
Some(e.into()),
)
})?;
Ok(())
} }
/// Delete existing group /// Delete existing group
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the group does not exist. /// Returns a `HttpError` if the group does not exist (`404 Not found`).
pub fn delete_group(config: &mut Config, name: &str) -> Result<(), ApiError> { pub fn delete_group(config: &mut Config, name: &str) -> Result<(), HttpError> {
// Check if the group exists // Check if the group exists
let _ = get_group(config, name)?; let _ = get_group(config, name)?;
@ -141,7 +139,7 @@ mod tests {
use crate::api::sendmail::tests::add_sendmail_endpoint_for_test; use crate::api::sendmail::tests::add_sendmail_endpoint_for_test;
use crate::api::test_helpers::*; use crate::api::test_helpers::*;
fn add_default_group(config: &mut Config) -> Result<(), ApiError> { fn add_default_group(config: &mut Config) -> Result<(), HttpError> {
add_sendmail_endpoint_for_test(config, "test")?; add_sendmail_endpoint_for_test(config, "test")?;
add_group( add_group(
@ -173,14 +171,14 @@ mod tests {
} }
#[test] #[test]
fn test_add_group() -> Result<(), ApiError> { fn test_add_group() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
assert!(add_default_group(&mut config).is_ok()); assert!(add_default_group(&mut config).is_ok());
Ok(()) Ok(())
} }
#[test] #[test]
fn test_update_group_fails_if_endpoint_does_not_exist() -> Result<(), ApiError> { fn test_update_group_fails_if_endpoint_does_not_exist() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_group(&mut config)?; add_default_group(&mut config)?;
@ -199,7 +197,7 @@ mod tests {
} }
#[test] #[test]
fn test_update_group_fails_if_digest_invalid() -> Result<(), ApiError> { fn test_update_group_fails_if_digest_invalid() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_group(&mut config)?; add_default_group(&mut config)?;
@ -215,7 +213,7 @@ mod tests {
} }
#[test] #[test]
fn test_update_group() -> Result<(), ApiError> { fn test_update_group() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_group(&mut config)?; add_default_group(&mut config)?;
@ -249,7 +247,7 @@ mod tests {
} }
#[test] #[test]
fn test_group_delete() -> Result<(), ApiError> { fn test_group_delete() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_default_group(&mut config)?; add_default_group(&mut config)?;

View File

@ -1,9 +1,6 @@
use std::collections::HashSet;
use std::error::Error as StdError;
use std::fmt::Display;
use crate::Config; use crate::Config;
use serde::Serialize; use proxmox_http_error::HttpError;
use std::collections::HashSet;
pub mod common; pub mod common;
pub mod filter; pub mod filter;
@ -13,81 +10,44 @@ pub mod group;
#[cfg(feature = "sendmail")] #[cfg(feature = "sendmail")]
pub mod sendmail; pub mod sendmail;
#[derive(Debug, Serialize)] // We have our own, local versions of http_err and http_bail, because
pub struct ApiError { // we don't want to wrap the error in anyhow::Error. If we were to do that,
/// HTTP Error code // we would need to downcast in the perlmod bindings, since we need
code: u16, // to return `HttpError` from there.
/// Error message #[macro_export]
message: String, macro_rules! http_err {
#[serde(skip_serializing)] ($status:ident, $($fmt:tt)+) => {{
/// The underlying cause of the error proxmox_http_error::HttpError::new(
source: Option<Box<dyn StdError + Send + Sync + 'static>>, proxmox_http_error::StatusCode::$status,
format!($($fmt)+)
)
}};
} }
impl ApiError { #[macro_export]
fn new<S: AsRef<str>>( macro_rules! http_bail {
message: S, ($status:ident, $($fmt:tt)+) => {{
code: u16, return Err($crate::api::http_err!($status, $($fmt)+));
source: Option<Box<dyn StdError + Send + Sync + 'static>>, }};
) -> Self {
Self {
message: message.as_ref().into(),
code,
source,
}
}
pub fn bad_request<S: AsRef<str>>(
message: S,
source: Option<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self {
Self::new(message, 400, source)
}
pub fn not_found<S: AsRef<str>>(
message: S,
source: Option<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self {
Self::new(message, 404, source)
}
pub fn internal_server_error<S: AsRef<str>>(
message: S,
source: Option<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self {
Self::new(message, 500, source)
}
} }
impl Display for ApiError { pub use http_bail;
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { pub use http_err;
f.write_str(&format!("{} {}", self.code, self.message))
}
}
impl StdError for ApiError { fn verify_digest(config: &Config, digest: Option<&[u8]>) -> Result<(), HttpError> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match &self.source {
None => None,
Some(source) => Some(&**source),
}
}
}
fn verify_digest(config: &Config, digest: Option<&[u8]>) -> Result<(), ApiError> {
if let Some(digest) = digest { if let Some(digest) = digest {
if config.digest != *digest { if config.digest != *digest {
return Err(ApiError::bad_request( http_bail!(
"detected modified configuration - file changed by other user? Try again.", BAD_REQUEST,
None, "detected modified configuration - file changed by other user? Try again."
)); );
} }
} }
Ok(()) Ok(())
} }
fn ensure_endpoint_exists(#[allow(unused)] config: &Config, name: &str) -> Result<(), ApiError> { fn ensure_endpoint_exists(#[allow(unused)] config: &Config, name: &str) -> Result<(), HttpError> {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut exists = false; let mut exists = false;
@ -101,16 +61,16 @@ fn ensure_endpoint_exists(#[allow(unused)] config: &Config, name: &str) -> Resul
} }
if !exists { if !exists {
Err(ApiError::not_found( http_bail!(NOT_FOUND, "endpoint '{name}' does not exist")
format!("endpoint '{name}' does not exist"),
None,
))
} else { } else {
Ok(()) Ok(())
} }
} }
fn ensure_endpoints_exist<T: AsRef<str>>(config: &Config, endpoints: &[T]) -> Result<(), ApiError> { fn ensure_endpoints_exist<T: AsRef<str>>(
config: &Config,
endpoints: &[T],
) -> Result<(), HttpError> {
for endpoint in endpoints { for endpoint in endpoints {
ensure_endpoint_exists(config, endpoint.as_ref())?; ensure_endpoint_exists(config, endpoint.as_ref())?;
} }
@ -118,18 +78,18 @@ fn ensure_endpoints_exist<T: AsRef<str>>(config: &Config, endpoints: &[T]) -> Re
Ok(()) Ok(())
} }
fn ensure_unique(config: &Config, entity: &str) -> Result<(), ApiError> { fn ensure_unique(config: &Config, entity: &str) -> Result<(), HttpError> {
if config.config.sections.contains_key(entity) { if config.config.sections.contains_key(entity) {
return Err(ApiError::bad_request( http_bail!(
format!("Cannot create '{entity}', an entity with the same name already exists"), BAD_REQUEST,
None, "Cannot create '{entity}', an entity with the same name already exists"
)); );
} }
Ok(()) Ok(())
} }
fn get_referrers(config: &Config, entity: &str) -> Result<HashSet<String>, ApiError> { fn get_referrers(config: &Config, entity: &str) -> Result<HashSet<String>, HttpError> {
let mut referrers = HashSet::new(); let mut referrers = HashSet::new();
for group in group::get_groups(config)? { for group in group::get_groups(config)? {
@ -165,16 +125,16 @@ fn get_referrers(config: &Config, entity: &str) -> Result<HashSet<String>, ApiEr
Ok(referrers) Ok(referrers)
} }
fn ensure_unused(config: &Config, entity: &str) -> Result<(), ApiError> { fn ensure_unused(config: &Config, entity: &str) -> Result<(), HttpError> {
let referrers = get_referrers(config, entity)?; let referrers = get_referrers(config, entity)?;
if !referrers.is_empty() { if !referrers.is_empty() {
let used_by = referrers.into_iter().collect::<Vec<_>>().join(", "); let used_by = referrers.into_iter().collect::<Vec<_>>().join(", ");
return Err(ApiError::bad_request( http_bail!(
format!("cannot delete '{entity}', referenced by: {used_by}"), BAD_REQUEST,
None, "cannot delete '{entity}', referenced by: {used_by}"
)); );
} }
Ok(()) Ok(())
@ -240,7 +200,7 @@ mod tests {
use crate::filter::FilterConfig; use crate::filter::FilterConfig;
use crate::group::GroupConfig; use crate::group::GroupConfig;
fn prepare_config() -> Result<Config, ApiError> { fn prepare_config() -> Result<Config, HttpError> {
let mut config = super::test_helpers::empty_config(); let mut config = super::test_helpers::empty_config();
filter::add_filter( filter::add_filter(
@ -316,7 +276,7 @@ mod tests {
} }
#[test] #[test]
fn test_get_referrers_for_entity() -> Result<(), ApiError> { fn test_get_referrers_for_entity() -> Result<(), HttpError> {
let config = prepare_config().unwrap(); let config = prepare_config().unwrap();
assert_eq!( assert_eq!(

View File

@ -1,4 +1,6 @@
use crate::api::ApiError; use proxmox_http_error::HttpError;
use crate::api::{http_bail, http_err};
use crate::endpoints::sendmail::{ use crate::endpoints::sendmail::{
DeleteableSendmailProperty, SendmailConfig, SendmailConfigUpdater, SENDMAIL_TYPENAME, DeleteableSendmailProperty, SendmailConfig, SendmailConfigUpdater, SENDMAIL_TYPENAME,
}; };
@ -7,32 +9,36 @@ use crate::Config;
/// Get a list of all sendmail endpoints. /// Get a list of all sendmail endpoints.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns a list of all sendmail endpoints or an `ApiError` if the config is erroneous. /// Returns a list of all sendmail endpoints or a `HttpError` if the config is
pub fn get_endpoints(config: &Config) -> Result<Vec<SendmailConfig>, ApiError> { /// erroneous (`500 Internal server error`).
pub fn get_endpoints(config: &Config) -> Result<Vec<SendmailConfig>, HttpError> {
config config
.config .config
.convert_to_typed_array(SENDMAIL_TYPENAME) .convert_to_typed_array(SENDMAIL_TYPENAME)
.map_err(|e| ApiError::internal_server_error("Could not fetch endpoints", Some(e.into()))) .map_err(|e| http_err!(NOT_FOUND, "Could not fetch endpoints: {e}"))
} }
/// Get sendmail endpoint with given `name`. /// Get sendmail endpoint with given `name`.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// Returns the endpoint or an `ApiError` if the endpoint was not found. /// Returns the endpoint or a `HttpError` if the endpoint was not found (`404 Not found`).
pub fn get_endpoint(config: &Config, name: &str) -> Result<SendmailConfig, ApiError> { pub fn get_endpoint(config: &Config, name: &str) -> Result<SendmailConfig, HttpError> {
config config
.config .config
.lookup(SENDMAIL_TYPENAME, name) .lookup(SENDMAIL_TYPENAME, name)
.map_err(|_| ApiError::not_found(format!("endpoint '{name}' not found"), None)) .map_err(|_| http_err!(NOT_FOUND, "endpoint '{name}' not found"))
} }
/// Add a new sendmail endpoint. /// Add a new sendmail endpoint.
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if an endpoint with the same name already exists, /// Returns a `HttpError` if:
/// or if the endpoint could not be saved. /// - an entity with the same name already exists (`400 Bad request`)
pub fn add_endpoint(config: &mut Config, endpoint: &SendmailConfig) -> Result<(), ApiError> { /// - a referenced filter does not exist (`400 Bad request`)
/// - the configuration could not be saved (`500 Internal server error`)
/// - mailto *and* mailto_user are both set to `None`
pub fn add_endpoint(config: &mut Config, endpoint: &SendmailConfig) -> Result<(), HttpError> {
super::ensure_unique(config, &endpoint.name)?; super::ensure_unique(config, &endpoint.name)?;
if let Some(filter) = &endpoint.filter { if let Some(filter) = &endpoint.filter {
@ -41,37 +47,39 @@ pub fn add_endpoint(config: &mut Config, endpoint: &SendmailConfig) -> Result<()
} }
if endpoint.mailto.is_none() && endpoint.mailto_user.is_none() { if endpoint.mailto.is_none() && endpoint.mailto_user.is_none() {
return Err(ApiError::bad_request( http_bail!(
"must at least provide one recipient, either in mailto or in mailto-user", BAD_REQUEST,
None, "must at least provide one recipient, either in mailto or in mailto-user"
)); );
} }
config config
.config .config
.set_data(&endpoint.name, SENDMAIL_TYPENAME, endpoint) .set_data(&endpoint.name, SENDMAIL_TYPENAME, endpoint)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!("could not save endpoint '{}'", endpoint.name), INTERNAL_SERVER_ERROR,
Some(e.into()), "could not save endpoint '{}': {e}",
endpoint.name
) )
})?; })
Ok(())
} }
/// Update existing sendmail endpoint /// Update existing sendmail endpoint
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the config could not be saved. /// Returns a `HttpError` if:
/// - a referenced filter does not exist (`400 Bad request`)
/// - the configuration could not be saved (`500 Internal server error`)
/// - mailto *and* mailto_user are both set to `None`
pub fn update_endpoint( pub fn update_endpoint(
config: &mut Config, config: &mut Config,
name: &str, name: &str,
updater: &SendmailConfigUpdater, updater: &SendmailConfigUpdater,
delete: Option<&[DeleteableSendmailProperty]>, delete: Option<&[DeleteableSendmailProperty]>,
digest: Option<&[u8]>, digest: Option<&[u8]>,
) -> Result<(), ApiError> { ) -> Result<(), HttpError> {
super::verify_digest(config, digest)?; super::verify_digest(config, digest)?;
let mut endpoint = get_endpoint(config, name)?; let mut endpoint = get_endpoint(config, name)?;
@ -115,31 +123,33 @@ pub fn update_endpoint(
} }
if endpoint.mailto.is_none() && endpoint.mailto_user.is_none() { if endpoint.mailto.is_none() && endpoint.mailto_user.is_none() {
return Err(ApiError::bad_request( http_bail!(
"must at least provide one recipient, either in mailto or in mailto-user", BAD_REQUEST,
None, "must at least provide one recipient, either in mailto or in mailto-user"
)); );
} }
config config
.config .config
.set_data(name, SENDMAIL_TYPENAME, &endpoint) .set_data(name, SENDMAIL_TYPENAME, &endpoint)
.map_err(|e| { .map_err(|e| {
ApiError::internal_server_error( http_err!(
format!("could not save endpoint '{name}'"), INTERNAL_SERVER_ERROR,
Some(e.into()), "could not save endpoint '{}': {e}",
endpoint.name
) )
})?; })
Ok(())
} }
/// Delete existing sendmail endpoint /// Delete existing sendmail endpoint
/// ///
/// The caller is responsible for any needed permission checks. /// The caller is responsible for any needed permission checks.
/// The caller also responsible for locking the configuration files. /// The caller also responsible for locking the configuration files.
/// Returns an `ApiError` if the endpoint does not exist. /// Returns a `HttpError` if:
pub fn delete_endpoint(config: &mut Config, name: &str) -> Result<(), ApiError> { /// - an entity with the same name already exists (`400 Bad request`)
/// - a referenced filter does not exist (`400 Bad request`)
/// - the configuration could not be saved (`500 Internal server error`)
pub fn delete_endpoint(config: &mut Config, name: &str) -> Result<(), HttpError> {
// Check if the endpoint exists // Check if the endpoint exists
let _ = get_endpoint(config, name)?; let _ = get_endpoint(config, name)?;
super::ensure_unused(config, name)?; super::ensure_unused(config, name)?;
@ -154,7 +164,10 @@ pub mod tests {
use super::*; use super::*;
use crate::api::test_helpers::*; use crate::api::test_helpers::*;
pub fn add_sendmail_endpoint_for_test(config: &mut Config, name: &str) -> Result<(), ApiError> { pub fn add_sendmail_endpoint_for_test(
config: &mut Config,
name: &str,
) -> Result<(), HttpError> {
add_endpoint( add_endpoint(
config, config,
&SendmailConfig { &SendmailConfig {
@ -173,7 +186,7 @@ pub mod tests {
} }
#[test] #[test]
fn test_sendmail_create() -> Result<(), ApiError> { fn test_sendmail_create() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
assert_eq!(get_endpoints(&config)?.len(), 0); assert_eq!(get_endpoints(&config)?.len(), 0);
@ -186,7 +199,7 @@ pub mod tests {
} }
#[test] #[test]
fn test_update_not_existing_returns_error() -> Result<(), ApiError> { fn test_update_not_existing_returns_error() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
assert!(update_endpoint(&mut config, "test", &Default::default(), None, None,).is_err()); assert!(update_endpoint(&mut config, "test", &Default::default(), None, None,).is_err());
@ -195,7 +208,7 @@ pub mod tests {
} }
#[test] #[test]
fn test_update_invalid_digest_returns_error() -> Result<(), ApiError> { fn test_update_invalid_digest_returns_error() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?; add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?;
@ -219,7 +232,7 @@ pub mod tests {
} }
#[test] #[test]
fn test_sendmail_update() -> Result<(), ApiError> { fn test_sendmail_update() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?; add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?;
@ -275,7 +288,7 @@ pub mod tests {
} }
#[test] #[test]
fn test_sendmail_delete() -> Result<(), ApiError> { fn test_sendmail_delete() -> Result<(), HttpError> {
let mut config = empty_config(); let mut config = empty_config();
add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?; add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?;