1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-22 20:32:05 +01:00

Wip: Remove allowNested, add FileUtil

This commit is contained in:
Paul Schaub 2021-08-19 16:44:29 +02:00
parent 772f69788b
commit 4e83281213
9 changed files with 129 additions and 132 deletions

View file

@ -16,8 +16,6 @@
package org.pgpainless.cli.commands; package org.pgpainless.cli.commands;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -101,43 +99,4 @@ public class ArmorTest {
assertTrue(armored.contains("SGVsbG8sIFdvcmxkIQo=")); assertTrue(armored.contains("SGVsbG8sIFdvcmxkIQo="));
} }
@Test
@FailOnSystemExit
public void doesNotNestArmorByDefault() {
String armored = "-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.69\n" +
"\n" +
"SGVsbG8sIFdvcmxkCg==\n" +
"=fkLo\n" +
"-----END PGP MESSAGE-----";
System.setIn(new ByteArrayInputStream(armored.getBytes(StandardCharsets.UTF_8)));
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
PGPainlessCLI.execute("armor");
assertEquals(armored, out.toString());
}
@Test
@FailOnSystemExit
public void testAllowNested() {
String armored = "-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.69\n" +
"\n" +
"SGVsbG8sIFdvcmxkCg==\n" +
"=fkLo\n" +
"-----END PGP MESSAGE-----";
System.setIn(new ByteArrayInputStream(armored.getBytes(StandardCharsets.UTF_8)));
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
PGPainlessCLI.execute("armor", "--allow-nested");
assertNotEquals(armored, out.toString());
assertTrue(out.toString().contains(
"LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tClZlcnNpb246IEJDUEcgdjEuNjkK\n" +
"ClNHVnNiRzhzSUZkdmNteGtDZz09Cj1ma0xvCi0tLS0tRU5EIFBHUCBNRVNTQUdF\n" +
"LS0tLS0="));
}
} }

View file

@ -18,9 +18,6 @@ package org.pgpainless.sop;
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.io.PushbackInputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
@ -32,38 +29,20 @@ import sop.operation.Armor;
public class ArmorImpl implements Armor { public class ArmorImpl implements Armor {
public static final byte[] ARMOR_START = "-----BEGIN PGP".getBytes(Charset.forName("UTF8"));
boolean allowNested = false;
@Override @Override
public Armor label(ArmorLabel label) throws SOPGPException.UnsupportedOption { public Armor label(ArmorLabel label) throws SOPGPException.UnsupportedOption {
throw new SOPGPException.UnsupportedOption("Setting custom Armor labels not supported."); throw new SOPGPException.UnsupportedOption("Setting custom Armor labels not supported.");
} }
@Override
public Armor allowNested() throws SOPGPException.UnsupportedOption {
allowNested = true;
return this;
}
@Override @Override
public Ready data(InputStream data) throws SOPGPException.BadData { public Ready data(InputStream data) throws SOPGPException.BadData {
return new Ready() { return new Ready() {
@Override @Override
public void writeTo(OutputStream outputStream) throws IOException { public void writeTo(OutputStream outputStream) throws IOException {
PushbackInputStream pbIn = new PushbackInputStream(data, ARMOR_START.length);
byte[] buffer = new byte[ARMOR_START.length];
int read = pbIn.read(buffer);
pbIn.unread(buffer, 0, read);
if (!allowNested && Arrays.equals(ARMOR_START, buffer)) {
Streams.pipeAll(pbIn, System.out);
} else {
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(System.out); ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(System.out);
Streams.pipeAll(pbIn, armor); Streams.pipeAll(data, armor);
armor.close(); armor.close();
} }
}
}; };
} }
} }

View file

