Skip to content

Commit 94fbba4

Browse files
committed
Add unwrap/panic detection for Push Model files
Assisted-By: Claude <[email protected]> Signed-off-by: Sergio Arroutbi <[email protected]>
1 parent 07463b8 commit 94fbba4

File tree

9 files changed

+103
-86
lines changed

9 files changed

+103
-86
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,17 @@ mod tests {
264264
let mut config = create_test_config(&uri, "", "", "");
265265
config.max_retries = 3; // Allow up to 3 retries
266266

267-
let client = AttestationClient::new(&config).unwrap();
267+
let client = AttestationClient::new(&config).unwrap(); //#[allow_ci]
268268
let result = client.send_negotiation(&config).await;
269269

270270
// The final request should be successful
271271
assert!(result.is_ok());
272-
let response = result.unwrap();
272+
let response = result.unwrap(); //#[allow_ci]
273273
assert_eq!(response.status_code, StatusCode::CREATED);
274274

275275
// The server should have received 3 requests in total (2 failures + 1 success)
276276
let received_requests =
277-
mock_server.received_requests().await.unwrap();
277+
mock_server.received_requests().await.unwrap(); //#[allow_ci]
278278
assert_eq!(received_requests.len(), 3);
279279
}
280280

@@ -283,12 +283,12 @@ mod tests {
283283
let negotiation_config =
284284
create_test_config("http://127.0.0.1:9999/test", "", "", "");
285285

286-
let client = AttestationClient::new(&negotiation_config).unwrap();
286+
let client = AttestationClient::new(&negotiation_config).unwrap(); //#[allow_ci]
287287
let result =
288288
client.send_negotiation(&negotiation_config.clone()).await;
289289

290290
assert!(result.is_err());
291-
let err_msg = result.unwrap_err().to_string();
291+
let err_msg = result.unwrap_err().to_string(); //#[allow_ci]
292292
assert!(err_msg.contains("error sending request"));
293293
}
294294

@@ -304,32 +304,32 @@ mod tests {
304304
let client_result = AttestationClient::new(&config);
305305

306306
assert!(client_result.is_err());
307-
let err_msg = client_result.unwrap_err().to_string();
307+
let err_msg = client_result.unwrap_err().to_string(); //#[allow_ci]
308308
assert!(err_msg.contains("Failed to open"));
309309
}
310310

311311
#[actix_rt::test]
312312
async fn test_send_negotiation_bad_certs() {
313-
let temp_dir = tempdir().unwrap();
313+
let temp_dir = tempdir().unwrap(); //#[allow_ci]
314314
let ca_path = temp_dir.path().join("ca.pem");
315315
let cert_path = temp_dir.path().join("cert.pem");
316316
let key_path = temp_dir.path().join("key.pem");
317317

318-
File::create(&ca_path).unwrap();
319-
File::create(&cert_path).unwrap();
320-
File::create(&key_path).unwrap();
318+
File::create(&ca_path).unwrap(); //#[allow_ci]
319+
File::create(&cert_path).unwrap(); //#[allow_ci]
320+
File::create(&key_path).unwrap(); //#[allow_ci]
321321

322322
let config = create_test_config(
323323
"https://1.2.3.4:9999/test",
324-
ca_path.to_str().unwrap(),
325-
cert_path.to_str().unwrap(),
326-
key_path.to_str().unwrap(),
324+
ca_path.to_str().unwrap(), //#[allow_ci]
325+
cert_path.to_str().unwrap(), //#[allow_ci]
326+
key_path.to_str().unwrap(), //#[allow_ci]
327327
);
328328

329329
let client_result = AttestationClient::new(&config);
330330

331331
assert!(client_result.is_err());
332-
let err_msg = client_result.unwrap_err().to_string();
332+
let err_msg = client_result.unwrap_err().to_string(); //#[allow_ci]
333333
assert!(err_msg.to_lowercase().contains("certificate"));
334334
}
335335

@@ -344,15 +344,15 @@ mod tests {
344344
"", "", "",
345345
);
346346

347-
let client = AttestationClient::new(&config).unwrap();
347+
let client = AttestationClient::new(&config).unwrap(); //#[allow_ci]
348348
let result = client.send_negotiation(&config).await;
349349

