Skip to content

Commit

Permalink
Validate k8s_gateway_api::HttpRouteSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Takumi Sue <u630868b@alumni.osaka-u.ac.jp>
  • Loading branch information
mikutas committed Jul 26, 2023
1 parent e9da7ea commit 3c0119d
Showing 1 changed file with 73 additions and 27 deletions.
100 changes: 73 additions & 27 deletions policy-controller/src/admission.rs
Expand Up @@ -123,6 +123,10 @@ impl Admission {
return self.admit_spec::<HttpRouteSpec>(req).await;
}

if is_kind::<k8s_gateway_api::HttpRoute>(&req) {
return self.admit_spec::<k8s_gateway_api::HttpRouteSpec>(req).await;
}

AdmissionResponse::invalid(format_args!(
"unsupported resource type: {}.{}.{}",
req.kind.group, req.kind.version, req.kind.kind
Expand Down Expand Up @@ -422,36 +426,35 @@ impl Validate<ServerAuthorizationSpec> for Admission {
}
}

#[async_trait::async_trait]
impl Validate<HttpRouteSpec> for Admission {
async fn validate(self, _ns: &str, _name: &str, spec: HttpRouteSpec) -> Result<()> {
use index::http_route;

fn validate_match(
httproute::HttpRouteMatch {
path,
headers,
query_params,
method,
}: httproute::HttpRouteMatch,
) -> Result<()> {
let _ = path.map(http_route::path_match).transpose()?;
let _ = method
.as_deref()
.map(core::http_route::Method::try_from)
.transpose()?;

for q in query_params.into_iter().flatten() {
http_route::query_param_match(q)?;
}
use index::http_route;
fn validate_match(
httproute::HttpRouteMatch {
path,
headers,
query_params,
method,
}: httproute::HttpRouteMatch,
) -> Result<()> {
let _ = path.map(http_route::path_match).transpose()?;
let _ = method
.as_deref()
.map(core::http_route::Method::try_from)
.transpose()?;

for q in query_params.into_iter().flatten() {
http_route::query_param_match(q)?;
}

for h in headers.into_iter().flatten() {
http_route::header_match(h)?;
}
for h in headers.into_iter().flatten() {
http_route::header_match(h)?;
}

Ok(())
}
Ok(())
}

#[async_trait::async_trait]
impl Validate<HttpRouteSpec> for Admission {
async fn validate(self, _ns: &str, _name: &str, spec: HttpRouteSpec) -> Result<()> {
fn validate_filter(filter: httproute::HttpRouteFilter) -> Result<()> {
match filter {
httproute::HttpRouteFilter::RequestHeaderModifier {
Expand Down Expand Up @@ -513,3 +516,46 @@ impl Validate<HttpRouteSpec> for Admission {
Ok(())
}
}

#[async_trait::async_trait]
impl Validate<k8s_gateway_api::HttpRouteSpec> for Admission {
async fn validate(
self,
_ns: &str,
_name: &str,
spec: k8s_gateway_api::HttpRouteSpec,
) -> Result<()> {
fn validate_filter(filter: k8s_gateway_api::HttpRouteFilter) -> Result<()> {
match filter {
k8s_gateway_api::HttpRouteFilter::RequestHeaderModifier {
request_header_modifier,
} => http_route::req_header_modifier(request_header_modifier).map(|_| ()),
k8s_gateway_api::HttpRouteFilter::RequestRedirect { request_redirect } => {
http_route::req_redirect(request_redirect).map(|_| ())
}

Check failure on line 535 in policy-controller/src/admission.rs

View workflow job for this annotation

GitHub Actions / check

error[E0425]: cannot find function `req_header_modifier` in module `http_route` --> policy-controller/src/admission.rs:535:34 | 535 | } => http_route::req_header_modifier(request_header_modifier).map(|_| ()), | ^^^^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `header_modifier` | ::: /__w/linkerd2/linkerd2/policy-controller/k8s/index/src/http_route.rs:147:1 | 147 | / pub fn header_modifier( 148 | | api::HttpRequestHeaderFilter { set, add, remove }: api::HttpRequestHeaderFilter, 149 | | ) -> Result<http_route::HeaderModifierFilter> { | |_____________________________________________- similarly named function `header_modifier` defined here

Check failure on line 535 in policy-controller/src/admission.rs

View workflow job for this annotation

GitHub Actions / clippy

error[E0425]: cannot find function `req_header_modifier` in module `http_route` --> policy-controller/src/admission.rs:535:34 | 535 | } => http_route::req_header_modifier(request_header_modifier).map(|_| ()), | ^^^^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `header_modifier` | ::: /__w/linkerd2/linkerd2/policy-controller/k8s/index/src/http_route.rs:147:1 | 147 | / pub fn header_modifier( 148 | | api::HttpRequestHeaderFilter { set, add, remove }: api::HttpRequestHeaderFilter, 149 | | ) -> Result<http_route::HeaderModifierFilter> { | |_____________________________________________- similarly named function `header_modifier` defined here

Check failure on line 535 in policy-controller/src/admission.rs

View workflow job for this annotation

GitHub Actions / clippy

error[E0425]: cannot find function `req_header_modifier` in module `http_route` --> policy-controller/src/admission.rs:535:34 | 535 | } => http_route::req_header_modifier(request_header_modifier).map(|_| ()), | ^^^^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `header_modifier` | ::: /__w/linkerd2/linkerd2/policy-controller/k8s/index/src/http_route.rs:147:1 | 147 | / pub fn header_modifier( 148 | | api::HttpRequestHeaderFilter { set, add, remove }: api::HttpRequestHeaderFilter, 149 | | ) -> Result<http_route::HeaderModifierFilter> { | |_____________________________________________- similarly named function `header_modifier` defined here
k8s_gateway_api::HttpRouteFilter::RequestMirror { .. } => Ok(()),
k8s_gateway_api::HttpRouteFilter::URLRewrite { .. } => Ok(()),
k8s_gateway_api::HttpRouteFilter::ExtensionRef { .. } => Ok(()),
}
}

// Validate the rules in this spec.
// This is essentially equivalent to the indexer's conversion function
// from `HttpRouteSpec` to `InboundRouteBinding`, except that we don't
// actually allocate stuff in order to return an `InboundRouteBinding`.
for k8s_gateway_api::HttpRouteRule {
filters, matches, ..
} in spec.rules.into_iter().flatten()
{
for m in matches.into_iter().flatten() {
validate_match(m)?;
}

for f in filters.into_iter().flatten() {
validate_filter(f)?;
}
}

Ok(())
}
}

0 comments on commit 3c0119d

Please sign in to comment.