Skip to content

Commit

Permalink
feat: remove Tags handoffs
Browse files Browse the repository at this point in the history
This removes the "Tags" struct from being passed around in various
calls. Instead the `sentry` mod pulls the `Tags` from the `extensions`
associated with either the request or response.

Closes #403
  • Loading branch information
jrconlin committed Oct 16, 2020
1 parent f0aa464 commit aaab6dd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 215 deletions.
11 changes: 2 additions & 9 deletions src/db/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::web::extractors::{
BsoParam, CollectionParam, HawkIdentifier, PreConditionHeader, PreConditionHeaderOpt,
};
use crate::web::middleware::SyncServerRequest;
use crate::web::tags::Tags;
use crate::web::X_LAST_MODIFIED;
use actix_http::http::{HeaderValue, Method, StatusCode};
use actix_http::Error;
Expand All @@ -22,7 +21,6 @@ use std::future::Future;
pub struct DbTransactionPool {
pool: Box<dyn DbPool>,
is_read: bool,
tags: Tags,
user_id: HawkIdentifier,
collection: Option<String>,
bso_opt: Option<String>,
Expand Down Expand Up @@ -189,11 +187,7 @@ impl FromRequest for DbTransactionPool {
.to_str()
.unwrap_or("NONE");

let tags = match req.extensions().get::<Tags>() {
Some(t) => t.clone(),
None => Tags::from_request_head(req.head()),
};
let col_result = CollectionParam::extrude(&req.uri(), &mut req.extensions_mut(), &tags);
let col_result = CollectionParam::extrude(&req.uri(), &mut req.extensions_mut());
let state = match req.app_data::<Data<ServerState>>() {
Some(v) => v,
None => {
Expand Down Expand Up @@ -222,11 +216,10 @@ impl FromRequest for DbTransactionPool {
let bso_opt = bso.map(|b| b.bso);

let is_read = matches!(method, Method::GET | Method::HEAD);
let precondition = PreConditionHeaderOpt::extrude(&req.headers(), Some(tags.clone()))?;
let precondition = PreConditionHeaderOpt::extrude(&req.headers())?;
let pool = Self {
pool: state.db_pool.clone(),
is_read,
tags,
user_id,
collection,
bso_opt,
Expand Down
2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ impl ApiError {
ref description,
ref location,
name,
ref _tags,
ref _metric_label,
) => {
match description.as_ref() {
Expand All @@ -169,7 +168,6 @@ impl ApiError {
ValidationErrorKind::FromValidationErrors(
ref _err,
ref location,
ref _tags,
_metric_label,
) => {
if *location == RequestErrorLocation::Body {
Expand Down
3 changes: 0 additions & 3 deletions src/web/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use time::Duration;
use actix_web::dev::ConnectionInfo;
use actix_web::http::Uri;

use super::tags::Tags;
use super::{
error::{HawkErrorKind, ValidationErrorKind},
extractors::RequestErrorLocation,
Expand Down Expand Up @@ -164,7 +163,6 @@ impl HawkPayload {
secrets: &Secrets,
ci: &ConnectionInfo,
uri: &Uri,
tags: Option<Tags>,
) -> ApiResult<Self> {
let host_port: Vec<_> = ci.host().splitn(2, ':').collect();
let host = host_port[0];
Expand All @@ -174,7 +172,6 @@ impl HawkPayload {
"Invalid port (hostname:port) specified".to_owned(),
RequestErrorLocation::Header,
None,
tags,
label!("request.validate.hawk.invalid_port"),
)
})?
Expand Down
36 changes: 7 additions & 29 deletions src/web/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use serde::{
use serde_json::{Error as JsonError, Value};

use super::extractors::RequestErrorLocation;
use super::tags::Tags;
use crate::error::ApiError;

/// An error occurred during HAWK authentication.
Expand Down Expand Up @@ -116,19 +115,12 @@ impl ValidationError {
#[derive(Debug, Fail)]
pub enum ValidationErrorKind {
#[fail(display = "{}", _0)]
FromDetails(
String,
RequestErrorLocation,
Option<String>,
Option<Tags>,
Option<String>,
),
FromDetails(String, RequestErrorLocation, Option<String>, Option<String>),

#[fail(display = "{}", _0)]
FromValidationErrors(
#[cause] validator::ValidationErrors,
RequestErrorLocation,
Option<Tags>,
Option<String>,
),
}
Expand All @@ -152,27 +144,18 @@ impl From<Context<ValidationErrorKind>> for ValidationError {
fn from(inner: Context<ValidationErrorKind>) -> Self {
trace!("Validation Error: {:?}", inner.get_context());
let status = match inner.get_context() {
ValidationErrorKind::FromDetails(
ref _description,
ref location,
Some(ref name),
_,
_,
) if *location == RequestErrorLocation::Header => {
ValidationErrorKind::FromDetails(ref _description, ref location, Some(ref name), _)
if *location == RequestErrorLocation::Header =>
{
match name.to_ascii_lowercase().as_str() {
"accept" => StatusCode::NOT_ACCEPTABLE,
"content-type" => StatusCode::UNSUPPORTED_MEDIA_TYPE,
_ => StatusCode::BAD_REQUEST,
}
}
ValidationErrorKind::FromDetails(
ref _description,
ref location,
Some(ref name),
_,
_,
) if *location == RequestErrorLocation::Path
&& ["bso", "collection"].contains(&name.as_ref()) =>
ValidationErrorKind::FromDetails(ref _description, ref location, Some(ref name), _)
if *location == RequestErrorLocation::Path
&& ["bso", "collection"].contains(&name.as_ref()) =>
{
StatusCode::NOT_FOUND
}
Expand Down Expand Up @@ -231,22 +214,19 @@ impl Serialize for ValidationErrorKind {
ref description,
ref location,
ref name,
ref tags,
ref _metric_label,
) => {
seq.serialize_element(&SerializedValidationError {
description,
location,
name: name.as_ref().map(|name| &**name),
value: None,
tags: tags.as_ref(),
})?;
}

ValidationErrorKind::FromValidationErrors(
ref errors,
ref location,
ref tags,
ref _metric_label,
) => {
for (field, field_errors) in errors.clone().field_errors().iter() {
Expand All @@ -256,7 +236,6 @@ impl Serialize for ValidationErrorKind {
location,
name: Some(field),
value: field_error.params.get("value"),
tags: tags.clone().as_ref(),
})?;
}
}
Expand All @@ -273,5 +252,4 @@ struct SerializedValidationError<'e> {
pub location: &'e RequestErrorLocation,
pub name: Option<&'e str>,
pub value: Option<&'e Value>,
pub tags: Option<&'e Tags>,
}
Loading

0 comments on commit aaab6dd

Please sign in to comment.