added body digests checks

master
Edward Shen 2019-05-02 03:21:18 -04:00
parent 90ce6167cf
commit f7e6dd3246
Signed by: edward
GPG Key ID: F350507060ED6C90
6 changed files with 22 additions and 5 deletions

1
Cargo.lock generated
View File

@ -657,6 +657,7 @@ dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)",
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -14,3 +14,4 @@ tokio = "0.1"
tera = "0.11"
env_logger = "0.6"
chrono = { version = "0.4", features = ["serde"] }
ring = "^0"

View File

@ -25,7 +25,8 @@
endpoints: [
(label: "Or expect different reponse codes (like 418)", endpoint: "http://error418.net/", code: 418),
(label: "Or response checking!", endpoint: "http://urlecho.appspot.com/echo", body: "None"),
(label: "Or both!", endpoint: "http://urlecho.appspot.com/echo?status=503&Content-Type=text%2Fhtml&body=None", body: "None", code: 503),
(label: "Or response hash comparison!", endpoint: "http://urlecho.appspot.com/echo", body_hash: "dc937b59892604f5a86ac96936cd7ff09e25f18ae6b758e8014a24c7fa039e91"),
(label: "Or a combination!", endpoint: "http://urlecho.appspot.com/echo?status=503&Content-Type=text%2Fhtml&body=None", body: "None", code: 503),
(label: "Or expect the endpoint to fail entirely!", endpoint: "http://lol.arpa", should_err: true),
],
),

View File

@ -7,6 +7,7 @@ pub struct EndpointConfig {
pub port: Option<u16>,
pub code: Option<u16>,
pub body: Option<String>,
pub body_hash: Option<String>,
pub follow_redirects: Option<bool>,
pub should_err: Option<bool>,
}

View File

@ -8,6 +8,7 @@ extern crate serde;
extern crate tokio;
#[macro_use]
extern crate tera;
extern crate ring;
mod config;
mod handlers;

View File

@ -5,6 +5,10 @@ use crate::{
};
use chrono::prelude::Utc;
use reqwest::{Client, RedirectPolicy, Url, UrlError};
use ring::{
digest::{digest, SHA256},
test::from_hex,
};
use std::time::Duration;
pub fn update_state(state: State) {
@ -52,11 +56,9 @@ fn get_result(
match ping_result {
Ok(mut res) => {
let res_body = res.text().expect("could not get body of request");
let does_code_match = res.status() == code;
let does_body_match = body.is_empty() || res_body == body;
let mut error = None;
if !does_code_match {
if res.status() != code {
error = Some(format!(
"Status code mismatch: {} != {}.",
res.status().as_u16(),
@ -64,7 +66,17 @@ fn get_result(
));
}
if !does_body_match {
if let Some(expected_hash) = &endpoint.body_hash {
let expected = from_hex(expected_hash).unwrap();
let actual = digest(&SHA256, String::from(res_body).as_bytes());
if &expected != &actual.as_ref() {
error = Some(if let Some(msg) = error {
format!("{} Body hash mismatch.", msg)
} else {
String::from("Body hash mismatch.")
});
}
} else if !body.is_empty() && res_body != body {
error = Some(if let Some(msg) = error {
format!(
"{} Body mismatch: {} != {}.",