Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#9882] Cleanup #9896

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.navercorp.pinpoint.common.server.util.AddressFilter;
import com.navercorp.pinpoint.grpc.channelz.ChannelzRegistry;
import com.navercorp.pinpoint.grpc.security.SslContextFactory;
import com.navercorp.pinpoint.grpc.security.SslServerProperties;
import io.grpc.ServerCallExecutorSupplier;
import io.grpc.ServerInterceptor;
import io.grpc.ServerServiceDefinition;
Expand All @@ -20,6 +19,8 @@
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -91,15 +92,23 @@ private GrpcReceiver createReceiver(GrpcSslReceiverProperties properties,
receiver.setServerInterceptorList(serverInterceptorList);
receiver.setChannelzRegistry(channelzRegistry);

SslContext sslContext = newSslContext(properties);
SslContext sslContext = newSslContext(properties.getGrpcSslProperties());
receiver.setSslContext(sslContext);
return receiver;
}

private SslContext newSslContext(GrpcSslReceiverProperties properties) throws SSLException {
final SslServerProperties sslServerConfig = properties.getGrpcSslProperties().toSslServerProperties();
logger.debug("Enable sslConfig.({})", sslServerConfig);
return SslContextFactory.create(sslServerConfig);
private SslContext newSslContext(GrpcSslProperties properties) throws SSLException {
logger.debug("Enable sslConfig.({})", properties);

try {
InputStream keyChain = properties.getKeyCertChainResource().getInputStream();
InputStream key = properties.getKeyResource().getInputStream();
SslContextFactory factory = new SslContextFactory(properties.getProviderType());
return factory.forServer(keyChain, key);
} catch (IOException e) {
throw new SSLException(e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import com.navercorp.pinpoint.collector.grpc.config.SpringResource;
import com.navercorp.pinpoint.grpc.security.SslServerProperties;
import org.springframework.core.io.Resource;

import java.io.IOException;
Expand Down Expand Up @@ -51,11 +49,6 @@ public Resource getKeyCertChainResource() {
return keyCertChainResource;
}

public SslServerProperties toSslServerProperties() {
SpringResource keyResource = new SpringResource(this.keyResource);
SpringResource keyCertChainResource = new SpringResource(this.keyCertChainResource);
return new SslServerProperties(providerType, keyResource, keyCertChainResource);
}

public static Builder newBuilder() {
return new Builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public ManagedChannel build(String channelName, String host, int port) {
if (sslClientConfig.isEnable()) {
SslContext sslContext = null;
try {
sslContext = SslContextFactory.create(sslClientConfig);
SslContextFactory factory = new SslContextFactory(sslClientConfig.getSslProviderType());
sslContext = factory.forClient(sslClientConfig);
} catch (SSLException e) {
throw new SecurityException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManagerFactory;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.List;
import java.util.Objects;
Expand All @@ -40,17 +41,19 @@ public final class SslContextFactory {

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

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

SslProvider sslProvider = getSslProvider(serverProperties.getSslProviderType());
public SslContextFactory(String providerType) throws SSLException {
Objects.requireNonNull(providerType, "providerType");
this.sslProvider = getSslProvider(providerType);
}

SslContextBuilder sslContextBuilder;
try {
Resource keyCertChainFileResource = serverProperties.getKeyCertChainResource();
Resource keyResource = serverProperties.getKeyResource();
public SslContext forServer(InputStream keyCertChainInputStream, InputStream keyInputStream) throws SSLException {
Objects.requireNonNull(keyCertChainInputStream, "keyCertChainInputStream");
Objects.requireNonNull(keyInputStream, "keyInputStream");

sslContextBuilder = SslContextBuilder.forServer(keyCertChainFileResource.getInputStream(), keyResource.getInputStream());
try {
SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(keyCertChainInputStream, keyInputStream);
SslContext sslContext = createSslContext(sslContextBuilder, sslProvider);

assertValidCipherSuite(sslContext);
Expand All @@ -63,18 +66,15 @@ public static SslContext create(SslServerProperties serverProperties) throws SSL
}
}

public static SslContext create(SslClientConfig clientConfig) throws SSLException {
public SslContext forClient(SslClientConfig clientConfig) throws SSLException {
Objects.requireNonNull(clientConfig, "clientConfig");

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

SslProvider sslProvider = getSslProvider(clientConfig.getSslProviderType());

SslContextBuilder sslContextBuilder = null;
try {
sslContextBuilder = SslContextBuilder.forClient();
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();

Resource trustCertResource = clientConfig.getTrustCertResource();
if (trustCertResource != null) {
Expand All @@ -85,7 +85,8 @@ public static SslContext create(SslClientConfig clientConfig) throws SSLExceptio
trustManagerFactory.init((KeyStore)null);
sslContextBuilder.trustManager(trustManagerFactory);
}


SslProvider sslProvider = getSslProvider(clientConfig.getSslProviderType());
SslContext sslContext = createSslContext(sslContextBuilder, sslProvider);

assertValidCipherSuite(sslContext);
Expand All @@ -98,7 +99,7 @@ public static SslContext create(SslClientConfig clientConfig) throws SSLExceptio
}
}

private static SslContext createSslContext(SslContextBuilder sslContextBuilder, SslProvider sslProvider) throws SSLException {
private SslContext createSslContext(SslContextBuilder sslContextBuilder, SslProvider sslProvider) throws SSLException {
sslContextBuilder.sslProvider(sslProvider);

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

private static void assertValidCipherSuite(SslContext sslContext) throws SSLException {
private void assertValidCipherSuite(SslContext sslContext) throws SSLException {
Objects.requireNonNull(sslContext, "sslContext must not be null");

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

static SslProvider getSslProvider(String providerType) throws SSLException {
SslProvider getSslProvider(String providerType) throws SSLException {
if (StringUtils.isEmpty(providerType)) {
return SslProvider.OPENSSL;
}
Expand Down

This file was deleted.