Skip to content

Commit 652a78a

Browse files
authored
Support pass SSLSession in Invocation to check permission (#15049)
1 parent ef7e02f commit 652a78a

12 files changed

Lines changed: 82 additions & 3 deletions

File tree

dubbo-common/src/main/java/org/apache/dubbo/common/ssl/CertManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public CertManager(FrameworkModel frameworkModel) {
3232

3333
public ProviderCert getProviderConnectionConfig(URL localAddress, SocketAddress remoteAddress) {
3434
for (CertProvider certProvider : certProviders) {
35-
if (certProvider.isSupport(localAddress)) {
36-
ProviderCert cert = certProvider.getProviderConnectionConfig(localAddress);
35+
if (certProvider.isSupport(localAddress, remoteAddress)) {
36+
ProviderCert cert = certProvider.getProviderConnectionConfig(localAddress, remoteAddress);
3737
if (cert != null) {
3838
return cert;
3939
}

dubbo-common/src/main/java/org/apache/dubbo/common/ssl/CertProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@
2020
import org.apache.dubbo.common.extension.ExtensionScope;
2121
import org.apache.dubbo.common.extension.SPI;
2222

23+
import java.net.SocketAddress;
24+
2325
@SPI(scope = ExtensionScope.FRAMEWORK)
2426
public interface CertProvider {
2527
boolean isSupport(URL address);
2628

29+
default boolean isSupport(URL address, SocketAddress remoteAddress) {
30+
return isSupport(address);
31+
}
32+
2733
ProviderCert getProviderConnectionConfig(URL localAddress);
2834

35+
default ProviderCert getProviderConnectionConfig(URL localAddress, SocketAddress remoteAddress) {
36+
return getProviderConnectionConfig(localAddress);
37+
}
38+
2939
Cert getConsumerConnectionConfig(URL remoteAddress);
3040
}

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public interface Constants {
175175
String APACHE_HTTP_CLIENT = "apache-http-client";
176176

177177
String CONTENT_LENGTH_KEY = "content-length";
178+
String SSL_SESSION_KEY = "ssl-session";
178179

179180
String USE_SECURE_RANDOM_ID = "dubbo.application.use-secure-random-request-id";
180181

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.dubbo.common.ssl.CertManager;
2424
import org.apache.dubbo.common.ssl.ProviderCert;
2525
import org.apache.dubbo.remoting.ChannelHandler;
26+
import org.apache.dubbo.remoting.Constants;
2627
import org.apache.dubbo.remoting.api.ProtocolDetector;
2728
import org.apache.dubbo.remoting.api.WireProtocol;
2829
import org.apache.dubbo.remoting.buffer.ChannelBuffer;
@@ -42,6 +43,7 @@
4243
import io.netty.handler.ssl.SslContext;
4344
import io.netty.handler.ssl.SslHandler;
4445
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
46+
import io.netty.util.AttributeKey;
4547

4648
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
4749

@@ -55,6 +57,7 @@ public class NettyPortUnificationServerHandler extends ByteToMessageDecoder {
5557
private final Map<String, WireProtocol> protocols;
5658
private final Map<String, URL> urlMapper;
5759
private final Map<String, ChannelHandler> handlerMapper;
60+
private static final AttributeKey<SSLSession> SSL_SESSION_KEY = AttributeKey.valueOf(Constants.SSL_SESSION_KEY);
5861

5962
public NettyPortUnificationServerHandler(
6063
URL url,
@@ -89,6 +92,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
8992
SSLSession session =
9093
ctx.pipeline().get(SslHandler.class).engine().getSession();
9194
LOGGER.info("TLS negotiation succeed with session: " + session);
95+
ctx.channel().attr(SSL_SESSION_KEY).set(session);
9296
} else {
9397
LOGGER.error(
9498
INTERNAL_ERROR,

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.apache.dubbo.common.utils.NetUtils;
2323
import org.apache.dubbo.remoting.Channel;
2424
import org.apache.dubbo.remoting.ChannelHandler;
25+
import org.apache.dubbo.remoting.Constants;
26+
27+
import javax.net.ssl.SSLSession;
2528

2629
import java.net.InetSocketAddress;
2730
import java.util.Map;
@@ -30,7 +33,9 @@
3033
import io.netty.channel.ChannelDuplexHandler;
3134
import io.netty.channel.ChannelHandlerContext;
3235
import io.netty.channel.ChannelPromise;
36+
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
3337
import io.netty.handler.timeout.IdleStateEvent;
38+
import io.netty.util.AttributeKey;
3439

3540
/**
3641
* NettyServerHandler.
@@ -44,6 +49,8 @@ public class NettyServerHandler extends ChannelDuplexHandler {
4449
*/
4550
private final Map<String, Channel> channels = new ConcurrentHashMap<>();
4651

52+
private static final AttributeKey<SSLSession> SSL_SESSION_KEY = AttributeKey.valueOf(Constants.SSL_SESSION_KEY);
53+
4754
private final URL url;
4855

4956
private final ChannelHandler handler;
@@ -123,6 +130,15 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
123130
}
124131
}
125132
super.userEventTriggered(ctx, evt);
133+
if (evt instanceof SslHandshakeCompletionEvent) {
134+
SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
135+
if (handshakeEvent.isSuccess()) {
136+
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
137+
channel.setAttribute(
138+
Constants.SSL_SESSION_KEY,
139+
ctx.channel().attr(SSL_SESSION_KEY).get());
140+
}
141+
}
126142
}
127143

128144
@Override

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/ssl/SslClientTlsHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2121
import org.apache.dubbo.common.logger.LoggerFactory;
22+
import org.apache.dubbo.remoting.Constants;
2223

2324
import javax.net.ssl.SSLEngine;
2425
import javax.net.ssl.SSLSession;
@@ -28,13 +29,14 @@
2829
import io.netty.handler.ssl.SslContext;
2930
import io.netty.handler.ssl.SslHandler;
3031
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
32+
import io.netty.util.AttributeKey;
3133

3234
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
3335

3436
public class SslClientTlsHandler extends ChannelInboundHandlerAdapter {
3537

3638
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(SslClientTlsHandler.class);
37-
39+
private static final AttributeKey<SSLSession> SSL_SESSION_KEY = AttributeKey.valueOf(Constants.SSL_SESSION_KEY);
3840
private final SslContext sslContext;
3941

4042
public SslClientTlsHandler(URL url) {
@@ -60,6 +62,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
6062
ctx.pipeline().get(SslHandler.class).engine().getSession();
6163
logger.info("TLS negotiation succeed with: " + session.getPeerHost());
6264
ctx.pipeline().remove(this);
65+
ctx.channel().attr(SSL_SESSION_KEY).set(session);
6366
} else {
6467
logger.error(
6568
INTERNAL_ERROR,

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/ssl/SslServerTlsHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.dubbo.common.ssl.AuthPolicy;
2323
import org.apache.dubbo.common.ssl.CertManager;
2424
import org.apache.dubbo.common.ssl.ProviderCert;
25+
import org.apache.dubbo.remoting.Constants;
2526

2627
import javax.net.ssl.SSLSession;
2728

@@ -34,6 +35,7 @@
3435
import io.netty.handler.ssl.SslContext;
3536
import io.netty.handler.ssl.SslHandler;
3637
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
38+
import io.netty.util.AttributeKey;
3739

3840
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
3941

@@ -43,6 +45,7 @@ public class SslServerTlsHandler extends ByteToMessageDecoder {
4345
private final URL url;
4446

4547
private final boolean sslDetected;
48+
private static final AttributeKey<SSLSession> SSL_SESSION_KEY = AttributeKey.valueOf(Constants.SSL_SESSION_KEY);
4649

4750
public SslServerTlsHandler(URL url) {
4851
this.url = url;
@@ -74,6 +77,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
7477
logger.info("TLS negotiation succeed with: " + session.getPeerHost());
7578
// Remove after handshake success.
7679
ctx.pipeline().remove(this);
80+
ctx.channel().attr(SSL_SESSION_KEY).set(session);
7781
} else {
7882
logger.error(
7983
INTERNAL_ERROR,

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public void encode(Channel channel, OutputStream output, Object message) throws
124124
public Object decode(Channel channel, InputStream input) throws IOException {
125125
int contentLength = input.available();
126126
getAttributes().put(Constants.CONTENT_LENGTH_KEY, contentLength);
127+
Object sslSession = channel.getAttribute(Constants.SSL_SESSION_KEY);
128+
if (null != sslSession) {
129+
put(Constants.SSL_SESSION_KEY, sslSession);
130+
}
127131

128132
ObjectInput in = CodecSupport.getSerialization(serializationType).deserialize(channel.getUrl(), input);
129133
this.put(SERIALIZATION_ID_KEY, serializationType);

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCall.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2222
import org.apache.dubbo.common.logger.LoggerFactory;
2323
import org.apache.dubbo.common.utils.StringUtils;
24+
import org.apache.dubbo.remoting.Constants;
2425
import org.apache.dubbo.rpc.CancellationContext;
2526
import org.apache.dubbo.rpc.Invoker;
2627
import org.apache.dubbo.rpc.RpcContext;
@@ -39,6 +40,8 @@
3940
import org.apache.dubbo.rpc.protocol.tri.stream.ServerStream;
4041
import org.apache.dubbo.rpc.protocol.tri.stream.StreamUtils;
4142

43+
import javax.net.ssl.SSLSession;
44+
4245
import java.util.Map;
4346
import java.util.Objects;
4447
import java.util.concurrent.Executor;
@@ -264,6 +267,10 @@ protected RpcInvocation buildInvocation(MethodDescriptor methodDescriptor) {
264267
inv.setReturnTypes(methodDescriptor.getReturnTypes());
265268
inv.setObjectAttachments(StreamUtils.toAttachments(requestMetadata));
266269
inv.put(REMOTE_ADDRESS_KEY, stream.remoteAddress());
270+
SSLSession sslSession = stream.getSslSession();
271+
if (null != sslSession) {
272+
inv.put(Constants.SSL_SESSION_KEY, sslSession);
273+
}
267274
// handle timeout
268275
String timeout = (String) requestMetadata.get(TripleHeaderEnum.TIMEOUT.getHeader());
269276
try {

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/Stream.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.apache.dubbo.rpc.TriRpcStatus;
2020

21+
import javax.net.ssl.SSLSession;
22+
2123
import java.net.SocketAddress;
2224

2325
import io.netty.handler.codec.http2.Http2Headers;
@@ -74,6 +76,13 @@ interface Listener {
7476
*/
7577
SocketAddress remoteAddress();
7678

79+
/**
80+
* Get ssl session.
81+
*
82+
* @return ssl session
83+
*/
84+
SSLSession getSslSession();
85+
7786
/**
7887
* Request n message from remote peer.
7988
*

0 commit comments

Comments
 (0)