Repository: atlas Updated Branches: refs/heads/master cc629d709 -> ec396b931
ATLAS-2780: Add username/password option to AtlasAdminClient Change-Id: Iad6269043bb3ae5ef0df9307c9bb57ac66998e87 Project: http://git-wip-us.apache.org/repos/asf/atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/ec396b93 Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/ec396b93 Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/ec396b93 Branch: refs/heads/master Commit: ec396b931b21df05ce52d4b323370ac9e64b426b Parents: cc629d7 Author: apoorvnaik <[email protected]> Authored: Thu Jul 5 14:35:42 2018 -0700 Committer: apoorvnaik <[email protected]> Committed: Thu Jul 5 22:19:50 2018 -0700 ---------------------------------------------------------------------- .../java/org/apache/atlas/AtlasAdminClient.java | 68 +++++++++++++++----- .../apache/atlas/utils/AuthenticationUtil.java | 7 +- 2 files changed, 57 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/atlas/blob/ec396b93/client/client-v1/src/main/java/org/apache/atlas/AtlasAdminClient.java ---------------------------------------------------------------------- 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 @@ import java.util.Arrays; */ 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 @@ public class AtlasAdminClient { static { OPTIONS.addOption(STATUS); OPTIONS.addOption(STATS); + OPTIONS.addOption(CREDENTIALS); } public static void main(String[] args) throws AtlasException, ParseException { @@ -72,19 +75,17 @@ public class AtlasAdminClient { 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 @@ public class AtlasAdminClient { 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 @@ public class AtlasAdminClient { } } 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 @@ public class AtlasAdminClient { 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); } } http://git-wip-us.apache.org/repos/asf/atlas/blob/ec396b93/intg/src/main/java/org/apache/atlas/utils/AuthenticationUtil.java ---------------------------------------------------------------------- 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 @@ public final class AuthenticationUtil { 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 @@ public final class AuthenticationUtil { } } 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};
