Code cleanup

This commit is contained in:
Paul Schaub 2023-01-22 15:07:17 +01:00
parent e73c7e5f91
commit 8cacf7dd57
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
13 changed files with 34 additions and 21 deletions

View file

@ -179,14 +179,17 @@ public class ExternalSOP implements SOP {
* @throws IOException in case of an IO error * @throws IOException in case of an IO error
*/ */
private static void mapExitCodeOrException(@Nonnull Process process) throws InterruptedException, IOException { private static void mapExitCodeOrException(@Nonnull Process process) throws InterruptedException, IOException {
// wait for process termination
int exitCode = process.waitFor(); int exitCode = process.waitFor();
if (exitCode == 0) { if (exitCode == 0) {
// we're good, bye
return; return;
} }
// Read error message
InputStream errIn = process.getErrorStream(); InputStream errIn = process.getErrorStream();
String errorMessage = readFully(errIn); String errorMessage = readString(errIn);
switch (exitCode) { switch (exitCode) {
case SOPGPException.NoSignature.EXIT_CODE: case SOPGPException.NoSignature.EXIT_CODE:
@ -285,7 +288,7 @@ public class ExternalSOP implements SOP {
* @return string * @return string
* @throws IOException in case of an IO error * @throws IOException in case of an IO error
*/ */
public static String readFully(@Nonnull InputStream inputStream) throws IOException { public static String readString(@Nonnull InputStream inputStream) throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ByteArrayOutputStream bOut = new ByteArrayOutputStream();
byte[] buf = new byte[4096]; byte[] buf = new byte[4096];
int r; int r;
@ -297,13 +300,16 @@ public class ExternalSOP implements SOP {
/** /**
* Execute the given command on the given {@link Runtime} with the given list of environment variables. * Execute the given command on the given {@link Runtime} with the given list of environment variables.
* This command does not transform any input data, and instead is purely a producer.
* *
* @param runtime runtime * @param runtime runtime
* @param commandList command * @param commandList command
* @param envList environment variables * @param envList environment variables
* @return ready to read the result from * @return ready to read the result from
*/ */
public static Ready ready(@Nonnull Runtime runtime, @Nonnull List<String> commandList, @Nonnull List<String> envList) { public static Ready executeProducingOperation(@Nonnull Runtime runtime,
@Nonnull List<String> commandList,
@Nonnull List<String> envList) {
String[] command = commandList.toArray(new String[0]); String[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]); String[] env = envList.toArray(new String[0]);
@ -322,6 +328,7 @@ public class ExternalSOP implements SOP {
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
ExternalSOP.finish(process); ExternalSOP.finish(process);
} }
}; };
@ -333,6 +340,7 @@ public class ExternalSOP implements SOP {
/** /**
* Execute the given command on the given runtime using the given environment variables. * Execute the given command on the given runtime using the given environment variables.
* The given input stream provides input for the process. * The given input stream provides input for the process.
* This command is a transformation, meaning it is given input data and transforms it into output data.
* *
* @param runtime runtime * @param runtime runtime
* @param commandList command * @param commandList command
@ -340,7 +348,7 @@ public class ExternalSOP implements SOP {
* @param standardIn stream of input data for the process * @param standardIn stream of input data for the process
* @return ready to read the result from * @return ready to read the result from
*/ */
public static Ready ready(@Nonnull Runtime runtime, @Nonnull List<String> commandList, @Nonnull List<String> envList, @Nonnull InputStream standardIn) { public static Ready executeTransformingOperation(@Nonnull Runtime runtime, @Nonnull List<String> commandList, @Nonnull List<String> envList, @Nonnull InputStream standardIn) {
String[] command = commandList.toArray(new String[0]); String[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]); String[] env = envList.toArray(new String[0]);
try { try {
@ -362,7 +370,10 @@ public class ExternalSOP implements SOP {
processOut.flush(); processOut.flush();
processOut.close(); processOut.close();
} catch (IOException e) { } catch (IOException e) {
// ignore // Perhaps the stream is already closed, in which case we ignore the exception.
if (!"Stream closed".equals(e.getMessage())) {
throw e;
}
} }
while ((r = processIn.read(buf)) > 0) { while ((r = processIn.read(buf)) > 0) {

View file

@ -38,6 +38,6 @@ public class ArmorExternal implements Armor {
@Override @Override
public Ready data(InputStream data) throws SOPGPException.BadData, IOException { public Ready data(InputStream data) throws SOPGPException.BadData, IOException {
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList, data); return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, data);
} }
} }

View file

@ -31,6 +31,6 @@ public class DearmorExternal implements Dearmor {
@Override @Override
public Ready data(InputStream data) throws SOPGPException.BadData, IOException { public Ready data(InputStream data) throws SOPGPException.BadData, IOException {
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList, data); return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, data);
} }
} }

View file

@ -66,7 +66,7 @@ public class DecryptExternal implements Decrypt {
throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException { throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
String envVar = "VERIFY_WITH_" + verifyWithCounter++; String envVar = "VERIFY_WITH_" + verifyWithCounter++;
commandList.add("--verify-with=@ENV:" + envVar); commandList.add("--verify-with=@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(cert)); envList.add(envVar + "=" + ExternalSOP.readString(cert));
return this; return this;
} }
@ -93,7 +93,7 @@ public class DecryptExternal implements Decrypt {
throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException { throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
String envVar = "KEY_" + keyCounter++; String envVar = "KEY_" + keyCounter++;
commandList.add("@ENV:" + envVar); commandList.add("@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(key)); envList.add(envVar + "=" + ExternalSOP.readString(key));
return this; return this;
} }
@ -151,7 +151,7 @@ public class DecryptExternal implements Decrypt {
ExternalSOP.finish(process); ExternalSOP.finish(process);
FileInputStream sessionKeyOutIn = new FileInputStream(sessionKeyOut); FileInputStream sessionKeyOutIn = new FileInputStream(sessionKeyOut);
String line = ExternalSOP.readFully(sessionKeyOutIn); String line = ExternalSOP.readString(sessionKeyOutIn);
SessionKey sessionKey = SessionKey.fromString(line.trim()); SessionKey sessionKey = SessionKey.fromString(line.trim());
sessionKeyOutIn.close(); sessionKeyOutIn.close();
sessionKeyOut.delete(); sessionKeyOut.delete();

View file

@ -52,7 +52,7 @@ public class DetachedSignExternal implements DetachedSign {
public DetachedSign key(InputStream key) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException { public DetachedSign key(InputStream key) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
String envVar = "KEY_" + keyCounter++; String envVar = "KEY_" + keyCounter++;
commandList.add("@ENV:" + envVar); commandList.add("@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(key)); envList.add(envVar + "=" + ExternalSOP.readString(key));
return this; return this;
} }

View file

@ -68,12 +68,12 @@ public class DetachedVerifyExternal implements DetachedVerify {
@Override @Override
public List<Verification> data(InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData { public List<Verification> data(InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
commandList.add("@ENV:SIGNATURE"); commandList.add("@ENV:SIGNATURE");
envList.add("SIGNATURE=" + ExternalSOP.readFully(signatures)); envList.add("SIGNATURE=" + ExternalSOP.readString(signatures));
for (InputStream cert : certs) { for (InputStream cert : certs) {
String envVar = "CERT_" + certCounter++; String envVar = "CERT_" + certCounter++;
commandList.add("@ENV:" + envVar); commandList.add("@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(cert)); envList.add(envVar + "=" + ExternalSOP.readString(cert));
} }
String[] command = commandList.toArray(new String[0]); String[] command = commandList.toArray(new String[0]);

View file

@ -53,7 +53,7 @@ public class EncryptExternal implements Encrypt {
IOException { IOException {
String envVar = "SIGN_WITH_" + SIGN_WITH_COUNTER++; String envVar = "SIGN_WITH_" + SIGN_WITH_COUNTER++;
commandList.add("--sign-with=@ENV:" + envVar); commandList.add("--sign-with=@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(key)); envList.add(envVar + "=" + ExternalSOP.readString(key));
return this; return this;
} }
@ -81,13 +81,13 @@ public class EncryptExternal implements Encrypt {
IOException { IOException {
String envVar = "CERT_" + CERT_COUNTER++; String envVar = "CERT_" + CERT_COUNTER++;
commandList.add("@ENV:" + envVar); commandList.add("@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(cert)); envList.add(envVar + "=" + ExternalSOP.readString(cert));
return this; return this;
} }
@Override @Override
public Ready plaintext(InputStream plaintext) public Ready plaintext(InputStream plaintext)
throws IOException, SOPGPException.KeyIsProtected { throws IOException, SOPGPException.KeyIsProtected {
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList, plaintext); return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, plaintext);
} }
} }

View file

@ -36,6 +36,6 @@ public class ExtractCertExternal implements ExtractCert {
@Override @Override
public Ready key(InputStream keyInputStream) throws SOPGPException.BadData { public Ready key(InputStream keyInputStream) throws SOPGPException.BadData {
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList, keyInputStream); return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, keyInputStream);
} }
} }

View file

@ -54,6 +54,6 @@ public class GenerateKeyExternal implements GenerateKey {
@Override @Override
public Ready generate() public Ready generate()
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo { throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList); return ExternalSOP.executeProducingOperation(Runtime.getRuntime(), commandList, envList);
} }
} }

View file

@ -43,7 +43,7 @@ public class InlineSignExternal implements InlineSign {
public InlineSign key(InputStream key) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException { public InlineSign key(InputStream key) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
String envVar = "KEY_" + keyCounter++; String envVar = "KEY_" + keyCounter++;
commandList.add("@ENV:" + envVar); commandList.add("@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(key)); envList.add(envVar + "=" + ExternalSOP.readString(key));
return this; return this;
} }
@ -63,6 +63,6 @@ public class InlineSignExternal implements InlineSign {
@Override @Override
public Ready data(InputStream data) throws IOException, SOPGPException.KeyIsProtected, SOPGPException.ExpectedText { public Ready data(InputStream data) throws IOException, SOPGPException.KeyIsProtected, SOPGPException.ExpectedText {
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList, data); return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, data);
} }
} }

View file

@ -57,7 +57,7 @@ public class InlineVerifyExternal implements InlineVerify {
public InlineVerify cert(InputStream cert) throws SOPGPException.BadData, IOException { public InlineVerify cert(InputStream cert) throws SOPGPException.BadData, IOException {
String envVar = "CERT_" + certCounter++; String envVar = "CERT_" + certCounter++;
commandList.add("@ENV:" + envVar); commandList.add("@ENV:" + envVar);
envList.add(envVar + "=" + ExternalSOP.readFully(cert)); envList.add(envVar + "=" + ExternalSOP.readString(cert));
return this; return this;
} }

View file

@ -230,6 +230,7 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
@Test @Test
public void verifyMissingCertCausesMissingArg() { public void verifyMissingCertCausesMissingArg() {
ignoreIf("sqop", Is.leq, "0.27.3");
ignoreIf("PGPainless-SOP", Is.geq, "0.0.0"); // PGPainless uses picocli which throws ignoreIf("PGPainless-SOP", Is.geq, "0.0.0"); // PGPainless uses picocli which throws
// UNSUPPORTED_OPTION for missing arg // UNSUPPORTED_OPTION for missing arg
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);

View file

@ -281,6 +281,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
@Test @Test
public void missingArgsTest() throws IOException { public void missingArgsTest() throws IOException {
ignoreIf("sqop", Is.leq, "0.27.3");
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
assertThrows(SOPGPException.MissingArg.class, () -> getSop().encrypt() assertThrows(SOPGPException.MissingArg.class, () -> getSop().encrypt()