001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.security; 019 020import org.apache.hbase.thirdparty.io.netty.channel.ChannelPipeline; 021import org.apache.hbase.thirdparty.io.netty.handler.codec.LengthFieldBasedFrameDecoder; 022 023import java.io.IOException; 024import java.net.InetAddress; 025 026import javax.security.sasl.Sasl; 027 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProvider; 030import org.apache.hadoop.security.token.Token; 031import org.apache.hadoop.security.token.TokenIdentifier; 032import org.apache.yetus.audience.InterfaceAudience; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036/** 037 * Implement SASL logic for netty rpc client. 038 * @since 2.0.0 039 */ 040@InterfaceAudience.Private 041public class NettyHBaseSaslRpcClient extends AbstractHBaseSaslRpcClient { 042 private static final Logger LOG = LoggerFactory.getLogger(NettyHBaseSaslRpcClient.class); 043 044 public NettyHBaseSaslRpcClient(Configuration conf, SaslClientAuthenticationProvider provider, 045 Token<? extends TokenIdentifier> token, InetAddress serverAddr, SecurityInfo securityInfo, 046 boolean fallbackAllowed, String rpcProtection) throws IOException { 047 super(conf, provider, token, serverAddr, securityInfo, fallbackAllowed, rpcProtection); 048 } 049 050 public void setupSaslHandler(ChannelPipeline p) { 051 String qop = (String) saslClient.getNegotiatedProperty(Sasl.QOP); 052 LOG.trace("SASL client context established. Negotiated QoP {}", qop); 053 if (qop == null || "auth".equalsIgnoreCase(qop)) { 054 return; 055 } 056 // add wrap and unwrap handlers to pipeline. 057 p.addFirst(new SaslWrapHandler(saslClient), 058 new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4), 059 new SaslUnwrapHandler(saslClient)); 060 } 061 062 public String getSaslQOP() { 063 return (String) saslClient.getNegotiatedProperty(Sasl.QOP); 064 } 065}