Skip to content

Commit

Permalink
dag: configure 502 if include references another (#5016) (#5157)
Browse files Browse the repository at this point in the history
Set 502 response if include references another root

Fixes #5016

Signed-off-by: Lan Liang <gcslyp@gmail.com>
  • Loading branch information
liangyuanpeng committed Apr 13, 2023
1 parent c813560 commit 98f876a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/5157-liangyuanpeng-small.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set 502 response if include references another root.
47 changes: 47 additions & 0 deletions internal/dag/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10722,6 +10722,53 @@ func TestDAGInsert(t *testing.T) {
},
),
},
"insert httproxy with include references another root": {
objs: []interface{}{
&contour_api_v1.HTTPProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "example-com",
},
Spec: contour_api_v1.HTTPProxySpec{
VirtualHost: &contour_api_v1.VirtualHost{
Fqdn: "example.com",
},
Includes: []contour_api_v1.Include{{
Conditions: []contour_api_v1.MatchCondition{{
Prefix: "/finance",
}},
Name: "other-root",
Namespace: "default",
}},
},
},
&contour_api_v1.HTTPProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "other-root",
Namespace: "default",
},
Spec: contour_api_v1.HTTPProxySpec{
VirtualHost: &contour_api_v1.VirtualHost{
Fqdn: "example2.com",
},
},
},
},
want: listeners(
&Listener{
Name: HTTP_LISTENER_NAME,
Port: 8080,
VirtualHosts: virtualhosts(
virtualhost("example.com", &Route{
PathMatchCondition: prefixString("/finance"),
DirectResponse: &DirectResponse{
StatusCode: http.StatusBadGateway,
},
}),
),
},
),
},
"insert httproxy w/ conditions": {
objs: []interface{}{
proxy1c, s1,
Expand Down
24 changes: 15 additions & 9 deletions internal/dag/httpproxy_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,18 @@ func addRoutes(vhost vhost, routes []*Route) {
}
}

func addStatusBadGatewayRoute(routes []*Route, conds []contour_api_v1.MatchCondition) []*Route {
if len(conds) > 0 {
routes = append(routes, &Route{
PathMatchCondition: mergePathMatchConditions(conds),
HeaderMatchConditions: mergeHeaderMatchConditions(conds),
QueryParamMatchConditions: mergeQueryParamMatchConditions(conds),
DirectResponse: directResponse(http.StatusBadGateway, ""),
})
}
return routes
}

func (p *HTTPProxyProcessor) computeRoutes(
validCond *contour_api_v1.DetailedCondition,
rootProxy *contour_api_v1.HTTPProxy,
Expand Down Expand Up @@ -618,21 +630,15 @@ func (p *HTTPProxyProcessor) computeRoutes(
"include %s/%s not found", namespace, include.Name)

// Set 502 response when include was not found but include condition was valid.
if len(include.Conditions) > 0 {
routes = append(routes, &Route{
PathMatchCondition: mergePathMatchConditions(include.Conditions),
HeaderMatchConditions: mergeHeaderMatchConditions(include.Conditions),
QueryParamMatchConditions: mergeQueryParamMatchConditions(include.Conditions),
DirectResponse: directResponse(http.StatusBadGateway, ""),
})
}

routes = addStatusBadGatewayRoute(routes, include.Conditions)
continue
}

if includedProxy.Spec.VirtualHost != nil {
validCond.AddErrorf(contour_api_v1.ConditionTypeIncludeError, "RootIncludesRoot",
"root httpproxy cannot include another root httpproxy")
// Set 502 response if include references another root
routes = addStatusBadGatewayRoute(routes, include.Conditions)
continue
}

Expand Down

0 comments on commit 98f876a

Please sign in to comment.