mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-08 17:59:43 +02:00
Rename module to external-sop and make backend in tests configurable
This commit is contained in:
parent
28912618ea
commit
e602cc16cc
12 changed files with 80 additions and 30 deletions
27
external-sop/build.gradle
Normal file
27
external-sop/build.gradle
Normal file
|
@ -0,0 +1,27 @@
|
|||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
plugins {
|
||||
id 'java-library'
|
||||
}
|
||||
|
||||
group 'org.pgpainless'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
|
||||
api project(":sop-java")
|
||||
|
||||
api "org.slf4j:slf4j-api:$slf4jVersion"
|
||||
testImplementation "ch.qos.logback:logback-classic:$logbackVersion"
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
91
external-sop/src/main/java/sop/external/ExternalSOP.java
vendored
Normal file
91
external-sop/src/main/java/sop/external/ExternalSOP.java
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external;
|
||||
|
||||
import sop.SOP;
|
||||
import sop.external.operation.ExtractCertExternal;
|
||||
import sop.external.operation.GenerateKeyExternal;
|
||||
import sop.external.operation.VersionExternal;
|
||||
import sop.operation.Armor;
|
||||
import sop.operation.Dearmor;
|
||||
import sop.operation.Decrypt;
|
||||
import sop.operation.DetachedSign;
|
||||
import sop.operation.DetachedVerify;
|
||||
import sop.operation.Encrypt;
|
||||
import sop.operation.ExtractCert;
|
||||
import sop.operation.GenerateKey;
|
||||
import sop.operation.InlineDetach;
|
||||
import sop.operation.InlineSign;
|
||||
import sop.operation.InlineVerify;
|
||||
import sop.operation.Version;
|
||||
|
||||
public class ExternalSOP implements SOP {
|
||||
|
||||
private final String binaryName;
|
||||
|
||||
public ExternalSOP(String binaryName) {
|
||||
this.binaryName = binaryName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Version version() {
|
||||
return new VersionExternal(binaryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey generateKey() {
|
||||
return new GenerateKeyExternal(binaryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtractCert extractCert() {
|
||||
return new ExtractCertExternal(binaryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DetachedSign detachedSign() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InlineSign inlineSign() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DetachedVerify detachedVerify() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InlineVerify inlineVerify() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InlineDetach inlineDetach() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Encrypt encrypt() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Decrypt decrypt() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Armor armor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dearmor dearmor() {
|
||||
return null;
|
||||
}
|
||||
}
|
69
external-sop/src/main/java/sop/external/operation/ExtractCertExternal.java
vendored
Normal file
69
external-sop/src/main/java/sop/external/operation/ExtractCertExternal.java
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation;
|
||||
|
||||
import sop.Ready;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.ExtractCert;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExtractCertExternal implements ExtractCert {
|
||||
|
||||
private final String binary;
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
private boolean noArmor;
|
||||
|
||||
public ExtractCertExternal(String binary) {
|
||||
this.binary = binary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtractCert noArmor() {
|
||||
this.noArmor = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ready key(InputStream keyInputStream) throws IOException, SOPGPException.BadData {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
|
||||
commandList.add(binary);
|
||||
commandList.add("extract-cert");
|
||||
|
||||
if (noArmor) {
|
||||
commandList.add("--no-armor");
|
||||
}
|
||||
|
||||
String[] command = commandList.toArray(new String[0]);
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
OutputStream stdOut = process.getOutputStream();
|
||||
InputStream stdIn = process.getInputStream();
|
||||
|
||||
return new Ready() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
byte[] buf = new byte[4096];
|
||||
int r;
|
||||
while ((r = keyInputStream.read(buf)) > 0) {
|
||||
stdOut.write(buf, 0, r);
|
||||
}
|
||||
|
||||
while ((r = stdIn.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0 , r);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
89
external-sop/src/main/java/sop/external/operation/GenerateKeyExternal.java
vendored
Normal file
89
external-sop/src/main/java/sop/external/operation/GenerateKeyExternal.java
vendored
Normal file
|
@ -0,0 +1,89 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation;
|
||||
|
||||
import sop.Ready;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.GenerateKey;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateKeyExternal implements GenerateKey {
|
||||
|
||||
private final String binary;
|
||||
private boolean noArmor = false;
|
||||
private List<String> userIds = new ArrayList<>();
|
||||
private String keyPassword;
|
||||
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
public GenerateKeyExternal(String binary) {
|
||||
this.binary = binary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey noArmor() {
|
||||
this.noArmor = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey userId(String userId) {
|
||||
this.userIds.add(userId);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey withKeyPassword(String password)
|
||||
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
this.keyPassword = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ready generate()
|
||||
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
|
||||
commandList.add(binary);
|
||||
commandList.add("generate-key");
|
||||
|
||||
if (noArmor) {
|
||||
commandList.add("--no-armor");
|
||||
}
|
||||
|
||||
if (keyPassword != null) {
|
||||
commandList.add("--with-key-password");
|
||||
commandList.add(keyPassword);
|
||||
}
|
||||
|
||||
for (String userId : userIds) {
|
||||
commandList.add(userId);
|
||||
}
|
||||
|
||||
String[] command = commandList.toArray(new String[0]);
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
InputStream stdIn = process.getInputStream();
|
||||
|
||||
return new Ready() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
byte[] buf = new byte[4096];
|
||||
int r;
|
||||
while ((r = stdIn.read(buf)) >= 0) {
|
||||
outputStream.write(buf, 0, r);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
87
external-sop/src/main/java/sop/external/operation/VersionExternal.java
vendored
Normal file
87
external-sop/src/main/java/sop/external/operation/VersionExternal.java
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation;
|
||||
|
||||
import sop.operation.Version;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class VersionExternal implements Version {
|
||||
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
private final String binary;
|
||||
|
||||
public VersionExternal(String binaryName) {
|
||||
this.binary = binaryName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String[] command = new String[] {binary, "version"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line = stdInput.readLine().trim();
|
||||
if (line.contains(" ")) {
|
||||
return line.substring(0, line.lastIndexOf(" "));
|
||||
}
|
||||
return line;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
String[] command = new String[] {binary, "version"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line = stdInput.readLine().trim();
|
||||
if (line.contains(" ")) {
|
||||
return line.substring(line.lastIndexOf(" ") + 1);
|
||||
}
|
||||
return line;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBackendVersion() {
|
||||
String[] command = new String[] {binary, "version", "--backend"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = stdInput.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExtendedVersion() {
|
||||
String[] command = new String[] {binary, "version", "--extended"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = stdInput.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
8
external-sop/src/main/java/sop/external/operation/package-info.java
vendored
Normal file
8
external-sop/src/main/java/sop/external/operation/package-info.java
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Bindings for SOP subcommands to a SOP binary.
|
||||
*/
|
||||
package sop.external.operation;
|
8
external-sop/src/main/java/sop/external/package-info.java
vendored
Normal file
8
external-sop/src/main/java/sop/external/package-info.java
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Implementation of sop-java which delegates execution to a binary implementing the SOP command line interface.
|
||||
*/
|
||||
package sop.external;
|
5
external-sop/src/main/resources/sop/external/.gitignore
vendored
Normal file
5
external-sop/src/main/resources/sop/external/.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
# SPDX-FileCopyrightText: 2023 Paul Schaub <info@pgpainless.org>
|
||||
#
|
||||
# SPDX-License-Identifier: CC0-1.0
|
||||
|
||||
backend.local.properties
|
7
external-sop/src/main/resources/sop/external/backend.properties
vendored
Normal file
7
external-sop/src/main/resources/sop/external/backend.properties
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
# SPDX-FileCopyrightText: 2023 Paul Schaub <info@pgpainless.org>
|
||||
#
|
||||
# SPDX-License-Identifier: CC0-1.0
|
||||
|
||||
## Do not change this file. To overwrite the SOP backend used during testing,
|
||||
## simply create a file 'backend.local.properties' in this directory and override sop.backend in there.
|
||||
sop.backend=/path/to/backend
|
100
external-sop/src/test/java/sop/external/ExternalSOPTest.java
vendored
Normal file
100
external-sop/src/test/java/sop/external/ExternalSOPTest.java
vendored
Normal file
|
@ -0,0 +1,100 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sop.SOP;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@EnabledIf("sop.external.ExternalSOPTest#externalSopInstalled")
|
||||
public class ExternalSOPTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSOPTest.class);
|
||||
|
||||
private final SOP sop;
|
||||
|
||||
public ExternalSOPTest() {
|
||||
String backend = readSopBackendFromProperties();
|
||||
sop = new ExternalSOP(backend);
|
||||
}
|
||||
|
||||
private static String readSopBackendFromProperties() {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
InputStream resourceIn = ExternalSOPTest.class.getResourceAsStream("backend.local.properties");
|
||||
if (resourceIn == null) {
|
||||
LOGGER.info("Could not find backend.local.properties file. Try backend.properties instead.");
|
||||
resourceIn = ExternalSOPTest.class.getResourceAsStream("backend.properties");
|
||||
}
|
||||
if (resourceIn == null) {
|
||||
throw new FileNotFoundException("Could not find backend.properties file.");
|
||||
}
|
||||
|
||||
properties.load(resourceIn);
|
||||
String backend = properties.getProperty("sop.backend");
|
||||
return backend;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean externalSopInstalled() {
|
||||
String binary = readSopBackendFromProperties();
|
||||
if (binary == null) {
|
||||
return false;
|
||||
}
|
||||
return new File(binary).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void versionNameTest() {
|
||||
assertEquals("sqop", sop.version().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void versionVersionTest() {
|
||||
String version = sop.version().getVersion();
|
||||
assertTrue(version.matches("\\d+(\\.\\d+)*"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void backendVersionTest() {
|
||||
String backend = sop.version().getBackendVersion();
|
||||
assertFalse(backend.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extendedVersionTest() {
|
||||
String extended = sop.version().getExtendedVersion();
|
||||
assertFalse(extended.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateKeyTest() throws IOException {
|
||||
String key = new String(sop.generateKey().userId("Alice").generate().getBytes());
|
||||
assertTrue(key.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void extractCertTest() throws IOException {
|
||||
InputStream keyIn = sop.generateKey().userId("Alice").generate().getInputStream();
|
||||
String cert = new String(sop.extractCert().key(keyIn).getBytes());
|
||||
assertTrue(cert.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----\n"));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue