From 3fdb7ef42f50af831df515950c29b84da284caab Mon Sep 17 00:00:00 2001 From: James McMullan Date: Wed, 4 Sep 2024 10:59:46 -0400 Subject: [PATCH] HPCC4J-637 DFSClient: FileUtility add credential prompting - Added prompting for credentials if not specified via command line Signed-off-by: James McMullan James.McMullan@lexisnexis.com --- .../hpccsystems/dfs/client/FileUtility.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/dfsclient/src/main/java/org/hpccsystems/dfs/client/FileUtility.java b/dfsclient/src/main/java/org/hpccsystems/dfs/client/FileUtility.java index 116c37c21..c41ae0946 100644 --- a/dfsclient/src/main/java/org/hpccsystems/dfs/client/FileUtility.java +++ b/dfsclient/src/main/java/org/hpccsystems/dfs/client/FileUtility.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.io.BufferedInputStream; +import java.io.Console; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -322,6 +323,28 @@ public JSONArray generateResultsMessage() } }; + private static String[] getCredentials(CommandLine cmd) + { + Console console = System.console(); + + String user = cmd.getOptionValue("user"); + boolean userIsEmpty = user == null || user.isEmpty(); + if (userIsEmpty) + { + user = new String(console.readLine("Enter username: ")); + userIsEmpty = user == null || user.isEmpty(); + } + + String pass = cmd.getOptionValue("pass"); + boolean passIsEmpty = pass == null || pass.isEmpty(); + if (!userIsEmpty && passIsEmpty) + { + pass = new String(console.readPassword("Enter password for " + user + ": ")); + } + + return new String[] {user, pass}; + } + private static enum FileFormat { THOR, @@ -1198,8 +1221,10 @@ private static void performRead(String[] args, TaskContext context) } String connString = cmd.getOptionValue("url"); - String user = cmd.getOptionValue("user"); - String pass = cmd.getOptionValue("pass"); + + String[] creds = getCredentials(cmd); + String user = creds[0]; + String pass = creds[1]; String outputPath = cmd.getOptionValue("out","."); @@ -1376,8 +1401,10 @@ private static void performReadTest(String[] args, TaskContext context) } String connString = cmd.getOptionValue("url"); - String user = cmd.getOptionValue("user"); - String pass = cmd.getOptionValue("pass"); + + String[] creds = getCredentials(cmd); + String user = creds[0]; + String pass = creds[1]; String outputPath = cmd.getOptionValue("out","."); @@ -1560,8 +1587,9 @@ private static void performCopy(String[] args, TaskContext context) + numThreadsStr + ", must be an integer. Defaulting to: " + NUM_DEFAULT_THREADS + " threads."); } - String user = cmd.getOptionValue("user"); - String pass = cmd.getOptionValue("pass"); + String[] creds = getCredentials(cmd); + String user = creds[0]; + String pass = creds[1]; String destClusterName = cmd.getOptionValue("dest_cluster"); @@ -1741,8 +1769,9 @@ private static void performWrite(String[] args, TaskContext context) + numThreadsStr + ", must be an integer. Defaulting to: " + NUM_DEFAULT_THREADS + " threads."); } - String user = cmd.getOptionValue("user"); - String pass = cmd.getOptionValue("pass"); + String[] creds = getCredentials(cmd); + String user = creds[0]; + String pass = creds[1]; String destClusterName = cmd.getOptionValue("dest_cluster");