diff --git a/crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs b/crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs index aad2daf154f..4a5d6eea00d 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/models/auth_method.rs @@ -57,3 +57,22 @@ impl AuthMethod { } } } + +pub fn try_get_device_id_from_credentials_file(credentials_path: &Utf8Path) -> Option { + if let Ok(contents) = std::fs::read_to_string(credentials_path) { + if let Ok(credentials) = toml::from_str::(&contents) { + return Some(credentials.c8y.device_id); + } + } + None +} + +#[derive(Debug, serde::Deserialize)] +struct CredentialsFile { + c8y: C8y, +} + +#[derive(Debug, serde::Deserialize)] +struct C8y { + device_id: String, +} 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 f02bd84b394..0e72cdd00a0 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,4 +1,5 @@ use super::models::timestamp::TimeFormat; +use crate::auth_method::try_get_device_id_from_credentials_file; use crate::auth_method::AuthMethod; use crate::AptConfig; use crate::AutoFlag; @@ -1195,6 +1196,20 @@ fn default_http_bind_address(dto: &TEdgeConfigDto) -> IpAddr { } fn device_id(reader: &TEdgeConfigReader) -> Result { + let c8y_profile: Option<&ProfileName> = None; + let c8y_config = reader.c8y.try_get(c8y_profile)?; + + if c8y_config + .auth_method + .is_basic(&c8y_config.credentials_path) + { + if let Some(device_id) = + try_get_device_id_from_credentials_file(&c8y_config.credentials_path) + { + return Ok(device_id); + } + } + let pem = PemCertificate::from_pem_file(&reader.device.cert_path) .map_err(|err| cert_error_into_config_error(ReadOnlyKey::DeviceId.to_cow_str(), err))?; let device_id = pem