Skip to content

Commit

Permalink
fix errorprone errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Furer committed Jul 1, 2021
1 parent 7805d96 commit 93fedaf
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 90 deletions.
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ Note also custom cross-field link:grpc-spring-boot-starter-demo/src/main/java/or

As described in <<Interceptors ordering>> chapter, you can give `validation` interceptor the higher precedence than `security` interceptor and set `grpc.security.auth.fail-fast` property to `false`. +
In this scenario, if call is both unauthenticated and invalid, the client will get `Status.INVALID_ARGUMENT` instead of `Status.PERMISSION_DENIED/Status.UNAUTHENTICATED` response status.
Demo is https://github.com/LogNet/grpc-spring-boot-starter/blob/master/grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/ValidationWithSecurityTest.java[here]



By adding `GRpcErrorHandler` bean to your application, you get a chance to send your custom response headers. The error handler will be called with `Status.INVALID_ARGUMENT` and incoming request message that is failed.

Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ subprojects {
tasks.withType(JavaCompile).configureEach {
options.errorprone.disableWarningsInGeneratedCode = true

options.errorprone.enabled = (null != System.getenv("CI"))
options.errorprone.enabled = (it.name.startsWith("compile") && it.name.endsWith("TestJava") ) ? false:(null != System.getenv("CI"))


}
configurations.all {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class PreferPureNettyCollisionTest extends DefaultNettyCollisionTest {


@Test
@Override
public void contextLoads() {
assertNettyBuilderClass(io.grpc.netty.NettyServerBuilder.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class PreferShadedNettyCollisionTest extends DefaultNettyCollisionTest {


@Test
@Override
public void contextLoads() {
assertNettyBuilderClass(io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,158 +1,96 @@
package org.lognet.springboot.grpc;


import com.fasterxml.jackson.databind.node.ObjectNode;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.GreeterOuterClass;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.prometheus.PrometheusConfig;
import org.awaitility.Awaitility;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.auth.FailedAuthGrpcSecurityConfig;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.AuthCallCredentials;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;


@SpringBootTest(classes = DemoApp.class
,properties = {
"grpc.security.auth.fail-fast=false", // give metric interceptor a chance to record failed authentication
"grpc.security.auth.interceptor-order=4",
"grpc.metrics.interceptor-order=2",
}
)
, properties = {
"grpc.security.auth.fail-fast=false", // give metric interceptor a chance to record failed authentication
"grpc.security.auth.interceptor-order=4",
"grpc.metrics.interceptor-order=2",
}
)
@RunWith(SpringRunner.class)
@Import({MetricWithSecurityTest.TestCfg.class})
@ActiveProfiles("measure")
public class MetricWithSecurityTest extends GrpcServerTestBase {




@Autowired
private MeterRegistry registry;



@Autowired
private PrometheusConfig registryConfig;


@TestConfiguration
static class TestCfg extends GrpcSecurityConfigurerAdapter {
@Override
public void configure(GrpcSecurity builder) throws Exception {

builder.authorizeRequests()
.anyMethod().authenticated()
.and()
.authenticationProvider(new AuthenticationProvider() {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
throw new BadCredentialsException("");
}

@Override
public boolean supports(Class<?> authentication) {
return true;
}
})
.userDetailsService(new InMemoryUserDetailsManager());
static class TestCfg {
@Bean
public FailedAuthGrpcSecurityConfig securityConfig() {
return new FailedAuthGrpcSecurityConfig();
}



}

@Test
public void validationShouldInvokedBeforeAuthTest() {
final GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(super.getChannel());
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(49)// valid
.clearName()//invalid
.build());
});

assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));


}

@Override
public void simpleGreeting() throws Exception {
public void simpleGreeting() throws Exception {
AuthCallCredentials callCredentials = new AuthCallCredentials(
AuthHeader.builder().basic("user","pwd".getBytes())
AuthHeader.builder().basic("user", "pwd".getBytes())
);

final GreeterGrpc.GreeterBlockingStub greeterFutureStub = GreeterGrpc.newBlockingStub(selectedChanel);
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () ->
greeterFutureStub
.withCallCredentials(callCredentials)
.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName(name).build())
greeterFutureStub
.withCallCredentials(callCredentials)
.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName(name).build())
);
assertThat(e.getStatus().getCode(),Matchers.is(Status.Code.UNAUTHENTICATED));
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED));

final Timer timer = registry.find("grpc.server.calls").timer();
assertThat(timer,notNullValue(Timer.class));
assertThat(timer, notNullValue(Timer.class));

Awaitility
.waitAtMost(Duration.ofMillis(registryConfig.step().toMillis() * 2))
.until(timer::count,greaterThan(0L));

assertThat(timer.max(TimeUnit.MILLISECONDS),greaterThan(0d));
assertThat(timer.mean(TimeUnit.MILLISECONDS),greaterThan(0d));
assertThat(timer.totalTime(TimeUnit.MILLISECONDS),greaterThan(0d));
.until(timer::count, greaterThan(0L));

assertThat(timer.max(TimeUnit.MILLISECONDS), greaterThan(0d));
assertThat(timer.mean(TimeUnit.MILLISECONDS), greaterThan(0d));
assertThat(timer.totalTime(TimeUnit.MILLISECONDS), greaterThan(0d));


final String resultTag = timer.getId().getTag("result");
assertThat(resultTag,notNullValue());
assertThat(resultTag,is(Status.UNAUTHENTICATED.getCode().name()));
assertThat(resultTag, notNullValue());
assertThat(resultTag, is(Status.UNAUTHENTICATED.getCode().name()));

}









}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
@ActiveProfiles("disable-security")
public class OrderedInterceptorsTest extends GrpcServerTestBase{

@LocalRunningGrpcPort
int runningPort;


private static List<Integer> calledInterceptors = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.lognet.springboot.grpc.auth;

import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

public class FailedAuthGrpcSecurityConfig extends GrpcSecurityConfigurerAdapter {
@Override
public void configure(GrpcSecurity builder) throws Exception {

builder.authorizeRequests()
.anyMethod().authenticated()
.and()
.authenticationProvider(new AuthenticationProvider() {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
throw new BadCredentialsException("");
}

@Override
public boolean supports(Class<?> authentication) {
return true;
}
})
.userDetailsService(new InMemoryUserDetailsManager());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.lognet.springboot.grpc.auth;


import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.GreeterOuterClass;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.GrpcServerTestBase;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;


@SpringBootTest(classes = DemoApp.class
,properties = {
"grpc.security.auth.fail-fast=false", // give validation interceptor a chance to validate message before authentication failure
"grpc.security.auth.interceptor-order=4" // run after validator
}
)
@RunWith(SpringRunner.class)
@Import({ValidationWithSecurityTest.TestCfg.class})
public class ValidationWithSecurityTest extends GrpcServerTestBase {

@TestConfiguration
static class TestCfg{
@Bean
public FailedAuthGrpcSecurityConfig securityConfig(){
return new FailedAuthGrpcSecurityConfig();
}
}

@Test
public void validationShouldInvokedBeforeAuthTest() {
final GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(super.getChannel());
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
.setAge(49)// valid
.clearName()//invalid
.build());
});

assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));


}

@Override
public void simpleGreeting() throws Exception {
// do nothing
}
}

0 comments on commit 93fedaf

Please sign in to comment.