@@ -136,7 +136,9 @@ public class KnoxCLI extends Configured implements Tool {
136
136
" [" + TopologyConverter .USAGE + "]\n " +
137
137
" [" + JWKGenerator .USAGE + "]\n " +
138
138
" [" + GenerateDescriptorCommand .USAGE + "]\n " +
139
- " [" + TokenMigration .USAGE + "]\n " ;
139
+ " [" + TokenMigration .USAGE + "]\n " +
140
+ " [" + CreateListAliasesCommand .USAGE + "]\n " ;
141
+ private static final String CLUSTER_STRING_SEPARATOR = "," ;
140
142
141
143
/** allows stdout to be captured if necessary */
142
144
public PrintStream out = System .out ;
@@ -275,6 +277,12 @@ private int init(String[] args) throws IOException {
275
277
printKnoxShellUsage ();
276
278
return -1 ;
277
279
}
280
+ } else if (args [i ].equals ("create-list-aliases" )) {
281
+ command = new CreateListAliasesCommand ();
282
+ if (args .length < 3 || "--help" .equals (alias )) {
283
+ printKnoxShellUsage ();
284
+ return -1 ;
285
+ }
278
286
} else if (args [i ].equals ("create-cert" )) {
279
287
command = new CertCreateCommand ();
280
288
if ((args .length > i + 1 ) && args [i + 1 ].equals ("--help" )) {
@@ -337,12 +345,15 @@ private int init(String[] args) throws IOException {
337
345
}
338
346
} else if ( args [i ].equals ("list-topologies" ) ){
339
347
command = new ListTopologiesCommand ();
340
- }else if ( args [i ].equals ("--cluster" ) || args [i ].equals ("--topology" ) ) {
348
+ } else if ( args [i ].equals ("--cluster" ) || args [i ].equals ("--topology" ) ) {
341
349
if ( i +1 >= args .length || args [i +1 ].startsWith ( "-" ) ) {
342
350
printKnoxShellUsage ();
343
351
return -1 ;
344
352
}
345
353
this .cluster = args [++i ];
354
+ if (command instanceof CreateListAliasesCommand ) {
355
+ ((CreateListAliasesCommand ) command ).toMap (this .cluster );
356
+ }
346
357
} else if (args [i ].equals ("service-test" )) {
347
358
if ( i + 1 >= args .length ) {
348
359
printKnoxShellUsage ();
@@ -663,6 +674,12 @@ private void printKnoxShellUsage() {
663
674
out .println (JWKGenerator .USAGE + "\n \n " + JWKGenerator .DESC );
664
675
out .println ();
665
676
out .println ( div );
677
+ out .println (BatchAliasCreateCommand .USAGE + "\n \n " + BatchAliasCreateCommand .DESC );
678
+ out .println ();
679
+ out .println ( div );
680
+ out .println (CreateListAliasesCommand .USAGE + "\n \n " + CreateListAliasesCommand .DESC );
681
+ out .println ();
682
+ out .println ( div );
666
683
}
667
684
}
668
685
@@ -701,31 +718,34 @@ protected RemoteConfigurationRegistryClientService getRemoteConfigRegistryClient
701
718
702
719
private class AliasListCommand extends Command {
703
720
704
- public static final String USAGE = "list-alias [--cluster clustername ]" ;
721
+ public static final String USAGE = "list-alias [--cluster cluster1,clusterN ]" ;
705
722
public static final String DESC = "The list-alias command lists all of the aliases\n " +
706
- "for the given hadoop --cluster. The default\n " +
723
+ "for the given hadoop --cluster(s) . The default\n " +
707
724
"--cluster being the gateway itself." ;
708
725
709
726
@ Override
710
727
public void execute () throws Exception {
711
728
AliasService as = getAliasService ();
712
- KeystoreService keystoreService = getKeystoreService ();
729
+ KeystoreService keystoreService = getKeystoreService ();
713
730
714
731
if (cluster == null ) {
715
732
cluster = "__gateway" ;
716
733
}
717
- boolean credentialStoreForClusterAvailable =
718
- keystoreService .isCredentialStoreForClusterAvailable (cluster );
719
- if (credentialStoreForClusterAvailable ) {
720
- out .println ("Listing aliases for: " + cluster );
721
- List <String > aliases = as .getAliasesForCluster (cluster );
722
- for (String alias : aliases ) {
723
- out .println (alias );
724
- }
725
- out .println ("\n " + aliases .size () + " items." );
726
- } else {
727
- out .println ("Invalid cluster name provided: " + cluster );
728
- }
734
+ String [] clusters = cluster .split (CLUSTER_STRING_SEPARATOR );
735
+ for (String currentCluster : clusters ) {
736
+ boolean credentialStoreForClusterAvailable =
737
+ keystoreService .isCredentialStoreForClusterAvailable (currentCluster );
738
+ if (credentialStoreForClusterAvailable ) {
739
+ out .println ("Listing aliases for: " + currentCluster );
740
+ List <String > aliases = as .getAliasesForCluster (currentCluster );
741
+ for (String alias : aliases ) {
742
+ out .println (alias );
743
+ }
744
+ out .println ("\n " + aliases .size () + " items." );
745
+ } else {
746
+ out .println ("Invalid cluster name provided: " + currentCluster );
747
+ }
748
+ }
729
749
}
730
750
731
751
@ Override
@@ -1037,8 +1057,8 @@ public class BatchAliasCreateCommand extends Command {
1037
1057
+ "the --value option or --generate (will create a random secret\n "
1038
1058
+ "for you) or user will be prompt to provide password." ;
1039
1059
1040
- private List <String > names = new ArrayList <>();
1041
- private List <String > values = new ArrayList <>();
1060
+ protected List <String > names = new ArrayList <>();
1061
+ protected List <String > values = new ArrayList <>();
1042
1062
1043
1063
public void addName (String alias ) {
1044
1064
if (names .contains (alias )) {
@@ -1061,6 +1081,23 @@ public void execute() throws Exception {
1061
1081
if (cluster == null ) {
1062
1082
cluster = "__gateway" ;
1063
1083
}
1084
+ fillMissingValues (aliases , generated );
1085
+ as .addAliasesForCluster (cluster , aliases );
1086
+ printResults (generated , aliases );
1087
+ }
1088
+
1089
+ protected void printResults (List <String > generated , Map <String , String > aliases ) {
1090
+ if (!generated .isEmpty ()) {
1091
+ out .println (generated .size () + " alias(es) have been successfully generated: " + generated );
1092
+ }
1093
+ List <String > created = new ArrayList <>(aliases .keySet ());
1094
+ created .removeAll (generated );
1095
+ if (!created .isEmpty ()) {
1096
+ out .println (created .size () + " alias(es) have been successfully created: " + created );
1097
+ }
1098
+ }
1099
+
1100
+ protected void fillMissingValues (Map <String , String > aliases , List <String > generated ) {
1064
1101
for (Map .Entry <String , String > entry : aliases .entrySet ()) {
1065
1102
if (entry .getValue () == null ) {
1066
1103
if (Boolean .parseBoolean (generate )) {
@@ -1071,15 +1108,6 @@ public void execute() throws Exception {
1071
1108
}
1072
1109
}
1073
1110
}
1074
- as .addAliasesForCluster (cluster , aliases );
1075
- if (!generated .isEmpty ()) {
1076
- out .println (generated .size () + " alias(es) have been successfully generated: " + generated );
1077
- }
1078
- List <String > created = new ArrayList <>(aliases .keySet ());
1079
- created .removeAll (generated );
1080
- if (!created .isEmpty ()) {
1081
- out .println (created .size () + " alias(es) have been successfully created: " + created );
1082
- }
1083
1111
}
1084
1112
1085
1113
private Map <String , String > toMap () {
@@ -1096,6 +1124,68 @@ public String getUsage() {
1096
1124
}
1097
1125
}
1098
1126
1127
+ public class CreateListAliasesCommand extends BatchAliasCreateCommand {
1128
+ public static final String USAGE = "create-list-aliases " +
1129
+ "--alias alias1 [--value value1] " +
1130
+ "--alias alias2 [--value value2] " +
1131
+ "--alias aliasN [--value valueN] ... " +
1132
+ "--cluster cluster1 " +
1133
+ "--alias aliasN [--value valueN] ..." +
1134
+ "--cluster clusterN " +
1135
+ "[--generate]" ;
1136
+ public static final String DESC = "The create-list-aliases command will create multiple aliases\n "
1137
+ + "and secret pairs within the same credential store for the\n "
1138
+ + "indicated --cluster(s) otherwise within the gateway\n "
1139
+ + "credential store. The actual secret may be specified via\n "
1140
+ + "the --value option or --generate (will create a random secret\n "
1141
+ + "for you) or user will be prompt to provide password." ;
1142
+
1143
+ private final Map <String , Map <String , String >> aliasMap = new LinkedHashMap <>();
1144
+
1145
+ @ Override
1146
+ public void execute () throws Exception {
1147
+ if (cluster == null || !names .isEmpty ()) {
1148
+ cluster = "__gateway" ;
1149
+ this .toMap (cluster );
1150
+ }
1151
+
1152
+ AliasService aliasService = getAliasService ();
1153
+
1154
+ for (Map .Entry <String , Map <String , String >> aliasesMapEntry : aliasMap .entrySet ()) {
1155
+ List <String > generated = new ArrayList <>();
1156
+ this .fillMissingValues (aliasesMapEntry .getValue (), generated );
1157
+ aliasService .addAliasesForCluster (aliasesMapEntry .getKey (), aliasesMapEntry .getValue ());
1158
+ this .printResults (generated , aliasesMapEntry .getValue ());
1159
+ this .listAliasesForCluster (aliasesMapEntry .getKey (), aliasService );
1160
+ }
1161
+ }
1162
+
1163
+ private void listAliasesForCluster (String cluster , AliasService as ) throws AliasServiceException {
1164
+ out .println ("Listing aliases for: " + cluster );
1165
+ List <String > aliases = as .getAliasesForCluster (cluster );
1166
+ for (String alias : aliases ) {
1167
+ out .println (alias );
1168
+ }
1169
+ out .println ("\n " + aliases .size () + " items." );
1170
+ }
1171
+
1172
+ private void toMap (String cluster ) {
1173
+ Map <String , String > parsedAliases = new LinkedHashMap <>();
1174
+ for (int i = 0 ; i < values .size (); i ++) {
1175
+ parsedAliases .put (names .get (i ), values .get (i ));
1176
+ }
1177
+
1178
+ names .clear ();
1179
+ values .clear ();
1180
+ aliasMap .put (cluster , parsedAliases );
1181
+ }
1182
+
1183
+ @ Override
1184
+ public String getUsage () {
1185
+ return USAGE + ":\n \n " + DESC ;
1186
+ }
1187
+ }
1188
+
1099
1189
public static char [] promptUserForPassword () {
1100
1190
char [] password = null ;
1101
1191
Console c = System .console ();
0 commit comments