@ -45,8 +45,7 @@ public class SignImpl implements Sign {
private boolean armor = true; private boolean armor = true;
private SignAs mode = SignAs.Binary; private SignAs mode = SignAs.Binary;
private List<PGPSecretKeyRing> keys = new ArrayList<>(); private final SigningOptions signingOptions = new SigningOptions();
private SigningOptions signingOptions = new SigningOptions();
@Override @Override
public Sign noArmor() { public Sign noArmor() {

View file

@ -0,0 +1,89 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sop.cli.picocli;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import sop.exception.SOPGPException;
public class FileUtil {
private static final String ERROR_AMBIGUOUS = "File name '%s' is ambiguous. File with the same name exists on the filesystem.";
private static final String ERROR_ENV_FOUND = "Environment variable '%s' not set.";
private static final String ERROR_OUTPUT_EXISTS = "Output file '%s' already exists.";
private static final String ERROR_INPUT_NOT_EXIST = "File '%s' does not exist.";
private static final String ERROR_CANNOT_CREATE_FILE = "Output file '%s' cannot be created: %s";
public static final String PRFX_ENV = "@ENV:";
public static final String PRFX_FD = "@FD:";
public static File getFile(String fileName) {
if (fileName == null) {
throw new NullPointerException("File name cannot be null.");
}
if (fileName.startsWith(PRFX_ENV)) {
if (new File(fileName).exists()) {
throw new SOPGPException.AmbiguousInput(String.format(ERROR_AMBIGUOUS, fileName));
}
String envName = fileName.substring(PRFX_ENV.length());
String envValue = System.getenv(envName);
if (envValue == null) {
throw new IllegalArgumentException(String.format(ERROR_ENV_FOUND, envName));
}
return new File(envValue);
} else if (fileName.startsWith(PRFX_FD)) {
if (new File(fileName).exists()) {
throw new SOPGPException.AmbiguousInput(String.format(ERROR_AMBIGUOUS, fileName));
}
throw new IllegalArgumentException("File descriptors not supported.");
}
return new File(fileName);
}
public static FileInputStream getFileInputStream(String fileName) {
File file = getFile(fileName);
try {
FileInputStream inputStream = new FileInputStream(file);
return inputStream;
} catch (FileNotFoundException e) {
throw new SOPGPException.MissingInput(String.format(ERROR_INPUT_NOT_EXIST, fileName), e);
}
}
public static File createNewFileOrThrow(File file) throws IOException {
if (file == null) {
throw new NullPointerException("File cannot be null.");
}
try {
if (!file.createNewFile()) {
throw new SOPGPException.OutputExists(String.format(ERROR_OUTPUT_EXISTS, file.getAbsolutePath()));
}
} catch (IOException e) {
throw new IOException(String.format(ERROR_CANNOT_CREATE_FILE, file.getAbsolutePath(), e.getMessage()));
}
return file;
}
}

View file

@ -33,21 +33,9 @@ public class ArmorCmd implements Runnable {
@CommandLine.Option(names = {"--label"}, description = "Label to be used in the header and tail of the armoring.", paramLabel = "{auto|sig|key|cert|message}") @CommandLine.Option(names = {"--label"}, description = "Label to be used in the header and tail of the armoring.", paramLabel = "{auto|sig|key|cert|message}")
ArmorLabel label; ArmorLabel label;
@CommandLine.Option(names = {"--allow-nested"}, description = "Allow additional armoring of already armored input")
boolean allowNested = false;
@Override @Override
public void run() { public void run() {
Armor armor = SopCLI.getSop().armor(); Armor armor = SopCLI.getSop().armor();
if (allowNested) {
try {
armor.allowNested();
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
Print.errln("Option --allow-nested is not supported.");
Print.trace(unsupportedOption);
System.exit(unsupportedOption.getExitCode());
}
}
if (label != null) { if (label != null) {
try { try {
armor.label(label); armor.label(label);

View file

@ -32,6 +32,7 @@ import sop.ReadyWithResult;
import sop.SessionKey; import sop.SessionKey;
import sop.Verification; import sop.Verification;
import sop.cli.picocli.DateParser; import sop.cli.picocli.DateParser;
import sop.cli.picocli.FileUtil;
import sop.cli.picocli.SopCLI; import sop.cli.picocli.SopCLI;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
import sop.operation.Decrypt; import sop.operation.Decrypt;
@ -42,8 +43,15 @@ import sop.util.HexUtil;
exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE) exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE)
public class DecryptCmd implements Runnable { public class DecryptCmd implements Runnable {
private static final String SESSION_KEY_OUT = "--session-key-out";
private static final String VERIFY_OUT = "--verify-out";
private static final String ERROR_UNSUPPORTED_OPTION = "Option '%s' is not supported.";
private static final String ERROR_FILE_NOT_EXIST = "File '%s' does not exist.";
private static final String ERROR_OUTPUT_OF_OPTION_EXISTS = "Target %s of option %s already exists.";
@CommandLine.Option( @CommandLine.Option(
names = {"--session-key-out"}, names = {SESSION_KEY_OUT},
description = "Can be used to learn the session key on successful decryption", description = "Can be used to learn the session key on successful decryption",
paramLabel = "SESSIONKEY") paramLabel = "SESSIONKEY")
File sessionKeyOut; File sessionKeyOut;
@ -60,7 +68,7 @@ public class DecryptCmd implements Runnable {
paramLabel = "PASSWORD") paramLabel = "PASSWORD")
List<String> withPassword = new ArrayList<>(); List<String> withPassword = new ArrayList<>();
@CommandLine.Option(names = {"--verify-out"}, @CommandLine.Option(names = {VERIFY_OUT},
description = "Produces signature verification status to the designated file", description = "Produces signature verification status to the designated file",
paramLabel = "VERIFICATIONS") paramLabel = "VERIFICATIONS")
File verifyOut; File verifyOut;
@ -92,7 +100,8 @@ public class DecryptCmd implements Runnable {
@Override @Override
public void run() { public void run() {
throwIfVerifyOutExists(verifyOut); throwIfOutputExists(verifyOut, VERIFY_OUT);
throwIfOutputExists(sessionKeyOut, SESSION_KEY_OUT);
Decrypt decrypt = SopCLI.getSop().decrypt(); Decrypt decrypt = SopCLI.getSop().decrypt();
if (decrypt == null) { if (decrypt == null) {
@ -107,7 +116,8 @@ public class DecryptCmd implements Runnable {
setDecryptWith(keys, decrypt); setDecryptWith(keys, decrypt);
if (verifyOut != null && certs.isEmpty()) { if (verifyOut != null && certs.isEmpty()) {
throw new SOPGPException.IncompleteVerification("--verify-out is requested, but no --verify-with was provided."); String errorMessage = "Option %s is requested, but no option %s was provided.";
throw new SOPGPException.IncompleteVerification(String.format(errorMessage, VERIFY_OUT, "--verify-with"));
} }
try { try {
@ -122,11 +132,19 @@ public class DecryptCmd implements Runnable {
} }
} }
private void throwIfOutputExists(File outputFile, String optionName) {
if (outputFile == null) {
return;
}
if (outputFile.exists()) {
throw new SOPGPException.OutputExists(String.format(ERROR_OUTPUT_OF_OPTION_EXISTS, outputFile.getAbsolutePath(), optionName));
}
}
private void writeVerifyOut(DecryptionResult result) throws IOException { private void writeVerifyOut(DecryptionResult result) throws IOException {
if (verifyOut != null) { if (verifyOut != null) {
if (!verifyOut.createNewFile()) { FileUtil.createNewFileOrThrow(verifyOut);
throw new IOException("Cannot create file " + verifyOut.getAbsolutePath());
}
try (FileOutputStream outputStream = new FileOutputStream(verifyOut)) { try (FileOutputStream outputStream = new FileOutputStream(verifyOut)) {
PrintWriter writer = new PrintWriter(outputStream); PrintWriter writer = new PrintWriter(outputStream);
for (Verification verification : result.getVerifications()) { for (Verification verification : result.getVerifications()) {
@ -141,9 +159,7 @@ public class DecryptCmd implements Runnable {
private void writeSessionKeyOut(DecryptionResult result) throws IOException { private void writeSessionKeyOut(DecryptionResult result) throws IOException {
if (sessionKeyOut != null) { if (sessionKeyOut != null) {
if (sessionKeyOut.exists()) { FileUtil.createNewFileOrThrow(sessionKeyOut);
throw new SOPGPException.OutputExists("Target " + sessionKeyOut.getAbsolutePath() + " of option --session-key-out already exists.");
}
try (FileOutputStream outputStream = new FileOutputStream(sessionKeyOut)) { try (FileOutputStream outputStream = new FileOutputStream(sessionKeyOut)) {
if (!result.getSessionKey().isPresent()) { if (!result.getSessionKey().isPresent()) {
@ -157,16 +173,6 @@ public class DecryptCmd implements Runnable {
} }
} }
private void throwIfVerifyOutExists(File verifyOut) throws SOPGPException.OutputExists {
if (verifyOut == null) {
return;
}
if (verifyOut.exists()) {
throw new SOPGPException.OutputExists("Target " + verifyOut.getAbsolutePath() + " of option --verify-out already exists.");
}
}
private void setDecryptWith(List<File> keys, Decrypt decrypt) { private void setDecryptWith(List<File> keys, Decrypt decrypt) {
for (File key : keys) { for (File key : keys) {
try (FileInputStream keyIn = new FileInputStream(key)) { try (FileInputStream keyIn = new FileInputStream(key)) {
@ -176,7 +182,7 @@ public class DecryptCmd implements Runnable {
} catch (SOPGPException.BadData badData) { } catch (SOPGPException.BadData badData) {
throw new SOPGPException.BadData("File " + key.getAbsolutePath() + " does not contain a private key.", badData); throw new SOPGPException.BadData("File " + key.getAbsolutePath() + " does not contain a private key.", badData);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new SOPGPException.MissingInput("File " + key.getAbsolutePath() + " does not exist.", e); throw new SOPGPException.MissingInput(String.format(ERROR_FILE_NOT_EXIST, key.getAbsolutePath()), e);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -188,7 +194,7 @@ public class DecryptCmd implements Runnable {
try (FileInputStream certIn = new FileInputStream(cert)) { try (FileInputStream certIn = new FileInputStream(cert)) {
decrypt.verifyWithCert(certIn); decrypt.verifyWithCert(certIn);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new SOPGPException.MissingInput("File " + cert.getAbsolutePath() + " does not exist.", e); throw new SOPGPException.MissingInput(String.format(ERROR_FILE_NOT_EXIST, cert.getAbsolutePath()), e);
} catch (SOPGPException.BadData badData) { } catch (SOPGPException.BadData badData) {
throw new SOPGPException.BadData("File " + cert.getAbsolutePath() + " does not contain a valid certificate.", badData); throw new SOPGPException.BadData("File " + cert.getAbsolutePath() + " does not contain a valid certificate.", badData);
} catch (IOException ioException) { } catch (IOException ioException) {
@ -210,7 +216,7 @@ public class DecryptCmd implements Runnable {
try { try {
decrypt.withSessionKey(new SessionKey(algorithm, key)); decrypt.withSessionKey(new SessionKey(algorithm, key));
} catch (SOPGPException.UnsupportedOption unsupportedOption) { } catch (SOPGPException.UnsupportedOption unsupportedOption) {
throw new SOPGPException.UnsupportedOption("Unsupported option '--with-session-key'.", unsupportedOption); throw new SOPGPException.UnsupportedOption(String.format(ERROR_UNSUPPORTED_OPTION, "--with-session-key"), unsupportedOption);
} }
} }
} }
@ -220,7 +226,7 @@ public class DecryptCmd implements Runnable {
try { try {
decrypt.withPassword(password); decrypt.withPassword(password);
} catch (SOPGPException.UnsupportedOption unsupportedOption) { } catch (SOPGPException.UnsupportedOption unsupportedOption) {
throw new SOPGPException.UnsupportedOption("Unsupported option '--with-password'.", unsupportedOption); throw new SOPGPException.UnsupportedOption(String.format(ERROR_UNSUPPORTED_OPTION, "--with-password"), unsupportedOption);
} }
} }
} }
@ -230,7 +236,7 @@ public class DecryptCmd implements Runnable {
try { try {
decrypt.verifyNotAfter(notAfterDate); decrypt.verifyNotAfter(notAfterDate);
} catch (SOPGPException.UnsupportedOption unsupportedOption) { } catch (SOPGPException.UnsupportedOption unsupportedOption) {
throw new SOPGPException.UnsupportedOption("Option '--not-after' not supported.", unsupportedOption); throw new SOPGPException.UnsupportedOption(String.format(ERROR_UNSUPPORTED_OPTION, "--not-after"), unsupportedOption);
} }
} }
@ -239,7 +245,7 @@ public class DecryptCmd implements Runnable {
try { try {
decrypt.verifyNotBefore(notBeforeDate); decrypt.verifyNotBefore(notBeforeDate);
} catch (SOPGPException.UnsupportedOption unsupportedOption) { } catch (SOPGPException.UnsupportedOption unsupportedOption) {
throw new SOPGPException.UnsupportedOption("Option '--not-before' not supported.", unsupportedOption); throw new SOPGPException.UnsupportedOption(String.format(ERROR_UNSUPPORTED_OPTION, "--not-before"), unsupportedOption);
} }
} }
} }

View file

@ -51,18 +51,6 @@ public class ArmorCmdTest {
SopCLI.setSopInstance(sop); SopCLI.setSopInstance(sop);
} }
@Test
public void assertAllowNestedIsCalledWhenFlagged() throws SOPGPException.UnsupportedOption {
SopCLI.main(new String[] {"armor", "--allow-nested"});
verify(armor, times(1)).allowNested();
}
@Test
public void assertAllowNestedIsNotCalledByDefault() throws SOPGPException.UnsupportedOption {
SopCLI.main(new String[] {"armor"});
verify(armor, never()).allowNested();
}
@Test @Test
public void assertLabelIsNotCalledByDefault() throws SOPGPException.UnsupportedOption { public void assertLabelIsNotCalledByDefault() throws SOPGPException.UnsupportedOption {
SopCLI.main(new String[] {"armor"}); SopCLI.main(new String[] {"armor"});
@ -97,14 +85,6 @@ public class ArmorCmdTest {
SopCLI.main(new String[] {"armor", "--label", "Sig"}); SopCLI.main(new String[] {"armor", "--label", "Sig"});
} }
@Test
@ExpectSystemExitWithStatus(37)
public void ifAllowNestedUnsupportedExit37() throws SOPGPException.UnsupportedOption {
when(armor.allowNested()).thenThrow(new SOPGPException.UnsupportedOption("Allowing nested Armor not supported."));
SopCLI.main(new String[] {"armor", "--allow-nested"});
}
@Test @Test
@ExpectSystemExitWithStatus(41) @ExpectSystemExitWithStatus(41)
public void ifBadDataExit41() throws SOPGPException.BadData { public void ifBadDataExit41() throws SOPGPException.BadData {

View file

@ -253,6 +253,10 @@ public abstract class SOPGPException extends RuntimeException {
public static final int EXIT_CODE = 73; public static final int EXIT_CODE = 73;
public AmbiguousInput(String message) {
super(message);
}
@Override @Override
public int getExitCode() { public int getExitCode() {
return EXIT_CODE; return EXIT_CODE;

View file

@ -31,13 +31,6 @@ public interface Armor {
*/ */
Armor label(ArmorLabel label) throws SOPGPException.UnsupportedOption; Armor label(ArmorLabel label) throws SOPGPException.UnsupportedOption;
/**
* Allow nested Armoring.
*
* @return builder instance
*/
Armor allowNested() throws SOPGPException.UnsupportedOption;
/** /**
* Armor the provided data. * Armor the provided data.
* *