From 901e2dfae7ee3512b7d1f527facde08ead6bc2fb Mon Sep 17 00:00:00 2001 From: Tim Bruijnzeels Date: Thu, 18 Jul 2019 12:05:22 +0200 Subject: [PATCH] Accept invalid certs when connecting to localhost or 127.0.0.1 - useful when testing. --- ca/src/publishing.rs | 8 -------- client/src/client.rs | 6 ------ commons/src/util/httpclient.rs | 31 +++++++++++++------------------ daemon/src/test.rs | 4 ++-- daemon/tests/client_publish.rs | 2 +- 5 files changed, 16 insertions(+), 35 deletions(-) diff --git a/ca/src/publishing.rs b/ca/src/publishing.rs index 424ee147..0285782e 100644 --- a/ca/src/publishing.rs +++ b/ca/src/publishing.rs @@ -140,18 +140,10 @@ impl PubClients { match client.server_info() { PubServerInfo::KrillServer(service_uri, token) => { let uri = format!("{}publication/{}", service_uri, handle); - let service_uri = service_uri.clone(); let token = token.clone(); let handle = handle.clone(); thread::spawn(move ||{ - // Note, I could not think of a convenient way to pass down - // the test context, since there are different threads - // involved when testing. So, for now, just setting test - // mode whenever the publication is done at localhost. - if service_uri.as_str().starts_with("https://localhost") { - httpclient::TEST_MODE.with(|m| { *m.borrow_mut() = true; }); - } match httpclient::post_json(&uri, delta, Some(&token)) { Err(httpclient::Error::ErrorWithJson(_code, err)) => { let err: ErrorCode = err.into(); diff --git a/client/src/client.rs b/client/src/client.rs index 6bb0e3b7..6ccc0bbf 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -71,12 +71,6 @@ impl KrillClient { } } -// #[cfg(test)] - pub fn test(options: Options) -> Result { - httpclient::TEST_MODE.with(|m| { *m.borrow_mut() = true; }); - Self::process(options) - } - fn health(&self) -> Result { httpclient::get_ok( &self.resolve_uri("api/v1/health"), diff --git a/commons/src/util/httpclient.rs b/commons/src/util/httpclient.rs index dab66568..c4aac063 100644 --- a/commons/src/util/httpclient.rs +++ b/commons/src/util/httpclient.rs @@ -1,5 +1,4 @@ //! Some helper functions for HTTP calls -use std::cell::RefCell; use std::io::Read; use std::time::Duration; use bytes::Bytes; @@ -18,8 +17,6 @@ use crate::api::admin::Token; const JSON_CONTENT: &str = "application/json"; -thread_local!(pub static TEST_MODE: RefCell = RefCell::new(false)); - /// Performs a GET request that expects a json response that can be /// deserialized into the an owned value of the expected type. Returns an error /// if nothing is returned. @@ -28,7 +25,7 @@ pub fn get_json( token: Option<&Token> ) -> Result { let headers = headers(Some(JSON_CONTENT), token)?; - let res = client()?.get(uri).headers(headers).send()?; + let res = client(uri)?.get(uri).headers(headers).send()?; process_json_response(res) } @@ -40,7 +37,7 @@ pub fn get_text( token: Option<&Token> ) -> Result { let headers = headers(Some(content_type), token)?; - let res = client()?.get(uri).headers(headers).send()?; + let res = client(uri)?.get(uri).headers(headers).send()?; match opt_text_response(res)? { Some(res) => Ok(res), None => Err(Error::EmptyResponse) @@ -51,7 +48,7 @@ pub fn get_text( /// response body. pub fn get_ok(uri: &str, token: Option<&Token>) -> Result<(), Error> { let headers = headers(None, token)?; - let res = client()?.get(uri).headers(headers).send()?; + let res = client(uri)?.get(uri).headers(headers).send()?; opt_text_response(res)?; // Will return nice errors with possible body. Ok(()) } @@ -66,7 +63,7 @@ pub fn post_json( ) -> Result<(), Error> { let headers = headers(Some(JSON_CONTENT), token)?; let body = serde_json::to_string(&data)?; - let res = client()?.post(uri).headers(headers).body(body).send()?; + let res = client(uri)?.post(uri).headers(headers).body(body).send()?; if let Some(res) = opt_text_response(res)? { Err(Error::UnexpectedResponse(res)) } else { @@ -85,14 +82,14 @@ pub fn post_json_with_response( ) -> Result { let headers = headers(Some(JSON_CONTENT), token)?; let body = serde_json::to_string(&data)?; - let res = client()?.post(uri).headers(headers).body(body).send()?; + let res = client(uri)?.post(uri).headers(headers).body(body).send()?; process_json_response(res) } /// Performs a POST with no data to the given URI and expects and empty 200 OK response. pub fn post_empty(uri: &str, token: Option<&Token>) -> Result<(), Error> { let headers = headers(Some(JSON_CONTENT), token)?; - let res = client()?.post(uri).headers(headers).send()?; + let res = client(uri)?.post(uri).headers(headers).send()?; if let Some(res) = opt_text_response(res)? { Err(Error::UnexpectedResponse(res)) } else { @@ -113,7 +110,7 @@ pub fn post_binary( let headers = headers(Some(content_type), None)?; let body = data.to_vec(); - let mut res = client()?.post(uri).headers(headers).body(body).send()?; + let mut res = client(uri)?.post(uri).headers(headers).body(body).send()?; match res.status() { StatusCode::OK => { @@ -143,22 +140,20 @@ pub fn delete( token: Option<&Token> ) -> Result<(), Error> { let headers = headers(None, token)?; - client()?.delete(uri).headers(headers).send()?; + client(uri)?.delete(uri).headers(headers).send()?; Ok(()) } -fn client() -> Result { - +fn client(uri: &str) -> Result { let builder = Client::builder().gzip(true).timeout(Duration::from_secs(300)); - let builder = if TEST_MODE.with(|m| *m.borrow()) { + if uri.starts_with("https://localhost") || uri.starts_with("https://127.0.0.1") { builder.danger_accept_invalid_certs(true) + .build().map_err(Error::RequestError) } else { - builder - }; - - builder.build().map_err(Error::RequestError) + builder.build().map_err(Error::RequestError) + } } fn headers( diff --git a/daemon/src/test.rs b/daemon/src/test.rs index 6763d1c5..4be78092 100644 --- a/daemon/src/test.rs +++ b/daemon/src/test.rs @@ -50,7 +50,7 @@ fn health_check() -> Result { Command::Health ); - KrillClient::test(krillc_opts) + KrillClient::process(krillc_opts) } @@ -61,7 +61,7 @@ pub fn execute_krillc_command(command: Command) -> ApiResponse { ReportFormat::Json, command ); - match KrillClient::test(krillc_opts) { + match KrillClient::process(krillc_opts) { Ok(res) => res, // ok Err(e) => { panic!("{}", e) diff --git a/daemon/tests/client_publish.rs b/daemon/tests/client_publish.rs index 88cf4a20..5b6b26d8 100644 --- a/daemon/tests/client_publish.rs +++ b/daemon/tests/client_publish.rs @@ -59,7 +59,7 @@ fn execute_krillc_command(command: Command) { ReportFormat::Default, command ); - match KrillClient::test(krillc_opts) { + match KrillClient::process(krillc_opts) { Ok(_res) => {}, // ok Err(e) => { panic!("{}", e)