Skip to content

Commit 5f3b5ec

Browse files
committed
Refactor for store options mixin
1 parent f0d47c9 commit 5f3b5ec

File tree

6 files changed

+69
-55
lines changed

6 files changed

+69
-55
lines changed

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

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import picocli.CommandLine.Command;
4242
import picocli.CommandLine.IExecutionExceptionHandler;
4343
import picocli.CommandLine.IVersionProvider;
44+
import picocli.CommandLine.Mixin;
4445
import picocli.CommandLine.Option;
4546
import picocli.CommandLine.ParseResult;
4647
import picocli.CommandLine.ScopeType;
@@ -68,17 +69,14 @@ public class Main extends ACommand {
6869

6970
public CommandLine commandLine = new CommandLine(this);
7071

71-
@Option(names = { "--keystore" },
72-
defaultValue = "${env:CONVEX_KEYSTORE:-" + Constants.KEYSTORE_FILENAME+ "}",
73-
scope = ScopeType.INHERIT,
74-
description = "Keystore filename. Default: ${DEFAULT-VALUE}")
75-
private String keyStoreFilename;
72+
@Mixin
73+
private StoreMixin storeMixin;
7674

7775
@Option(names = { "-k","--key" },
7876
defaultValue = "${env:CONVEX_KEY}",
7977
scope = ScopeType.INHERIT,
8078
description = "Public key to use. Default: ${DEFAULT-VALUE}")
81-
private String keySpec;
79+
public String publicKey;
8280

8381
@Option(names = { "-p","--keypass" },
8482
defaultValue = "${env:CONVEX_KEY_PASSWORD}",
@@ -92,14 +90,7 @@ public class Main extends ACommand {
9290
description = "Apply strict security. Will forbid actions with dubious security implications.")
9391
private boolean paranoid;
9492

95-
/**
96-
* Password for keystore. Option named to match Java keytool
97-
*/
98-
@Option(names = {"--storepass" },
99-
scope = ScopeType.INHERIT,
100-
defaultValue = "${env:CONVEX_KEYSTORE_PASSWORD}",
101-
description = "Password to read/write to the Keystore")
102-
private String keystorePassword;
93+
10394

10495
@Option(names = { "-n","--noninteractive" },
10596
scope = ScopeType.INHERIT,
@@ -219,7 +210,7 @@ public int handleExecutionException(Exception ex, CommandLine commandLine, Parse
219210
String msg=ce.getMessage();
220211
informError(msg);
221212
Throwable cause = ce.getCause();
222-
if ((verbose>=2) && (cause != null)) {
213+
if ((verbose>=3) && (cause != null)) {
223214
err.println("Underlying cause: ");
224215
cause.printStackTrace(err);
225216
}
@@ -243,12 +234,12 @@ public int handleExecutionException(Exception ex, CommandLine commandLine, Parse
243234
public char[] getStorePassword() {
244235
char[] storepass = null;
245236

246-
if (this.keystorePassword != null) {
247-
storepass = this.keystorePassword.toCharArray();
237+
if (storeMixin.keystorePassword != null) {
238+
storepass = storeMixin.keystorePassword.toCharArray();
248239
} else {
249240
if (!nonInteractive) {
250241
storepass = readPassword("Enter Keystore Password: ");
251-
keystorePassword=new String(storepass);
242+
storeMixin.keystorePassword=new String(storepass);
252243
}
253244

254245
if (storepass == null) {
@@ -288,20 +279,9 @@ public char[] getKeyPassword() {
288279
return keypass;
289280
}
290281

291-
/**
292-
* Gets the keystore file name currently used for the CLI
293-
*
294-
* @return File name, or null if not specified
295-
*/
296-
public File getKeyStoreFile() {
297-
if (keyStoreFilename != null) {
298-
File f = Utils.getPath(keyStoreFilename);
299-
return f;
300-
}
301-
return null;
302-
}
303282

304-
private KeyStore keyStore = null;
283+
KeyStore keyStore = null;
284+
305285

306286
/**
307287
* Gets the current key store
@@ -331,7 +311,7 @@ public KeyStore loadKeyStore() {
331311
* @return KeyStore instance, or null if does not exist
332312
*/
333313
public KeyStore loadKeyStore(boolean isCreate, char[] password) {
334-
File keyFile = getKeyStoreFile();
314+
File keyFile = storeMixin.getKeyStoreFile();
335315
try {
336316
if (keyFile.exists()) {
337317
keyStore = PFXTools.loadStore(keyFile, password);
@@ -373,7 +353,7 @@ public AKeyPair loadKeyFromStore(String publicKey) {
373353

374354
char[] storePassword = getStorePassword();
375355

376-
File keyFile = getKeyStoreFile();
356+
File keyFile = storeMixin.getKeyStoreFile();
377357
try {
378358
if (!keyFile.exists()) {
379359
throw new CLIError("Cannot find keystore file " + keyFile.getCanonicalPath());
@@ -450,15 +430,15 @@ public void saveKeyStore(char[] storePassword) {
450430
if (keyStore == null)
451431
throw new CLIError("Trying to save a keystore that has not been loaded!");
452432
try {
453-
PFXTools.saveStore(keyStore, getKeyStoreFile(), storePassword);
433+
PFXTools.saveStore(keyStore, storeMixin.getKeyStoreFile(), storePassword);
454434
} catch (Throwable t) {
455435
throw Utils.sneakyThrow(t);
456436
}
457437
}
458438

459439
public void saveKeyStore() {
460-
if (keystorePassword==null) throw new CLIError("Key store password not provided");
461-
saveKeyStore(keystorePassword.toCharArray());
440+
if (storeMixin.keystorePassword==null) throw new CLIError("Key store password not provided");
441+
saveKeyStore(storeMixin.keystorePassword.toCharArray());
462442
}
463443

464444
public boolean isParanoid() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package convex.cli;
2+
3+
import java.io.File;
4+
5+
import convex.core.util.Utils;
6+
import picocli.CommandLine.Option;
7+
import picocli.CommandLine.ScopeType;
8+
9+
public class StoreMixin {
10+
11+
@Option(names = { "--keystore" },
12+
defaultValue = "${env:CONVEX_KEYSTORE:-" + Constants.KEYSTORE_FILENAME+ "}",
13+
scope = ScopeType.INHERIT,
14+
description = "Keystore filename. Default: ${DEFAULT-VALUE}")
15+
private String keyStoreFilename;
16+
17+
/**
18+
* Password for keystore. Option named to match Java keytool
19+
*/
20+
@Option(names = {"--storepass" },
21+
scope = ScopeType.INHERIT,
22+
defaultValue = "${env:CONVEX_KEYSTORE_PASSWORD}",
23+
description = "Password to read/write to the Keystore")
24+
String keystorePassword;
25+
26+
/**
27+
* Gets the keystore file name currently used for the CLI
28+
*
29+
* @return File name, or null if not specified
30+
*/
31+
public File getKeyStoreFile() {
32+
if (keyStoreFilename != null) {
33+
File f = Utils.getPath(keyStoreFilename);
34+
return f;
35+
}
36+
return null;
37+
}
38+
39+
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ public class KeyExport extends AKeyCommand {
3131

3232
@ParentCommand
3333
protected Key keyParent;
34-
35-
@Option(names = {"--public-key" },
36-
description = "Hex string of the public key in the Keystore to use for the peer.%n"
37-
+ "You only need to enter in the first distinct hex values of the public key.%n"
38-
+ "For example: 0xf0234 or f0234")
39-
private String keystorePublicKey ;
4034

4135
@Option(names={"-o", "--output-file"},
4236
description="Output file for the private key. Use '-' for STDOUT (default).")
@@ -61,17 +55,23 @@ private void ensureExportPassword() {
6155
if (cli().isParanoid()) {
6256
throw new CLIError("Strict security: attempting to export PEM with no passphrase.");
6357
} else {
64-
log.warn("No export password '--export-password' provided: Defaulting to blank.");
58+
log.warn("No export passphrase '--export-password' provided: Defaulting to blank.");
6559
}
6660
exportPassword="";
6761
}
6862
}
6963

7064
@Override
7165
public void run() {
66+
String keystorePublicKey=cli().publicKey;
7267
if ((keystorePublicKey == null)||(keystorePublicKey.isEmpty())) {
73-
log.warn("You need to provide at least --public-key parameter");
74-
return;
68+
if (outputFilename==null) {
69+
cli().inform("You must provide a --key parameter");
70+
showUsage();
71+
return;
72+
}
73+
74+
keystorePublicKey=cli().prompt("Enter public key to export: ");
7575
}
7676

7777
String publicKey = keystorePublicKey;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void run() {
9696
try {
9797
keyPair = PEMTools.decryptPrivateKeyFromPEM(importText, importPassphrase.toCharArray());
9898
} catch (Exception e) {
99-
throw new CLIError("Cannot decode PEM",e);
99+
throw new CLIError("Cannot decode PEM. File may be corrupt or wrong passphrase used.",e);
100100
}
101101
}
102102
if (keyPair==null) throw new CLIError("Unable to import keypair");

convex-cli/src/main/java/convex/cli/peer/PeerCreate.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package convex.cli.peer;
22

3-
import java.io.File;
43
import java.security.KeyStore;
54

65
import org.slf4j.Logger;
@@ -87,11 +86,7 @@ public void run() {
8786

8887
// save the new keypair in the keystore
8988
PFXTools.setKeyPair(keyStore, keyPair, mainParent.getKeyPassword());
90-
91-
File keyFile = mainParent.getKeyStoreFile();
92-
93-
// save the store to a file
94-
PFXTools.saveStore(keyStore, keyFile, mainParent.getStorePassword());
89+
mainParent.saveKeyStore();
9590

9691
// connect using the default first user
9792
Convex convex = mainParent.connect();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class KeyExportTest {
2121
private static final char[] KEYSTORE_PASSWORD = "testPassword".toCharArray();
22-
private static final char[] KEY_PASSWORD = "testKeytPassword".toCharArray();
22+
private static final char[] KEY_PASSWORD = "testKeyPassword".toCharArray();
2323
private static final char[] EXPORT_PASSWORD = "testExportPassword".toCharArray();
2424

2525
private static final File TEMP_FILE;
@@ -66,7 +66,7 @@ public void testKeyGenerateAndExport() throws Exception {
6666
"--storepass", new String(KEYSTORE_PASSWORD),
6767
"--keypass", new String(KEY_PASSWORD),
6868
"--keystore", KEYSTORE_FILENAME,
69-
"--public-key", publicKey,
69+
"--key", publicKey,
7070
"--export-password", new String(EXPORT_PASSWORD)
7171
);
7272
String s=tester.getOutput();
@@ -83,7 +83,7 @@ public void testKeyGenerateAndExport() throws Exception {
8383
"--storepass", new String(KEYSTORE_PASSWORD),
8484
"--keypass", new String(KEY_PASSWORD),
8585
"--keystore", KEYSTORE_FILENAME,
86-
"--public-key", publicKey
86+
"--key", publicKey
8787
);
8888
String s2=tester.getOutput();
8989
assertEquals(ExitCodes.SUCCESS,tester.getResult());

0 commit comments

Comments
 (0)