HBASE-18387: [Thrift] Make principal configurable in DemoClient.java

Added optional (fourth) parameter "server-principal"
The solution is backward compatible, in case not given, uses "hbase" as default value
If the third parameter is skipped the fourth cannot be set.

Signed-off-by: Josh Elser <elserj@apache.org>
This commit is contained in:
Tamas Penzes 2017-08-08 13:45:09 +02:00 committed by Josh Elser
parent 8197a31bbc
commit ee15c2c296
2 changed files with 14 additions and 4 deletions

View File

@ -28,7 +28,8 @@ Example code.
2. If HBase server is not secure, or authentication is not enabled for the Thrift server, execute: 2. If HBase server is not secure, or authentication is not enabled for the Thrift server, execute:
{java -cp hbase-examples-[VERSION].jar:${HBASE_EXAMPLE_CLASSPATH} org.apache.hadoop.hbase.thrift.DemoClient <host> <port>} {java -cp hbase-examples-[VERSION].jar:${HBASE_EXAMPLE_CLASSPATH} org.apache.hadoop.hbase.thrift.DemoClient <host> <port>}
3. If HBase server is secure, and authentication is enabled for the Thrift server, run kinit at first, then execute: 3. If HBase server is secure, and authentication is enabled for the Thrift server, run kinit at first, then execute:
{java -cp hbase-examples-[VERSION].jar:${HBASE_EXAMPLE_CLASSPATH} org.apache.hadoop.hbase.thrift.DemoClient <host> <port> true} {java -cp hbase-examples-[VERSION].jar:${HBASE_EXAMPLE_CLASSPATH} org.apache.hadoop.hbase.thrift.DemoClient <host> <port> true <server-principal>}
<server-principal> should only be specified when the client connects to a secure cluster. It's default value is "hbase".
4. Here is a lazy example that just pulls in all hbase dependency jars and that goes against default location on localhost. 4. Here is a lazy example that just pulls in all hbase dependency jars and that goes against default location on localhost.
It should work with a standalone hbase instance started by doing ./bin/start-hbase.sh: It should work with a standalone hbase instance started by doing ./bin/start-hbase.sh:
{java -cp ./hbase-examples/target/hbase-examples-2.0.0-SNAPSHOT.jar:`./bin/hbase classpath` org.apache.hadoop.hbase.thrift.DemoClient localhost 9090} {java -cp ./hbase-examples/target/hbase-examples-2.0.0-SNAPSHOT.jar:`./bin/hbase classpath` org.apache.hadoop.hbase.thrift.DemoClient localhost 9090}

View File

@ -60,13 +60,14 @@ public class DemoClient {
CharsetDecoder decoder = null; CharsetDecoder decoder = null;
private static boolean secure = false; private static boolean secure = false;
private static String serverPrincipal = "hbase";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if (args.length < 2 || args.length > 3) { if (args.length < 2 || args.length > 4 || (args.length > 2 && !isBoolean(args[2]))) {
System.out.println("Invalid arguments!"); System.out.println("Invalid arguments!");
System.out.println("Usage: DemoClient host port [secure=false]"); System.out.println("Usage: DemoClient host port [secure=false [server-principal=hbase] ]");
System.exit(-1); System.exit(-1);
} }
@ -77,6 +78,10 @@ public class DemoClient {
secure = Boolean.parseBoolean(args[2]); secure = Boolean.parseBoolean(args[2]);
} }
if (args.length == 4) {
serverPrincipal = args[3];
}
final DemoClient client = new DemoClient(); final DemoClient client = new DemoClient();
Subject.doAs(getSubject(), Subject.doAs(getSubject(),
new PrivilegedExceptionAction<Void>() { new PrivilegedExceptionAction<Void>() {
@ -88,6 +93,10 @@ public class DemoClient {
}); });
} }
private static boolean isBoolean(String s){
return Boolean.TRUE.toString().equalsIgnoreCase(s) || Boolean.FALSE.toString().equalsIgnoreCase(s);
}
DemoClient() { DemoClient() {
decoder = Charset.forName("UTF-8").newDecoder(); decoder = Charset.forName("UTF-8").newDecoder();
} }
@ -123,7 +132,7 @@ public class DemoClient {
* The HBase cluster must be secure, allow proxy user. * The HBase cluster must be secure, allow proxy user.
*/ */
transport = new TSaslClientTransport("GSSAPI", null, transport = new TSaslClientTransport("GSSAPI", null,
"hbase", // Thrift server user name, should be an authorized proxy user. serverPrincipal, // Thrift server user name, should be an authorized proxy user.
host, // Thrift server domain host, // Thrift server domain
saslProperties, null, transport); saslProperties, null, transport);
} }