Skip to content

Commit f4272c8

Browse files
committed
Updates to CLI key commands and parameters
1 parent 06b5662 commit f4272c8

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

convex-cli/src/main/java/convex/cli/Main.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public class Main extends ACommand {
8080
description = "Public key to use. Default: ${DEFAULT-VALUE}")
8181
private String keySpec;
8282

83-
@Option(names = { "-p","--password" },
83+
@Option(names = { "-p","--keypass" },
8484
defaultValue = "${env:CONVEX_KEY_PASSWORD}",
8585
scope = ScopeType.INHERIT,
86-
description = "Keystore filename. Default: ${DEFAULT-VALUE}")
86+
description = "Key password in key store. Can also specify with CONVEX_KEY_PASSWORD environment variable.")
8787
private String keyPassword;
8888

8989
@Option(names = { "-S","--strict-security" },
@@ -217,15 +217,16 @@ public int handleExecutionException(Exception ex, CommandLine commandLine, Parse
217217
if (ex instanceof CLIError) {
218218
CLIError ce = (CLIError) ex;
219219
String msg=ce.getMessage();
220-
msg=noColour?msg:Coloured.red(msg);
221-
err.println(msg);
220+
informError(msg);
222221
Throwable cause = ce.getCause();
223-
if (cause != null) {
222+
if ((verbose>=2) && (cause != null)) {
224223
err.println("Underlying cause: ");
225224
cause.printStackTrace(err);
226225
}
227226
} else {
228-
ex.printStackTrace(err);
227+
if (verbose>=1) {
228+
ex.printStackTrace(err);
229+
}
229230
}
230231
// Exit with correct code for exception type
231232
return ExitCodes.getExitCode(ex);
@@ -519,6 +520,10 @@ public String loadFileAsString(String fname) {
519520
public void informSuccess(String message) {
520521
inform(1, noColour?message:Coloured.green(message));
521522
}
523+
524+
public void informError(String message) {
525+
inform(1, noColour?message:Coloured.red(message));
526+
}
522527

523528
public void inform(String message) {
524529
inform(1, noColour?message:Coloured.yellow(message));

convex-cli/src/main/java/convex/cli/key/Key.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Key extends ATopCommand {
2727
@Override
2828
public void run() {
2929
// sub command run with no command provided
30-
CommandLine.usage(new Key(), System.out);
30+
showUsage();
3131
}
3232

3333

convex-cli/src/main/java/convex/cli/key/KeyImport.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import convex.core.data.Blobs;
1313
import picocli.CommandLine.Command;
1414
import picocli.CommandLine.Option;
15-
import picocli.CommandLine.Parameters;
1615
import picocli.CommandLine.ParentCommand;
1716

1817

@@ -25,7 +24,7 @@
2524
*
2625
*/
2726
@Command(name="import",
28-
description="Import key pairs to the keystore.")
27+
description="Import key pairs to the keystore. Can specify either a raw key with --text or --import-file")
2928
public class KeyImport extends AKeyCommand {
3029

3130
@ParentCommand
@@ -38,13 +37,14 @@ public class KeyImport extends AKeyCommand {
3837
@Option(names={"-t", "--text"},
3938
description="Text string to import.")
4039
private String importText;
41-
42-
@Option(names={"--import-password"},
43-
description="Password for the imported key.")
44-
private String importPassword;
4540

46-
@Parameters(index = "0", arity = "0..1", description = "Type of file imported. Supports: pem, seed, bip39")
47-
private String type;
41+
@Option(names={"--passphrase"},
42+
description="Passphrase for BIP39 or encrypted PEM imported key")
43+
private String importPassphrase;
44+
45+
@Option(names={"--type"},
46+
description="Text string to import. Type of file imported. Supports: pem, seed, bip39. Will attempt to autodetect")
47+
private String type;
4848

4949
@Override
5050
public void run() {
@@ -54,22 +54,18 @@ public void run() {
5454
importText=cli().loadFileAsString(importFilename);
5555
}
5656
if (importText == null || importText.length() == 0) {
57-
throw new CLIError("You need to provide '--text' or import filename '--import-file' to import a private key");
58-
}
59-
60-
// Get import password
61-
if (importPassword == null) {
62-
if (cli().isInteractive()) {
63-
importPassword=new String(System.console().readPassword("Enter import password:"));
64-
} else {
65-
throw new CLIError("--import-password not provided during non-interatice import");
66-
}
57+
showUsage();
58+
return;
6759
}
6860

6961
// Parse input as hex string, will be null if not parsed. For BIP39 is 64 bytes, Ed25519 32
7062
ABlob hex=Blobs.parse(importText.trim());
7163
if (type==null) {
64+
if (cli().isParanoid()) {
65+
cli().informError("Not permitted to infer key import type in strict mode");
66+
}
7267
cli().inform("No import file type specified, attempting to auto-detect");
68+
7369
if (hex!=null) {
7470
if (hex.count()==AKeyPair.SEED_LENGTH) {
7571
type="seed";
@@ -88,14 +84,14 @@ public void run() {
8884
} else if ("bip39".equals(type)) {
8985
if (hex==null) {
9086
try {
91-
hex=BIP39.getSeed(importText, importPassword);
87+
hex=BIP39.getSeed(importText, importPassphrase);
9288
} catch (Exception e) {
9389
throw new CLIError("Error interpreting BIP39 seed",e);
9490
}
9591
}
9692
keyPair=BIP39.seedToKeyPair(hex.toFlatBlob());
9793
} else if ("pem".equals(type)) {
98-
PrivateKey privateKey = PEMTools.decryptPrivateKeyFromPEM(importText, importPassword.toCharArray());
94+
PrivateKey privateKey = PEMTools.decryptPrivateKeyFromPEM(importText, importPassphrase.toCharArray());
9995
keyPair = AKeyPair.create(privateKey);
10096
}
10197
if (keyPair==null) throw new CLIError("Unable to import keypair");

convex-cli/src/test/java/convex/cli/key/KeyExportTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testKeyGenerateAndExport() {
4242
CLTester tester = CLTester.run(
4343
"key", "generate",
4444
"--storepass", new String(KEYSTORE_PASSWORD),
45-
"--password", new String(KEY_PASSWORD),
45+
"--keypass", new String(KEY_PASSWORD),
4646
"--keystore", KEYSTORE_FILENAME
4747
);
4848
assertEquals(ExitCodes.SUCCESS,tester.getResult());
@@ -63,7 +63,7 @@ public void testKeyGenerateAndExport() {
6363
"key",
6464
"export",
6565
"--storepass", new String(KEYSTORE_PASSWORD),
66-
"--password", new String(KEY_PASSWORD),
66+
"--keypass", new String(KEY_PASSWORD),
6767
"--keystore", KEYSTORE_FILENAME,
6868
"--public-key", publicKey,
6969
"--export-password", new String(EXPORT_PASSWORD)
@@ -80,7 +80,7 @@ public void testKeyGenerateAndExport() {
8080
"export",
8181
"seed",
8282
"--storepass", new String(KEYSTORE_PASSWORD),
83-
"--password", new String(KEY_PASSWORD),
83+
"--keypass", new String(KEY_PASSWORD),
8484
"--keystore", KEYSTORE_FILENAME,
8585
"--public-key", publicKey
8686
);

convex-cli/src/test/java/convex/cli/key/KeyImportTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class KeyImportTest {
1919

2020
private static final char[] KEYSTORE_PASSWORD = "testPassword".toCharArray();
21-
private static final char[] IMPORT_PASSWORD = "testImportPassword".toCharArray();
21+
private static final char[] IMPORT_PASSPHRASE = "testImportPassphrase".toCharArray();
2222

2323
private static final String KEY_PASSWORD="testPass";
2424

@@ -34,17 +34,18 @@ public void testKeyImportPEM() {
3434

3535
AKeyPair keyPair = SodiumKeyPair.generate();
3636
AccountKey accountKey=keyPair.getAccountKey();
37-
String pemText = PEMTools.encryptPrivateKeyToPEM(keyPair.getPrivate(), IMPORT_PASSWORD);
37+
String pemText = PEMTools.encryptPrivateKeyToPEM(keyPair.getPrivate(), IMPORT_PASSPHRASE);
3838

3939
CLTester tester = CLTester.run(
4040
"key",
4141
"import",
42-
"pem",
42+
"--type","pem",
4343
"-n",
4444
"--storepass", new String(KEYSTORE_PASSWORD),
4545
"--keystore", KEYSTORE_FILENAME,
4646
"--text", pemText,
47-
"--import-password", new String(IMPORT_PASSWORD)
47+
"--passphrase",new String(IMPORT_PASSPHRASE),
48+
"--keypass", new String(KEY_PASSWORD)
4849
);
4950
assertEquals(ExitCodes.SUCCESS,tester.getResult());
5051

@@ -66,12 +67,12 @@ public void testKeyImportSeed() {
6667
CLTester tester = CLTester.run(
6768
"key",
6869
"import",
69-
"seed",
70+
"--type","seed",
7071
"--storepass", new String(KEYSTORE_PASSWORD),
7172
"--keystore", KEYSTORE_FILENAME,
7273
"--text", keyPair.getSeed().toString(),
73-
"--password", KEY_PASSWORD,
74-
"--import-password", new String("") // BIP39 password
74+
"--keypass", KEY_PASSWORD,
75+
"--passphrase", new String("") // BIP39 password, ignored
7576
);
7677
assertEquals(ExitCodes.SUCCESS,tester.getResult());
7778

@@ -80,7 +81,6 @@ public void testKeyImportSeed() {
8081
CLTester t2=CLTester.run(
8182
"key" ,
8283
"list",
83-
"--password",KEY_PASSWORD,
8484
"--storepass", new String(KEYSTORE_PASSWORD),
8585
"--keystore", KEYSTORE_FILENAME);
8686

@@ -98,12 +98,12 @@ public void testKeyImportBIP39() {
9898
CLTester tester = CLTester.run(
9999
"key",
100100
"import",
101-
"bip39",
101+
"--type","bip39",
102102
"--storepass", new String(KEYSTORE_PASSWORD),
103103
"--keystore", KEYSTORE_FILENAME,
104-
"--password",KEY_PASSWORD,
104+
"--keypass",KEY_PASSWORD,
105105
"--text", "elder mail trick garage hour enjoy attack fringe problem motion poem security caught false penalty",
106-
"--import-password", new String("")
106+
"--passphrase", new String("")
107107
);
108108
assertEquals(ExitCodes.SUCCESS,tester.getResult());
109109

@@ -112,7 +112,6 @@ public void testKeyImportBIP39() {
112112
CLTester t2=CLTester.run(
113113
"key" ,
114114
"list",
115-
"--password",KEY_PASSWORD,
116115
"--storepass", new String(KEYSTORE_PASSWORD),
117116
"--keystore", KEYSTORE_FILENAME);
118117

0 commit comments

Comments
 (0)