You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@knox.apache.org by GitBox <gi...@apache.org> on 2022/06/09 13:21:27 UTC

[GitHub] [knox] smolnar82 commented on a diff in pull request #583: KNOX-2752 knoxcli should support batch alias creation

smolnar82 commented on code in PR #583:
URL: https://github.com/apache/knox/pull/583#discussion_r893495981


##########
gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java:
##########
@@ -977,6 +995,104 @@ public String getUsage() {
 
  }
 
+  public class BatchAliasCreateCommand extends Command {
+    public static final String USAGE = "create-aliases " +
+            "--alias alias1 [--value value1] " +
+            "--alias alias2 [--value value2] " +
+            "--alias aliasN [--value valueN] ... " +
+            "[--cluster clustername] " +
+            "[--generate]";
+    public static final String DESC = "The create-aliases command will create multiple aliases\n"
+            + "and secret pairs within the same credential store for the\n"
+            + "indicated --cluster otherwise within the gateway\n"
+            + "credential store. The actual secret may be specified via\n"
+            + "the --value option or --generate (will create a random secret\n"
+            + "for you) or user will be prompt to provide password.";
+
+    private List<String> names = new ArrayList<>();
+    private List<String> values = new ArrayList<>();
+
+    public void addName(String alias) {
+      if (names.contains(alias)) {
+        out.println("Duplicated alias " + alias);
+        System.exit(1);
+      }
+      names.add(alias);
+      values.add(null);
+    }
+
+    public void addValue(String value) {
+      values.set(values.size() -1, value);
+    }
+
+    @Override
+    public void execute() throws Exception {
+      Map<String, String> aliases = toMap();
+      List<String> generated = new ArrayList<>();
+      AliasService as = getAliasService();
+      if (cluster == null) {
+        cluster = "__gateway";
+      }
+      for (Map.Entry<String, String> entry : aliases.entrySet()) {
+        if (entry.getValue() == null) {
+          if (Boolean.parseBoolean(generate)) {
+            entry.setValue(PasswordUtils.generatePassword(16));
+            generated.add(entry.getKey());
+          } else {
+            entry.setValue(new String(promptUserForPassword()));
+          }
+        }
+      }
+      as.addAliasesForCluster(cluster, aliases);
+      if (!generated.isEmpty()) {
+        out.println(generated.size() + " alias(es) have been successfully generated: " + generated);
+      }
+      List<String> created = new ArrayList<>(aliases.keySet());
+      created.removeAll(generated);
+      if (!created.isEmpty()) {
+        out.println(created.size() + " alias(es) have been successfully created: " + created);
+      }
+    }
+
+    private Map<String, String> toMap() {
+      Map<String,String> aliases = new LinkedHashMap<>();
+      for (int i = 0; i < names.size(); i++) {
+        aliases.put(names.get(i), values.get(i));
+      }
+      return aliases;
+    }
+
+    @Override
+    public String getUsage() {
+      return USAGE + ":\n\n" + DESC;
+    }
+  }
+
+  public static char[] promptUserForPassword() {

Review Comment:
   As discussed offline, the previous occurrence (w/ protected access modifier) has to be removed to void code duplication.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org