Skip to content

Commit

Permalink
fixup! Add c8y.registration_mode enum instead of c8y.use_basic_auth bool
Browse files Browse the repository at this point in the history
Signed-off-by: Rina Fujino <[email protected]>
  • Loading branch information
rina23q committed Oct 24, 2024
1 parent f48924b commit a89f23d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Self, Self::Err> {
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(),
}),
Expand All @@ -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)
}
Expand All @@ -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,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub mod apt_config;
pub mod auth_method;
pub mod auto;
pub mod c8y_software_management;
pub mod connect_url;
pub mod flag;
pub mod host_port;
pub mod ipaddress;
pub mod port;
pub mod registration_mode;
pub mod seconds;
pub mod templates_set;

Expand Down
11 changes: 6 additions & 5 deletions crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -381,7 +381,7 @@ impl_append_remove_for_single_value!(
u32,
AptConfig,
MqttPayloadLimit,
RegistrationMode
AuthMethod
);

impl AppendRemoveItem for TemplatesSet {
Expand Down Expand Up @@ -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.")]
Expand Down
7 changes: 2 additions & 5 deletions crates/core/c8y_api/src/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
}),
Expand Down
23 changes: 11 additions & 12 deletions crates/core/tedge/src/cli/connect/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/core/tedge_mapper/src/c8y/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a89f23d

Please sign in to comment.