Skip to content

Commit cb8070e

Browse files
sarroutbiansasaki
authored andcommitted
Do not use certificate on insecure mode
Refactor the get_https_client function to eliminate code duplication between the secure and insecure client configuration paths. This change creates a single reqwest::Client::builder instance and conditionally applies either danger_accept_invalid_certs(true) for insecure mode or loads and adds the root certificate specified via --certificate for secure mode. This correctly avoids reading and processing the certificate file unnecessarily when running with --insecure, fixing a logical inconsistency where the certificate was loaded even if ignored. Signed-off-by: Sergio Arroutbi <[email protected]>
1 parent 17a33ae commit cb8070e

File tree

1 file changed

+15
-18
lines changed
  • keylime-push-model-agent/src

1 file changed

+15
-18
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,19 @@ fn get_attestation_request_url(args: &Args) -> String {
3232
}
3333

3434
fn get_https_client(args: &Args) -> Result<reqwest::Client, Box<dyn Error>> {
35-
let mut buf = Vec::new();
36-
File::open(args.certificate.clone())?.read_to_end(&mut buf)?;
37-
let cert = reqwest::Certificate::from_pem(&buf)?;
38-
if args.insecure.is_some() && args.insecure.unwrap() {
39-
return Ok(reqwest::Client::builder()
40-
.connection_verbose(true)
41-
.add_root_certificate(cert)
42-
.danger_accept_invalid_certs(true)
43-
.timeout(Duration::from_millis(args.timeout))
44-
.build()?);
45-
}
46-
Ok(reqwest::Client::builder()
35+
let mut builder = reqwest::Client::builder()
4736
.connection_verbose(true)
48-
.add_root_certificate(cert)
49-
.timeout(Duration::from_millis(args.timeout))
50-
.build()?)
37+
.timeout(Duration::from_millis(args.timeout));
38+
39+
if args.insecure.unwrap_or(false) {
40+
builder = builder.danger_accept_invalid_certs(true);
41+
} else {
42+
let mut buf = Vec::new();
43+
File::open(args.certificate.clone())?.read_to_end(&mut buf)?;
44+
let cert = reqwest::Certificate::from_pem(&buf)?;
45+
builder = builder.add_root_certificate(cert);
46+
}
47+
Ok(builder.build()?)
5148
}
5249

5350
fn get_client(args: &Args) -> Result<reqwest::Client, Box<dyn Error>> {
@@ -201,7 +198,7 @@ mod tests {
201198
verifier_url: "https://1.2.3.4:5678".to_string(),
202199
timeout: TEST_TIMEOUT_MILLIS,
203200
certificate: "/tmp/unexisting_cert_file".to_string(),
204-
insecure: Some(true),
201+
insecure: Some(false),
205202
id: "12345678".to_string(),
206203
json_file: None,
207204
method: None,
@@ -255,7 +252,7 @@ mod tests {
255252
{
256253
Ok(_) => unreachable!(),
257254
Err(e) => {
258-
assert!(e.to_string().contains("builder error"))
255+
assert!(e.to_string().contains("error sending request"))
259256
}
260257
}
261258
match send_attestation_request(&Args {
@@ -275,7 +272,7 @@ mod tests {
275272
{
276273
Ok(_) => unreachable!(),
277274
Err(e) => {
278-
assert!(e.to_string().contains("builder error"))
275+
assert!(e.to_string().contains("error sending request"))
279276
}
280277
}
281278
// Clean up the test certificate file

0 commit comments

Comments
 (0)