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 // Compare version strings
implementation 'org.apache.maven:maven-artifact:3.6.3' implementation 'org.apache.maven:maven-artifact:3.6.3'
// @Nonnull, @Nullable...
implementation "com.google.code.findbugs:jsr305:$jsrVersion"
} }
test { test {

View file

@ -33,9 +33,11 @@ import sop.operation.InlineVerify;
import sop.operation.Version; import sop.operation.Version;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -44,14 +46,24 @@ public class ExternalSOP implements SOP {
private final String binaryName; private final String binaryName;
private final Properties properties; private final Properties properties;
private final TempDirProvider tempDirProvider;
public ExternalSOP(String binaryName) { public ExternalSOP(String binaryName) {
this(binaryName, new Properties()); this(binaryName, new Properties());
} }
public ExternalSOP(String binaryName, Properties 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.binaryName = binaryName;
this.properties = properties; this.properties = properties;
this.tempDirProvider = tempDirProvider;
} }
@Override @Override
@ -101,7 +113,7 @@ public class ExternalSOP implements SOP {
@Override @Override
public Decrypt decrypt() { public Decrypt decrypt() {
return new DecryptExternal(binaryName, properties); return new DecryptExternal(binaryName, properties, tempDirProvider);
} }
@Override @Override
@ -303,4 +315,17 @@ public class ExternalSOP implements SOP {
throw new RuntimeException(e); 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.operation.Decrypt;
import sop.util.UTCUtil; import sop.util.UTCUtil;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -23,6 +24,7 @@ import java.util.Properties;
public class DecryptExternal implements Decrypt { public class DecryptExternal implements Decrypt {
private final ExternalSOP.TempDirProvider tempDirProvider;
private final List<String> commandList = new ArrayList<>(); private final List<String> commandList = new ArrayList<>();
private final List<String> envList; private final List<String> envList;
@ -32,7 +34,8 @@ public class DecryptExternal implements Decrypt {
private int keyCounter = 0; private int keyCounter = 0;
private int withKeyPasswordCounter = 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(binary);
this.commandList.add("decrypt"); this.commandList.add("decrypt");
this.envList = ExternalSOP.propertiesToEnv(environment); this.envList = ExternalSOP.propertiesToEnv(environment);
@ -41,14 +44,14 @@ public class DecryptExternal implements Decrypt {
@Override @Override
public Decrypt verifyNotBefore(Date timestamp) public Decrypt verifyNotBefore(Date timestamp)
throws SOPGPException.UnsupportedOption { throws SOPGPException.UnsupportedOption {
this.commandList.add("--not-before=" + UTCUtil.formatUTCDate(timestamp)); this.commandList.add("--verify-not-before=" + UTCUtil.formatUTCDate(timestamp));
return this; return this;
} }
@Override @Override
public Decrypt verifyNotAfter(Date timestamp) public Decrypt verifyNotAfter(Date timestamp)
throws SOPGPException.UnsupportedOption { throws SOPGPException.UnsupportedOption {
this.commandList.add("--not-after=" + UTCUtil.formatUTCDate(timestamp)); this.commandList.add("--verify-not-after=" + UTCUtil.formatUTCDate(timestamp));
return this; return this;
} }
@ -101,6 +104,14 @@ public class DecryptExternal implements Decrypt {
public ReadyWithResult<DecryptionResult> ciphertext(InputStream ciphertext) public ReadyWithResult<DecryptionResult> ciphertext(InputStream ciphertext)
throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt, throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt,
SOPGPException.KeyIsProtected, IOException { 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[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]); String[] env = envList.toArray(new String[0]);
try { try {