This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b8752b0531a05e7b29360dfc3660aeb143c3006a Author: Benoit TELLIER <[email protected]> AuthorDate: Fri Dec 1 09:09:03 2023 +0100 JAMES-3965 DKIMSign: Use fileSystem in order to resolve privateKeyFilepath --- .../org/apache/james/jdkim/mailets/DKIMSign.java | 12 +++++++-- .../apache/james/jdkim/mailets/DKIMSignTest.java | 29 +++++++++++++++------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java index b5e5d18c9f..0d2b61ab21 100644 --- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java +++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java @@ -35,10 +35,12 @@ import java.util.Collections; import java.util.List; import java.util.Optional; +import javax.inject.Inject; import javax.mail.Header; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; +import org.apache.james.filesystem.api.FileSystem; import org.apache.james.jdkim.DKIMSigner; import org.apache.james.jdkim.api.BodyHasher; import org.apache.james.jdkim.api.Headers; @@ -92,7 +94,7 @@ import com.github.fge.lambdas.Throwing; * <pre><code> * <mailet match="All" class="DKIMSign"> * <signatureTemplate>v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;</signatureTemplate> - * <privateKeyFilepath>dkim-signing.pem</privateKeyFilepath> + * <privateKeyFilepath>conf://dkim-signing.pem</privateKeyFilepath> * </mailet> * </code></pre> * @@ -102,10 +104,16 @@ import com.github.fge.lambdas.Throwing; */ public class DKIMSign extends GenericMailet { + private final FileSystem fileSystem; private String signatureTemplate; private PrivateKey privateKey; private boolean forceCRLF; + @Inject + public DKIMSign(FileSystem fileSystem) { + this.fileSystem = fileSystem; + } + /** * @return the signatureTemplate */ @@ -131,7 +139,7 @@ public class DKIMSign extends GenericMailet { .map(String::getBytes) .map(ByteArrayInputStream::new) .map(byteArrayInputStream -> (InputStream) byteArrayInputStream) - .orElseGet(Throwing.supplier(() -> ClassLoader.getSystemResourceAsStream(getInitParameter("privateKeyFilepath"))).sneakyThrow()); + .orElseGet(Throwing.supplier(() -> fileSystem.getResource(getInitParameter("privateKeyFilepath"))).sneakyThrow()); privateKey = extractPrivateKey(pem, passphrase); } catch (NoSuchAlgorithmException e) { diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java index 590b9a0039..44549626b3 100644 --- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java +++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java @@ -27,6 +27,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Properties; +import java.util.UUID; import javax.mail.Address; import javax.mail.MessagingException; @@ -35,31 +36,41 @@ import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; +import org.apache.james.filesystem.api.FileSystem; import org.apache.james.jdkim.api.SignatureRecord; import org.apache.james.jdkim.exceptions.FailException; import org.apache.james.jdkim.exceptions.PermFailException; +import org.apache.james.server.core.JamesServerResourceLoader; +import org.apache.james.server.core.filesystem.FileSystemImpl; import org.apache.james.util.MimeMessageUtil; import org.apache.mailet.Mail; import org.apache.mailet.Mailet; import org.apache.mailet.base.test.FakeMail; import org.apache.mailet.base.test.FakeMailContext; import org.apache.mailet.base.test.FakeMailetConfig; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class DKIMSignTest { - - private static final String PKCS1_PEM_FILE = "test-dkim-pkcs1.pem"; - private static final String PKCS8_PEM_FILE = "test-dkim-pkcs8.pem"; + private static final String PKCS1_PEM_FILE = "classpath://test-dkim-pkcs1.pem"; + private static final String PKCS8_PEM_FILE = "classpath://test-dkim-pkcs8.pem"; private static final FakeMailContext FAKE_MAIL_CONTEXT = FakeMailContext.defaultContext(); + FileSystem fileSystem; + + @BeforeEach + void setUp() { + fileSystem = new FileSystemImpl(new JamesServerResourceLoader("../testsFileSystemExtension/" + UUID.randomUUID())); + } + @ParameterizedTest @ValueSource(strings = {PKCS1_PEM_FILE, PKCS8_PEM_FILE}) void testDKIMSign(String pemFile) throws MessagingException, IOException, FailException { String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <[email protected]>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: [email protected]\r\nTo: [email protected]\r\n\r\nbody\r\nprova\r\n"; - Mailet mailet = new DKIMSign(); + Mailet mailet = new DKIMSign(fileSystem); FakeMailetConfig mci = FakeMailetConfig.builder() .mailetName("Test") @@ -111,7 +122,7 @@ class DKIMSignTest { FailException { String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <[email protected]>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: [email protected]\r\nTo: [email protected]\r\n\r\nbody\r\nprova\r\n"; - Mailet mailet = new DKIMSign(); + Mailet mailet = new DKIMSign(fileSystem); FakeMailetConfig mci = FakeMailetConfig.builder() .mailetName("Test") @@ -158,7 +169,7 @@ class DKIMSignTest { FailException { String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <[email protected]>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: [email protected]\r\nTo: [email protected]\r\n\r\nbody\r\nprova\r\n"; - Mailet mailet = new DKIMSign(); + Mailet mailet = new DKIMSign(fileSystem); FakeMailetConfig mci = FakeMailetConfig.builder() .mailetName("Test") @@ -212,7 +223,7 @@ class DKIMSignTest { mm.addRecipient(RecipientType.TO, new InternetAddress("[email protected]")); mm.setText("An 8bit encoded body with €uro symbol.", "ISO-8859-15"); - Mailet mailet = new DKIMSign(); + Mailet mailet = new DKIMSign(fileSystem); FakeMailetConfig mci = FakeMailetConfig.builder() .mailetName("Test") @@ -276,7 +287,7 @@ class DKIMSignTest { .mimeMessage(mm) .build(); - Mailet mailet = new DKIMSign(); + Mailet mailet = new DKIMSign(fileSystem); mailet.init(mci); Mailet m7bit = new ConvertTo7Bit(); @@ -325,7 +336,7 @@ class DKIMSignTest { .mimeMessage(mm) .build(); - Mailet mailet = new DKIMSign(); + Mailet mailet = new DKIMSign(fileSystem); mailet.init(mci); Mailet m7bit = new ConvertTo7Bit(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
