Skip to content

Commit 1a38999

Browse files
committed
closes #279
1 parent 2dbd3ef commit 1a38999

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

grpc-spring-boot-starter-demo/src/pureNettyTest/java/org/lognet/springboot/grpc/PureNettyTransportConfigTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.lognet.springboot.grpc;
22

3+
import io.grpc.ManagedChannelBuilder;
34
import io.grpc.ServerBuilder;
45
import io.grpc.examples.GreeterGrpc;
6+
import io.grpc.netty.GrpcSslContexts;
7+
import io.grpc.netty.NettyChannelBuilder;
8+
import io.netty.handler.ssl.SslContext;
59
import org.hamcrest.Matchers;
610
import org.junit.BeforeClass;
711
import org.junit.runner.RunWith;
@@ -15,8 +19,12 @@
1519
import org.springframework.context.annotation.Configuration;
1620
import org.springframework.context.annotation.Import;
1721
import org.springframework.context.event.EventListener;
22+
import org.springframework.core.io.Resource;
23+
import org.springframework.test.context.ActiveProfiles;
1824
import org.springframework.test.context.junit4.SpringRunner;
1925

26+
import javax.validation.Valid;
27+
import java.io.IOException;
2028
import java.net.SocketAddress;
2129
import java.util.List;
2230

@@ -29,7 +37,6 @@
2937
properties = {
3038
"grpc.netty-server.additional-listen-addresses[0]=localhost:0",
3139
"grpc.netty-server.primary-listen-address=localhost:0"
32-
3340
}
3441
)
3542
@Import(PureNettyTransportConfigTest.TestConfig.class)
@@ -55,6 +62,13 @@ public GRpcServerBuilderConfigurer customGrpcServerBuilderConfigurer() {
5562
@Autowired
5663
private GRpcServerBuilderConfigurer configurer;
5764

65+
@Override
66+
protected void setupTransportSecurity(ManagedChannelBuilder<?> channelBuilder, Resource certChain) throws IOException {
67+
SslContext sslContext = GrpcSslContexts.forClient().trustManager(certChain.getInputStream()).build();
68+
((NettyChannelBuilder)channelBuilder)
69+
.useTransportSecurity()
70+
.sslContext(sslContext);
71+
}
5872

5973
@BeforeClass
6074
public static void before() {

grpc-spring-boot-starter-demo/src/pureNettyTest/resources/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ management:
88
base-path: "/"
99
exposure:
1010
include: "*"
11-
11+
grpc:
12+
security:
13+
cert-chain: classpath:cert/server-cert.pem
14+
private-key: classpath:cert/server-key.pem
1215

1316
logging:
1417
level:

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.grpc.inprocess.InProcessChannelBuilder;
1010
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
1111
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
12+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
1213
import io.grpc.reflection.v1alpha.ServerReflectionGrpc;
1314
import org.junit.After;
1415
import org.junit.Before;
@@ -79,9 +80,9 @@ public void setupChannels() throws IOException {
7980
.map(GRpcServerProperties.SecurityProperties::getCertChain)
8081
.orElse(null);
8182
if(null!= certChain){
82-
((NettyChannelBuilder)channelBuilder)
83-
.useTransportSecurity()
84-
.sslContext(GrpcSslContexts.forClient().trustManager(certChain.getInputStream()).build());
83+
84+
setupTransportSecurity(channelBuilder,certChain);
85+
8586
}else{
8687
channelBuilder.usePlaintext();
8788
}
@@ -99,6 +100,12 @@ public void setupChannels() throws IOException {
99100
selectedChanel = getChannel();
100101
}
101102

103+
protected void setupTransportSecurity(ManagedChannelBuilder<?> channelBuilder, Resource certChain) throws IOException {
104+
SslContext sslContext = GrpcSslContexts.forClient().trustManager(certChain.getInputStream()).build();
105+
((NettyChannelBuilder)channelBuilder)
106+
.useTransportSecurity()
107+
.sslContext(sslContext);
108+
}
102109
protected Channel getChannel(){
103110
return Optional.ofNullable(channel).orElse(inProcChannel);
104111
}

grpc-spring-boot-starter/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ dependencies {
270270
compile "io.grpc:grpc-netty-shaded:${grpcVersion}"
271271
compile "io.grpc:grpc-services:${grpcVersion}"
272272
compile(group: 'org.springframework.boot', name: 'spring-boot-starter')
273-
compile 'io.netty:netty-tcnative-boringssl-static:2.0.25.Final'
273+
compile 'io.netty:netty-tcnative-boringssl-static:2.0.47.Final'
274274

275275
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
276276

0 commit comments

Comments
 (0)