-
Notifications
You must be signed in to change notification settings - Fork 433
/
MetricWithSecurityTest.java
96 lines (77 loc) · 3.49 KB
/
MetricWithSecurityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.lognet.springboot.grpc;
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.prometheus.PrometheusConfig;
import org.awaitility.Awaitility;
import org.hamcrest.Matchers;
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.springframework.beans.factory.annotation.Autowired;
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.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.Duration;
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.assertThrows;
@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",
}
)
@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 {
@Bean
public FailedAuthGrpcSecurityConfig securityConfig() {
return new FailedAuthGrpcSecurityConfig();
}
}
@Override
public void simpleGreeting() throws Exception {
AuthCallCredentials callCredentials = new AuthCallCredentials(
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())
);
assertThat(e.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED));
final Timer timer = registry.find("grpc.server.calls").timer();
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));
final String resultTag = timer.getId().getTag("result");
assertThat(resultTag, notNullValue());
assertThat(resultTag, is(Status.UNAUTHENTICATED.getCode().name()));
}
}