[
https://issues.apache.org/jira/browse/KNOX-2752?focusedWorklogId=779946&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-779946
]
ASF GitHub Bot logged work on KNOX-2752:
----------------------------------------
Author: ASF GitHub Bot
Created on: 09/Jun/22 13:21
Start Date: 09/Jun/22 13:21
Worklog Time Spent: 10m
Work Description: 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.
Issue Time Tracking
-------------------
Worklog Id: (was: 779946)
Time Spent: 20m (was: 10m)
> knoxcli should support batch alias creation
> -------------------------------------------
>
> Key: KNOX-2752
> URL: https://issues.apache.org/jira/browse/KNOX-2752
> Project: Apache Knox
> Issue Type: Bug
> Components: KnoxCLI
> Reporter: Attila Magyar
> Assignee: Attila Magyar
> Priority: Major
> Time Spent: 20m
> Remaining Estimate: 0h
>
> Currently we can only create aliases one by one:
> {code}
> bin/knoxcli.sh create-alias name --cluster cl1 --value value
> {code}
> This is very slow if we want to create multiple aliases.
> KnoxCLI should support creating multiple aliases for the same cluster in one
> batch
> {code}
> bin/knoxcli.sh create-alias name1 --value value1 create-alias name2 --value
> value2
> {code}
--
This message was sent by Atlassian Jira
(v8.20.7#820007)