Skip to content

Commit c0f66f3

Browse files
fix terminal messages and ignore leading and trailing whitespaces
1 parent 09f698e commit c0f66f3

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertGenUtils.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@
5555
import java.time.ZoneOffset;
5656
import java.time.ZonedDateTime;
5757
import java.util.Collection;
58+
import java.util.Collections;
5859
import java.util.HashSet;
5960
import java.util.Locale;
6061
import java.util.Map;
6162
import java.util.Objects;
6263
import java.util.Set;
64+
import java.util.TreeMap;
6365

6466
import javax.net.ssl.X509ExtendedKeyManager;
6567
import javax.net.ssl.X509ExtendedTrustManager;
@@ -79,25 +81,29 @@ public class CertGenUtils {
7981
/**
8082
* The mapping of key usage names to their corresponding integer values as defined in {@code KeyUsage} class.
8183
*/
82-
public static final Map<String, Integer> KEY_USAGE_MAPPINGS = Map.of(
83-
"digitalSignature",
84-
KeyUsage.digitalSignature,
85-
"nonRepudiation",
86-
KeyUsage.nonRepudiation,
87-
"keyEncipherment",
88-
KeyUsage.keyEncipherment,
89-
"dataEncipherment",
90-
KeyUsage.dataEncipherment,
91-
"keyAgreement",
92-
KeyUsage.keyAgreement,
93-
"keyCertSign",
94-
KeyUsage.keyCertSign,
95-
"cRLSign",
96-
KeyUsage.cRLSign,
97-
"encipherOnly",
98-
KeyUsage.encipherOnly,
99-
"decipherOnly",
100-
KeyUsage.decipherOnly
84+
public static final Map<String, Integer> KEY_USAGE_MAPPINGS = Collections.unmodifiableMap(
85+
new TreeMap<>(
86+
Map.of(
87+
"digitalSignature",
88+
KeyUsage.digitalSignature,
89+
"nonRepudiation",
90+
KeyUsage.nonRepudiation,
91+
"keyEncipherment",
92+
KeyUsage.keyEncipherment,
93+
"dataEncipherment",
94+
KeyUsage.dataEncipherment,
95+
"keyAgreement",
96+
KeyUsage.keyAgreement,
97+
"keyCertSign",
98+
KeyUsage.keyCertSign,
99+
"cRLSign",
100+
KeyUsage.cRLSign,
101+
"encipherOnly",
102+
KeyUsage.encipherOnly,
103+
"decipherOnly",
104+
KeyUsage.decipherOnly
105+
)
106+
)
101107
);
102108

103109
/**

x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommand.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import java.util.ArrayList;
7070
import java.util.Arrays;
7171
import java.util.Collection;
72+
import java.util.Collections;
7273
import java.util.List;
7374
import java.util.Locale;
7475
import java.util.Map;
@@ -757,11 +758,12 @@ private CertOptions getCertificateConfiguration(
757758

758759
printHeader("What key usage should your certificate have?", terminal);
759760
terminal.println("The key usage extension defines the purpose of the key contained in the certificate.");
760-
terminal.println(
761-
"The usage restriction might be employed when a key, that could be used for more than one operation, is to be restricted."
762-
);
761+
terminal.println("The usage restriction might be employed when a key, that could be used for more than ");
762+
terminal.println("one operation, is to be restricted.");
763763
terminal.println("You may enter the key usage as a comma-delimited list of following values: ");
764-
terminal.println(" - " + CertGenUtils.KEY_USAGE_MAPPINGS.keySet().stream().sorted());
764+
for (String keyUsageName : CertGenUtils.KEY_USAGE_MAPPINGS.keySet()) {
765+
terminal.println(" - " + keyUsageName);
766+
}
765767
terminal.println("");
766768

767769
keyUsage = readKeyUsage(terminal, keyUsage);
@@ -939,11 +941,12 @@ private CertificateTool.CAInfo createNewCA(Terminal terminal) {
939941

940942
printHeader("What key usage should your CA have?", terminal);
941943
terminal.println("The key usage extension defines the purpose of the key contained in the certificate.");
942-
terminal.println(
943-
"The usage restriction might be employed when a key, that could be used for more than one operation, is to be restricted."
944-
);
944+
terminal.println("The usage restriction might be employed when a key, that could be used for more than ");
945+
terminal.println("one operation, is to be restricted.");
945946
terminal.println("You may enter the key usage as a comma-delimited list of following values: ");
946-
terminal.println(" - " + CertGenUtils.KEY_USAGE_MAPPINGS.keySet().stream().sorted());
947+
for (String keyUsageName : CertGenUtils.KEY_USAGE_MAPPINGS.keySet()) {
948+
terminal.println(" - " + keyUsageName);
949+
}
947950
terminal.println("");
948951

949952
keyUsage = readKeyUsage(terminal, keyUsage);
@@ -1040,6 +1043,7 @@ private static List<String> readKeyUsage(Terminal terminal, List<String> default
10401043
final String[] keyUsages = input.split(",");
10411044
final List<String> resolvedKeyUsages = new ArrayList<>(keyUsages.length);
10421045
for (String keyUsage : keyUsages) {
1046+
keyUsage = keyUsage.trim();
10431047
if (keyUsage.isEmpty()) {
10441048
terminal.println("Key usage cannot be empty");
10451049
return null;
@@ -1053,7 +1057,7 @@ private static List<String> readKeyUsage(Terminal terminal, List<String> default
10531057
}
10541058
resolvedKeyUsages.add(keyUsage);
10551059
}
1056-
return resolvedKeyUsages;
1060+
return Collections.unmodifiableList(resolvedKeyUsages);
10571061
});
10581062
}
10591063

0 commit comments

Comments
 (0)