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 8c5a53c
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 @@ -56,4 +56,23 @@ impl AuthMethod {
AuthMethod::Auto => AuthType::Certificate,
}
}

pub fn try_get_device_id(self, 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
Expand Up @@ -1195,6 +1195,21 @@ 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) = 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
Expand Down

0 comments on commit 8c5a53c

Please sign in to comment.