Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read device.id from credentials.toml when basic auth mode #3242

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading