ATLAS-2780: Add username/password option to AtlasAdminClient
Change-Id: Iad6269043bb3ae5ef0df9307c9bb57ac66998e87
diff --git a/client/client-v1/src/main/java/org/apache/atlas/AtlasAdminClient.java b/client/client-v1/src/main/java/org/apache/atlas/AtlasAdminClient.java
index f334f6c..d22963d 100644
--- a/client/client-v1/src/main/java/org/apache/atlas/AtlasAdminClient.java
+++ b/client/client-v1/src/main/java/org/apache/atlas/AtlasAdminClient.java
@@ -45,8 +45,10 @@
*/
public class AtlasAdminClient {
- private static final Option STATUS = new Option("status", false, "Get the status of an atlas instance");
- private static final Option STATS = new Option("stats", false, "Get the metrics of an atlas instance");
+ private static final Option STATUS = new Option("status", false, "Get the status of an atlas instance");
+ private static final Option STATS = new Option("stats", false, "Get the metrics of an atlas instance");
+ private static final Option CREDENTIALS = new Option("u", true, "Authorized atlas user credentials (<user>:<password>)");
+
private static final Options OPTIONS = new Options();
private static final int INVALID_OPTIONS_STATUS = 1;
@@ -55,6 +57,7 @@
static {
OPTIONS.addOption(STATUS);
OPTIONS.addOption(STATS);
+ OPTIONS.addOption(CREDENTIALS);
}
public static void main(String[] args) throws AtlasException, ParseException {
@@ -72,19 +75,17 @@
atlasServerUri = new String[] { AtlasConstants.DEFAULT_ATLAS_REST_ADDRESS };
}
- AtlasClient atlasClient = null;
- if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
- String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
- atlasClient = new AtlasClient(atlasServerUri, basicAuthUsernamePassword);
- } else {
- atlasClient = new AtlasClient(atlasServerUri);
- }
- return handleCommand(commandLine, atlasServerUri, atlasClient);
+ return handleCommand(commandLine, atlasServerUri);
}
- private int handleCommand(CommandLine commandLine, String[] atlasServerUri, AtlasClient atlasClient) {
+ private int handleCommand(CommandLine commandLine, String[] atlasServerUri) throws AtlasException {
+ AtlasClient atlasClient;
+
+ String[] providedUserPassword = getUserPassword(commandLine);
+
int cmdStatus = PROGRAM_ERROR_STATUS;
if (commandLine.hasOption(STATUS.getOpt())) {
+ atlasClient = initAtlasClient(atlasServerUri, providedUserPassword); // Status is open API, no auth needed
try {
System.out.println(atlasClient.getAdminStatus());
cmdStatus = 0;
@@ -93,6 +94,7 @@
printStandardHttpErrorDetails(e);
}
} else if (commandLine.hasOption(STATS.getOpt())) {
+ atlasClient = initAtlasClient(atlasServerUri, providedUserPassword); // Stats/metrics is open API, no auth needed
try {
AtlasMetrics atlasMetrics = atlasClient.getAtlasMetrics();
String json = AtlasType.toJson(atlasMetrics);
@@ -104,11 +106,43 @@
}
} else {
System.err.println("Unsupported option. Refer to usage for valid options.");
- printUsage(INVALID_OPTIONS_STATUS);
+ printUsage();
}
+
return cmdStatus;
}
+ private String[] getUserPassword(CommandLine commandLine) {
+ String[] basicAuthUsernamePassword = null;
+
+ // Parse the provided username password
+ if (commandLine.hasOption(CREDENTIALS.getOpt())) {
+ String value = commandLine.getOptionValue(CREDENTIALS.getOpt());
+ if (value != null) {
+ basicAuthUsernamePassword = value.split(":");
+ }
+ }
+ if (basicAuthUsernamePassword == null || basicAuthUsernamePassword.length != 2) {
+ System.err.println("Invalid credentials. Format: <user>:<password>");
+ }
+ return basicAuthUsernamePassword;
+ }
+
+ private AtlasClient initAtlasClient(final String[] atlasServerUri, final String[] providedUserNamePassword) throws AtlasException {
+ AtlasClient atlasClient;
+
+ if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
+ if (providedUserNamePassword == null || providedUserNamePassword.length < 2) {
+ atlasClient = new AtlasClient(atlasServerUri, AuthenticationUtil.getBasicAuthenticationInput());
+ } else {
+ atlasClient = new AtlasClient(atlasServerUri, providedUserNamePassword);
+ }
+ } else {
+ atlasClient = new AtlasClient(atlasServerUri);
+ }
+ return atlasClient;
+ }
+
private void printStandardHttpErrorDetails(AtlasServiceException e) {
System.err.println("Error details: ");
System.err.println("HTTP Status: " + e.getStatus().getStatusCode() + ","
@@ -118,23 +152,23 @@
private CommandLine parseCommandLineOptions(String[] args) {
if (args.length == 0) {
- printUsage(INVALID_OPTIONS_STATUS);
+ printUsage();
}
CommandLineParser parser = new GnuParser();
CommandLine commandLine = null;
try {
commandLine = parser.parse(OPTIONS, args);
} catch (ParseException e) {
- System.err.println("Could not parse command line options.");
- printUsage(INVALID_OPTIONS_STATUS);
+ System.err.println("Could not parse command line options. " + e.getMessage());
+ printUsage();
}
return commandLine;
}
- private void printUsage(int statusCode) {
+ private void printUsage() {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("atlas_admin.py", OPTIONS);
- System.exit(statusCode);
+ System.exit(AtlasAdminClient.INVALID_OPTIONS_STATUS);
}
}
diff --git a/intg/src/main/java/org/apache/atlas/utils/AuthenticationUtil.java b/intg/src/main/java/org/apache/atlas/utils/AuthenticationUtil.java
index af32afc..99b58fe 100644
--- a/intg/src/main/java/org/apache/atlas/utils/AuthenticationUtil.java
+++ b/intg/src/main/java/org/apache/atlas/utils/AuthenticationUtil.java
@@ -67,6 +67,11 @@
try {
Console console = System.console();
+ if (console == null) {
+ System.err.println("Couldn't get a console object for user input");
+ System.exit(1);
+ }
+
username = console.readLine("Enter username for atlas :- ");
char[] pwdChar = console.readPassword("Enter password for atlas :- ");
@@ -75,7 +80,7 @@
}
} catch (Exception e) {
- System.out.print("Error while reading ");
+ System.out.print("Error while reading user input");
System.exit(1);
}
return new String[]{username, password};