Wip: Add TempDirProvider

This commit is contained in:
Paul Schaub 2023-01-12 13:53:49 +01:00
parent 3eb2503852
commit e3b618a0a8
4 changed files with 44 additions and 5 deletions

View File

@ -23,6 +23,9 @@ dependencies {
// Compare version strings
implementation 'org.apache.maven:maven-artifact:3.6.3'
// @Nonnull, @Nullable...
implementation "com.google.code.findbugs:jsr305:$jsrVersion"
}
test {

View File

@ -33,9 +33,11 @@ import sop.operation.InlineVerify;
import sop.operation.Version;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@ -44,14 +46,24 @@ public class ExternalSOP implements SOP {
private final String binaryName;
private final Properties properties;
private final TempDirProvider tempDirProvider;
public ExternalSOP(String binaryName) {
this(binaryName, new Properties());
}
public ExternalSOP(String binaryName, Properties properties) {
this(binaryName, properties, defaultTempDirProvider());
}
public ExternalSOP(String binaryName, TempDirProvider tempDirProvider) {
this(binaryName, new Properties(), tempDirProvider);
}
public ExternalSOP(String binaryName, Properties properties, TempDirProvider tempDirProvider) {
this.binaryName = binaryName;
this.properties = properties;
this.tempDirProvider = tempDirProvider;
}
@Override
@ -101,7 +113,7 @@ public class ExternalSOP implements SOP {
@Override
public Decrypt decrypt() {
return new DecryptExternal(binaryName, properties);
return new DecryptExternal(binaryName, properties, tempDirProvider);
}
@Override
@ -303,4 +315,17 @@ public class ExternalSOP implements SOP {
throw new RuntimeException(e);
}
}
public interface TempDirProvider {
File provideTempDirectory() throws IOException;
}
public static TempDirProvider defaultTempDirProvider() {
return new TempDirProvider() {
@Override
public File provideTempDirectory() throws IOException {
return Files.createTempDirectory("ext-sop").toFile();
}
};
}
}

View File

@ -12,6 +12,7 @@ import sop.external.ExternalSOP;
import sop.operation.Decrypt;
import sop.util.UTCUtil;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -23,6 +24,7 @@ import java.util.Properties;
public class DecryptExternal implements Decrypt {
private final ExternalSOP.TempDirProvider tempDirProvider;
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;
@ -32,7 +34,8 @@ public class DecryptExternal implements Decrypt {
private int keyCounter = 0;
private int withKeyPasswordCounter = 0;
public DecryptExternal(String binary, Properties environment) {
public DecryptExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
this.tempDirProvider = tempDirProvider;
this.commandList.add(binary);
this.commandList.add("decrypt");
this.envList = ExternalSOP.propertiesToEnv(environment);
@ -41,14 +44,14 @@ public class DecryptExternal implements Decrypt {
@Override
public Decrypt verifyNotBefore(Date timestamp)
throws SOPGPException.UnsupportedOption {
this.commandList.add("--not-before=" + UTCUtil.formatUTCDate(timestamp));
this.commandList.add("--verify-not-before=" + UTCUtil.formatUTCDate(timestamp));
return this;
}
@Override
public Decrypt verifyNotAfter(Date timestamp)
throws SOPGPException.UnsupportedOption {
this.commandList.add("--not-after=" + UTCUtil.formatUTCDate(timestamp));
this.commandList.add("--verify-not-after=" + UTCUtil.formatUTCDate(timestamp));
return this;
}
@ -101,6 +104,14 @@ public class DecryptExternal implements Decrypt {
public ReadyWithResult<DecryptionResult> ciphertext(InputStream ciphertext)
throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt,
SOPGPException.KeyIsProtected, IOException {
File tempDir = tempDirProvider.provideTempDirectory();
File sessionKeyOut = new File(tempDir, "session-key-out");
commandList.add("--session-key-out=" + sessionKeyOut.getAbsolutePath());
File verifyOut = new File(tempDir, "verify-out");
commandList.add("--verify-out=" + verifyOut.getAbsolutePath());
String[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]);
try {

View File

@ -94,7 +94,7 @@ public abstract class SOPGPException extends RuntimeException {
public static final int EXIT_CODE = 19;
public MissingArg() {
}
public MissingArg(String message) {