Skip to content

Commit 93fedaf

Browse files
author
Alexander Furer
committed
fix errorprone errors
1 parent 7805d96 commit 93fedaf

File tree

8 files changed

+124
-90
lines changed

8 files changed

+124
-90
lines changed

README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ Note also custom cross-field link:grpc-spring-boot-starter-demo/src/main/java/or
510510

511511
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`. +
512512
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.
513+
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]
514+
515+
513516

514517
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.
515518

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ subprojects {
7272
tasks.withType(JavaCompile).configureEach {
7373
options.errorprone.disableWarningsInGeneratedCode = true
7474

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

7778
}
7879
configurations.all {

grpc-spring-boot-starter-demo/src/bothPureAndShadedNettyTest/java/org/lognet/springboot/grpc/PreferPureNettyCollisionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class PreferPureNettyCollisionTest extends DefaultNettyCollisionTest {
2626

2727

2828
@Test
29+
@Override
2930
public void contextLoads() {
3031
assertNettyBuilderClass(io.grpc.netty.NettyServerBuilder.class);
3132

grpc-spring-boot-starter-demo/src/bothPureAndShadedNettyTest/java/org/lognet/springboot/grpc/PreferShadedNettyCollisionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class PreferShadedNettyCollisionTest extends DefaultNettyCollisionTest {
2626

2727

2828
@Test
29+
@Override
2930
public void contextLoads() {
3031
assertNettyBuilderClass(io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder.class);
3132

Lines changed: 25 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,96 @@
11
package org.lognet.springboot.grpc;
22

33

4-
import com.fasterxml.jackson.databind.node.ObjectNode;
54
import io.grpc.Status;
65
import io.grpc.StatusRuntimeException;
76
import io.grpc.examples.GreeterGrpc;
87
import io.grpc.examples.GreeterOuterClass;
98
import io.micrometer.core.instrument.MeterRegistry;
109
import io.micrometer.core.instrument.Timer;
11-
import io.micrometer.core.instrument.simple.SimpleConfig;
1210
import io.micrometer.prometheus.PrometheusConfig;
1311
import org.awaitility.Awaitility;
1412
import org.hamcrest.Matchers;
15-
import org.junit.Ignore;
1613
import org.junit.Test;
1714
import org.junit.runner.RunWith;
15+
import org.lognet.springboot.grpc.auth.FailedAuthGrpcSecurityConfig;
1816
import org.lognet.springboot.grpc.demo.DemoApp;
1917
import org.lognet.springboot.grpc.security.AuthCallCredentials;
2018
import org.lognet.springboot.grpc.security.AuthHeader;
21-
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
22-
import org.lognet.springboot.grpc.security.GrpcSecurity;
23-
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
2419
import org.springframework.beans.factory.annotation.Autowired;
2520
import org.springframework.boot.test.context.SpringBootTest;
2621
import org.springframework.boot.test.context.TestConfiguration;
27-
import org.springframework.boot.test.web.client.TestRestTemplate;
22+
import org.springframework.context.annotation.Bean;
2823
import org.springframework.context.annotation.Import;
29-
import org.springframework.http.HttpStatus;
30-
import org.springframework.http.ResponseEntity;
31-
import org.springframework.security.authentication.AuthenticationProvider;
32-
import org.springframework.security.authentication.BadCredentialsException;
33-
import org.springframework.security.core.Authentication;
34-
import org.springframework.security.core.AuthenticationException;
35-
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
3624
import org.springframework.test.context.ActiveProfiles;
3725
import org.springframework.test.context.junit4.SpringRunner;
3826

3927
import java.time.Duration;
40-
import java.util.concurrent.ExecutionException;
4128
import java.util.concurrent.TimeUnit;
4229

4330
import static org.hamcrest.MatcherAssert.assertThat;
4431
import static org.hamcrest.Matchers.greaterThan;
4532
import static org.hamcrest.Matchers.is;
4633
import static org.hamcrest.Matchers.notNullValue;
47-
import static org.junit.Assert.assertEquals;
4834
import static org.junit.Assert.assertThrows;
49-
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
5035

5136

5237
@SpringBootTest(classes = DemoApp.class
53-
,properties = {
54-
"grpc.security.auth.fail-fast=false", // give metric interceptor a chance to record failed authentication
55-
"grpc.security.auth.interceptor-order=4",
56-
"grpc.metrics.interceptor-order=2",
57-
}
58-
)
38+
, properties = {
39+
"grpc.security.auth.fail-fast=false", // give metric interceptor a chance to record failed authentication
40+
"grpc.security.auth.interceptor-order=4",
41+
"grpc.metrics.interceptor-order=2",
42+
}
43+
)
5944
@RunWith(SpringRunner.class)
6045
@Import({MetricWithSecurityTest.TestCfg.class})
6146
@ActiveProfiles("measure")
6247
public class MetricWithSecurityTest extends GrpcServerTestBase {
6348

64-
65-
66-
6749
@Autowired
6850
private MeterRegistry registry;
6951

70-
71-
7252
@Autowired
7353
private PrometheusConfig registryConfig;
7454

75-
7655
@TestConfiguration
77-
static class TestCfg extends GrpcSecurityConfigurerAdapter {
78-
@Override
79-
public void configure(GrpcSecurity builder) throws Exception {
80-
81-
builder.authorizeRequests()
82-
.anyMethod().authenticated()
83-
.and()
84-
.authenticationProvider(new AuthenticationProvider() {
85-
@Override
86-
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
87-
throw new BadCredentialsException("");
88-
}
89-
90-
@Override
91-
public boolean supports(Class<?> authentication) {
92-
return true;
93-
}
94-
})
95-
.userDetailsService(new InMemoryUserDetailsManager());
56+
static class TestCfg {
57+
@Bean
58+
public FailedAuthGrpcSecurityConfig securityConfig() {
59+
return new FailedAuthGrpcSecurityConfig();
9660
}
97-
98-
99-
100-
}
101-
102-
@Test
103-
public void validationShouldInvokedBeforeAuthTest() {
104-
final GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(super.getChannel());
105-
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
106-
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
107-
.setAge(49)// valid
108-
.clearName()//invalid
109-
.build());
110-
});
111-
112-
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));
113-
114-
11561
}
11662

11763
@Override
118-
public void simpleGreeting() throws Exception {
64+
public void simpleGreeting() throws Exception {
11965
AuthCallCredentials callCredentials = new AuthCallCredentials(
120-
AuthHeader.builder().basic("user","pwd".getBytes())
66+
AuthHeader.builder().basic("user", "pwd".getBytes())
12167
);
12268

12369
final GreeterGrpc.GreeterBlockingStub greeterFutureStub = GreeterGrpc.newBlockingStub(selectedChanel);
12470
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () ->
125-
greeterFutureStub
126-
.withCallCredentials(callCredentials)
127-
.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName(name).build())
71+
greeterFutureStub
72+
.withCallCredentials(callCredentials)
73+
.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName(name).build())
12874
);
129-
assertThat(e.getStatus().getCode(),Matchers.is(Status.Code.UNAUTHENTICATED));
75+
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED));
13076

13177
final Timer timer = registry.find("grpc.server.calls").timer();
132-
assertThat(timer,notNullValue(Timer.class));
78+
assertThat(timer, notNullValue(Timer.class));
13379

13480
Awaitility
13581
.waitAtMost(Duration.ofMillis(registryConfig.step().toMillis() * 2))
136-
.until(timer::count,greaterThan(0L));
137-
138-
assertThat(timer.max(TimeUnit.MILLISECONDS),greaterThan(0d));
139-
assertThat(timer.mean(TimeUnit.MILLISECONDS),greaterThan(0d));
140-
assertThat(timer.totalTime(TimeUnit.MILLISECONDS),greaterThan(0d));
82+
.until(timer::count, greaterThan(0L));
14183

84+
assertThat(timer.max(TimeUnit.MILLISECONDS), greaterThan(0d));
85+
assertThat(timer.mean(TimeUnit.MILLISECONDS), greaterThan(0d));
86+
assertThat(timer.totalTime(TimeUnit.MILLISECONDS), greaterThan(0d));
14287

14388

14489
final String resultTag = timer.getId().getTag("result");
145-
assertThat(resultTag,notNullValue());
146-
assertThat(resultTag,is(Status.UNAUTHENTICATED.getCode().name()));
90+
assertThat(resultTag, notNullValue());
91+
assertThat(resultTag, is(Status.UNAUTHENTICATED.getCode().name()));
14792

14893
}
14994

15095

151-
152-
153-
154-
155-
156-
157-
15896
}

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/OrderedInterceptorsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
@ActiveProfiles("disable-security")
3737
public class OrderedInterceptorsTest extends GrpcServerTestBase{
3838

39-
@LocalRunningGrpcPort
40-
int runningPort;
39+
4140

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.lognet.springboot.grpc.auth;
2+
3+
import org.lognet.springboot.grpc.security.GrpcSecurity;
4+
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
5+
import org.springframework.security.authentication.AuthenticationProvider;
6+
import org.springframework.security.authentication.BadCredentialsException;
7+
import org.springframework.security.core.Authentication;
8+
import org.springframework.security.core.AuthenticationException;
9+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
10+
11+
public class FailedAuthGrpcSecurityConfig extends GrpcSecurityConfigurerAdapter {
12+
@Override
13+
public void configure(GrpcSecurity builder) throws Exception {
14+
15+
builder.authorizeRequests()
16+
.anyMethod().authenticated()
17+
.and()
18+
.authenticationProvider(new AuthenticationProvider() {
19+
@Override
20+
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
21+
throw new BadCredentialsException("");
22+
}
23+
24+
@Override
25+
public boolean supports(Class<?> authentication) {
26+
return true;
27+
}
28+
})
29+
.userDetailsService(new InMemoryUserDetailsManager());
30+
}
31+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.lognet.springboot.grpc.auth;
2+
3+
4+
import io.grpc.Status;
5+
import io.grpc.StatusRuntimeException;
6+
import io.grpc.examples.GreeterGrpc;
7+
import io.grpc.examples.GreeterOuterClass;
8+
import org.hamcrest.Matchers;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.lognet.springboot.grpc.GrpcServerTestBase;
12+
import org.lognet.springboot.grpc.demo.DemoApp;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.context.TestConfiguration;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Import;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.junit.Assert.assertThrows;
21+
22+
23+
@SpringBootTest(classes = DemoApp.class
24+
,properties = {
25+
"grpc.security.auth.fail-fast=false", // give validation interceptor a chance to validate message before authentication failure
26+
"grpc.security.auth.interceptor-order=4" // run after validator
27+
}
28+
)
29+
@RunWith(SpringRunner.class)
30+
@Import({ValidationWithSecurityTest.TestCfg.class})
31+
public class ValidationWithSecurityTest extends GrpcServerTestBase {
32+
33+
@TestConfiguration
34+
static class TestCfg{
35+
@Bean
36+
public FailedAuthGrpcSecurityConfig securityConfig(){
37+
return new FailedAuthGrpcSecurityConfig();
38+
}
39+
}
40+
41+
@Test
42+
public void validationShouldInvokedBeforeAuthTest() {
43+
final GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(super.getChannel());
44+
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> {
45+
stub.helloPersonValidResponse(GreeterOuterClass.Person.newBuilder()
46+
.setAge(49)// valid
47+
.clearName()//invalid
48+
.build());
49+
});
50+
51+
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.INVALID_ARGUMENT));
52+
53+
54+
}
55+
56+
@Override
57+
public void simpleGreeting() throws Exception {
58+
// do nothing
59+
}
60+
}

0 commit comments

Comments
 (0)