Skip to content

Commit

Permalink
Wip: Add TempDirProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Jan 12, 2023
1 parent cc6a707 commit 2af2c8e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions external-sop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 26 additions & 1 deletion external-sop/src/main/java/sop/external/ExternalSOP.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
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;
Expand All @@ -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
Expand Down Expand Up @@ -101,7 +113,7 @@ public Encrypt encrypt() {

@Override
public Decrypt decrypt() {
return new DecryptExternal(binaryName, properties);
return new DecryptExternal(binaryName, properties, tempDirProvider);
}

@Override
Expand Down Expand Up @@ -303,4 +315,17 @@ public void writeTo(OutputStream outputStream) throws IOException {
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();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sop.operation.Decrypt;
import sop.util.UTCUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -23,6 +24,7 @@

public class DecryptExternal implements Decrypt {

private final ExternalSOP.TempDirProvider tempDirProvider;
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;

Expand All @@ -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);
Expand All @@ -41,14 +44,14 @@ public DecryptExternal(String binary, Properties environment) {
@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;
}

Expand Down Expand Up @@ -101,6 +104,14 @@ public Decrypt withKeyPassword(byte[] password)
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 {
Expand Down
2 changes: 1 addition & 1 deletion sop-java/src/main/java/sop/exception/SOPGPException.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static class MissingArg extends SOPGPException {
public static final int EXIT_CODE = 19;

public MissingArg() {

}

public MissingArg(String message) {
Expand Down

0 comments on commit 2af2c8e

Please sign in to comment.