Skip to content

Commit c2dff39

Browse files
committed
HBASE-27279 Make SslHandler work with SaslWrapHandler/SaslUnwrapHandler (#4705)
Signed-off-by: Bryan Beaudreault <[email protected]> Reviewed-by: Andor Molnár <[email protected]> (cherry picked from commit 2b9d368) Conflicts: hbase-server/src/test/java/org/apache/hadoop/hbase/security/TestNettyTlsIPCRejectPlainText.java hbase-server/src/test/java/org/apache/hadoop/hbase/security/TestSecureIPC.java hbase-server/src/test/java/org/apache/hadoop/hbase/security/TestTlsWithKerberos.java
1 parent 3acf920 commit c2dff39

File tree

15 files changed

+817
-717
lines changed

15 files changed

+817
-717
lines changed

hbase-asyncfs/src/test/java/org/apache/hadoop/hbase/security/HBaseKerberosUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,13 @@ public static UserGroupInformation loginAndReturnUGI(Configuration conf, String
188188
UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, keyTabFileLocation);
189189
return ugi;
190190
}
191+
192+
public static UserGroupInformation loginKerberosPrincipal(String krbKeytab, String krbPrincipal)
193+
throws Exception {
194+
Configuration conf = new Configuration();
195+
conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
196+
UserGroupInformation.setConfiguration(conf);
197+
UserGroupInformation.loginUserFromKeytab(krbPrincipal, krbKeytab);
198+
return UserGroupInformation.getLoginUser();
199+
}
191200
}

hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/NettyRpcConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ private void saslNegotiate(final Channel ch) {
222222
return;
223223
}
224224
ch.pipeline().addBefore(BufferCallBeforeInitHandler.NAME, null, new SaslChallengeDecoder())
225-
.addBefore(BufferCallBeforeInitHandler.NAME, null, saslHandler);
225+
.addBefore(BufferCallBeforeInitHandler.NAME, NettyHBaseSaslRpcClientHandler.HANDLER_NAME,
226+
saslHandler);
226227
NettyFutureUtils.addListener(saslPromise, new FutureListener<Boolean>() {
227228

228229
@Override

hbase-client/src/main/java/org/apache/hadoop/hbase/security/NettyHBaseSaslRpcClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public NettyHBaseSaslRpcClient(Configuration conf, SaslClientAuthenticationProvi
4545
super(conf, provider, token, serverAddr, securityInfo, fallbackAllowed, rpcProtection);
4646
}
4747

48-
public void setupSaslHandler(ChannelPipeline p) {
48+
public void setupSaslHandler(ChannelPipeline p, String addAfter) {
4949
String qop = (String) saslClient.getNegotiatedProperty(Sasl.QOP);
5050
LOG.trace("SASL client context established. Negotiated QoP {}", qop);
5151
if (qop == null || "auth".equalsIgnoreCase(qop)) {
5252
return;
5353
}
5454
// add wrap and unwrap handlers to pipeline.
55-
p.addFirst(new SaslWrapHandler(saslClient::wrap),
56-
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
57-
new SaslUnwrapHandler(saslClient::unwrap));
55+
p.addAfter(addAfter, null, new SaslUnwrapHandler(saslClient::unwrap))
56+
.addAfter(addAfter, null, new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
57+
.addAfter(addAfter, null, new SaslWrapHandler(saslClient::wrap));
5858
}
5959

6060
public String getSaslQOP() {

hbase-client/src/main/java/org/apache/hadoop/hbase/security/NettyHBaseSaslRpcClientHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class NettyHBaseSaslRpcClientHandler extends SimpleChannelInboundHandler<
4747

4848
private static final Logger LOG = LoggerFactory.getLogger(NettyHBaseSaslRpcClientHandler.class);
4949

50+
public static final String HANDLER_NAME = "SaslRpcClientHandler";
51+
5052
private final Promise<Boolean> saslPromise;
5153

5254
private final UserGroupInformation ugi;
@@ -86,7 +88,7 @@ private void tryComplete(ChannelHandlerContext ctx) {
8688
}
8789

8890
ChannelPipeline p = ctx.pipeline();
89-
saslRpcClient.setupSaslHandler(p);
91+
saslRpcClient.setupSaslHandler(p, HANDLER_NAME);
9092
p.remove(SaslChallengeDecoder.class);
9193
p.remove(this);
9294

hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/NettyHBaseSaslRpcServerHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Excep
8989
boolean useWrap = qop != null && !"auth".equalsIgnoreCase(qop);
9090
ChannelPipeline p = ctx.pipeline();
9191
if (useWrap) {
92-
p.addFirst(new SaslWrapHandler(saslServer::wrap));
93-
p.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
92+
p.addBefore(DECODER_NAME, null, new SaslWrapHandler(saslServer::wrap)).addLast(
93+
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
9494
new SaslUnwrapHandler(saslServer::unwrap));
9595
}
96-
conn.setupDecoder();
96+
conn.setupHandler();
9797
p.remove(this);
9898
p.remove(DECODER_NAME);
9999
}

hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/NettyRpcServer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ protected void initChannel(Channel ch) throws Exception {
124124
if (conf.getBoolean(HBASE_SERVER_NETTY_TLS_ENABLED, false)) {
125125
initSSL(pipeline, conf.getBoolean(HBASE_SERVER_NETTY_TLS_SUPPORTPLAINTEXT, true));
126126
}
127-
pipeline.addLast(NettyRpcServerPreambleHandler.DECODER_NAME, preambleDecoder);
128-
pipeline.addLast(createNettyRpcServerPreambleHandler(),
129-
new NettyRpcServerResponseEncoder(metrics));
127+
pipeline.addLast(NettyRpcServerPreambleHandler.DECODER_NAME, preambleDecoder)
128+
.addLast(createNettyRpcServerPreambleHandler());
130129
}
131130
});
132131
try {

hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/NettyRpcServerPreambleHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Excep
6161
p.addLast(NettyHBaseSaslRpcServerHandler.DECODER_NAME, decoder);
6262
p.addLast(new NettyHBaseSaslRpcServerHandler(rpcServer, conn));
6363
} else {
64-
conn.setupDecoder();
64+
conn.setupHandler();
6565
}
6666
// add first and then remove, so the single decode decoder will pass the remaining bytes to the
6767
// handler above.

hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/NettyServerRpcConnection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.hbase.thirdparty.com.google.protobuf.Message;
3434
import org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf;
3535
import org.apache.hbase.thirdparty.io.netty.channel.Channel;
36-
import org.apache.hbase.thirdparty.io.netty.channel.ChannelPipeline;
3736

3837
import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader;
3938

@@ -70,10 +69,11 @@ class NettyServerRpcConnection extends ServerRpcConnection {
7069
this.remotePort = inetSocketAddress.getPort();
7170
}
7271

73-
void setupDecoder() {
74-
ChannelPipeline p = channel.pipeline();
75-
p.addLast("frameDecoder", new NettyRpcFrameDecoder(rpcServer.maxRequestSize, this));
76-
p.addLast("decoder", new NettyRpcServerRequestDecoder(rpcServer.metrics, this));
72+
void setupHandler() {
73+
channel.pipeline()
74+
.addLast("frameDecoder", new NettyRpcFrameDecoder(rpcServer.maxRequestSize, this))
75+
.addLast("decoder", new NettyRpcServerRequestDecoder(rpcServer.metrics, this))
76+
.addLast("encoder", new NettyRpcServerResponseEncoder(rpcServer.metrics));
7777
}
7878

7979
void process(ByteBuf buf) throws IOException, InterruptedException {

0 commit comments

Comments
 (0)