Skip to content

Commit b2883d3

Browse files
authored
Merge pull request #3479 from Ruadhri17/basic-auth-fix-cert-warn
fix: skip chown for cert and key when using Basic Auth
2 parents 547a0b2 + 793c72d commit b2883d3

File tree

7 files changed

+39
-41
lines changed

7 files changed

+39
-41
lines changed

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

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

37-
#[derive(PartialEq, Eq)]
37+
#[derive(PartialEq, Eq, Debug, Clone, Copy, Display)]
3838
pub enum AuthType {
3939
Certificate,
4040
Basic,

crates/core/tedge/src/bridge/aws.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::time::Duration;
66
use tedge_api::mqtt_topics::Channel;
77
use tedge_api::mqtt_topics::EntityTopicId;
88
use tedge_api::mqtt_topics::MqttSchema;
9+
use tedge_config::models::auth_method::AuthType;
910
use tedge_config::models::HostPort;
1011
use tedge_config::models::TopicPrefix;
1112
use tedge_config::models::MQTT_TLS_PORT;
@@ -108,7 +109,7 @@ impl From<BridgeConfigAwsParams> for BridgeConfig {
108109
// to create the "Thing", so the first connection attempt can fail, but retrying
109110
// will give it a higher chance of success
110111
connection_check_attempts: 5,
111-
auth_method: None,
112+
auth_type: AuthType::Certificate,
112113
mosquitto_version: None,
113114
keepalive_interval,
114115
use_cryptoki: false,
@@ -166,7 +167,7 @@ fn test_bridge_config_from_aws_params() -> anyhow::Result<()> {
166167
bridge_attempt_unsubscribe: false,
167168
bridge_location: BridgeLocation::Mosquitto,
168169
connection_check_attempts: 5,
169-
auth_method: None,
170+
auth_type: AuthType::Certificate,
170171
mosquitto_version: None,
171172
keepalive_interval: Duration::from_secs(60),
172173
use_cryptoki: false,
@@ -229,7 +230,7 @@ fn test_bridge_config_aws_custom_topic_prefix() -> anyhow::Result<()> {
229230
bridge_attempt_unsubscribe: false,
230231
bridge_location: BridgeLocation::Mosquitto,
231232
connection_check_attempts: 5,
232-
auth_method: None,
233+
auth_type: AuthType::Certificate,
233234
mosquitto_version: None,
234235
keepalive_interval: Duration::from_secs(60),
235236
use_cryptoki: false,

crates/core/tedge/src/bridge/azure.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::time::Duration;
66
use tedge_api::mqtt_topics::Channel;
77
use tedge_api::mqtt_topics::EntityTopicId;
88
use tedge_api::mqtt_topics::MqttSchema;
9+
use tedge_config::models::auth_method::AuthType;
910
use tedge_config::models::HostPort;
1011
use tedge_config::models::TopicPrefix;
1112
use tedge_config::models::MQTT_TLS_PORT;
@@ -104,7 +105,7 @@ impl From<BridgeConfigAzureParams> for BridgeConfig {
104105
],
105106
bridge_location,
106107
connection_check_attempts: 1,
107-
auth_method: None,
108+
auth_type: AuthType::Certificate,
108109
mosquitto_version: None,
109110
keepalive_interval,
110111
use_cryptoki: false,
@@ -166,7 +167,7 @@ fn test_bridge_config_from_azure_params() -> anyhow::Result<()> {
166167
bridge_attempt_unsubscribe: false,
167168
bridge_location: BridgeLocation::Mosquitto,
168169
connection_check_attempts: 1,
169-
auth_method: None,
170+
auth_type: AuthType::Certificate,
170171
mosquitto_version: None,
171172
keepalive_interval: Duration::from_secs(60),
172173
use_cryptoki: false,
@@ -232,7 +233,7 @@ fn test_azure_bridge_config_with_custom_prefix() -> anyhow::Result<()> {
232233
bridge_attempt_unsubscribe: false,
233234
bridge_location: BridgeLocation::Mosquitto,
234235
connection_check_attempts: 1,
235-
auth_method: None,
236+
auth_type: AuthType::Certificate,
236237
mosquitto_version: None,
237238
keepalive_interval: Duration::from_secs(60),
238239
use_cryptoki: false,

crates/core/tedge/src/bridge/c8y.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::time::Duration;
77
use tedge_api::mqtt_topics::Channel;
88
use tedge_api::mqtt_topics::EntityTopicId;
99
use tedge_api::mqtt_topics::MqttSchema;
10-
use tedge_config::models::auth_method::AuthMethod;
10+
use tedge_config::models::auth_method::AuthType;
1111
use tedge_config::models::AutoFlag;
1212
use tedge_config::models::HostPort;
1313
use tedge_config::models::TemplatesSet;
@@ -89,8 +89,13 @@ impl From<BridgeConfigC8yParams> for BridgeConfig {
8989
format!(r#"error in 1 {topic_prefix}/ """#),
9090
];
9191

92-
let use_basic_auth = remote_username.is_some() && remote_password.is_some();
93-
if !use_basic_auth {
92+
let auth_type = if remote_username.is_some() {
93+
AuthType::Basic
94+
} else {
95+
AuthType::Certificate
96+
};
97+
98+
if auth_type == AuthType::Certificate {
9499
topics.extend(vec![
95100
// c8y JWT token retrieval
96101
format!(r#"s/uat out 0 {topic_prefix}/ """#),
@@ -148,12 +153,6 @@ impl From<BridgeConfigC8yParams> for BridgeConfig {
148153
AutoFlag::Auto => is_mosquitto_version_above_2(),
149154
};
150155

151-
let auth_method = if remote_username.is_some() {
152-
AuthMethod::Basic
153-
} else {
154-
AuthMethod::Certificate
155-
};
156-
157156
let service_name = format!("mosquitto-{topic_prefix}-bridge");
158157
let health = mqtt_schema.topic_for(
159158
&EntityTopicId::default_main_service(&service_name).unwrap(),
@@ -193,7 +192,7 @@ impl From<BridgeConfigC8yParams> for BridgeConfig {
193192
topics,
194193
bridge_location,
195194
connection_check_attempts: 1,
196-
auth_method: Some(auth_method),
195+
auth_type,
197196
mosquitto_version,
198197
keepalive_interval,
199198
use_cryptoki,
@@ -326,7 +325,7 @@ mod tests {
326325
bridge_attempt_unsubscribe: false,
327326
bridge_location: BridgeLocation::Mosquitto,
328327
connection_check_attempts: 1,
329-
auth_method: Some(AuthMethod::Certificate),
328+
auth_type: AuthType::Certificate,
330329
mosquitto_version: None,
331330
keepalive_interval: Duration::from_secs(60),
332331
use_cryptoki: false,
@@ -432,7 +431,7 @@ mod tests {
432431
bridge_attempt_unsubscribe: false,
433432
bridge_location: BridgeLocation::Mosquitto,
434433
connection_check_attempts: 1,
435-
auth_method: Some(AuthMethod::Basic),
434+
auth_type: AuthType::Basic,
436435
mosquitto_version: None,
437436
keepalive_interval: Duration::from_secs(60),
438437
use_cryptoki: false,

crates/core/tedge/src/bridge/config.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use camino::Utf8PathBuf;
22
use core::fmt;
33
use std::borrow::Cow;
44
use std::time::Duration;
5-
use tedge_config::models::auth_method::AuthMethod;
5+
use tedge_config::models::auth_method::AuthType;
66
use tedge_config::models::HostPort;
77
use tedge_config::models::MQTT_TLS_PORT;
88
use tedge_config::TEdgeConfigLocation;
@@ -39,7 +39,7 @@ pub struct BridgeConfig {
3939
pub bridge_attempt_unsubscribe: bool,
4040
pub topics: Vec<String>,
4141
pub connection_check_attempts: i32,
42-
pub auth_method: Option<AuthMethod>,
42+
pub auth_type: AuthType,
4343
pub mosquitto_version: Option<String>,
4444
pub keepalive_interval: Duration,
4545
pub use_cryptoki: bool,
@@ -84,8 +84,8 @@ impl BridgeConfig {
8484
if let Some(name) = &self.remote_username {
8585
writeln_async!(writer, "remote_username {}", name)?;
8686
}
87-
let use_basic_auth = self.remote_username.is_some() && self.remote_password.is_some();
88-
if use_basic_auth {
87+
88+
if self.auth_type == AuthType::Basic {
8989
if let Some(password) = &self.remote_password {
9090
writeln_async!(writer, "remote_password {}", password)?;
9191
}
@@ -190,7 +190,7 @@ mod test {
190190
bridge_attempt_unsubscribe: false,
191191
bridge_location: BridgeLocation::Mosquitto,
192192
connection_check_attempts: 1,
193-
auth_method: None,
193+
auth_type: AuthType::Certificate,
194194
mosquitto_version: None,
195195
keepalive_interval: Duration::from_secs(60),
196196
use_cryptoki: false,
@@ -263,7 +263,7 @@ keepalive_interval 60
263263
bridge_attempt_unsubscribe: false,
264264
bridge_location: BridgeLocation::Mosquitto,
265265
connection_check_attempts: 1,
266-
auth_method: None,
266+
auth_type: AuthType::Certificate,
267267
mosquitto_version: None,
268268
keepalive_interval: Duration::from_secs(60),
269269
use_cryptoki: false,
@@ -338,7 +338,7 @@ keepalive_interval 60
338338
bridge_attempt_unsubscribe: false,
339339
bridge_location: BridgeLocation::Mosquitto,
340340
connection_check_attempts: 1,
341-
auth_method: None,
341+
auth_type: AuthType::Certificate,
342342
mosquitto_version: None,
343343
keepalive_interval: Duration::from_secs(60),
344344
use_cryptoki: false,
@@ -413,7 +413,7 @@ keepalive_interval 60
413413
bridge_attempt_unsubscribe: false,
414414
bridge_location: BridgeLocation::Mosquitto,
415415
connection_check_attempts: 1,
416-
auth_method: None,
416+
auth_type: AuthType::Basic,
417417
mosquitto_version: None,
418418
keepalive_interval: Duration::from_secs(60),
419419
use_cryptoki: false,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,11 @@ async fn new_bridge(
10221022
}
10231023

10241024
pub async fn chown_certificate_and_key(bridge_config: &BridgeConfig) {
1025+
// Skip chown when using Basic Auth
1026+
if bridge_config.auth_type == AuthType::Basic {
1027+
return;
1028+
}
1029+
10251030
let (user, group) = match bridge_config.bridge_location {
10261031
BridgeLocation::BuiltIn => ("tedge", "tedge"),
10271032
BridgeLocation::Mosquitto => (crate::BROKER_USER, crate::BROKER_GROUP),

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::time::Instant;
99
use crate::system_services::SystemServiceError;
1010
use crate::system_services::SystemServiceManager;
1111
use camino::Utf8Path;
12-
use tedge_config::models::auth_method::AuthMethod;
1312
use tedge_config::models::auth_method::AuthType;
1413
use tedge_config::tedge_toml::MultiError;
1514
use yansi::Paint as _;
@@ -251,7 +250,7 @@ pub struct ConfigLogger<'a> {
251250
cloud_host: String,
252251
cert_path: &'a Utf8Path,
253252
bridge_location: BridgeLocation,
254-
auth_method: Option<AuthMethod>,
253+
auth_type: AuthType,
255254
service_manager: &'a dyn SystemServiceManager,
256255
mosquitto_version: Option<&'a str>,
257256
cloud: &'a MaybeBorrowedCloud<'a>,
@@ -275,7 +274,7 @@ impl<'a> ConfigLogger<'a> {
275274
cloud_host: config.address.to_string(),
276275
cert_path: &config.bridge_certfile,
277276
bridge_location: config.bridge_location,
278-
auth_method: config.auth_method,
277+
auth_type: config.auth_type,
279278
credentials_path,
280279
service_manager,
281280
mosquitto_version: config.mosquitto_version.as_deref(),
@@ -309,18 +308,11 @@ impl fmt::Display for ConfigLogger<'_> {
309308
self.log_single_entry(f, "cloud profile", &"<none>")?;
310309
}
311310
self.log_single_entry(f, "cloud host", &self.cloud_host)?;
312-
let mut auth_type = AuthType::Certificate;
313-
if let Some(auth_method) = self.auth_method {
314-
self.log_single_entry(f, "auth method", &auth_method)?;
315-
if let Some(path) = self.credentials_path {
316-
auth_type = auth_method.to_type(path);
317-
if AuthType::Basic == auth_type {
318-
self.log_single_entry(f, "credentials path", &path)?
319-
}
320-
}
321-
}
322-
if AuthType::Certificate == auth_type {
311+
self.log_single_entry(f, "auth type", &self.auth_type)?;
312+
if self.auth_type == AuthType::Certificate {
323313
self.log_single_entry(f, "certificate file", &self.cert_path)?;
314+
} else if let Some(path) = self.credentials_path {
315+
self.log_single_entry(f, "credentials path", &path)?
324316
}
325317
self.log_single_entry(f, "bridge", &self.bridge_location)?;
326318
self.log_single_entry(f, "service manager", &self.service_manager.name())?;

0 commit comments

Comments
 (0)