|
1 | 1 | package org.lognet.springboot.grpc; |
2 | 2 |
|
3 | 3 |
|
4 | | -import com.fasterxml.jackson.databind.node.ObjectNode; |
5 | 4 | import io.grpc.Status; |
6 | 5 | import io.grpc.StatusRuntimeException; |
7 | 6 | import io.grpc.examples.GreeterGrpc; |
8 | 7 | import io.grpc.examples.GreeterOuterClass; |
9 | 8 | import io.micrometer.core.instrument.MeterRegistry; |
10 | 9 | import io.micrometer.core.instrument.Timer; |
11 | | -import io.micrometer.core.instrument.simple.SimpleConfig; |
12 | 10 | import io.micrometer.prometheus.PrometheusConfig; |
13 | 11 | import org.awaitility.Awaitility; |
14 | 12 | import org.hamcrest.Matchers; |
15 | | -import org.junit.Ignore; |
16 | 13 | import org.junit.Test; |
17 | 14 | import org.junit.runner.RunWith; |
| 15 | +import org.lognet.springboot.grpc.auth.FailedAuthGrpcSecurityConfig; |
18 | 16 | import org.lognet.springboot.grpc.demo.DemoApp; |
19 | 17 | import org.lognet.springboot.grpc.security.AuthCallCredentials; |
20 | 18 | 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; |
24 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 20 | import org.springframework.boot.test.context.SpringBootTest; |
26 | 21 | import org.springframework.boot.test.context.TestConfiguration; |
27 | | -import org.springframework.boot.test.web.client.TestRestTemplate; |
| 22 | +import org.springframework.context.annotation.Bean; |
28 | 23 | 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; |
36 | 24 | import org.springframework.test.context.ActiveProfiles; |
37 | 25 | import org.springframework.test.context.junit4.SpringRunner; |
38 | 26 |
|
39 | 27 | import java.time.Duration; |
40 | | -import java.util.concurrent.ExecutionException; |
41 | 28 | import java.util.concurrent.TimeUnit; |
42 | 29 |
|
43 | 30 | import static org.hamcrest.MatcherAssert.assertThat; |
44 | 31 | import static org.hamcrest.Matchers.greaterThan; |
45 | 32 | import static org.hamcrest.Matchers.is; |
46 | 33 | import static org.hamcrest.Matchers.notNullValue; |
47 | | -import static org.junit.Assert.assertEquals; |
48 | 34 | import static org.junit.Assert.assertThrows; |
49 | | -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
50 | 35 |
|
51 | 36 |
|
52 | 37 | @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 | +) |
59 | 44 | @RunWith(SpringRunner.class) |
60 | 45 | @Import({MetricWithSecurityTest.TestCfg.class}) |
61 | 46 | @ActiveProfiles("measure") |
62 | 47 | public class MetricWithSecurityTest extends GrpcServerTestBase { |
63 | 48 |
|
64 | | - |
65 | | - |
66 | | - |
67 | 49 | @Autowired |
68 | 50 | private MeterRegistry registry; |
69 | 51 |
|
70 | | - |
71 | | - |
72 | 52 | @Autowired |
73 | 53 | private PrometheusConfig registryConfig; |
74 | 54 |
|
75 | | - |
76 | 55 | @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(); |
96 | 60 | } |
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 | | - |
115 | 61 | } |
116 | 62 |
|
117 | 63 | @Override |
118 | | - public void simpleGreeting() throws Exception { |
| 64 | + public void simpleGreeting() throws Exception { |
119 | 65 | AuthCallCredentials callCredentials = new AuthCallCredentials( |
120 | | - AuthHeader.builder().basic("user","pwd".getBytes()) |
| 66 | + AuthHeader.builder().basic("user", "pwd".getBytes()) |
121 | 67 | ); |
122 | 68 |
|
123 | 69 | final GreeterGrpc.GreeterBlockingStub greeterFutureStub = GreeterGrpc.newBlockingStub(selectedChanel); |
124 | 70 | 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()) |
128 | 74 | ); |
129 | | - assertThat(e.getStatus().getCode(),Matchers.is(Status.Code.UNAUTHENTICATED)); |
| 75 | + assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED)); |
130 | 76 |
|
131 | 77 | final Timer timer = registry.find("grpc.server.calls").timer(); |
132 | | - assertThat(timer,notNullValue(Timer.class)); |
| 78 | + assertThat(timer, notNullValue(Timer.class)); |
133 | 79 |
|
134 | 80 | Awaitility |
135 | 81 | .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)); |
141 | 83 |
|
| 84 | + assertThat(timer.max(TimeUnit.MILLISECONDS), greaterThan(0d)); |
| 85 | + assertThat(timer.mean(TimeUnit.MILLISECONDS), greaterThan(0d)); |
| 86 | + assertThat(timer.totalTime(TimeUnit.MILLISECONDS), greaterThan(0d)); |
142 | 87 |
|
143 | 88 |
|
144 | 89 | 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())); |
147 | 92 |
|
148 | 93 | } |
149 | 94 |
|
150 | 95 |
|
151 | | - |
152 | | - |
153 | | - |
154 | | - |
155 | | - |
156 | | - |
157 | | - |
158 | 96 | } |
0 commit comments