Skip to content

Commit a05c6be

Browse files
committed
feat: log the auth method when running tedge connect c8y
1 parent 51ac34a commit a05c6be

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl FromStr for AuthMethod {
3434
}
3535
}
3636

37+
#[derive(PartialEq, Eq)]
3738
pub enum AuthType {
3839
Certificate,
3940
Basic,

crates/core/tedge/src/cli/connect/command.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use anyhow::bail;
2121
use c8y_api::http_proxy::read_c8y_credentials;
2222
use c8y_api::smartrest::message::get_smartrest_template_id;
2323
use c8y_api::smartrest::message_ids::JWT_TOKEN;
24+
use camino::Utf8Path;
2425
use camino::Utf8PathBuf;
2526
use mqtt_channel::Topic;
2627
use rumqttc::Event;
@@ -91,6 +92,7 @@ impl ConnectCommand {
9192
let config = &self.config;
9293
let bridge_config = bridge_config(config, &self.cloud)?;
9394
let updated_mosquitto_config = CommonMosquittoConfig::from_tedge_config(config);
95+
let credentials_path = credentials_path_for(config, &self.cloud)?;
9496

9597
validate_config(config, &self.cloud)?;
9698

@@ -100,6 +102,7 @@ impl ConnectCommand {
100102
&bridge_config,
101103
&*self.service_manager,
102104
&self.cloud,
105+
credentials_path,
103106
);
104107
// If the bridge is part of the mapper, the bridge config file won't exist
105108
// TODO tidy me up once mosquitto is no longer required for bridge
@@ -134,7 +137,13 @@ impl ConnectCommand {
134137
false => format!("Connecting to {} with config", self.cloud),
135138
true => "Reconnecting with config".into(),
136139
};
137-
ConfigLogger::log(title, &bridge_config, &*self.service_manager, &self.cloud);
140+
ConfigLogger::log(
141+
title,
142+
&bridge_config,
143+
&*self.service_manager,
144+
&self.cloud,
145+
credentials_path,
146+
);
138147

139148
let device_type = &config.device.ty;
140149

@@ -202,6 +211,18 @@ impl ConnectCommand {
202211
}
203212
}
204213

214+
fn credentials_path_for<'a>(
215+
config: &'a TEdgeConfig,
216+
cloud: &Cloud,
217+
) -> Result<Option<&'a Utf8Path>, MultiError> {
218+
if let Cloud::C8y(profile) = cloud {
219+
let c8y_config = config.c8y.try_get(profile.as_deref())?;
220+
Ok(Some(&c8y_config.credentials_path))
221+
} else {
222+
Ok(None)
223+
}
224+
}
225+
205226
impl ConnectCommand {
206227
fn tenant_matches_configured_url(
207228
&self,

crates/core/tedge/src/cli/log.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::time::Instant;
77

88
use camino::Utf8Path;
99
use tedge_config::auth_method::AuthMethod;
10+
use tedge_config::auth_method::AuthType;
1011
use tedge_config::system_services::SystemServiceError;
1112
use tedge_config::system_services::SystemServiceManager;
1213
use tedge_config::MultiError;
@@ -251,6 +252,7 @@ pub struct ConfigLogger<'a> {
251252
service_manager: &'a dyn SystemServiceManager,
252253
mosquitto_version: Option<&'a str>,
253254
cloud: &'a MaybeBorrowedCloud<'a>,
255+
credentials_path: Option<&'a Utf8Path>,
254256
}
255257

256258
impl<'a> ConfigLogger<'a> {
@@ -260,6 +262,7 @@ impl<'a> ConfigLogger<'a> {
260262
config: &'a BridgeConfig,
261263
service_manager: &'a dyn SystemServiceManager,
262264
cloud: &'a MaybeBorrowedCloud<'a>,
265+
credentials_path: Option<&'a Utf8Path>,
263266
) {
264267
println!(
265268
"{}",
@@ -269,7 +272,8 @@ impl<'a> ConfigLogger<'a> {
269272
cloud_host: config.address.to_string(),
270273
cert_path: &config.bridge_certfile,
271274
bridge_location: config.bridge_location,
272-
auth_method: None,
275+
auth_method: config.auth_method,
276+
credentials_path,
273277
service_manager,
274278
mosquitto_version: config.mosquitto_version.as_deref(),
275279
cloud,
@@ -302,16 +306,25 @@ impl fmt::Display for ConfigLogger<'_> {
302306
self.log_single_entry(f, "cloud profile", &"<none>")?;
303307
}
304308
self.log_single_entry(f, "cloud host", &self.cloud_host)?;
305-
self.log_single_entry(f, "certificate file", &self.cert_path)?;
309+
let mut auth_type = AuthType::Certificate;
310+
if let Some(auth_method) = self.auth_method {
311+
self.log_single_entry(f, "auth method", &auth_method)?;
312+
if let Some(path) = self.credentials_path {
313+
auth_type = auth_method.to_type(path);
314+
if AuthType::Basic == auth_type {
315+
self.log_single_entry(f, "credentials path", &path)?
316+
}
317+
}
318+
}
319+
if AuthType::Certificate == auth_type {
320+
self.log_single_entry(f, "certificate file", &self.cert_path)?;
321+
}
306322
self.log_single_entry(f, "bridge", &self.bridge_location)?;
307323
self.log_single_entry(f, "service manager", &self.service_manager.name())?;
308324
if let Some(mosquitto_version) = self.mosquitto_version {
309325
self.log_single_entry(f, "mosquitto version", &mosquitto_version)?;
310326
}
311327

312-
if let Some(auth_method) = self.auth_method {
313-
self.log_single_entry(f, "authentication mode", &auth_method)?;
314-
}
315328
Ok(())
316329
}
317330
}

0 commit comments

Comments
 (0)