350350
assert!(
351351
result.is_ok(),
352352
"Request to mockoon failed: {:?}",
353353
result.err()
354354
);
355-
let response_info = result.unwrap();
355+
let response_info = result.unwrap(); //#[allow_ci]
356356
assert_eq!(
357357
response_info.status_code,
358358
StatusCode::CREATED,
@@ -365,7 +365,7 @@ mod tests {
365365
#[actix_rt::test]
366366
async fn test_handle_evidence_submission_no_location_header() {
367367
let config = create_test_config("http://localhost:3000", "", "", "");
368-
let client = AttestationClient::new(&config).unwrap();
368+
let client = AttestationClient::new(&config).unwrap(); //#[allow_ci]
369369

370370
// Create a response with no Location header
371371
let neg_response = ResponseInformation {
@@ -380,7 +380,7 @@ mod tests {
380380

381381
assert!(result.is_err());
382382
assert!(result
383-
.unwrap_err()
383+
.unwrap_err() //#[allow_ci]
384384
.to_string()
385385
.contains("missing 'Location' header"));
386386
}
@@ -408,19 +408,19 @@ mod tests {
408408
let config = create_test_config(&uri, "", "", "");
409409

410410
// Create the client
411-
let client = AttestationClient::new(&config).unwrap();
411+
let client = AttestationClient::new(&config).unwrap(); //#[allow_ci]
412412

413413
let result =
414414
client.send_evidence(single_serialized_body, &config).await;
415415

416416
// Assertions
417417
assert!(result.is_ok(), "send_evidence should succeed");
418-
let response = result.unwrap();
418+
let response = result.unwrap(); //#[allow_ci]
419419
assert_eq!(response.status_code, StatusCode::ACCEPTED);
420420

421421
// Verify that the mock server received exactly one request.
422422
let received_requests =
423-
mock_server.received_requests().await.unwrap();
423+
mock_server.received_requests().await.unwrap(); //#[allow_ci]
424424
assert_eq!(received_requests.len(), 1);
425425
}
426426
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tokio::sync::Mutex;
2222

2323
/// Configuration for the authentication client
2424
#[derive(Debug, Clone)]
25+
#[allow(dead_code)]
2526
pub struct AuthConfig {
2627
/// Base URL of the verifier (e.g., "https://verifier.example.com")
2728
pub verifier_base_url: String,
@@ -52,20 +53,23 @@ impl Default for AuthConfig {
5253

5354
/// Session token with expiration information
5455
#[derive(Debug, Clone)]
56+
#[allow(dead_code)]
5557
struct SessionToken {
5658
token: String,
5759
expires_at: DateTime<Utc>,
5860
session_id: u64,
5961
}
6062

6163
impl SessionToken {
64+
#[allow(dead_code)]
6265
fn is_valid(&self, buffer_minutes: i64) -> bool {
6366
let buffer = Duration::minutes(buffer_minutes);
6467
Utc::now() + buffer < self.expires_at
6568
}
6669
}
6770

6871
/// Mock TPM operations for testing
72+
#[allow(dead_code)]
6973
pub trait TpmOperations: Send + Sync {
7074
fn generate_proof(&self, challenge: &str) -> Result<ProofOfPossession>;
7175
}
@@ -92,6 +96,7 @@ impl TpmOperations for MockTpmOperations {
9296
}
9397

9498
/// Standalone authentication client implementing the challenge-response protocol
99+
#[allow(dead_code)]
95100
pub struct AuthenticationClient {
96101
config: AuthConfig,
97102
http_client: Client,
@@ -101,6 +106,7 @@ pub struct AuthenticationClient {
101106

102107
impl AuthenticationClient {
103108
/// Create a new authentication client with the given configuration
109+
#[allow(dead_code)]
104110
pub fn new(config: AuthConfig) -> Result<Self> {
105111
let timeout = std::time::Duration::from_millis(config.timeout_ms);
106112
let http_client = Client::builder()
@@ -117,6 +123,7 @@ impl AuthenticationClient {
117123
}
118124

119125
/// Create a new authentication client with custom TPM operations
126+
#[allow(dead_code)]
120127
pub fn with_tpm_ops(
121128
config: AuthConfig,
122129
tpm_ops: Box<dyn TpmOperations>,
@@ -136,6 +143,7 @@ impl AuthenticationClient {
136143
}
137144

138145
/// Get a valid authentication token, performing authentication if necessary
146+
#[allow(dead_code)]
139147
pub async fn get_auth_token(&self) -> Result<String> {
140148
let token_guard = self.session_token.lock().await;
141149

@@ -160,6 +168,7 @@ impl AuthenticationClient {
160168
}
161169

162170
/// Check if we currently have a valid token
171+
#[allow(dead_code)]
163172
pub async fn has_valid_token(&self) -> bool {
164173
let token_guard = self.session_token.lock().await;
165174
if let Some(ref token) = *token_guard {
@@ -170,13 +179,15 @@ impl AuthenticationClient {
170179
}
171180

172181
/// Clear the current token (e.g., after receiving 401)
182+
#[allow(dead_code)]
173183
pub async fn clear_token(&self) {
174184
let mut token_guard = self.session_token.lock().await;
175185
*token_guard = None;
176186
debug!("Authentication token cleared");
177187
}
178188

179189
/// Perform the complete authentication flow
190+
#[allow(dead_code)]
180191
async fn authenticate(&self) -> Result<String> {
181192
info!(
182193
"Starting authentication flow for agent: {}",
@@ -218,6 +229,7 @@ impl AuthenticationClient {
218229
}
219230

220231
/// Internal authentication implementation
232+
#[allow(dead_code)]
221233
async fn do_authenticate(&self) -> Result<String> {
222234
// Step 1: Request challenge
223235
debug!("Step 1: Requesting challenge from verifier");
@@ -240,6 +252,7 @@ impl AuthenticationClient {
240252
}
241253

242254
/// Step 1: Request challenge from verifier
255+
#[allow(dead_code)]
243256
async fn request_challenge(&self) -> Result<SessionResponse> {
244257
let session_request = SessionRequest {
245258
data: SessionRequestData {
@@ -287,6 +300,7 @@ impl AuthenticationClient {
287300
}
288301

289302
/// Step 2: Generate TPM proof of possession
303+
#[allow(dead_code)]
290304
fn generate_tpm_proof(
291305
&self,
292306
challenge_response: &SessionResponse,
@@ -311,6 +325,7 @@ impl AuthenticationClient {
311325
}
312326

313327
/// Step 3: Submit proof and get authentication result
328+
#[allow(dead_code)]
314329
async fn submit_proof(
315330
&self,
316331
session_id: u64,
@@ -372,6 +387,7 @@ impl AuthenticationClient {
372387
}
373388

374389
/// Step 4: Process authentication result and store token
390+
#[allow(dead_code)]
375391
async fn process_auth_result(
376392
&self,
377393
auth_response: SessionIdResponse,
@@ -412,6 +428,7 @@ impl AuthenticationClient {
412428
}
413429

414430
/// Make an authenticated HTTP request (convenience method for testing)
431+
#[allow(dead_code)]
415432
pub async fn make_authenticated_request(
416433
&self,
417434
method: Method,
@@ -460,7 +477,7 @@ mod tests {
460477
max_auth_retries: 2,
461478
};
462479

463-
AuthenticationClient::new(config).unwrap()
480+
AuthenticationClient::new(config).unwrap() //#[allow_ci]
464481
}
465482

466483
#[tokio::test]
@@ -532,14 +549,14 @@ mod tests {
532549
let client = create_test_client(&mock_server.uri()).await;
533550

534551
// Test authentication
535-
let token = client.get_auth_token().await.unwrap();
552+
let token = client.get_auth_token().await.unwrap(); //#[allow_ci]
536553
assert_eq!(token, "test-token-456");
537554

538555
// Test that token is cached
539556
assert!(client.has_valid_token().await);
540557

541558
// Test that subsequent calls use cached token
542-
let token2 = client.get_auth_token().await.unwrap();
559+
let token2 = client.get_auth_token().await.unwrap(); //#[allow_ci]
543560
assert_eq!(token2, "test-token-456");
544561
}
545562

@@ -610,7 +627,7 @@ mod tests {
610627
let result = client.get_auth_token().await;
611628
assert!(result.is_err());
612629
assert!(result
613-
.unwrap_err()
630+
.unwrap_err() //#[allow_ci]
614631
.to_string()
615632
.contains("Authentication failed"));
616633
}
@@ -691,11 +708,11 @@ mod tests {
691708
max_auth_retries: 2,
692709
};
693710

694-
let client = AuthenticationClient::new(config).unwrap();
711+
let client = AuthenticationClient::new(config).unwrap(); //#[allow_ci]
695712

696713
// Since token expires in 1 minute but we have 5 minute buffer,
697714
// it should be considered invalid and trigger re-authentication
698-
let token = client.get_auth_token().await.unwrap();
715+
let token = client.get_auth_token().await.unwrap(); //#[allow_ci]
699716
assert_eq!(token, "short-lived-token");
700717

701718
// Check that token is considered invalid due to buffer

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mod tests {
9898
let context_res = get_context_info(AVOID_TPM);
9999
assert!(context_res.is_ok());
100100
assert!(
101-
context_res.unwrap().is_none(),
101+
context_res.unwrap().is_none(), //#[allow_ci]
102102
"Context should be None when TPM is avoided"
103103
);
104104
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub async fn check_registration(
1212
context_info: Option<context_info::ContextInfo>,
1313
) -> Result<()> {
1414
if context_info.is_some() {
15-
crate::registration::register_agent(&mut context_info.unwrap())
15+
crate::registration::register_agent(&mut context_info.unwrap()) //#[allow_ci]
1616
.await?;
1717
}
1818
Ok(())

0 commit comments

Comments
 (0)