From a89f23d7bae99fb9fe71ec4d6416ea0ace6859b1 Mon Sep 17 00:00:00 2001 From: Rina Fujino Date: Thu, 24 Oct 2024 14:59:47 +0200 Subject: [PATCH] fixup! Add c8y.registration_mode enum instead of c8y.use_basic_auth bool Signed-off-by: Rina Fujino --- .../{registration_mode.rs => auth_method.rs} | 20 ++++++++-------- .../src/tedge_config_cli/models/mod.rs | 2 +- .../src/tedge_config_cli/tedge_config.rs | 11 +++++---- crates/core/c8y_api/src/http_proxy.rs | 7 ++---- crates/core/tedge/src/cli/connect/command.rs | 23 +++++++++---------- crates/core/tedge_mapper/src/c8y/mapper.rs | 2 +- .../smartrest_one/smartrest_one.robot | 2 +- 7 files changed, 32 insertions(+), 35 deletions(-) rename crates/common/tedge_config/src/tedge_config_cli/models/{registration_mode.rs => auth_method.rs} (69%) diff --git a/crates/common/tedge_config/src/tedge_config_cli/models/registration_mode.rs b/crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs similarity index 69% rename from crates/common/tedge_config/src/tedge_config_cli/models/registration_mode.rs rename to crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs index 23e4a0a5e2..aad2daf154 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/models/registration_mode.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs @@ -7,7 +7,7 @@ use strum_macros::Display; )] #[serde(rename_all = "kebab-case")] #[strum(serialize_all = "kebab-case")] -pub enum RegistrationMode { +pub enum AuthMethod { Certificate, Basic, Auto, @@ -19,14 +19,14 @@ pub struct InvalidRegistrationMode { input: String, } -impl FromStr for RegistrationMode { +impl FromStr for AuthMethod { type Err = InvalidRegistrationMode; fn from_str(input: &str) -> Result { match input { - "certificate" => Ok(RegistrationMode::Certificate), - "basic" => Ok(RegistrationMode::Basic), - "auto" => Ok(RegistrationMode::Auto), + "certificate" => Ok(AuthMethod::Certificate), + "basic" => Ok(AuthMethod::Basic), + "auto" => Ok(AuthMethod::Auto), _ => Err(InvalidRegistrationMode { input: input.to_string(), }), @@ -39,7 +39,7 @@ pub enum AuthType { Basic, } -impl RegistrationMode { +impl AuthMethod { pub fn is_basic(self, credentials_path: &Utf8Path) -> bool { matches!(self.to_type(credentials_path), AuthType::Basic) } @@ -50,10 +50,10 @@ impl RegistrationMode { pub fn to_type(self, credentials_path: &Utf8Path) -> AuthType { match self { - RegistrationMode::Certificate => AuthType::Certificate, - RegistrationMode::Basic => AuthType::Basic, - RegistrationMode::Auto if credentials_path.exists() => AuthType::Basic, - RegistrationMode::Auto => AuthType::Certificate, + AuthMethod::Certificate => AuthType::Certificate, + AuthMethod::Basic => AuthType::Basic, + AuthMethod::Auto if credentials_path.exists() => AuthType::Basic, + AuthMethod::Auto => AuthType::Certificate, } } } diff --git a/crates/common/tedge_config/src/tedge_config_cli/models/mod.rs b/crates/common/tedge_config/src/tedge_config_cli/models/mod.rs index 8572443c1a..7fb154ddf9 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/models/mod.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/models/mod.rs @@ -1,4 +1,5 @@ pub mod apt_config; +pub mod auth_method; pub mod auto; pub mod c8y_software_management; pub mod connect_url; @@ -6,7 +7,6 @@ pub mod flag; pub mod host_port; pub mod ipaddress; pub mod port; -pub mod registration_mode; pub mod seconds; pub mod templates_set; diff --git a/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs b/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs index e781f01c44..ca1b7f9719 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs @@ -1,5 +1,5 @@ use super::models::timestamp::TimeFormat; -use crate::registration_mode::RegistrationMode; +use crate::auth_method::AuthMethod; use crate::AptConfig; use crate::AutoFlag; use crate::AutoLogUpload; @@ -381,7 +381,7 @@ impl_append_remove_for_single_value!( u32, AptConfig, MqttPayloadLimit, - RegistrationMode + AuthMethod ); impl AppendRemoveItem for TemplatesSet { @@ -472,9 +472,10 @@ define_tedge_config! { #[doku(as = "PathBuf")] root_cert_path: Utf8PathBuf, - /// The type of authentication method - #[tedge_config(example = "certificate", example = "basic", example = "auto", default(variable = RegistrationMode::Certificate))] - registration_mode: RegistrationMode, + /// The authentication method used to connect Cumulocity + #[tedge_config(note = "In the auto mode, basic auth is used if c8y.credentials_path is set")] + #[tedge_config(example = "certificate", example = "basic", example = "auto", default(variable = AuthMethod::Certificate))] + auth_method: AuthMethod, /// The path where Cumulocity username/password are stored #[tedge_config(note = "The value must be the path of the credentials file.")] diff --git a/crates/core/c8y_api/src/http_proxy.rs b/crates/core/c8y_api/src/http_proxy.rs index e3249ad187..788162171c 100644 --- a/crates/core/c8y_api/src/http_proxy.rs +++ b/crates/core/c8y_api/src/http_proxy.rs @@ -14,8 +14,8 @@ use reqwest::Url; use std::collections::HashMap; use std::path::PathBuf; use std::time::Duration; +use tedge_config::auth_method::AuthType; use tedge_config::mqtt_config::MqttConfigBuildError; -use tedge_config::registration_mode::AuthType; use tedge_config::MultiError; use tedge_config::TEdgeConfig; use tedge_config::TopicPrefix; @@ -190,10 +190,7 @@ impl C8yAuthRetriever { let c8y_config = tedge_config.c8y.try_get(c8y_profile)?; let topic_prefix = c8y_config.bridge.topic_prefix.clone(); - match c8y_config - .registration_mode - .to_type(&c8y_config.credentials_path) - { + match c8y_config.auth_method.to_type(&c8y_config.credentials_path) { AuthType::Basic => Ok(Self::Basic { credentials_path: c8y_config.credentials_path.clone(), }), diff --git a/crates/core/tedge/src/cli/connect/command.rs b/crates/core/tedge/src/cli/connect/command.rs index 0c1f396c3d..b4d14c31e6 100644 --- a/crates/core/tedge/src/cli/connect/command.rs +++ b/crates/core/tedge/src/cli/connect/command.rs @@ -19,7 +19,7 @@ use rumqttc::QoS::AtLeastOnce; use std::path::Path; use std::sync::Arc; use std::time::Duration; -use tedge_config::registration_mode::AuthType; +use tedge_config::auth_method::AuthType; use tedge_config::system_services::*; use tedge_config::TEdgeConfig; use tedge_config::*; @@ -266,16 +266,15 @@ pub fn bridge_config( Cloud::C8y => { let c8y_config = config.c8y.try_get(profile)?; - let (remote_username, remote_password) = match c8y_config - .registration_mode - .to_type(&c8y_config.credentials_path) - { - AuthType::Certificate => (None, None), - AuthType::Basic => { - let (username, password) = read_c8y_credentials(&c8y_config.credentials_path)?; - (Some(username), Some(password)) - } - }; + let (remote_username, remote_password) = + match c8y_config.auth_method.to_type(&c8y_config.credentials_path) { + AuthType::Certificate => (None, None), + AuthType::Basic => { + let (username, password) = + read_c8y_credentials(&c8y_config.credentials_path)?; + (Some(username), Some(password)) + } + }; let params = BridgeConfigC8yParams { mqtt_host: c8y_config.mqtt.or_config_not_set()?.clone(), @@ -308,7 +307,7 @@ fn check_device_status_c8y( // TODO: Use SmartREST1 to check connection if c8y_config - .registration_mode + .auth_method .is_basic(&c8y_config.credentials_path) { return Ok(DeviceStatus::AlreadyExists); diff --git a/crates/core/tedge_mapper/src/c8y/mapper.rs b/crates/core/tedge_mapper/src/c8y/mapper.rs index 8631fb2efc..63574f5704 100644 --- a/crates/core/tedge_mapper/src/c8y/mapper.rs +++ b/crates/core/tedge_mapper/src/c8y/mapper.rs @@ -68,7 +68,7 @@ impl TEdgeComponent for CumulocityMapper { .map(|id| Cow::Owned(format!("s/dc/{id}"))); let use_certificate = c8y_config - .registration_mode + .auth_method .is_certificate(&c8y_config.credentials_path); let cloud_topics = [ ("s/dt", true), diff --git a/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot b/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot index 465d2d1f89..613bc31839 100644 --- a/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot +++ b/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot @@ -67,7 +67,7 @@ Custom Setup ${DEVICE_SN}= Setup skip_bootstrap=${True} Execute Command test -f ./bootstrap.sh && ./bootstrap.sh --no-connect || true Execute Command tedge config set mqtt.bridge.built_in ${use_builtin_bridge} - Execute Command tedge config set c8y.registration_mode basic + Execute Command tedge config set c8y.auth_method basic Set Suite Variable $DEVICE_SN