Skip to content

Commit 4436fa2

Browse files
committed
reactive buggy security test
1 parent aa27f6f commit 4436fa2

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
import io.grpc.examples.reactor.ReactiveHelloRequest;
4+
import io.grpc.examples.reactor.ReactiveHelloResponse;
5+
import io.grpc.examples.reactor.ReactorReactiveGreeterGrpc;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.lognet.springboot.grpc.GRpcService;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
9+
import org.springframework.context.annotation.Profile;
10+
import org.springframework.security.access.annotation.Secured;
11+
import org.springframework.transaction.annotation.Transactional;
12+
import reactor.core.publisher.Mono;
13+
14+
15+
@GRpcService
16+
@Slf4j
17+
@ConditionalOnClass(Transactional.class)
18+
@Profile("reactive-buggy-security")
19+
public class BuggyReactiveGreeterGrpcService extends ReactorReactiveGreeterGrpc.ReactiveGreeterImplBase {
20+
21+
22+
@Override
23+
@Secured({})
24+
public Mono<ReactiveHelloResponse> greet(Mono<ReactiveHelloRequest> request) {
25+
return super.greet(request);
26+
}
27+
28+
@Override
29+
@Secured({}) //invalid
30+
public Mono<ReactiveHelloResponse> greet(ReactiveHelloRequest request) {
31+
return super.greet(request);
32+
}
33+
}

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/ReactiveGreeterGrpcService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler;
1010
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope;
1111
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
12+
import org.springframework.context.annotation.Profile;
1213
import org.springframework.security.access.annotation.Secured;
1314
import org.springframework.security.core.context.SecurityContextHolder;
1415
import org.springframework.transaction.annotation.Transactional;
@@ -22,6 +23,7 @@
2223
@GRpcService
2324
@Slf4j
2425
@ConditionalOnClass(Transactional.class)
26+
@Profile("!reactive-buggy-security")
2527
public class ReactiveGreeterGrpcService extends ReactorReactiveGreeterGrpc.ReactiveGreeterImplBase {
2628

2729
private ReactiveGreeterService reactiveGreeterService;
@@ -39,6 +41,11 @@ public Mono<ReactiveHelloResponse> greet(Mono<ReactiveHelloRequest> request) {
3941

4042
}
4143

44+
@Override
45+
public Mono<ReactiveHelloResponse> greet(ReactiveHelloRequest request) {
46+
return super.greet(request); //for tests
47+
}
48+
4249
@Override
4350
public Flux<ReactiveHelloResponse> multiGreet(Mono<ReactiveHelloRequest> request) {
4451
return request.flatMapIterable(r ->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.lognet.springboot.grpc.reactive;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.lognet.springboot.grpc.auth.JwtAuthBaseTest;
7+
import org.lognet.springboot.grpc.demo.DemoApp;
8+
import org.lognet.springboot.rules.ExpectedStartupExceptionWithInspector;
9+
import org.lognet.springboot.rules.SpringRunnerWithGlobalExpectedExceptionInspected;
10+
import org.springframework.beans.factory.BeanCreationException;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.core.NestedExceptionUtils;
13+
import org.springframework.test.annotation.DirtiesContext;
14+
import org.springframework.test.context.ActiveProfiles;
15+
16+
import java.util.function.Predicate;
17+
18+
import static org.hamcrest.MatcherAssert.assertThat;
19+
import static org.hamcrest.Matchers.*;
20+
21+
22+
@Slf4j
23+
@RunWith(SpringRunnerWithGlobalExpectedExceptionInspected.class)
24+
@SpringBootTest(classes = DemoApp.class)
25+
@ActiveProfiles({"keycloack-test", "r2dbc-test", "reactive-buggy-security"})
26+
@DirtiesContext
27+
@ExpectedStartupExceptionWithInspector(BaggyReactiveSecurityTest.ExceptionInspector.class)
28+
public class BaggyReactiveSecurityTest extends JwtAuthBaseTest {
29+
30+
@Test
31+
public void contextStartupFails() {
32+
}
33+
34+
public static class ExceptionInspector implements Predicate<Throwable> {
35+
36+
@Override
37+
public boolean test(Throwable throwable) {
38+
39+
Throwable rootCause = NestedExceptionUtils.getRootCause(throwable);
40+
assertThat(rootCause, instanceOf(BeanCreationException.class));
41+
BeanCreationException beanCreationException = (BeanCreationException) rootCause;
42+
43+
assertThat(beanCreationException.getMessage(), allOf(
44+
notNullValue(String.class),
45+
stringContainsInOrder("Ambiguous", "Secured", "method")
46+
));
47+
48+
return true;
49+
}
50+
51+
}
52+
}

0 commit comments

Comments
 (0)