Skip to content

Commit b316978

Browse files
Updates
1 parent a7d74ca commit b316978

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ ngwaf-terraform-edge-deploy/terraform.tfvars
3434
local_lab/*
3535
ngwaf-compute-integration/latency_testing.sh
3636
ngwaf-compute-integration/fastly.toml
37+
ngwaf-compute-integration/fastly.toml

ngwaf-compute-integration/src/main.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use fastly::experimental::{inspect, InspectConfig, InspectError, InspectResponse
22
use fastly::handle::BodyHandle;
33
use fastly::http::{HeaderValue, StatusCode};
44
use fastly::{Request, Response};
5+
use std::collections::HashMap;
56

67
const HTTPME_BACKEND: &str = "HTTPME";
78

@@ -30,7 +31,7 @@ fn main(mut req: Request) -> Result<Response, fastly::Error> {
3031
let resp: Response = req.send(HTTPME_BACKEND)?;
3132

3233
// Return the response back to the client
33-
return Ok(resp)
34+
return Ok(resp);
3435
}
3536

3637
fn do_waf_inspect(mut req: Request) -> (Request, Response) {
@@ -66,38 +67,18 @@ fn do_waf_inspect(mut req: Request) -> (Request, Response) {
6667
let waf_result: Result<InspectResponse, InspectError> = inspect(inspectconf);
6768

6869
match waf_result {
69-
Ok(x) => {
70+
Ok(inspect_resp) => {
7071
// Handling WAF result
7172
println!(
7273
"waf_status_code: {}\nwaf_tags: {:?}\nwaf_decision_ms: {:?}\nwaf_verdict: {:?}",
73-
x.status(),
74-
x.tags(),
75-
x.decision_ms(),
76-
x.verdict(),
77-
);
78-
req.set_header(
79-
"waf-status",
80-
HeaderValue::from_str(&x.status().to_string()).unwrap(),
81-
);
82-
req.set_header(
83-
"waf-tags",
84-
HeaderValue::from_str(
85-
x.tags()
86-
.into_iter()
87-
.collect::<Vec<&str>>()
88-
.join(", ")
89-
.as_str(),
90-
)
91-
.unwrap(),
92-
);
93-
req.set_header(
94-
"waf-decision-ms",
95-
HeaderValue::from_str(format!("{:?}", &x.decision_ms()).as_str()).unwrap(),
96-
);
97-
req.set_header(
98-
"waf-verdict",
99-
HeaderValue::from_str(format!("{:?}", &x.verdict()).as_str()).unwrap(),
74+
inspect_resp.status(),
75+
inspect_resp.tags(),
76+
inspect_resp.decision_ms(),
77+
inspect_resp.verdict(),
10078
);
79+
let waf_inspection_header_val = format_waf_inspection_header(inspect_resp);
80+
81+
req.set_header("waf-inspect-data", waf_inspection_header_val);
10182
}
10283
Err(y) => match y {
10384
InspectError::InvalidConfig => {
@@ -139,3 +120,31 @@ fn do_waf_inspect(mut req: Request) -> (Request, Response) {
139120
}
140121
}
141122
}
123+
124+
fn format_waf_inspection_header(inspect_resp: InspectResponse) -> String {
125+
// Inspired by https://www.fastly.com/documentation/solutions/examples/filter-cookies-or-other-structured-headers/
126+
127+
println!("Inspection Response: {:?}", inspect_resp);
128+
129+
let mut filtered_cookie_header_value = "".to_string();
130+
131+
filtered_cookie_header_value.push_str(&format!("{}{:?};", "status=", inspect_resp.status()));
132+
filtered_cookie_header_value.push_str(&format!(
133+
"{}{};",
134+
"tags=",
135+
inspect_resp
136+
.tags()
137+
.into_iter()
138+
.collect::<Vec<&str>>()
139+
.join(",")
140+
.as_str()
141+
));
142+
filtered_cookie_header_value.push_str(&format!(
143+
"{}{:?};",
144+
"decision_ms=",
145+
inspect_resp.decision_ms()
146+
));
147+
filtered_cookie_header_value.push_str(&format!("{}{:?}", "verdict=", inspect_resp.verdict()));
148+
149+
return filtered_cookie_header_value;
150+
}

0 commit comments

Comments
 (0)