mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-22 12:26:37 +00:00
fix(targets): pass credentials to MQTT broker availability check (#2192)
Signed-off-by: houseme <housemecn@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,8 @@
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `broker_url` - URL of MQTT Broker, for example `mqtt://localhost:1883`
|
/// * `broker_url` - URL of MQTT Broker, for example `mqtt://localhost:1883`
|
||||||
/// * `topic` - Topic for testing connections
|
/// * `topic` - Topic for testing connections
|
||||||
|
/// * `username` - Optional username for authentication
|
||||||
|
/// * `password` - Optional password for authentication
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Ok(())` - If the connection is successful
|
/// * `Ok(())` - If the connection is successful
|
||||||
/// * `Err(String)` - If the connection fails, contains an error message
|
/// * `Err(String)` - If the connection fails, contains an error message
|
||||||
@@ -25,7 +27,12 @@
|
|||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let result = rustfs_targets::check_mqtt_broker_available("mqtt://localhost:1883", "test/topic").await;
|
/// let result = rustfs_targets::check_mqtt_broker_available(
|
||||||
|
/// "mqtt://localhost:1883",
|
||||||
|
/// "test/topic",
|
||||||
|
/// Some("myuser"),
|
||||||
|
/// Some("mypass"),
|
||||||
|
/// ).await;
|
||||||
/// if result.is_ok() {
|
/// if result.is_ok() {
|
||||||
/// println!("MQTT Broker is available");
|
/// println!("MQTT Broker is available");
|
||||||
/// } else {
|
/// } else {
|
||||||
@@ -42,7 +49,12 @@
|
|||||||
/// tokio = { version = "1", features = ["full"] }
|
/// tokio = { version = "1", features = ["full"] }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
pub async fn check_mqtt_broker_available(broker_url: &str, topic: &str) -> Result<(), String> {
|
pub async fn check_mqtt_broker_available(
|
||||||
|
broker_url: &str,
|
||||||
|
topic: &str,
|
||||||
|
username: Option<&str>,
|
||||||
|
password: Option<&str>,
|
||||||
|
) -> Result<(), String> {
|
||||||
use rumqttc::{AsyncClient, MqttOptions, QoS};
|
use rumqttc::{AsyncClient, MqttOptions, QoS};
|
||||||
let url = rustfs_utils::parse_url(broker_url).map_err(|e| format!("Broker URL parsing failed:{e}"))?;
|
let url = rustfs_utils::parse_url(broker_url).map_err(|e| format!("Broker URL parsing failed:{e}"))?;
|
||||||
let url = url.url();
|
let url = url.url();
|
||||||
@@ -55,6 +67,15 @@ pub async fn check_mqtt_broker_available(broker_url: &str, topic: &str) -> Resul
|
|||||||
let host = url.host_str().ok_or("Broker is missing host")?;
|
let host = url.host_str().ok_or("Broker is missing host")?;
|
||||||
let port = url.port().unwrap_or(1883);
|
let port = url.port().unwrap_or(1883);
|
||||||
let mut mqtt_options = MqttOptions::new("rustfs_check", host, port);
|
let mut mqtt_options = MqttOptions::new("rustfs_check", host, port);
|
||||||
|
|
||||||
|
// Set credentials if provided
|
||||||
|
if let Some(user) = username
|
||||||
|
&& !user.is_empty()
|
||||||
|
{
|
||||||
|
let pass = password.unwrap_or("");
|
||||||
|
mqtt_options.set_credentials(user, pass);
|
||||||
|
}
|
||||||
|
|
||||||
mqtt_options.set_keep_alive(std::time::Duration::from_secs(5));
|
mqtt_options.set_keep_alive(std::time::Duration::from_secs(5));
|
||||||
let (client, mut eventloop) = AsyncClient::new(mqtt_options, 1);
|
let (client, mut eventloop) = AsyncClient::new(mqtt_options, 1);
|
||||||
|
|
||||||
|
|||||||
@@ -225,7 +225,9 @@ impl Operation for NotificationTarget {
|
|||||||
let topic = kv_map
|
let topic = kv_map
|
||||||
.get(rustfs_config::MQTT_TOPIC)
|
.get(rustfs_config::MQTT_TOPIC)
|
||||||
.ok_or_else(|| s3_error!(InvalidArgument, "topic is required"))?;
|
.ok_or_else(|| s3_error!(InvalidArgument, "topic is required"))?;
|
||||||
check_mqtt_broker_available(endpoint, topic)
|
let username = kv_map.get(rustfs_config::MQTT_USERNAME).copied();
|
||||||
|
let password = kv_map.get(rustfs_config::MQTT_PASSWORD).copied();
|
||||||
|
check_mqtt_broker_available(endpoint, topic, username, password)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| s3_error!(InvalidArgument, "MQTT Broker unavailable: {}", e))?;
|
.map_err(|e| s3_error!(InvalidArgument, "MQTT Broker unavailable: {}", e))?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user