Unverified Commit 004eb687 authored by Harry Barber's avatar Harry Barber Committed by GitHub
Browse files

Updates and fixes to HttpSensitivityGenerator (#1755)

* Simplify sensitivity detection logic by assuming bindings only exist at top level input/output.

* Fix greedy label logic - they can now exist alongside of normal labels.

* Fix `httpPrefixHeader` false positive.

* Export `RequestFmt` and `ResponseFmt` types alongside the closures.

* More extensive use of `Writable` to improve readability.
parent 9405f000
Loading
Loading
Loading
Loading
+420 −299

File changed.

Preview size limit exceeded, changes collapsed.

+12 −9
Original line number Diff line number Diff line
@@ -333,15 +333,18 @@ ${operationImplementationStubs(operations)}
                        val (requestSpecVarName, operationName) = inner

                        rustBlock("") {
                            rustTemplate("let svc = #{ServerOperationHandler}::operation(registry.$operationName);", *codegenScope)
                            withBlock("let request_fmt =", ";") {
                                sensitivityGen.renderRequestFmt(writer)
                            }
                            withBlock("let response_fmt =", ";") {
                                sensitivityGen.renderResponseFmt(writer)
                            }
                            rustTemplate("let svc = #{SmithyHttpServer}::logging::InstrumentOperation::new(svc, \"$operationName\").request_fmt(request_fmt).response_fmt(response_fmt);", *codegenScope)
                            rustTemplate("(#{Tower}::util::BoxCloneService::new(svc), $requestSpecVarName)", *codegenScope)
                            rustTemplate(
                                """
                                let svc = #{ServerOperationHandler}::operation(registry.$operationName);
                                let request_fmt = #{RequestFmt:W};
                                let response_fmt = #{ResponseFmt:W};
                                let svc = #{SmithyHttpServer}::logging::InstrumentOperation::new(svc, "$operationName").request_fmt(request_fmt).response_fmt(response_fmt);
                                (#{Tower}::util::BoxCloneService::new(svc), $requestSpecVarName)
                                """,
                                "RequestFmt" to sensitivityGen.requestFmt().value,
                                "ResponseFmt" to sensitivityGen.responseFmt().value,
                                *codegenScope,
                            )
                        }
                        rust(",")
                    }
+145 −132

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@
//!        key_suffix: None,
//!     })
//!     .query(|name| QueryMarker { key: false, value: name == "bar" })
//!     .label(|index| index % 2 == 0);
//!     .label(|index| index % 2 == 0, None);
//! let response_fmt = ResponseFmt::new()
//!     .header(|name| {
//!         if name.as_str().starts_with("prefix-") {
+15 −18
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ use crate::logging::{MakeFmt, MakeIdentity};

use super::{
    headers::{HeaderMarker, MakeHeaders},
    uri::{MakeGreedyLabel, MakeLabel, MakeQuery, MakeUri, QueryMarker},
    uri::{GreedyLabel, MakeLabel, MakeQuery, MakeUri, QueryMarker},
};

/// Allows the modification the requests URIs [`Display`](std::fmt::Display) and headers
@@ -32,7 +32,10 @@ impl<Headers, Uri> Debug for RequestFmt<Headers, Uri> {
    }
}

impl Default for RequestFmt<MakeIdentity, MakeUri<MakeIdentity, MakeIdentity>> {
/// Default [`RequestFmt`].
pub type DefaultRequestFmt = RequestFmt<MakeIdentity, MakeUri<MakeIdentity, MakeIdentity>>;

impl Default for DefaultRequestFmt {
    fn default() -> Self {
        Self {
            headers: MakeIdentity,
@@ -41,7 +44,7 @@ impl Default for RequestFmt<MakeIdentity, MakeUri<MakeIdentity, MakeIdentity>> {
    }
}

impl RequestFmt<MakeIdentity, MakeUri<MakeIdentity, MakeIdentity>> {
impl DefaultRequestFmt {
    /// Constructs a new [`RequestFmt`] with no redactions.
    pub fn new() -> Self {
        Self::default()
@@ -67,27 +70,21 @@ impl<Header, P, Q> RequestFmt<Header, MakeUri<P, Q>> {
    /// Marks parts of the URI as sensitive.
    ///
    /// See [`Label`](super::uri::Label) for more info.
    pub fn label<F>(self, label: F) -> RequestFmt<Header, MakeUri<MakeLabel<F>, Q>>
    pub fn label<F>(
        self,
        label_marker: F,
        greedy_label: Option<GreedyLabel>,
    ) -> RequestFmt<Header, MakeUri<MakeLabel<F>, Q>>
    where
        F: Fn(usize) -> bool,
    {
        RequestFmt {
            headers: self.headers,
            uri: MakeUri {
                make_path: MakeLabel(label),
                make_query: self.uri.make_query,
                make_path: MakeLabel {
                    label_marker,
                    greedy_label,
                },
        }
    }

    /// Marks parts of the URI as sensitive.
    ///
    /// See [`GreedyLabel`](super::uri::GreedyLabel) for more info.
    pub fn greedy_label(self, position: usize) -> RequestFmt<Header, MakeUri<MakeGreedyLabel, Q>> {
        RequestFmt {
            headers: self.headers,
            uri: MakeUri {
                make_path: MakeGreedyLabel(position),
                make_query: self.uri.make_query,
            },
        }
Loading