Skip to content

Commit

Permalink
HBASE-22743 : ClientUtils for Demo Client classes (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
virajjasani authored and openinx committed Jul 28, 2019
1 parent 3318b4b commit 064f5f1
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
package org.apache.hadoop.hbase.rest;

import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;

import org.apache.hadoop.hbase.Cell;
Expand All @@ -37,6 +33,7 @@
import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.RemoteHTable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClientUtils;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;

Expand Down Expand Up @@ -112,32 +109,7 @@ static Subject getSubject() throws Exception {
return new Subject();
}

/*
* To authenticate the demo client, kinit should be invoked ahead. Here we try to get the
* Kerberos credential from the ticket cache.
*/
LoginContext context = new LoginContext("", new Subject(), null, new Configuration() {
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
Map<String, String> options = new HashMap<>();
options.put("useKeyTab", "false");
options.put("storeKey", "false");
options.put("doNotPrompt", "true");
options.put("useTicketCache", "true");
options.put("renewTGT", "true");
options.put("refreshKrb5Config", "true");
options.put("isInitiator", "true");
String ticketCache = System.getenv("KRB5CCNAME");
if (ticketCache != null) {
options.put("ticketCache", ticketCache);
}
options.put("debug", "true");

return new AppConfigurationEntry[] {
new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options) };
}
});
LoginContext context = ClientUtils.getLoginContext();
context.login();
return context.getSubject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@
package org.apache.hadoop.hbase.thrift;

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.security.PrivilegedExceptionAction;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.security.auth.Subject;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import javax.security.sasl.Sasl;
import org.apache.hadoop.hbase.thrift.generated.AlreadyExists;
Expand All @@ -42,6 +35,7 @@
import org.apache.hadoop.hbase.thrift.generated.TCell;
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClientUtils;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSaslClientTransport;
Expand All @@ -60,7 +54,6 @@ public class DemoClient {

static protected int port;
static protected String host;
CharsetDecoder decoder = null;

private static boolean secure = false;
private static String serverPrincipal = "hbase";
Expand Down Expand Up @@ -101,16 +94,6 @@ private static boolean isBoolean(String s){
}

DemoClient() {
decoder = Charset.forName("UTF-8").newDecoder();
}

// Helper to translate byte[]'s to UTF8 strings
private String utf8(byte[] buf) {
try {
return decoder.decode(ByteBuffer.wrap(buf)).toString();
} catch (CharacterCodingException e) {
return "[INVALID UTF-8]";
}
}

// Helper to translate strings to UTF8 bytes
Expand Down Expand Up @@ -146,15 +129,15 @@ private void run() throws Exception {
System.out.println("scanning tables...");

for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + utf8(name.array()));
System.out.println(" found: " + ClientUtils.utf8(name.array()));

if (utf8(name.array()).equals(utf8(t))) {
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array()));
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
client.disableTable(name);
}

System.out.println(" deleting table: " + utf8(name.array()));
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
client.deleteTable(name);
}
}
Expand All @@ -172,19 +155,20 @@ private void run() throws Exception {
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);

System.out.println("creating table: " + utf8(t));
System.out.println("creating table: " + ClientUtils.utf8(t));

try {
client.createTable(ByteBuffer.wrap(t), columns);
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}

System.out.println("column families in " + utf8(t) + ": ");
System.out.println("column families in " + ClientUtils.utf8(t) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));

for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
+ col2.maxVersions);
}

Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
Expand Down Expand Up @@ -358,31 +342,15 @@ private void printVersions(ByteBuffer row, List<TCell> versions) {
StringBuilder rowStr = new StringBuilder();

for (TCell cell : versions) {
rowStr.append(utf8(cell.value.array()));
rowStr.append(ClientUtils.utf8(cell.value.array()));
rowStr.append("; ");
}

System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
}

private void printRow(TRowResult rowResult) {
// copy values into a TreeMap to get them in sorted order
TreeMap<String, TCell> sorted = new TreeMap<>();

for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
sorted.put(utf8(column.getKey().array()), column.getValue());
}

StringBuilder rowStr = new StringBuilder();

for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
rowStr.append(entry.getKey());
rowStr.append(" => ");
rowStr.append(utf8(entry.getValue().value.array()));
rowStr.append("; ");
}

