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..e84bd21e7a1 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 @@ -56,4 +56,23 @@ impl AuthMethod { AuthMethod::Auto => AuthType::Certificate, } } + + pub fn try_get_device_id(self, 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..372fefbfd43 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 @@ -1195,6 +1195,21 @@ 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) = c8y_config + .auth_method + .try_get_device_id(&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