Skip to content

Commit

Permalink
feat: read device.id from credentials.toml when basic auth mode
Browse files Browse the repository at this point in the history
Signed-off-by: Rina Fujino <rina.fujino.23@gmail.com>
  • Loading branch information
rina23q committed Nov 12, 2024
1 parent 02a4ad8 commit 6ba5896
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ impl AuthMethod {
}
}
}

pub fn try_get_device_id_from_credentials_file(credentials_path: &Utf8Path) -> Option<String> {
if let Ok(contents) = std::fs::read_to_string(credentials_path) {
if let Ok(credentials) = toml::from_str::<CredentialsFile>(&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,
}
15 changes: 15 additions & 0 deletions crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -1195,6 +1196,20 @@ fn default_http_bind_address(dto: &TEdgeConfigDto) -> IpAddr {
}

fn device_id(reader: &TEdgeConfigReader) -> Result<String, ReadError> {
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
Expand Down

0 comments on commit 6ba5896

Please sign in to comment.