System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
ClientUtils.printRow(rowResult);
}

private void printRow(List<TRowResult> rows) {
Expand All @@ -396,37 +364,7 @@ static Subject getSubject() throws Exception {
return new Subject();
}

/*
* To authenticate the DemoClient, kinit should be invoked ahead.
* Here we try to get the Kerberos credential from the ticket cache.
*/
LoginContext context = new LoginContext("", new Subject(), null,
new Configuration() {
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
Map<String, String> options = new HashMap<>();
options.put("useKeyTab", "false");
options.put("storeKey", "false");
options.put("doNotPrompt", "true");
options.put("useTicketCache", "true");
options.put("renewTGT", "true");
options.put("refreshKrb5Config", "true");
options.put("isInitiator", "true");
String ticketCache = System.getenv("KRB5CCNAME");

if (ticketCache != null) {
options.put("ticketCache", ticketCache);
}

options.put("debug", "true");

return new AppConfigurationEntry[]{
new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
options)};
}
});

LoginContext context = ClientUtils.getLoginContext();
context.login();
return context.getSubject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.security.Principal;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
Expand All @@ -32,8 +29,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.AppConfigurationEntry;
Expand All @@ -45,6 +40,7 @@
import org.apache.hadoop.hbase.thrift.generated.TCell;
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClientUtils;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
Expand All @@ -69,7 +65,6 @@ public class HttpDoAsClient {

static protected int port;
static protected String host;
CharsetDecoder decoder = null;
private static boolean secure = false;
static protected String doAsUser = null;
static protected String principal = null;
Expand Down Expand Up @@ -112,16 +107,6 @@ public Void run() throws Exception {
}

HttpDoAsClient() {
decoder = Charset.forName("UTF-8").newDecoder();
}

// Helper to translate byte[]'s to UTF8 strings
private String utf8(byte[] buf) {
try {
return decoder.decode(ByteBuffer.wrap(buf)).toString();
} catch (CharacterCodingException e) {
return "[INVALID UTF-8]";
}
}

// Helper to translate strings to UTF8 bytes
Expand All @@ -146,13 +131,13 @@ private void run() throws Exception {
//
System.out.println("scanning tables...");
for (ByteBuffer name : refresh(client, httpClient).getTableNames()) {
System.out.println(" found: " + utf8(name.array()));
if (utf8(name.array()).equals(utf8(t))) {
System.out.println(" found: " + ClientUtils.utf8(name.array()));
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
if (refresh(client, httpClient).isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array()));
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
refresh(client, httpClient).disableTable(name);
}
System.out.println(" deleting table: " + utf8(name.array()));
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
refresh(client, httpClient).deleteTable(name);
}
}
Expand All @@ -172,19 +157,20 @@ private void run() throws Exception {
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);

System.out.println("creating table: " + utf8(t));
System.out.println("creating table: " + ClientUtils.utf8(t));
try {

refresh(client, httpClient).createTable(ByteBuffer.wrap(t), columns);
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}

System.out.println("column families in " + utf8(t) + ": ");
System.out.println("column families in " + ClientUtils.utf8(t) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = refresh(client, httpClient)
.getColumnDescriptors(ByteBuffer.wrap(t));
for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
+ col2.maxVersions);
}

transport.close();
Expand Down Expand Up @@ -236,27 +222,14 @@ private String generateTicket() throws GSSException {
private void printVersions(ByteBuffer row, List<TCell> versions) {
StringBuilder rowStr = new StringBuilder();
for (TCell cell : versions) {
rowStr.append(utf8(cell.value.array()));
rowStr.append(ClientUtils.utf8(cell.value.array()));
rowStr.append("; ");
}
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
}

private void printRow(TRowResult rowResult) {
// copy values into a TreeMap to get them in sorted order
TreeMap<String, TCell> sorted = new TreeMap<>();
for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
sorted.put(utf8(column.getKey().array()), column.getValue());
}

StringBuilder rowStr = new StringBuilder();
for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
rowStr.append(entry.getKey());
rowStr.append(" => ");
rowStr.append(utf8(entry.getValue().value.array()));
rowStr.append("; ");
}
System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
ClientUtils.printRow(rowResult);
}

static Subject getSubject() throws Exception {
Expand Down
Loading

0 comments on commit 064f5f1

Please sign in to comment.