spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Add ability to match Endpoint requests by http method

Open onobc opened this issue 4 years ago • 10 comments

What

This proposal adds the ability to match Actuator endpoint requests by http method.

Why

This provides a finer grained matching of requests. One example use is allowing unrestricted access to an endpoint for GET requests but restricting access to the endpoint for POST requests.

How

  • Support has been added to both servlet and reactive flavors of EndpointRequest

onobc avatar Jan 29 '22 16:01 onobc

We would also like to backport this to 2.6.x branch but am unsure if the changes to RequestMatcherProvider would prevent that.

onobc avatar Jan 29 '22 16:01 onobc

Thanks for the PR Chris.

We would also like to backport this to 2.6.x branch

We don't backport enhancement and this feels like one to me.

snicoll avatar Jan 30 '22 07:01 snicoll

Thanks for the PR Chris.

We would also like to backport this to 2.6.x branch

We don't backport enhancement and this feels like one to me.

TIL. I respect that sane policy.

And yep, this is an enhancement which is can easily be functionally worked around via composition w/ a guard on http method such as:

/**
 * Returns a matcher that includes the specified {@link Endpoint actuator endpoints} and http method.
 * @param httpMethod the http method to include
 * @param endpoints the endpoints to include
 * @return the configured {@link RequestMatcher}
 */
RequestMatcher to(HttpMethod httpMethod, String... endpoints) {
    final EndpointRequest.EndpointRequestMatcher matcher = EndpointRequest.to(endpoints);
    return (request) -> {
        if (httpMethod != null && !httpMethod.name().equals(request.getMethod())) {
            return false;
        }
        return matcher.matches(request);
    };
}

onobc avatar Jan 30 '22 17:01 onobc

I've pushed a polish commit to https://github.com/philwebb/spring-boot/tree/gh-29596.

I'd like someone else from the team to review. I'm wondering if we should include HttpMethod in the API or not. It's in spring-web and I wonder if it will always be available. Interestingly, the AntPathRequestMatcher uses Strings in the API but internally does rely on HttpMethod. I suspect that means we're fine to have it in out API.

philwebb avatar Jan 31 '22 05:01 philwebb

Given how the endpoint abstraction is designed at the moment, wouldn't it make more sense to provide the kind of operation that is requested on the endpoint? EndpointRequest we use an endpoint concept there so it feels natural to me to extend it to customize the "kind of operation" (read/write/delete).

snicoll avatar Jan 31 '22 08:01 snicoll

I agree that's natural for standard endpoints but I think it may be confusing for @ControllerEndpoint where you deal with HTTP methods directly.

wilkinsona avatar Jan 31 '22 08:01 wilkinsona

I'm wondering if we should include HttpMethod in the API or not. It's in spring-web and I wonder if it will always be available. Interestingly, the AntPathRequestMatcher uses Strings in the API but internally does rely on HttpMethod. I suspect that means we're fine to have it in out API.

Yep, I saw this as well @philwebb . I ended up keeping HttpMethod until we go down into the AntPathRequestMatcher. It is also used in org.springframework.security.config.annotation.web.builders.HttpSecurity.RequestMatcherConfigurer#mvcMatchers(org.springframework.http.HttpMethod, java.lang.String...) which is where these matchers will be primarily used.

onobc avatar Jan 31 '22 15:01 onobc

Given how the endpoint abstraction is designed at the moment, wouldn't it make more sense to provide the kind of operation that is requested on the endpoint? EndpointRequest we use an endpoint concept there so it feels natural to me to extend it to customize the "kind of operation" (read/write/delete).

I thought about this as well @snicoll . I ended up putting on the EndpointRequestMatcher directly to keep the functionality isolated to this particular case where the matchers are being used in the security mappings and where http methods are used.

onobc avatar Jan 31 '22 15:01 onobc

@philwebb was this merged in a different PR?

csterwa avatar Mar 17 '24 19:03 csterwa

@csterwa No, I don't believe this has been merged in any form as yet.

philwebb avatar Mar 18 '24 05:03 philwebb