From 8a66f0bc4f88a1a647471a5a32c5dfe458c4179c Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 14 Apr 2023 15:04:51 +0200 Subject: [PATCH] Implement sop encrypt --profile=XXX --- .../external/operation/EncryptExternal.java | 6 ++++++ .../sop/cli/picocli/commands/EncryptCmd.java | 13 +++++++++++++ .../src/main/java/sop/operation/Encrypt.java | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/external-sop/src/main/java/sop/external/operation/EncryptExternal.java b/external-sop/src/main/java/sop/external/operation/EncryptExternal.java index ca8ddf1..1a7208e 100644 --- a/external-sop/src/main/java/sop/external/operation/EncryptExternal.java +++ b/external-sop/src/main/java/sop/external/operation/EncryptExternal.java @@ -85,6 +85,12 @@ public class EncryptExternal implements Encrypt { return this; } + @Override + public Encrypt profile(String profileName) { + commandList.add("--profile=" + profileName); + return this; + } + @Override public Ready plaintext(InputStream plaintext) throws IOException, SOPGPException.KeyIsProtected { diff --git a/sop-java-picocli/src/main/java/sop/cli/picocli/commands/EncryptCmd.java b/sop-java-picocli/src/main/java/sop/cli/picocli/commands/EncryptCmd.java index 20b0779..079f9d8 100644 --- a/sop-java-picocli/src/main/java/sop/cli/picocli/commands/EncryptCmd.java +++ b/sop-java-picocli/src/main/java/sop/cli/picocli/commands/EncryptCmd.java @@ -41,6 +41,10 @@ public class EncryptCmd extends AbstractSopCmd { paramLabel = "PASSWORD") List withKeyPassword = new ArrayList<>(); + @CommandLine.Option(names = "--profile", + paramLabel = "PROFILE") + String profile; + @CommandLine.Parameters(index = "0..*", paramLabel = "CERTS") List certs = new ArrayList<>(); @@ -50,6 +54,15 @@ public class EncryptCmd extends AbstractSopCmd { Encrypt encrypt = throwIfUnsupportedSubcommand( SopCLI.getSop().encrypt(), "encrypt"); + if (profile != null) { + try { + encrypt.profile(profile); + } catch (SOPGPException.UnsupportedProfile e) { + String errorMsg = getMsg("sop.error.usage.profile_not_supported", "encrypt", profile); + throw new SOPGPException.UnsupportedProfile(errorMsg, e); + } + } + if (type != null) { try { encrypt.mode(type); diff --git a/sop-java/src/main/java/sop/operation/Encrypt.java b/sop-java/src/main/java/sop/operation/Encrypt.java index 09e5f12..fed1210 100644 --- a/sop-java/src/main/java/sop/operation/Encrypt.java +++ b/sop-java/src/main/java/sop/operation/Encrypt.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import sop.Profile; import sop.Ready; import sop.enums.EncryptAs; import sop.exception.SOPGPException; @@ -146,6 +147,24 @@ public interface Encrypt { return withCert(new ByteArrayInputStream(cert)); } + /** + * Pass in a profile. + * + * @param profile profile + * @return builder instance + */ + default Encrypt profile(Profile profile) { + return profile(profile.getName()); + } + + /** + * Pass in a profile identifier. + * + * @param profileName profile identifier + * @return builder instance + */ + Encrypt profile(String profileName); + /** * Encrypt the given data yielding the ciphertext. * @param plaintext plaintext