Commit ec396b93 by apoorvnaik

ATLAS-2780: Add username/password option to AtlasAdminClient

Change-Id: Iad6269043bb3ae5ef0df9307c9bb57ac66998e87
parent cc629d70
...@@ -45,8 +45,10 @@ import java.util.Arrays; ...@@ -45,8 +45,10 @@ import java.util.Arrays;
*/ */
public class AtlasAdminClient { public class AtlasAdminClient {
private static final Option STATUS = new Option("status", false, "Get the status 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 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 Options OPTIONS = new Options();
private static final int INVALID_OPTIONS_STATUS = 1; private static final int INVALID_OPTIONS_STATUS = 1;
...@@ -55,6 +57,7 @@ public class AtlasAdminClient { ...@@ -55,6 +57,7 @@ public class AtlasAdminClient {
static { static {
OPTIONS.addOption(STATUS); OPTIONS.addOption(STATUS);
OPTIONS.addOption(STATS); OPTIONS.addOption(STATS);
OPTIONS.addOption(CREDENTIALS);
} }
public static void main(String[] args) throws AtlasException, ParseException { public static void main(String[] args) throws AtlasException, ParseException {
...@@ -72,19 +75,17 @@ public class AtlasAdminClient { ...@@ -72,19 +75,17 @@ public class AtlasAdminClient {
atlasServerUri = new String[] { AtlasConstants.DEFAULT_ATLAS_REST_ADDRESS }; atlasServerUri = new String[] { AtlasConstants.DEFAULT_ATLAS_REST_ADDRESS };
} }
AtlasClient atlasClient = null; return handleCommand(commandLine, atlasServerUri);
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
atlasClient = new AtlasClient(atlasServerUri, basicAuthUsernamePassword);
} else {
atlasClient = new AtlasClient(atlasServerUri);
}
return handleCommand(commandLine, atlasServerUri, atlasClient);
} }
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; int cmdStatus = PROGRAM_ERROR_STATUS;
if (commandLine.hasOption(STATUS.getOpt())) { if (commandLine.hasOption(STATUS.getOpt())) {
atlasClient = initAtlasClient(atlasServerUri, providedUserPassword); // Status is open API, no auth needed
try { try {
System.out.println(atlasClient.getAdminStatus()); System.out.println(atlasClient.getAdminStatus());
cmdStatus = 0; cmdStatus = 0;
...@@ -93,6 +94,7 @@ public class AtlasAdminClient { ...@@ -93,6 +94,7 @@ public class AtlasAdminClient {
printStandardHttpErrorDetails(e); printStandardHttpErrorDetails(e);
} }
} else if (commandLine.hasOption(STATS.getOpt())) { } else if (commandLine.hasOption(STATS.getOpt())) {
atlasClient = initAtlasClient(atlasServerUri, providedUserPassword); // Stats/metrics is open API, no auth needed
try { try {
AtlasMetrics atlasMetrics = atlasClient.getAtlasMetrics(); AtlasMetrics atlasMetrics = atlasClient.getAtlasMetrics();
String json = AtlasType.toJson(atlasMetrics); String json = AtlasType.toJson(atlasMetrics);
...@@ -104,11 +106,43 @@ public class AtlasAdminClient { ...@@ -104,11 +106,43 @@ public class AtlasAdminClient {
} }
} else { } else {
System.err.println("Unsupported option. Refer to usage for valid options."); System.err.println("Unsupported option. Refer to usage for valid options.");
printUsage(INVALID_OPTIONS_STATUS); printUsage();
} }
return cmdStatus; 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) { private void printStandardHttpErrorDetails(AtlasServiceException e) {
System.err.println("Error details: "); System.err.println("Error details: ");
System.err.println("HTTP Status: " + e.getStatus().getStatusCode() + "," System.err.println("HTTP Status: " + e.getStatus().getStatusCode() + ","
...@@ -118,23 +152,23 @@ public class AtlasAdminClient { ...@@ -118,23 +152,23 @@ public class AtlasAdminClient {
private CommandLine parseCommandLineOptions(String[] args) { private CommandLine parseCommandLineOptions(String[] args) {
if (args.length == 0) { if (args.length == 0) {
printUsage(INVALID_OPTIONS_STATUS); printUsage();
} }
CommandLineParser parser = new GnuParser(); CommandLineParser parser = new GnuParser();
CommandLine commandLine = null; CommandLine commandLine = null;
try { try {
commandLine = parser.parse(OPTIONS, args); commandLine = parser.parse(OPTIONS, args);
} catch (ParseException e) { } catch (ParseException e) {
System.err.println("Could not parse command line options."); System.err.println("Could not parse command line options. " + e.getMessage());
printUsage(INVALID_OPTIONS_STATUS); printUsage();
} }
return commandLine; return commandLine;
} }
private void printUsage(int statusCode) { private void printUsage() {
HelpFormatter helpFormatter = new HelpFormatter(); HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("atlas_admin.py", OPTIONS); helpFormatter.printHelp("atlas_admin.py", OPTIONS);
System.exit(statusCode); System.exit(AtlasAdminClient.INVALID_OPTIONS_STATUS);
} }
} }
...@@ -67,6 +67,11 @@ public final class AuthenticationUtil { ...@@ -67,6 +67,11 @@ public final class AuthenticationUtil {
try { try {
Console console = System.console(); 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 :- "); username = console.readLine("Enter username for atlas :- ");
char[] pwdChar = console.readPassword("Enter password for atlas :- "); char[] pwdChar = console.readPassword("Enter password for atlas :- ");
...@@ -75,7 +80,7 @@ public final class AuthenticationUtil { ...@@ -75,7 +80,7 @@ public final class AuthenticationUtil {
} }
} catch (Exception e) { } catch (Exception e) {
System.out.print("Error while reading "); System.out.print("Error while reading user input");
System.exit(1); System.exit(1);
} }
return new String[]{username, password}; return new String[]{username, password};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment