Skip to content

Commit

Permalink
KNOX-3071: Optional listing for create-aliases, create aliases for mu…
Browse files Browse the repository at this point in the history
…ltiple clusters in create-aliases command
  • Loading branch information
hanicz committed Dec 6, 2024
1 parent 4734d72 commit 1c1a5e0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 84 deletions.
111 changes: 33 additions & 78 deletions gateway-server/src/main/java/org/apache/knox/gateway/util/KnoxCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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<String> names = new ArrayList<>();
protected List<String> values = new ArrayList<>();
private final Map<String, Map<String, String>> aliasMap = new LinkedHashMap<>();

public void addName(String alias) {
if (names.contains(alias)) {
Expand All @@ -1075,18 +1073,25 @@ public void addValue(String value) {

@Override
public void execute() throws Exception {
Map<String, String> aliases = toMap();
List<String> 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<String, Map<String, String>> aliasesMapEntry : aliasMap.entrySet()) {
List<String> 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<String> generated, Map<String, String> aliases) {
private void printResults(List<String> generated, Map<String, String> aliases) {
if (!generated.isEmpty()) {
out.println(generated.size() + " alias(es) have been successfully generated: " + generated);
}
Expand All @@ -1097,7 +1102,7 @@ protected void printResults(List<String> generated, Map<String, String> aliases)
}
}

protected void fillMissingValues(Map<String, String> aliases, List<String> generated) {
private void fillMissingValues(Map<String, String> aliases, List<String> generated) {
for (Map.Entry<String, String> entry : aliases.entrySet()) {
if (entry.getValue() == null) {
if (Boolean.parseBoolean(generate)) {
Expand All @@ -1110,59 +1115,9 @@ protected void fillMissingValues(Map<String, String> aliases, List<String> gener
}
}

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 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<String, Map<String, String>> 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<String, Map<String, String>> aliasesMapEntry : aliasMap.entrySet()) {
List<String> 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<String> aliases = as.getAliasesForCluster(cluster);
List<String> aliases = aliasService.getAliasesForCluster(cluster);
for (String alias : aliases) {
out.println(alias);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit 1c1a5e0

Please sign in to comment.