From 1c1a5e0fdcd6d08752f9d60b8b4bac630d0c6c21 Mon Sep 17 00:00:00 2001 From: thanicz Date: Fri, 6 Dec 2024 11:40:47 +0100 Subject: [PATCH] KNOX-3071: Optional listing for create-aliases, create aliases for multiple clusters in create-aliases command --- .../org/apache/knox/gateway/util/KnoxCLI.java | 111 ++++++------------ .../apache/knox/gateway/util/KnoxCLITest.java | 36 +++++- 2 files changed, 63 insertions(+), 84 deletions(-) diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java b/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java index 14653d371..b2e1eb073 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java @@ -136,8 +136,7 @@ public class KnoxCLI extends Configured implements Tool { " [" + TopologyConverter.USAGE + "]\n" + " [" + JWKGenerator.USAGE + "]\n" + " [" + GenerateDescriptorCommand.USAGE + "]\n" + - " [" + TokenMigration.USAGE + "]\n" + - " [" + CreateListAliasesCommand.USAGE + "]\n"; + " [" + TokenMigration.USAGE + "]\n"; private static final String CLUSTER_STRING_SEPARATOR = ","; /** allows stdout to be captured if necessary */ @@ -164,6 +163,7 @@ public class KnoxCLI extends Configured implements Tool { private boolean migrateExpiredTokens; private boolean verbose; private String alias; + private boolean listAliases; private String remoteRegistryClient; private String remoteRegistryEntryName; @@ -277,12 +277,6 @@ private int init(String[] args) throws IOException { printKnoxShellUsage(); return -1; } - } else if (args[i].equals("create-list-aliases")) { - command = new CreateListAliasesCommand(); - if (args.length < 3 || "--help".equals(alias)) { - printKnoxShellUsage(); - return -1; - } } else if (args[i].equals("create-cert")) { command = new CertCreateCommand(); if ((args.length > i + 1) && args[i + 1].equals("--help")) { @@ -351,8 +345,8 @@ private int init(String[] args) throws IOException { return -1; } this.cluster = args[++i]; - if(command instanceof CreateListAliasesCommand) { - ((CreateListAliasesCommand) command).toMap(this.cluster); + if(command instanceof BatchAliasCreateCommand) { + ((BatchAliasCreateCommand) command).toMap(this.cluster); } } else if (args[i].equals("service-test")) { if( i + 1 >= args.length) { @@ -462,6 +456,8 @@ private int init(String[] args) throws IOException { this.master = args[++i]; } else if (args[i].equals("--force")) { this.force = true; + } else if (args[i].equals("--list")) { + this.listAliases = true; } else if (args[i].equals("--help")) { printKnoxShellUsage(); return -1; @@ -677,9 +673,6 @@ private void printKnoxShellUsage() { out.println(BatchAliasCreateCommand.USAGE + "\n\n" + BatchAliasCreateCommand.DESC); out.println(); out.println( div ); - out.println(CreateListAliasesCommand.USAGE + "\n\n" + CreateListAliasesCommand.DESC); - out.println(); - out.println( div ); } } @@ -1048,17 +1041,22 @@ public class BatchAliasCreateCommand extends Command { "--alias alias1 [--value value1] " + "--alias alias2 [--value value2] " + "--alias aliasN [--value valueN] ... " + - "[--cluster clustername] " + - "[--generate]"; + "--cluster cluster1 " + + "--alias aliasN [--value valueN] ..." + + "--cluster clusterN " + + "[--generate] " + + "[--list]"; 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" + + "indicated --cluster(s) 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."; + + "for you) or user will be prompt to provide password.\n" + + "Optionally the aliases for the clusters can be listed with --list."; protected List names = new ArrayList<>(); protected List values = new ArrayList<>(); + private final Map> aliasMap = new LinkedHashMap<>(); public void addName(String alias) { if (names.contains(alias)) { @@ -1075,18 +1073,25 @@ public void addValue(String value) { @Override public void execute() throws Exception { - Map aliases = toMap(); - List generated = new ArrayList<>(); - AliasService as = getAliasService(); - if (cluster == null) { + if (cluster == null || !names.isEmpty()) { cluster = "__gateway"; + this.toMap(cluster); + } + + AliasService aliasService = getAliasService(); + + for (Map.Entry> aliasesMapEntry : aliasMap.entrySet()) { + List generated = new ArrayList<>(); + fillMissingValues(aliasesMapEntry.getValue(), generated); + aliasService.addAliasesForCluster(aliasesMapEntry.getKey(), aliasesMapEntry.getValue()); + printResults(generated, aliasesMapEntry.getValue()); + if(listAliases) { + listAliasesForCluster(aliasesMapEntry.getKey(), aliasService); + } } - fillMissingValues(aliases, generated); - as.addAliasesForCluster(cluster, aliases); - printResults(generated, aliases); } - protected void printResults(List generated, Map aliases) { + private void printResults(List generated, Map aliases) { if (!generated.isEmpty()) { out.println(generated.size() + " alias(es) have been successfully generated: " + generated); } @@ -1097,7 +1102,7 @@ protected void printResults(List generated, Map aliases) } } - protected void fillMissingValues(Map aliases, List generated) { + private void fillMissingValues(Map aliases, List generated) { for (Map.Entry entry : aliases.entrySet()) { if (entry.getValue() == null) { if (Boolean.parseBoolean(generate)) { @@ -1110,59 +1115,9 @@ protected void fillMissingValues(Map aliases, List gener } } - private Map toMap() { - Map 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 class CreateListAliasesCommand extends BatchAliasCreateCommand { - public static final String USAGE = "create-list-aliases " + - "--alias alias1 [--value value1] " + - "--alias alias2 [--value value2] " + - "--alias aliasN [--value valueN] ... " + - "--cluster cluster1 " + - "--alias aliasN [--value valueN] ..." + - "--cluster clusterN " + - "[--generate]"; - public static final String DESC = "The create-list-aliases command will create multiple aliases\n" - + "and secret pairs within the same credential store for the\n" - + "indicated --cluster(s) 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 final Map> aliasMap = new LinkedHashMap<>(); - - @Override - public void execute() throws Exception { - if (cluster == null || !names.isEmpty()) { - cluster = "__gateway"; - this.toMap(cluster); - } - - AliasService aliasService = getAliasService(); - - for (Map.Entry> aliasesMapEntry : aliasMap.entrySet()) { - List generated = new ArrayList<>(); - this.fillMissingValues(aliasesMapEntry.getValue(), generated); - aliasService.addAliasesForCluster(aliasesMapEntry.getKey(), aliasesMapEntry.getValue()); - this.printResults(generated, aliasesMapEntry.getValue()); - this.listAliasesForCluster(aliasesMapEntry.getKey(), aliasService); - } - } - - private void listAliasesForCluster(String cluster, AliasService as) throws AliasServiceException { + private void listAliasesForCluster(String cluster, AliasService aliasService) throws AliasServiceException { out.println("Listing aliases for: " + cluster); - List aliases = as.getAliasesForCluster(cluster); + List aliases = aliasService.getAliasesForCluster(cluster); for (String alias : aliases) { out.println(alias); } diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/util/KnoxCLITest.java b/gateway-server/src/test/java/org/apache/knox/gateway/util/KnoxCLITest.java index 9d6363789..d4553c436 100644 --- a/gateway-server/src/test/java/org/apache/knox/gateway/util/KnoxCLITest.java +++ b/gateway-server/src/test/java/org/apache/knox/gateway/util/KnoxCLITest.java @@ -1376,9 +1376,9 @@ public void testCreateAndListForMultipleClusters() throws Exception { GatewayConfigImpl config = new GatewayConfigImpl(); outContent.reset(); - String[] args1 = {"create-list-aliases", "--alias", "alias1", "--value", "value1", "--cluster", "cluster1", + String[] args1 = {"create-aliases", "--alias", "alias1", "--value", "value1", "--cluster", "cluster1", "--alias", "alias2", "--value", "value2", "--alias", "alias1", "--value", "value1", "--cluster", "cluster2", - "--master", "master"}; + "--master", "master", "--list"}; int rc; KnoxCLI cli = new KnoxCLI(); cli.setConf(config); @@ -1404,9 +1404,9 @@ public void testCreateAndListForMultipleClustersWithGenerate() throws Exception GatewayConfigImpl config = new GatewayConfigImpl(); outContent.reset(); - String[] args1 = {"create-list-aliases", "--alias", "alias1", "--cluster", "cluster1", "--alias", + String[] args1 = {"create-aliases", "--alias", "alias1", "--cluster", "cluster1", "--alias", "alias2", "--value", "value2", "--alias", "alias3", "--cluster", "cluster2", - "--master", "master", "--generate"}; + "--master", "master", "--generate", "--list"}; int rc; KnoxCLI cli = new KnoxCLI(); cli.setConf(config); @@ -1434,9 +1434,9 @@ public void testCreateAndListForMultipleClustersNoCLuster() throws Exception { GatewayConfigImpl config = new GatewayConfigImpl(); outContent.reset(); - String[] args1 = {"create-list-aliases", "--alias", "alias1", "--cluster", "cluster1", "--alias", + String[] args1 = {"create-aliases", "--alias", "alias1", "--cluster", "cluster1", "--alias", "alias2", "--value", "value2", "--alias", "alias3", - "--master", "master", "--generate"}; + "--master", "master", "--generate", "--list"}; int rc; KnoxCLI cli = new KnoxCLI(); cli.setConf(config); @@ -1459,6 +1459,30 @@ public void testCreateAndListForMultipleClustersNoCLuster() throws Exception { outContent.toString(StandardCharsets.UTF_8.name()).contains("alias3")); } + @Test + public void testCreateAndListForMultipleClustersNoListing() throws Exception { + GatewayConfigImpl config = new GatewayConfigImpl(); + + outContent.reset(); + String[] args1 = {"create-aliases", "--alias", "alias1", "--value", "value1", "--cluster", "cluster1", + "--alias", "alias2", "--value", "value2", "--alias", "alias1", "--value", "value1", "--cluster", "cluster2", + "--master", "master"}; + int rc; + KnoxCLI cli = new KnoxCLI(); + cli.setConf(config); + rc = cli.run(args1); + assertEquals(0, rc); + assertTrue(outContent.toString(StandardCharsets.UTF_8.name()), + outContent.toString(StandardCharsets.UTF_8.name()).contains("1 alias(es) have been successfully created: [alias1]")); + assertFalse(outContent.toString(StandardCharsets.UTF_8.name()), + outContent.toString(StandardCharsets.UTF_8.name()).contains("Listing aliases for: cluster1")); + + assertTrue(outContent.toString(StandardCharsets.UTF_8.name()), + outContent.toString(StandardCharsets.UTF_8.name()).contains("2 alias(es) have been successfully created: [alias2, alias1]")); + assertFalse(outContent.toString(StandardCharsets.UTF_8.name()), + outContent.toString(StandardCharsets.UTF_8.name()).contains("Listing aliases for: cluster2")); + } + private void testGeneratingJWK(JWSAlgorithm jwkAlgorithm) throws Exception { testGeneratingJWK(jwkAlgorithm, null); }