Skip to content

Commit

Permalink
Updates to key import for more generic usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 25, 2024
1 parent c6ad1f4 commit 6b1e9c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
23 changes: 23 additions & 0 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.UnrecoverableKeyException;
Expand Down Expand Up @@ -456,5 +460,24 @@ public char[] readPassword(String prompt) {
return c.readPassword(prompt);
}

public String loadTextFile(String fname) {
String result=null;
try {
fname=fname.trim();
if ("-".equals(fname)) {
byte[] bs=System.in.readAllBytes();
result = new String(bs);
}
Path path=Paths.get(fname);
if (!path.toFile().exists()) {
throw new CLIError("Import file does not exist: "+path);
}
result = Files.readString(path, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new CLIError("Unable to read import file",e);
}
return result;
}


}
38 changes: 17 additions & 21 deletions convex-cli/src/main/java/convex/cli/key/KeyImport.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package convex.cli.key;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.Console;
import java.security.PrivateKey;

import org.bouncycastle.util.Arrays;
Expand Down Expand Up @@ -40,34 +36,34 @@ public class KeyImport extends AKeyCommand {
description="Import file for the the keypair.")
private String importFilename;

@Option(names={"--pem-text"},
description="PEM format text to import.")
@Option(names={"-t", "--text"},
description="Text string to import.")
private String importText;

@Option(names={"--pem-password"},
description="Password of the imported PEM key.")
@Option(names={"--import-password"},
description="Password for the imported key.")
private String importPassword;

@Override
public void run() {
// Ensure importText is filled
if (importFilename != null && importFilename.length() > 0) {
Path path=Paths.get(importFilename);
try {
if (!path.toFile().exists()) {
throw new CLIError("Import file does not exist: "+path);
}
importText = Files.readString(path, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new CLIError("Unable to read import file: "+path,e);
}
if (importText!=null) throw new CLIError("Please provide either --import-file or --text, not both!");
importText=cli().loadTextFile(importFilename);
}
if (importText == null || importText.length() == 0) {
throw new CLIError("You need to provide '--pem-text' or import filename '--import-file' to import a private key");
throw new CLIError("You need to provide '--text' or import filename '--import-file' to import a private key");
}

if (importPassword == null || importPassword.length() == 0) {
log.warn("You need to provide an import password '--import-password' of the imported encrypted PEM data");
if (importPassword == null) {
if (cli().isInteractive()) {
importPassword=new String(System.console().readPassword("Enter import password:"));
} else {
throw new CLIError("--import-password not provided during non-interatice import");
}
}



PrivateKey privateKey = PEMTools.decryptPrivateKeyFromPEM(importText, importPassword.toCharArray());
AKeyPair keyPair = AKeyPair.create(privateKey);
Expand Down
5 changes: 2 additions & 3 deletions convex-cli/src/test/java/convex/cli/key/KeyImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public void testKeyImport() {
"-n",
"--keystore-password", new String(KEYSTORE_PASSWORD),
"--keystore", KEYSTORE_FILENAME,
"--pem-text", pemText,
"--pem-password", new String(IMPORT_PASSWORD)
"--text", pemText,
"--import-password", new String(IMPORT_PASSWORD)
);
assertEquals("",tester.getError());
assertEquals(ExitCodes.SUCCESS,tester.getResult());

CLTester t2=CLTester.run(
Expand Down

0 comments on commit 6b1e9c1

Please sign in to comment.