Skip to content

Commit 1879c4a

Browse files
committed
[#9882] Extract Grpc SSL Module
1 parent a8de915 commit 1879c4a

19 files changed

+176
-175
lines changed

collector/src/main/java/com/navercorp/pinpoint/collector/PinpointCollectorModule.java

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.navercorp.pinpoint.collector.config.CollectorProperties;
66
import com.navercorp.pinpoint.collector.config.FlinkContextModule;
77
import com.navercorp.pinpoint.collector.config.MetricConfiguration;
8+
import com.navercorp.pinpoint.collector.grpc.ssl.GrpcSslModule;
89
import com.navercorp.pinpoint.common.server.CommonsServerConfiguration;
910
import com.navercorp.pinpoint.common.server.config.TypeLoaderConfiguration;
1011
import org.springframework.context.annotation.Bean;
@@ -33,6 +34,8 @@
3334
ClusterModule.class,
3435

3536
MetricConfiguration.class,
37+
38+
GrpcSslModule.class
3639
})
3740
@ComponentScan(basePackages = {
3841
"com.navercorp.pinpoint.collector.handler",
+7-10
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.navercorp.pinpoint.collector.grpc.config;
17+
package com.navercorp.pinpoint.collector.grpc.ssl;
1818

1919
import com.navercorp.pinpoint.collector.receiver.BindAddress;
20+
import org.springframework.beans.factory.annotation.Qualifier;
2021
import org.springframework.boot.context.properties.ConfigurationProperties;
2122
import org.springframework.context.annotation.Bean;
2223
import org.springframework.context.annotation.Configuration;
23-
import org.springframework.core.env.Environment;
2424

2525
/**
2626
* @author Taejin Koo
2727
*/
2828
@Configuration
2929
public class GrpcAgentDataSslReceiverConfiguration {
3030

31-
static final String AGENT_SSL_PROPERTIES = "grpcAgentSslReceiverProperties";
32-
3331
public static final String BIND_ADDRESS = "collector.receiver.grpc.agent.ssl.bindaddress";
3432

3533
public static final String SSL = "collector.receiver.grpc.ssl";
@@ -51,16 +49,15 @@ public GrpcSslProperties.Builder newGrpcSslConfigurationBuilder() {
5149
return GrpcSslProperties.newBuilder();
5250
}
5351

54-
@Bean("grpcAgentSslReceiverProperties")
55-
public GrpcSslReceiverProperties grpcAgentSslReceiverConfig(Environment environment) throws Exception {
56-
57-
boolean enable = environment.getProperty("collector.receiver.grpc.agent.ssl.enable", boolean.class, false);
52+
@Bean
53+
public GrpcSslReceiverProperties grpcAgentSslReceiverProperties(
54+
@Qualifier(GrpcAgentDataSslReceiverConfiguration.SSL) GrpcSslProperties.Builder sslPropertiesBuilder) throws Exception {
5855

5956
BindAddress bindAddress = newBindAddressBuilder().build();
6057

61-
GrpcSslProperties grpcSslConfiguration = newGrpcSslConfigurationBuilder().build();
58+
GrpcSslProperties grpcSslConfiguration = sslPropertiesBuilder.build();
6259

63-
return new GrpcSslReceiverProperties(enable, bindAddress, grpcSslConfiguration);
60+
return new GrpcSslReceiverProperties(bindAddress, grpcSslConfiguration);
6461
}
6562

6663
}
+5-9
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.navercorp.pinpoint.collector.grpc.config;
17+
package com.navercorp.pinpoint.collector.grpc.ssl;
1818

1919
import com.navercorp.pinpoint.collector.receiver.BindAddress;
2020
import org.springframework.beans.factory.annotation.Qualifier;
2121
import org.springframework.boot.context.properties.ConfigurationProperties;
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
24-
import org.springframework.core.env.Environment;
2524

2625
/**
2726
* @author Taejin Koo
2827
*/
2928
@Configuration
3029
public class GrpcSpanSslReceiverConfiguration {
3130

32-
static final String SPAN_SSL_PROPERTIES = "grpcSpanSslReceiverProperties";
33-
3431
public static final String BIND_ADDRESS = "collector.receiver.grpc.span.ssl.bindaddress";
3532

3633
public GrpcSpanSslReceiverConfiguration() {
@@ -44,18 +41,17 @@ public BindAddress.Builder newBindAddressBuilder() {
4441
return builder;
4542
}
4643

47-
@Bean(SPAN_SSL_PROPERTIES)
44+
@Bean
4845
public GrpcSslReceiverProperties grpcSpanSslReceiverProperties(
49-
Environment environment,
5046
@Qualifier(GrpcAgentDataSslReceiverConfiguration.SSL) GrpcSslProperties.Builder sslPropertiesBuilder) throws Exception {
5147

52-
boolean enable = environment.getProperty("collector.receiver.grpc.span.ssl.enable", boolean.class, false);
53-
5448
BindAddress bindAddress = newBindAddressBuilder().build();
5549

5650
GrpcSslProperties grpcSslConfiguration = sslPropertiesBuilder.build();
5751

58-
return new GrpcSslReceiverProperties(enable, bindAddress, grpcSslConfiguration);
52+
return new GrpcSslReceiverProperties(bindAddress, grpcSslConfiguration);
5953
}
6054

55+
56+
6157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.navercorp.pinpoint.collector.grpc.ssl;
2+
3+
import com.navercorp.pinpoint.collector.grpc.config.GrpcReceiverProperties;
4+
import com.navercorp.pinpoint.collector.receiver.grpc.GrpcReceiver;
5+
import com.navercorp.pinpoint.common.server.util.AddressFilter;
6+
import com.navercorp.pinpoint.grpc.channelz.ChannelzRegistry;
7+
import com.navercorp.pinpoint.grpc.security.SslContextFactory;
8+
import com.navercorp.pinpoint.grpc.security.SslServerProperties;
9+
import io.grpc.ServerCallExecutorSupplier;
10+
import io.grpc.ServerInterceptor;
11+
import io.grpc.ServerServiceDefinition;
12+
import io.grpc.ServerTransportFilter;
13+
import io.netty.handler.ssl.SslContext;
14+
import org.apache.logging.log4j.LogManager;
15+
import org.apache.logging.log4j.Logger;
16+
import org.springframework.beans.factory.annotation.Qualifier;
17+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.ComponentScan;
20+
import org.springframework.context.annotation.Configuration;
21+
22+
import javax.net.ssl.SSLException;
23+
import java.util.List;
24+
import java.util.concurrent.Executor;
25+
26+
@Configuration
27+
@ConditionalOnProperty(value = "pinpoint.modules.collector.grpc.ssl.enabled", havingValue = "true")
28+
@ComponentScan(basePackages = "com.navercorp.pinpoint.collector.grpc.ssl")
29+
public class GrpcSslModule {
30+
private final Logger logger = LogManager.getLogger(this.getClass());
31+
32+
@Bean
33+
public GrpcReceiver grpcAgentSslReceiver(@Qualifier("grpcAgentSslReceiverProperties") GrpcSslReceiverProperties properties,
34+
@Qualifier("grpcAgentReceiverProperties") GrpcReceiverProperties grpcReceiverProperties,
35+
AddressFilter addressFilter,
36+
@Qualifier("agentServiceList") List<?> serviceList,
37+
@Qualifier("agentInterceptorList")List<ServerInterceptor> serverInterceptorList,
38+
ChannelzRegistry channelzRegistry,
39+
@Qualifier("grpcAgentServerExecutor") Executor executor,
40+
@Qualifier("grpcAgentServerCallExecutorSupplier") ServerCallExecutorSupplier serverCallExecutorSupplier) throws SSLException {
41+
GrpcReceiver receiver = createReceiver(properties, grpcReceiverProperties, addressFilter, serviceList, serverInterceptorList, channelzRegistry, executor);
42+
receiver.setServerCallExecutorSupplier(serverCallExecutorSupplier);
43+
44+
return receiver;
45+
}
46+
47+
@Bean
48+
public GrpcReceiver grpcSpanSslReceiver(@Qualifier("grpcSpanSslReceiverProperties") GrpcSslReceiverProperties properties,
49+
@Qualifier("grpcSpanReceiverProperties") GrpcReceiverProperties grpcReceiverProperties,
50+
AddressFilter addressFilter,
51+
@Qualifier("spanServiceList") List<ServerServiceDefinition> serviceList,
52+
@Qualifier("spanInterceptorList") List<ServerInterceptor> serverInterceptorList,
53+
ChannelzRegistry channelzRegistry,
54+
@Qualifier("grpcSpanServerExecutor") Executor executor,
55+
@Qualifier("serverTransportFilterList") List<ServerTransportFilter> transportFilterList) throws SSLException {
56+
GrpcReceiver receiver = createReceiver(properties, grpcReceiverProperties, addressFilter, serviceList, serverInterceptorList, channelzRegistry, executor);
57+
receiver.setTransportFilterList(transportFilterList);
58+
return receiver;
59+
}
60+
61+
@Bean
62+
public GrpcReceiver grpcStatSslReceiver(@Qualifier("grpcStatSslReceiverProperties") GrpcSslReceiverProperties properties,
63+
@Qualifier("grpcStatReceiverProperties") GrpcReceiverProperties grpcReceiverProperties,
64+
AddressFilter addressFilter,
65+
@Qualifier("statServiceList") List<ServerServiceDefinition> serviceList,
66+
@Qualifier("statInterceptorList") List<ServerInterceptor> serverInterceptorList,
67+
ChannelzRegistry channelzRegistry,
68+
@Qualifier("grpcStatServerExecutor") Executor executor,
69+
@Qualifier("serverTransportFilterList") List<ServerTransportFilter> transportFilterList) throws SSLException {
70+
GrpcReceiver receiver = createReceiver(properties, grpcReceiverProperties, addressFilter, serviceList, serverInterceptorList, channelzRegistry, executor);
71+
receiver.setTransportFilterList(transportFilterList);
72+
return receiver;
73+
}
74+
75+
private GrpcReceiver createReceiver(GrpcSslReceiverProperties properties,
76+
GrpcReceiverProperties grpcReceiverProperties,
77+
AddressFilter addressFilter,
78+
List<?> serviceList,
79+
List<ServerInterceptor> serverInterceptorList,
80+
ChannelzRegistry channelzRegistry,
81+
Executor executor) throws SSLException {
82+
GrpcReceiver receiver = new GrpcReceiver();
83+
receiver.setBindAddress(properties.getBindAddress());
84+
receiver.setServerOption(grpcReceiverProperties.getServerOption());
85+
86+
receiver.setEnable(true);
87+
88+
receiver.setExecutor(executor);
89+
receiver.setAddressFilter(addressFilter);
90+
receiver.setBindableServiceList(serviceList);
91+
receiver.setServerInterceptorList(serverInterceptorList);
92+
receiver.setChannelzRegistry(channelzRegistry);
93+
94+
SslContext sslContext = newSslContext(properties);
95+
receiver.setSslContext(sslContext);
96+
return receiver;
97+
}
98+
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);
103+
}
104+
105+
}

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

+13-33
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.navercorp.pinpoint.collector.grpc.config;
17+
package com.navercorp.pinpoint.collector.grpc.ssl;
1818

19+
import com.navercorp.pinpoint.collector.grpc.config.SpringResource;
1920
import com.navercorp.pinpoint.grpc.security.SslServerProperties;
2021
import org.springframework.core.io.Resource;
2122

@@ -27,21 +28,15 @@
2728
*/
2829
public class GrpcSslProperties {
2930

30-
private final boolean enable;
3131
private final String providerType;
3232
private final Resource keyResource;
3333
private final Resource keyCertChainResource;
3434

35-
private GrpcSslProperties(boolean enable, String providerType,
35+
private GrpcSslProperties(String providerType,
3636
Resource keyResource, Resource keyCertChainResource) {
37-
this.enable = enable;
3837
this.providerType = providerType;
39-
this.keyResource = keyResource;
40-
this.keyCertChainResource = keyCertChainResource;
41-
}
42-
43-
public boolean isEnable() {
44-
return enable;
38+
this.keyResource = Objects.requireNonNull(keyResource, "keyResource");
39+
this.keyCertChainResource = Objects.requireNonNull(keyCertChainResource, "keyCertChainResource");
4540
}
4641

4742
public String getProviderType() {
@@ -57,12 +52,9 @@ public Resource getKeyCertChainResource() {
5752
}
5853

5954
public SslServerProperties toSslServerProperties() {
60-
if (enable) {
61-
return new SslServerProperties(enable, providerType,
62-
new SpringResource(keyResource), new SpringResource(keyCertChainResource));
63-
} else {
64-
return SslServerProperties.DISABLED_CONFIG;
65-
}
55+
SpringResource keyResource = new SpringResource(this.keyResource);
56+
SpringResource keyCertChainResource = new SpringResource(this.keyCertChainResource);
57+
return new SslServerProperties(providerType, keyResource, keyCertChainResource);
6658
}
6759

6860
public static Builder newBuilder() {
@@ -71,22 +63,13 @@ public static Builder newBuilder() {
7163

7264
public static class Builder {
7365

74-
private boolean enable;
7566
private String providerType;
7667
private Resource keyFilePath;
7768
private Resource keyCertFilePath;
7869

7970
private Builder() {
8071
}
8172

82-
public boolean isEnable() {
83-
return enable;
84-
}
85-
86-
public void setEnable(boolean enable) {
87-
this.enable = enable;
88-
}
89-
9073
public String getProviderType() {
9174
return providerType;
9275
}
@@ -112,20 +95,17 @@ public void setKeyCertFilePath(Resource keyCertFilePath) {
11295
}
11396

11497
public GrpcSslProperties build() throws IOException {
115-
if (enable) {
116-
Objects.requireNonNull(providerType);
117-
return new GrpcSslProperties(this.enable, this.providerType, this.keyFilePath, this.keyCertFilePath);
118-
} else {
119-
return new GrpcSslProperties(this.enable, this.providerType, null, null);
120-
}
98+
Objects.requireNonNull(providerType, "providerType");
99+
Objects.requireNonNull(keyFilePath, "keyFilePath does not exists");
100+
Objects.requireNonNull(keyCertFilePath, "keyCertFilePath does not exists");
101+
return new GrpcSslProperties(this.providerType, this.keyFilePath, this.keyCertFilePath);
121102
}
122103
}
123104

124105
@Override
125106
public String toString() {
126107
return "GrpcSslProperties{" +
127-
"enable=" + enable +
128-
", providerType='" + providerType + '\'' +
108+
"providerType='" + providerType + '\'' +
129109
", keyResource='" + keyResource + '\'' +
130110
", keyCertChainResource='" + keyCertChainResource + '\'' +
131111
'}';
+4-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.navercorp.pinpoint.collector.grpc.config;
17+
package com.navercorp.pinpoint.collector.grpc.ssl;
1818

1919
import com.navercorp.pinpoint.collector.receiver.BindAddress;
2020
import org.apache.logging.log4j.LogManager;
@@ -31,30 +31,22 @@ public class GrpcSslReceiverProperties {
3131

3232
protected final Logger logger = LogManager.getLogger(getClass());
3333

34-
private final boolean enable;
3534
private final BindAddress bindAddress;
3635
private final GrpcSslProperties grpcSslConfiguration;
3736

38-
GrpcSslReceiverProperties(boolean enable,
39-
BindAddress bindAddress,
37+
GrpcSslReceiverProperties(BindAddress bindAddress,
4038
GrpcSslProperties grpcSslConfiguration) {
41-
this.enable = enable;
4239

4340
this.bindAddress = Objects.requireNonNull(bindAddress, "bindAddress");
4441
this.grpcSslConfiguration = Objects.requireNonNull(grpcSslConfiguration, "grpcSslConfiguration");
4542
}
4643

4744
@PostConstruct
4845
public void log() {
49-
this.logger.info("enable:{}", this.enable);
5046
this.logger.info("bindAddress:{}", bindAddress);
5147
this.logger.info("grpcSslConfiguration:{}", grpcSslConfiguration);
5248
}
5349

54-
public boolean isEnable() {
55-
return enable;
56-
}
57-
5850
public BindAddress getBindAddress() {
5951
return bindAddress;
6052
}
@@ -65,8 +57,8 @@ public GrpcSslProperties getGrpcSslProperties() {
6557

6658
@Override
6759
public String toString() {
68-
return "GrpcSslReceiverProperties{" + "enable=" + enable +
69-
", bindAddress=" + bindAddress +
60+
return "GrpcSslReceiverProperties{" +
61+
"bindAddress=" + bindAddress +
7062
", grpcSslConfiguration=" + grpcSslConfiguration +
7163
'}';
7264
}

0 commit comments

Comments
 (0)