001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020
021package org.apache.hadoop.hbase.util;
022
023import java.nio.ByteBuffer;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.SortedMap;
027import java.util.TreeMap;
028import javax.security.auth.Subject;
029import javax.security.auth.login.AppConfigurationEntry;
030import javax.security.auth.login.Configuration;
031import javax.security.auth.login.LoginContext;
032import javax.security.auth.login.LoginException;
033
034import org.apache.commons.lang3.StringUtils;
035import org.apache.hadoop.hbase.thrift.generated.TCell;
036import org.apache.hadoop.hbase.thrift.generated.TRowResult;
037import org.apache.yetus.audience.InterfaceAudience;
038
039/**
040 * Common Utility class for clients
041 */
042@InterfaceAudience.Private
043public final class ClientUtils {
044
045  private ClientUtils() {
046    // Empty block
047  }
048
049  /**
050   * To authenticate the demo client, kinit should be invoked ahead. Here we try to get the
051   * Kerberos credential from the ticket cache
052   *
053   * @return LoginContext Object
054   * @throws LoginException Exception thrown if unable to get LoginContext
055   */
056  public static LoginContext getLoginContext() throws LoginException {
057
058    return new LoginContext(StringUtils.EMPTY, new Subject(), null, new Configuration() {
059      @Override
060      public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
061        Map<String, String> options = new HashMap<>();
062        options.put("useKeyTab", "false");
063        options.put("storeKey", "false");
064        options.put("doNotPrompt", "true");
065        options.put("useTicketCache", "true");
066        options.put("renewTGT", "true");
067        options.put("refreshKrb5Config", "true");
068        options.put("isInitiator", "true");
069        String ticketCache = System.getenv("KRB5CCNAME");
070        if (ticketCache != null) {
071          options.put("ticketCache", ticketCache);
072        }
073        options.put("debug", "true");
074
075        return new AppConfigurationEntry[]{new AppConfigurationEntry(
076            "com.sun.security.auth.module.Krb5LoginModule",
077            AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options)};
078      }
079    });
080
081  }
082
083  /**
084   * copy values into a TreeMap to get them in sorted order and print it
085   *
086   * @param rowResult Holds row name and then a map of columns to cells
087   */
088  public static void printRow(final TRowResult rowResult) {
089
090    TreeMap<String, TCell> sorted = new TreeMap<>();
091    for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
092      sorted.put(utf8(column.getKey().array()), column.getValue());
093    }
094
095    StringBuilder rowStr = new StringBuilder();
096    for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
097      rowStr.append(entry.getKey());
098      rowStr.append(" => ");
099      rowStr.append(utf8(entry.getValue().value.array()));
100      rowStr.append("; ");
101    }
102    System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
103
104  }
105
106  /**
107   * Helper to translate byte[]'s to UTF8 strings
108   *
109   * @param buf byte array buffer
110   * @return UTF8 decoded string value
111   */
112  public static String utf8(final byte[] buf) {
113    try {
114      return Bytes.toString(buf);
115    } catch (IllegalArgumentException e) {
116      return "[INVALID UTF-8]";
117    }
118  }
119
120}