Accept invalid certs when connecting to localhost or 127.0.0.1 - useful when testing.

This commit is contained in:
Tim Bruijnzeels
2019-07-18 12:05:22 +02:00
parent a6cbddbdf5
commit 901e2dfae7
5 changed files with 16 additions and 35 deletions
-8
View File
@@ -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();
-6
View File
@@ -71,12 +71,6 @@ impl KrillClient {
}
}
// #[cfg(test)]
pub fn test(options: Options) -> Result<ApiResponse, Error> {
httpclient::TEST_MODE.with(|m| { *m.borrow_mut() = true; });
Self::process(options)
}
fn health(&self) -> Result<ApiResponse, Error> {
httpclient::get_ok(
&self.resolve_uri("api/v1/health"),
+13 -18
View File
@@ -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<bool> = 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<T: DeserializeOwned>(
token: Option<&Token>
) -> Result<T, Error> {
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<String, Error> {
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<T: DeserializeOwned>(
) -> Result<T, 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()?;
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<Client, Error> {
fn client(uri: &str) -> Result<Client, Error> {
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(
+2 -2
View File
@@ -50,7 +50,7 @@ fn health_check() -> Result<ApiResponse, Error> {
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)
+1 -1
View File
@@ -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)