Skip to content

Commit 28b4296

Browse files
authored
Ignore hosts from ALB health checks when they are not present (#495)
ALB health checks don't include a host value. Signed-off-by: David Calavera <[email protected]>
1 parent c737920 commit 28b4296

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

lambda-http/src/request.rs

+41-20
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,24 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
7676

7777
let builder = http::Request::builder()
7878
.uri({
79-
let scheme = ag
80-
.headers
81-
.get(x_forwarded_proto())
82-
.and_then(|s| s.to_str().ok())
83-
.unwrap_or("https");
8479
let host = ag
8580
.headers
8681
.get(http::header::HOST)
8782
.and_then(|s| s.to_str().ok())
88-
.or(ag.request_context.domain_name.as_deref())
89-
.unwrap_or_default();
90-
83+
.or(ag.request_context.domain_name.as_deref());
9184
let path = apigw_path_with_stage(&ag.request_context.stage, &raw_path);
92-
let mut url = format!("{}://{}{}", scheme, host, path);
9385

86+
let mut url = match host {
87+
None => path,
88+
Some(host) => {
89+
let scheme = ag
90+
.headers
91+
.get(x_forwarded_proto())
92+
.and_then(|s| s.to_str().ok())
93+
.unwrap_or("https");
94+
format!("{}://{}{}", scheme, host, path)
95+
}
96+
};
9497
if let Some(query) = ag.raw_query_string {
9598
url.push('?');
9699
url.push_str(&query);
@@ -199,18 +202,20 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
199202

200203
let builder = http::Request::builder()
201204
.uri({
202-
let scheme = alb
203-
.headers
204-
.get(x_forwarded_proto())
205-
.and_then(|s| s.to_str().ok())
206-
.unwrap_or("https");
207-
let host = alb
208-
.headers
209-
.get(http::header::HOST)
210-
.and_then(|s| s.to_str().ok())
211-
.unwrap_or_default();
205+
let host = alb.headers.get(http::header::HOST).and_then(|s| s.to_str().ok());
206+
207+
let mut url = match host {
208+
None => raw_path.clone(),
209+
Some(host) => {
210+
let scheme = alb
211+
.headers
212+
.get(x_forwarded_proto())
213+
.and_then(|s| s.to_str().ok())
214+
.unwrap_or("https");
215+
format!("{}://{}{}", scheme, host, &raw_path)
216+
}
217+
};
212218

213-
let mut url = format!("{}://{}{}", scheme, host, &raw_path);
214219
if !alb.multi_value_query_string_parameters.is_empty() {
215220
url.push('?');
216221
url.push_str(&alb.multi_value_query_string_parameters.to_query_string());
@@ -625,4 +630,20 @@ mod tests {
625630
assert_eq!(req.method(), "GET");
626631
assert_eq!(req.uri(), "/test/test/hello?name=me");
627632
}
633+
634+
#[test]
635+
fn deserialize_alb_no_host() {
636+
// generated from ALB health checks
637+
let input = include_str!("../tests/data/alb_no_host.json");
638+
let result = from_str(input);
639+
assert!(
640+
result.is_ok(),
641+
"event was not parsed as expected {:?} given {}",
642+
result,
643+
input
644+
);
645+
let req = result.expect("failed to parse request");
646+
assert_eq!(req.method(), "GET");
647+
assert_eq!(req.uri(), "/v1/health/");
648+
}
628649
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"body": "",
3+
"headers": {
4+
"user-agent": "ELB-HealthChecker/2.0"
5+
},
6+
"httpMethod": "GET",
7+
8+
"isBase64Encoded": false,
9+
10+
"path": "/v1/health/",
11+
"queryStringParameters": {},
12+
"requestContext": {
13+
"elb": {
14+
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:111111111:targetgroup/cdn-ingestor-tg-usea1-dev/3fe2aca58c0da101"
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)