Skip to content

Commit c56784f

Browse files
author
Dave Syer
committed
Remove netty-specific credentials factory in client
See also #10
1 parent 158e779 commit c56784f

File tree

5 files changed

+126
-131
lines changed

5 files changed

+126
-131
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.grpc.internal;
17+
18+
import java.net.Socket;
19+
import java.security.InvalidAlgorithmParameterException;
20+
import java.security.KeyStore;
21+
import java.security.KeyStoreException;
22+
import java.security.Provider;
23+
import java.security.cert.CertificateException;
24+
import java.security.cert.X509Certificate;
25+
26+
import javax.net.ssl.ManagerFactoryParameters;
27+
import javax.net.ssl.SSLEngine;
28+
import javax.net.ssl.TrustManager;
29+
import javax.net.ssl.TrustManagerFactory;
30+
import javax.net.ssl.TrustManagerFactorySpi;
31+
import javax.net.ssl.X509ExtendedTrustManager;
32+
import javax.net.ssl.X509TrustManager;
33+
34+
/**
35+
* A custom implementation of the TrustManagerFactory class that provides an insecure
36+
* trust manager. This trust manager does not perform any certificate validation and
37+
* accepts all certificates. It is intended for testing or development purposes only and
38+
* should not be used in production environments.
39+
*/
40+
public class InsecureTrustManagerFactory extends TrustManagerFactory {
41+
42+
public static final TrustManagerFactory INSTANCE = new InsecureTrustManagerFactory();
43+
44+
private static final Provider provider = new Provider("", "0.0", "") {
45+
private static final long serialVersionUID = -2680540247105807895L;
46+
47+
};
48+
49+
protected InsecureTrustManagerFactory() {
50+
super(new SimpleTrustManagerFactorySpi(), provider, "");
51+
}
52+
53+
private final static class InsecureTrustManager extends X509ExtendedTrustManager {
54+
55+
static final InsecureTrustManager INSTANCE = new InsecureTrustManager();
56+
57+
static final X509Certificate[] EMPTY_CERTS = new X509Certificate[] {};
58+
59+
@Override
60+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
61+
}
62+
63+
@Override
64+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
65+
}
66+
67+
@Override
68+
public X509Certificate[] getAcceptedIssuers() {
69+
return EMPTY_CERTS;
70+
}
71+
72+
@Override
73+
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket)
74+
throws CertificateException {
75+
}
76+
77+
@Override
78+
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket)
79+
throws CertificateException {
80+
}
81+
82+
@Override
83+
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine)
84+
throws CertificateException {
85+
}
86+
87+
@Override
88+
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine)
89+
throws CertificateException {
90+
}
91+
92+
}
93+
94+
private final static class SimpleTrustManagerFactorySpi extends TrustManagerFactorySpi {
95+
96+
static final TrustManager[] TRUST_ALL = new X509TrustManager[] { InsecureTrustManager.INSTANCE };
97+
98+
@Override
99+
protected void engineInit(KeyStore keyStore) throws KeyStoreException {
100+
}
101+
102+
@Override
103+
protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
104+
throws InvalidAlgorithmParameterException {
105+
}
106+
107+
@Override
108+
protected TrustManager[] engineGetTrustManagers() {
109+
return TRUST_ALL;
110+
}
111+
112+
}
113+
114+
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcChannelFactoryConfigurations.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientAutoConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838

3939
@Configuration(proxyBeanMethods = false)
4040
@EnableConfigurationProperties(GrpcClientProperties.class)
41-
@Import({ GrpcChannelFactoryConfigurations.ShadedNettyChannelFactoryConfiguration.class,
42-
GrpcChannelFactoryConfigurations.NettyChannelFactoryConfiguration.class, GrpcCodecConfiguration.class })
41+
@Import(GrpcCodecConfiguration.class)
4342
public class GrpcClientAutoConfiguration {
4443

4544
@Bean
@@ -52,6 +51,12 @@ public DefaultGrpcChannelFactory defaultGrpcChannelFactory(final List<GrpcChanne
5251
return factory;
5352
}
5453

54+
@Bean
55+
@ConditionalOnMissingBean(ChannelCredentialsProvider.class)
56+
public ChannelCredentialsProvider channelCredentialsProvider(GrpcClientProperties channels, SslBundles bundles) {
57+
return new NamedChannelCredentialsProvider(bundles, channels);
58+
}
59+
5560
@Bean
5661
public GrpcChannelConfigurer sslGrpcChannelConfigurer(GrpcClientProperties channels) {
5762
return (authority, builder) -> {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
import org.springframework.grpc.autoconfigure.client.GrpcClientProperties.NamedChannel;
2323
import org.springframework.grpc.client.ChannelCredentialsProvider;
2424
import org.springframework.grpc.client.NegotiationType;
25+
import org.springframework.grpc.internal.InsecureTrustManagerFactory;
2526

2627
import io.grpc.ChannelCredentials;
2728
import io.grpc.InsecureChannelCredentials;
2829
import io.grpc.TlsChannelCredentials;
29-
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
3030

31-
public class NettyChannelCredentialsProvider implements ChannelCredentialsProvider {
31+
public class NamedChannelCredentialsProvider implements ChannelCredentialsProvider {
3232

3333
private final GrpcClientProperties channels;
3434

3535
private final SslBundles bundles;
3636

37-
public NettyChannelCredentialsProvider(SslBundles bundles, GrpcClientProperties channels) {
37+
public NamedChannelCredentialsProvider(SslBundles bundles, GrpcClientProperties channels) {
3838
this.bundles = bundles;
3939
this.channels = channels;
4040
}
@@ -47,11 +47,11 @@ public ChannelCredentials getChannelCredentials(String path) {
4747
return InsecureChannelCredentials.create();
4848
}
4949
if (bundle != null) {
50-
TrustManagerFactory trustManager = channel.isSecure() ? bundle.getManagers().getTrustManagerFactory()
50+
TrustManagerFactory trustManagers = channel.isSecure() ? bundle.getManagers().getTrustManagerFactory()
5151
: InsecureTrustManagerFactory.INSTANCE;
5252
return TlsChannelCredentials.newBuilder()
5353
.keyManager(bundle.getManagers().getKeyManagerFactory().getKeyManagers())
54-
.trustManager(trustManager.getTrustManagers())
54+
.trustManager(trustManagers.getTrustManagers())
5555
.build();
5656
}
5757
else {

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ShadedNettyChannelCredentialsProvider.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)