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 java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.ipc.RemoteException;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf;
027import org.apache.hbase.thirdparty.io.netty.channel.ChannelHandlerContext;
028import org.apache.hbase.thirdparty.io.netty.handler.codec.ByteToMessageDecoder;
029
030/**
031 * Decode the sasl challenge sent by RpcServer.
032 */
033@InterfaceAudience.Private
034public class SaslChallengeDecoder extends ByteToMessageDecoder {
035
036  private static final int MAX_CHALLENGE_SIZE = 1024 * 1024; // 1M
037
038  private ByteBuf tryDecodeChallenge(ByteBuf in, int offset, int readableBytes) throws IOException {
039    if (readableBytes < 4) {
040      return null;
041    }
042    int len = in.getInt(offset);
043    if (len <= 0) {
044      // fall back to simple
045      in.readerIndex(offset + 4);
046      return in.retainedSlice(offset, 4);
047    }
048    if (len > MAX_CHALLENGE_SIZE) {
049      throw new IOException(
050        "Sasl challenge too large(" + len + "), max allowed is " + MAX_CHALLENGE_SIZE);
051    }
052    int totalLen = 4 + len;
053    if (readableBytes < totalLen) {
054      return null;
055    }
056    in.readerIndex(offset + totalLen);
057    return in.retainedSlice(offset, totalLen);
058  }
059
060  // will throw a RemoteException out if data is enough, so do not need to return anything.
061  private void tryDecodeError(ByteBuf in, int offset, int readableBytes) throws IOException {
062    if (readableBytes < 4) {
063      return;
064    }
065    int classLen = in.getInt(offset);
066    if (classLen <= 0) {
067      throw new IOException("Invalid exception class name length " + classLen);
068    }
069    if (classLen > MAX_CHALLENGE_SIZE) {
070      throw new IOException("Exception class name length too large(" + classLen
071        + "), max allowed is " + MAX_CHALLENGE_SIZE);
072    }
073    if (readableBytes < 4 + classLen + 4) {
074      return;
075    }
076    int msgLen = in.getInt(offset + 4 + classLen);
077    if (msgLen <= 0) {
078      throw new IOException("Invalid exception message length " + msgLen);
079    }
080    if (msgLen > MAX_CHALLENGE_SIZE) {
081      throw new IOException(
082        "Exception message length too large(" + msgLen + "), max allowed is " + MAX_CHALLENGE_SIZE);
083    }
084    int totalLen = classLen + msgLen + 8;
085    if (readableBytes < totalLen) {
086      return;
087    }
088    String className = in.toString(offset + 4, classLen, HConstants.UTF8_CHARSET);
089    String msg = in.toString(offset + classLen + 8, msgLen, HConstants.UTF8_CHARSET);
090    in.readerIndex(offset + totalLen);
091    throw new RemoteException(className, msg);
092  }
093
094  @Override
095  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
096    int readableBytes = in.readableBytes();
097    if (readableBytes < 4) {
098      return;
099    }
100    int offset = in.readerIndex();
101    int status = in.getInt(offset);
102    if (status == SaslStatus.SUCCESS.state) {
103      ByteBuf challenge = tryDecodeChallenge(in, offset + 4, readableBytes - 4);
104      if (challenge != null) {
105        out.add(challenge);
106      }
107    } else {
108      tryDecodeError(in, offset + 4, readableBytes - 4);
109    }
110  }
111}