Skip to content

Commit 09324f5

Browse files
committed
keylime/config: Use macro to implement PushModelConfigTrait
Use the procedural macro 'define_view_trait' to create a view limited by a trait for the AgentConfig instead of creating a new structure. This removes the PushModelConfig structure. The AgentConfig should be used directly instead, which has the PushModelConfigTrait implemented using the procedural macro. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
1 parent 4119de1 commit 09324f5

File tree

7 files changed

+573
-659
lines changed

7 files changed

+573
-659
lines changed

keylime-push-model-agent/src/main.rs

Lines changed: 303 additions & 228 deletions
Large diffs are not rendered by default.

keylime-push-model-agent/src/registration.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use keylime::{
66
error::Result,
77
};
88

9-
pub async fn check_registration(
9+
pub async fn check_registration<T: PushModelConfigTrait>(
10+
config: &T,
1011
context_info: Option<context_info::ContextInfo>,
1112
) -> Result<()> {
12-
let reg_config = keylime::config::PushModelConfig::default();
1313
if context_info.is_some() {
1414
crate::registration::register_agent(
15-
&reg_config,
15+
config,
1616
&mut context_info.unwrap(),
1717
)
1818
.await?;
@@ -25,31 +25,39 @@ pub async fn register_agent<T: PushModelConfigTrait>(
2525
context_info: &mut context_info::ContextInfo,
2626
) -> Result<()> {
2727
let ac = AgentRegistrationConfig {
28-
contact_ip: config.get_contact_ip(),
29-
contact_port: config.get_contact_port(),
30-
registrar_ip: config.get_registrar_ip(),
31-
registrar_port: config.get_registrar_port(),
32-
enable_iak_idevid: config.get_enable_iak_idevid(),
33-
ek_handle: config.get_ek_handle(),
28+
contact_ip: config.contact_ip().to_string(),
29+
contact_port: config.contact_port(),
30+
registrar_ip: config.registrar_ip().to_string(),
31+
registrar_port: config.registrar_port(),
32+
enable_iak_idevid: config.enable_iak_idevid(),
33+
// TODO: make it to not panic on failure
34+
ek_handle: config
35+
.ek_handle()
36+
.expect("failed to get ek_handle")
37+
.to_string(),
3438
};
3539

3640
let cert_config = cert::CertificateConfig {
37-
agent_uuid: config.get_uuid(),
38-
contact_ip: config.get_contact_ip(),
39-
contact_port: config.get_contact_port(),
40-
server_cert: config.get_server_cert(),
41-
server_key: config.get_server_key(),
42-
server_key_password: config.get_server_key_password(),
41+
agent_uuid: config.uuid().to_string(),
42+
contact_ip: config.contact_ip().to_string(),
43+
contact_port: config.contact_port(),
44+
server_cert: config.server_cert().to_string(),
45+
server_key: config.server_key().to_string(),
46+
server_key_password: config.server_key_password().to_string(),
4347
};
4448

4549
let server_cert_key = cert::cert_from_server_key(&cert_config)?;
4650

4751
let aa = AgentRegistration {
4852
ak: context_info.ak.clone(),
4953
ek_result: context_info.ek_result.clone(),
50-
api_versions: config.get_registrar_api_versions(),
54+
api_versions: config
55+
.registrar_api_versions()?
56+
.iter()
57+
.map(|e| e.to_string())
58+
.collect(),
5159
agent_registration_config: ac,
52-
agent_uuid: config.get_uuid(),
60+
agent_uuid: config.uuid().to_string(),
5361
mtls_cert: Some(server_cert_key.0),
5462
device_id: None, // TODO: Check how to proceed with device ID
5563
attest: None, // TODO: Check how to proceed with attestation, normally, no device ID means no attest
@@ -63,25 +71,31 @@ pub async fn register_agent<T: PushModelConfigTrait>(
6371
}
6472
} // register_agent
6573

74+
#[cfg(feature = "testing")]
6675
#[cfg(test)]
6776
mod tests {
6877
use super::*;
6978

70-
#[cfg(feature = "testing")]
71-
use keylime::context_info::{AlgorithmConfigurationString, ContextInfo};
79+
use keylime::{
80+
config::get_testing_config,
81+
context_info::{AlgorithmConfigurationString, ContextInfo},
82+
tpm::testing,
83+
};
7284

7385
#[actix_rt::test]
7486
async fn test_avoid_registration() {
75-
let result = check_registration(None).await;
87+
let _mutex = testing::lock_tests().await;
88+
let tmpdir = tempfile::tempdir().expect("failed to create tempdir");
89+
let config = get_testing_config(tmpdir.path());
90+
let result = check_registration(&config, None).await;
7691
assert!(result.is_ok());
7792
}
7893

7994
#[tokio::test]
80-
#[cfg(feature = "testing")]
8195
async fn test_register_agent() {
82-
use keylime::tpm::testing;
8396
let _mutex = testing::lock_tests().await;
84-
let config = keylime::config::PushModelConfig::default();
97+
let tmpdir = tempfile::tempdir().expect("failed to create tmpdir");
98+
let config = get_testing_config(tmpdir.path());
8599
let alg_config = AlgorithmConfigurationString {
86100
tpm_encryption_alg: "rsa".to_string(),
87101
tpm_hash_alg: "sha256".to_string(),

keylime-push-model-agent/src/struct_filler.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,12 @@ pub struct FillerFromHardware<'a> {
5353

5454
impl<'a> FillerFromHardware<'a> {
5555
pub fn new(tpm_context_info: &'a mut ContextInfo) -> Self {
56-
// TODO: Change config obtaining here to avoid repetitions
56+
// TODO: Change this to avoid loading the configuration multiple times
5757
// TODO: Modify here to avoid panic on failure
58-
let global_config = AgentConfig::new();
59-
let ml_path = match global_config {
60-
Ok(config) => config.measuredboot_ml_path.clone(),
61-
Err(_) => "".to_string(),
62-
};
63-
let uefi_log_handler =
64-
uefi_log_handler::UefiLogHandler::new(&ml_path);
58+
let config =
59+
AgentConfig::new().expect("failed to load configuration");
60+
let ml_path = config.measuredboot_ml_path();
61+
let uefi_log_handler = uefi_log_handler::UefiLogHandler::new(ml_path);
6562
match uefi_log_handler {
6663
Ok(handler) => FillerFromHardware {
6764
tpm_context_info,
@@ -81,8 +78,10 @@ impl<'a> FillerFromHardware<'a> {
8178
fn get_attestation_request_final(
8279
&mut self,
8380
) -> structures::AttestationRequest {
84-
// TODO: Change config obtaining here to avoid repetitions
85-
let config = keylime::config::PushModelConfig::default();
81+
// TODO: Change this to avoid loading the configuration multiple times
82+
// TODO Modify this to not panic on failure
83+
let config =
84+
AgentConfig::new().expect("failed to load configuration");
8685
let tpmc_ref = self.tpm_context_info.get_mutable_tpm_context();
8786
let tpm_banks_sha1 =
8887
tpmc_ref.pcr_banks(HashAlgorithm::Sha1).unwrap_or_else(|_| {
@@ -95,6 +94,7 @@ impl<'a> FillerFromHardware<'a> {
9594
error!("Failed to get PCR banks for SHA256");
9695
vec![]
9796
});
97+
// TODO: Change this to avoid loading the configuration multiple times
9898
// TODO Modify this to not panic on failure
9999
let default =
100100
AgentConfig::new().expect("failed to load default config");
@@ -139,21 +139,23 @@ impl<'a> FillerFromHardware<'a> {
139139
structures::EvidenceSupported::EvidenceLog {
140140
evidence_type: "uefi_log".to_string(),
141141
capabilities: structures::LogCapabilities {
142-
evidence_version: Some(config.get_uefi_logs_evidence_version()),
142+
evidence_version: Some(config.uefi_logs_evidence_version().to_string()),
143143
entry_count: uefi_count,
144-
supports_partial_access: config.get_uefi_logs_supports_partial_access(),
145-
appendable: config.get_uefi_logs_appendable(),
146-
formats: config.get_uefi_logs_formats(),
144+
supports_partial_access: config.uefi_logs_supports_partial_access(),
145+
appendable: config.uefi_logs_appendable(),
146+
// TODO: make this to not panic on failure
147+
formats: config.uefi_logs_formats().expect("failed to get uefi_logs_formats").iter().map(|e| e.to_string()).collect(),
147148
},
148149
},
149150
structures::EvidenceSupported::EvidenceLog {
150151
evidence_type: "ima_log".to_string(),
151152
capabilities: structures::LogCapabilities {
152153
evidence_version: None,
153154
entry_count: ima_log_count,
154-
supports_partial_access: config.get_ima_logs_supports_partial_access(),
155-
appendable: config.get_ima_logs_appendable(),
156-
formats: config.get_ima_logs_formats(),
155+
supports_partial_access: config.ima_logs_supports_partial_access(),
156+
appendable: config.ima_logs_appendable(),
157+
// TODO: make this to not panic on failure
158+
formats: config.ima_logs_formats().expect("failed to get ima_log_formats").iter().map(|e| e.to_string()).collect(),
157159
},
158160
},
159161
],
@@ -393,7 +395,7 @@ mod tests {
393395
use super::*;
394396

395397
#[cfg(feature = "testing")]
396-
use keylime::tpm::testing;
398+
use keylime::{config::get_testing_config, context_info, tpm::testing};
397399

398400
#[test]
399401
fn get_attestation_request_test() {
@@ -658,14 +660,14 @@ mod tests {
658660
#[tokio::test]
659661
#[cfg(feature = "testing")]
660662
async fn test_attestation_request_final() {
661-
use keylime::context_info;
662663
let _mutex = testing::lock_tests().await;
663-
let config = keylime::config::PushModelConfig::default();
664+
let tmpdir = tempfile::tempdir().expect("failed to create tmpdir");
665+
let config = get_testing_config(tmpdir.path());
664666
let mut context_info = context_info::ContextInfo::new_from_str(
665667
context_info::AlgorithmConfigurationString {
666-
tpm_encryption_alg: config.get_tpm_encryption_alg(),
667-
tpm_hash_alg: config.get_tpm_hash_alg(),
668-
tpm_signing_alg: config.get_tpm_signing_alg(),
668+
tpm_encryption_alg: config.tpm_encryption_alg().to_string(),
669+
tpm_hash_alg: config.tpm_hash_alg().to_string(),
670+
tpm_signing_alg: config.tpm_signing_alg().to_string(),
669671
agent_data_path: "".to_string(),
670672
},
671673
)
@@ -683,12 +685,13 @@ mod tests {
683685
async fn test_session_request() {
684686
use keylime::context_info;
685687
let _mutex = testing::lock_tests().await;
686-
let config = keylime::config::PushModelConfig::default();
688+
let tmpdir = tempfile::tempdir().expect("failed to create tmpdir");
689+
let config = get_testing_config(tmpdir.path());
687690
let mut context_info = context_info::ContextInfo::new_from_str(
688691
context_info::AlgorithmConfigurationString {
689-
tpm_encryption_alg: config.get_tpm_encryption_alg(),
690-
tpm_hash_alg: config.get_tpm_hash_alg(),
691-
tpm_signing_alg: config.get_tpm_signing_alg(),
692+
tpm_encryption_alg: config.tpm_encryption_alg().to_string(),
693+
tpm_hash_alg: config.tpm_hash_alg().to_string(),
694+
tpm_signing_alg: config.tpm_signing_alg().to_string(),
692695
agent_data_path: "".to_string(),
693696
},
694697
)
@@ -706,12 +709,13 @@ mod tests {
706709
async fn test_evidence_handling_request() {
707710
use keylime::context_info;
708711
let _mutex = testing::lock_tests().await;
709-
let config = keylime::config::PushModelConfig::default();
712+
let tmpdir = tempfile::tempdir().expect("failed to create tmpdir");
713+
let config = get_testing_config(tmpdir.path());
710714
let mut context_info = context_info::ContextInfo::new_from_str(
711715
context_info::AlgorithmConfigurationString {
712-
tpm_encryption_alg: config.get_tpm_encryption_alg(),
713-
tpm_hash_alg: config.get_tpm_hash_alg(),
714-
tpm_signing_alg: config.get_tpm_signing_alg(),
716+
tpm_encryption_alg: config.tpm_encryption_alg().to_string(),
717+
tpm_hash_alg: config.tpm_hash_alg().to_string(),
718+
tpm_signing_alg: config.tpm_signing_alg().to_string(),
715719
agent_data_path: "".to_string(),
716720
},
717721
)

keylime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ config.workspace = true
1717
glob.workspace = true
1818
hex.workspace = true
1919
libc.workspace = true
20+
keylime-macros.workspace = true
2021
log.workspace = true
2122
once_cell.workspace = true
2223
openssl.workspace = true

0 commit comments

Comments
 (0)