Skip to content

Commit 8c0d90e

Browse files
committed
[#9882] Cleanup
1 parent 85edb6c commit 8c0d90e

File tree

5 files changed

+35
-91
lines changed

5 files changed

+35
-91
lines changed

collector/src/main/java/com/navercorp/pinpoint/collector/grpc/ssl/GrpcSslModule.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.navercorp.pinpoint.common.server.util.AddressFilter;
66
import com.navercorp.pinpoint.grpc.channelz.ChannelzRegistry;
77
import com.navercorp.pinpoint.grpc.security.SslContextFactory;
8-
import com.navercorp.pinpoint.grpc.security.SslServerProperties;
98
import io.grpc.ServerCallExecutorSupplier;
109
import io.grpc.ServerInterceptor;
1110
import io.grpc.ServerServiceDefinition;
@@ -20,6 +19,8 @@
2019
import org.springframework.context.annotation.Configuration;
2120

2221
import javax.net.ssl.SSLException;
22+
import java.io.IOException;
23+
import java.io.InputStream;
2324
import java.util.List;
2425
import java.util.concurrent.Executor;
2526

@@ -91,15 +92,23 @@ private GrpcReceiver createReceiver(GrpcSslReceiverProperties properties,
9192
receiver.setServerInterceptorList(serverInterceptorList);
9293
receiver.setChannelzRegistry(channelzRegistry);
9394

94-
SslContext sslContext = newSslContext(properties);
95+
SslContext sslContext = newSslContext(properties.getGrpcSslProperties());
9596
receiver.setSslContext(sslContext);
9697
return receiver;
9798
}
9899

99-
private SslContext newSslContext(GrpcSslReceiverProperties properties) throws SSLException {
100-
final SslServerProperties sslServerConfig = properties.getGrpcSslProperties().toSslServerProperties();
101-
logger.debug("Enable sslConfig.({})", sslServerConfig);
102-
return SslContextFactory.create(sslServerConfig);
100+
private SslContext newSslContext(GrpcSslProperties properties) throws SSLException {
101+
logger.debug("Enable sslConfig.({})", properties);
102+
103+
try {
104+
InputStream keyChain = properties.getKeyCertChainResource().getInputStream();
105+
InputStream key = properties.getKeyResource().getInputStream();
106+
SslContextFactory factory = new SslContextFactory(properties.getProviderType());
107+
return factory.forServer(keyChain, key);
108+
} catch (IOException e) {
109+
throw new SSLException(e);
110+
}
103111
}
104112

113+
105114
}

collector/src/main/java/com/navercorp/pinpoint/collector/grpc/ssl/GrpcSslProperties.java

-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.navercorp.pinpoint.collector.grpc.ssl;
1818

19-
import com.navercorp.pinpoint.collector.grpc.config.SpringResource;
20-
import com.navercorp.pinpoint.grpc.security.SslServerProperties;
2119
import org.springframework.core.io.Resource;
2220

2321
import java.io.IOException;
@@ -51,11 +49,6 @@ public Resource getKeyCertChainResource() {
5149
return keyCertChainResource;
5250
}
5351

54-
public SslServerProperties toSslServerProperties() {
55-
SpringResource keyResource = new SpringResource(this.keyResource);
56-
SpringResource keyCertChainResource = new SpringResource(this.keyCertChainResource);
57-
return new SslServerProperties(providerType, keyResource, keyCertChainResource);
58-
}
5952

6053
public static Builder newBuilder() {
6154
return new Builder();

grpc/src/main/java/com/navercorp/pinpoint/grpc/client/DefaultChannelFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ public ManagedChannel build(String channelName, String host, int port) {
154154
if (sslClientConfig.isEnable()) {
155155
SslContext sslContext = null;
156156
try {
157-
sslContext = SslContextFactory.create(sslClientConfig);
157+
SslContextFactory factory = new SslContextFactory(sslClientConfig.getSslProviderType());
158+
sslContext = factory.forClient(sslClientConfig);
158159
} catch (SSLException e) {
159160
throw new SecurityException(e);
160161
}

grpc/src/main/java/com/navercorp/pinpoint/grpc/security/SslContextFactory.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import javax.net.ssl.SSLException;
3131
import javax.net.ssl.TrustManagerFactory;
32+
import java.io.InputStream;
3233
import java.security.KeyStore;
3334
import java.util.List;
3435
import java.util.Objects;
@@ -40,17 +41,19 @@ public final class SslContextFactory {
4041

4142
private static final Logger LOGGER = LogManager.getLogger(SslContextFactory.class);
4243

43-
public static SslContext create(SslServerProperties serverProperties) throws SSLException {
44-
Objects.requireNonNull(serverProperties, "serverProperties");
44+
private final SslProvider sslProvider;
4545

46-
SslProvider sslProvider = getSslProvider(serverProperties.getSslProviderType());
46+
public SslContextFactory(String providerType) throws SSLException {
47+
Objects.requireNonNull(providerType, "providerType");
48+
this.sslProvider = getSslProvider(providerType);
49+
}
4750

48-
SslContextBuilder sslContextBuilder;
49-
try {
50-
Resource keyCertChainFileResource = serverProperties.getKeyCertChainResource();
51-
Resource keyResource = serverProperties.getKeyResource();
51+
public SslContext forServer(InputStream keyCertChainInputStream, InputStream keyInputStream) throws SSLException {
52+
Objects.requireNonNull(keyCertChainInputStream, "keyCertChainInputStream");
53+
Objects.requireNonNull(keyInputStream, "keyInputStream");
5254

53-
sslContextBuilder = SslContextBuilder.forServer(keyCertChainFileResource.getInputStream(), keyResource.getInputStream());
55+
try {
56+
SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(keyCertChainInputStream, keyInputStream);
5457
SslContext sslContext = createSslContext(sslContextBuilder, sslProvider);
5558

5659
assertValidCipherSuite(sslContext);
@@ -63,18 +66,15 @@ public static SslContext create(SslServerProperties serverProperties) throws SSL
6366
}
6467
}
6568

66-
public static SslContext create(SslClientConfig clientConfig) throws SSLException {
69+
public SslContext forClient(SslClientConfig clientConfig) throws SSLException {
6770
Objects.requireNonNull(clientConfig, "clientConfig");
6871

6972
if (!clientConfig.isEnable()) {
7073
throw new IllegalArgumentException("sslConfig is disabled.");
7174
}
7275

73-
SslProvider sslProvider = getSslProvider(clientConfig.getSslProviderType());
74-
75-
SslContextBuilder sslContextBuilder = null;
7676
try {
77-
sslContextBuilder = SslContextBuilder.forClient();
77+
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
7878

7979
Resource trustCertResource = clientConfig.getTrustCertResource();
8080
if (trustCertResource != null) {
@@ -85,7 +85,8 @@ public static SslContext create(SslClientConfig clientConfig) throws SSLExceptio
8585
trustManagerFactory.init((KeyStore)null);
8686
sslContextBuilder.trustManager(trustManagerFactory);
8787
}
88-
88+
89+
SslProvider sslProvider = getSslProvider(clientConfig.getSslProviderType());
8990
SslContext sslContext = createSslContext(sslContextBuilder, sslProvider);
9091

9192
assertValidCipherSuite(sslContext);
@@ -98,7 +99,7 @@ public static SslContext create(SslClientConfig clientConfig) throws SSLExceptio
9899
}
99100
}
100101

101-
private static SslContext createSslContext(SslContextBuilder sslContextBuilder, SslProvider sslProvider) throws SSLException {
102+
private SslContext createSslContext(SslContextBuilder sslContextBuilder, SslProvider sslProvider) throws SSLException {
102103
sslContextBuilder.sslProvider(sslProvider);
103104

104105
sslContextBuilder.protocols(SecurityConstants.DEFAULT_SUPPORT_PROTOCOLS.toArray(new String[0]));
@@ -108,7 +109,7 @@ private static SslContext createSslContext(SslContextBuilder sslContextBuilder,
108109
return configure.build();
109110
}
110111

111-
private static void assertValidCipherSuite(SslContext sslContext) throws SSLException {
112+
private void assertValidCipherSuite(SslContext sslContext) throws SSLException {
112113
Objects.requireNonNull(sslContext, "sslContext must not be null");
113114

114115
List<String> supportedCipherSuiteList = sslContext.cipherSuites();
@@ -125,7 +126,7 @@ private static void assertValidCipherSuite(SslContext sslContext) throws SSLExce
125126
LOGGER.info("Support cipher list : {} {}", sslContext, supportedCipherSuiteList);
126127
}
127128

128-
static SslProvider getSslProvider(String providerType) throws SSLException {
129+
SslProvider getSslProvider(String providerType) throws SSLException {
129130
if (StringUtils.isEmpty(providerType)) {
130131
return SslProvider.OPENSSL;
131132
}

grpc/src/main/java/com/navercorp/pinpoint/grpc/security/SslServerProperties.java

-60
This file was deleted.

0 commit comments

Comments
 (0)