You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/12/08 10:39:56 UTC

[01/13] james-project git commit: JAMES-2246 Add integration tests for SMTP authentication

Repository: james-project
Updated Branches:
  refs/heads/master 1fb731afb -> 90e28ece8


JAMES-2246 Add integration tests for SMTP authentication


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b8c70676
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b8c70676
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b8c70676

Branch: refs/heads/master
Commit: b8c7067688741188def5091be746cafec9fed765
Parents: cc9707f
Author: benwa <bt...@linagora.com>
Authored: Tue Dec 5 14:04:27 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:33:49 2017 +0700

----------------------------------------------------------------------
 .../james/mailets/SmtpAuthIntegrationTest.java  | 171 +++++++++++++++++++
 1 file changed, 171 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b8c70676/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
new file mode 100644
index 0000000..8b6160e
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
@@ -0,0 +1,171 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.mailets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.Null;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.SMTPAuthSuccessful;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class SmtpAuthIntegrationTest {
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final int IMAP_PORT = 1143;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String FROM = "fromuser@" + JAMES_APACHE_ORG;
+
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        ProcessorConfiguration rootProcessor = ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(SMTPAuthSuccessful.class)
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "bounces")
+                .build())
+            .build();
+
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + JAMES_APACHE_ORG)
+            .threads(5)
+            .addProcessor(rootProcessor)
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(deliverOnlyTransport())
+            .addProcessor(bounces())
+            .addProcessor(CommonProcessors.sieveManagerCheck())
+            .build();
+
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_AND_IMAP_MODULE)
+            .build(temporaryFolder, mailetContainer);
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+    }
+
+    private ProcessorConfiguration deliverOnlyTransport() {
+        return ProcessorConfiguration.builder()
+            .state("transport")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoveMimeHeader.class)
+                .addProperty("name", "bcc")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(LocalDelivery.class)
+                .build())
+            .build();
+    }
+
+    private ProcessorConfiguration bounces() {
+        return ProcessorConfiguration.builder()
+            .state("bounces")
+            .enableJmx(true)
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(Null.class)
+                .build())
+            .build();
+    }
+
+    @After
+    public void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    public void authenticatedSmtpSessionsShouldBeDelivered() throws Exception {
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void nonAuthenticatedSmtpSessionsShouldNotBeDelivered() throws Exception {
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+
+            assertThat(imapMessageReader.userReceivedMessage(FROM, PASSWORD)).isFalse();
+        }
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[08/13] james-project git commit: JAMES-2246 Adding tests for SMTP message size limitations

Posted by bt...@apache.org.
JAMES-2246 Adding tests for SMTP message size limitations


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/27556e98
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/27556e98
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/27556e98

Branch: refs/heads/master
Commit: 27556e984ede210a3db2ff52e1a13a1177b531cb
Parents: 138e838
Author: benwa <bt...@linagora.com>
Authored: Tue Dec 5 15:12:02 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:35:50 2017 +0700

----------------------------------------------------------------------
 .../james/smtp/SmtpSizeLimitationTest.java      | 153 +++++++++++++++++++
 1 file changed, 153 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/27556e98/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpSizeLimitationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpSizeLimitationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpSizeLimitationTest.java
new file mode 100644
index 0000000..5da56b6
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpSizeLimitationTest.java
@@ -0,0 +1,153 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.smtp;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.TemporaryJamesServer;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.mailets.configuration.SmtpConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.RecipientIsLocal;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.google.common.base.Strings;
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class SmtpSizeLimitationTest {
+    private static final String DEFAULT_DOMAIN = "james.org";
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String USER = "user@" + JAMES_APACHE_ORG;
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+    }
+
+    private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception {
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + DEFAULT_DOMAIN)
+            .threads(5)
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("root")
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "transport")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("transport")
+                .enableJmx(true)
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(RemoveMimeHeader.class)
+                    .addProperty("name", "bcc")
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(RecipientIsLocal.class)
+                    .mailet(LocalDelivery.class)
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "bounces")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.localAddressError())
+            .addProcessor(CommonProcessors.relayDenied())
+            .addProcessor(CommonProcessors.bounces())
+            .build();
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_ONLY_MODULE)
+            .withSmtpConfiguration(smtpConfiguration.build())
+            .build(temporaryFolder, mailetContainer);
+
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(USER, PASSWORD);
+    }
+
+    @After
+    public void tearDown() {
+        if (jamesServer != null) {
+            jamesServer.shutdown();
+        }
+    }
+
+    @Test
+    public void messageShouldNotBeAcceptedWhenOverSized() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .doNotVerifyIdentity()
+            .withMaxMessageSizeInKb(10));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessageWithHeaders(USER, USER, Strings.repeat("Long message", 1024));
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageSendingFailed);
+        }
+    }
+
+    @Test
+    public void messageShouldBeAcceptedWhenNotOverSized() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .doNotVerifyIdentity()
+            .withMaxMessageSizeInKb(10));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessageWithHeaders(USER, USER,"Short message");
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[10/13] james-project git commit: JAMES-2246 SMTPSender should report failed sending

Posted by bt...@apache.org.
JAMES-2246 SMTPSender should report failed sending


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/90e28ece
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/90e28ece
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/90e28ece

Branch: refs/heads/master
Commit: 90e28ece8b759c1cd1d73c44d77127f8531c5481
Parents: 2c55ee7
Author: benwa <bt...@linagora.com>
Authored: Thu Dec 7 15:50:22 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:35:50 2017 +0700

----------------------------------------------------------------------
 .../src/main/java/org/apache/james/utils/SMTPMessageSender.java | 5 +++++
 1 file changed, 5 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/90e28ece/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
index 2fb175e..01cd174 100644
--- a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
+++ b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
@@ -130,6 +130,11 @@ public class SMTPMessageSender implements Closeable {
             .contains("250 2.6.0 Message received");
     }
 
+    public boolean messageSendingFailed() throws IOException {
+        String replyString = smtpClient.getReplyString().trim();
+        return replyString.startsWith("4") || replyString.startsWith("5");
+    }
+
     public boolean messageHaveNotBeenSent() throws IOException {
         return !messageHasBeenSent();
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[13/13] james-project git commit: JAMES-2246 Adding tests for SMTP identity verifications

Posted by bt...@apache.org.
JAMES-2246 Adding tests for SMTP identity verifications


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/138e8386
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/138e8386
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/138e8386

Branch: refs/heads/master
Commit: 138e83869142fbc03a2860a1ad544f48d5bfae08
Parents: 0ce56b7
Author: benwa <bt...@linagora.com>
Authored: Tue Dec 5 15:14:18 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:35:50 2017 +0700

----------------------------------------------------------------------
 .../smtp/SmtpIdentityVerificationTest.java      | 165 +++++++++++++++++++
 1 file changed, 165 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/138e8386/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java
new file mode 100644
index 0000000..114d902
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java
@@ -0,0 +1,165 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.smtp;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.TemporaryJamesServer;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.mailets.configuration.SmtpConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.RecipientIsLocal;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class SmtpIdentityVerificationTest {
+    private static final String DEFAULT_DOMAIN = "james.org";
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final String PASSWORD = "secret";
+    private static final String ATTACKER_PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String ATTACKER = "attacker@" + JAMES_APACHE_ORG;
+    private static final String USER = "user@" + JAMES_APACHE_ORG;
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+    }
+
+    private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception {
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + DEFAULT_DOMAIN)
+            .threads(5)
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("root")
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "transport")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("transport")
+                .enableJmx(true)
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(RemoveMimeHeader.class)
+                    .addProperty("name", "bcc")
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(RecipientIsLocal.class)
+                    .mailet(LocalDelivery.class)
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.localAddressError())
+            .addProcessor(CommonProcessors.relayDenied())
+            .addProcessor(CommonProcessors.bounces())
+            .build();
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_ONLY_MODULE)
+            .withSmtpConfiguration(smtpConfiguration.build())
+            .build(temporaryFolder, mailetContainer);
+
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(USER, PASSWORD);
+        dataProbe.addUser(ATTACKER, ATTACKER_PASSWORD);
+    }
+
+    @After
+    public void tearDown() {
+        if (jamesServer != null) {
+            jamesServer.shutdown();
+        }
+    }
+
+    @Test
+    public void smtpShouldAcceptMessageWhenIdentityIsMatching() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .verifyIdentity());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessage(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+        }
+    }
+
+    @Test
+    public void smtpShouldAcceptMessageWhenIdentityIsNotMatchingButNotChecked() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .doNotVerifyIdentity());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, ATTACKER, ATTACKER_PASSWORD)) {
+
+            messageSender.sendMessage(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+        }
+    }
+
+    @Test
+    public void smtpShouldRejectMessageWhenIdentityIsNotMatching() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .verifyIdentity());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, ATTACKER, ATTACKER_PASSWORD)) {
+
+            messageSender.sendMessage(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageSendingFailed);
+        }
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[02/13] james-project git commit: JAMES-2246 Move memory related meta-modules to MemoryJamesServerMain

Posted by bt...@apache.org.
JAMES-2246 Move memory related meta-modules to MemoryJamesServerMain


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/cc9707f3
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/cc9707f3
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/cc9707f3

Branch: refs/heads/master
Commit: cc9707f3ea67320e0a227b03a41ef5dc74452322
Parents: 31f473f
Author: benwa <bt...@linagora.com>
Authored: Tue Dec 5 14:04:06 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:33:49 2017 +0700

----------------------------------------------------------------------
 .../org/apache/james/MemoryJamesServerMain.java | 16 +++++++++++++++
 .../GatewayRemoteDeliveryIntegrationTest.java   | 21 ++------------------
 2 files changed, 18 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/cc9707f3/server/container/guice/memory-guice/src/main/java/org/apache/james/MemoryJamesServerMain.java
----------------------------------------------------------------------
diff --git a/server/container/guice/memory-guice/src/main/java/org/apache/james/MemoryJamesServerMain.java b/server/container/guice/memory-guice/src/main/java/org/apache/james/MemoryJamesServerMain.java
index d1dcf41..720b762 100644
--- a/server/container/guice/memory-guice/src/main/java/org/apache/james/MemoryJamesServerMain.java
+++ b/server/container/guice/memory-guice/src/main/java/org/apache/james/MemoryJamesServerMain.java
@@ -19,6 +19,7 @@
 
 package org.apache.james;
 
+import org.apache.commons.configuration.DefaultConfigurationBuilder;
 import org.apache.james.modules.MailboxModule;
 import org.apache.james.modules.data.MemoryDataJmapModule;
 import org.apache.james.modules.data.MemoryDataModule;
@@ -30,10 +31,12 @@ import org.apache.james.modules.protocols.ManageSieveServerModule;
 import org.apache.james.modules.protocols.POP3ServerModule;
 import org.apache.james.modules.protocols.ProtocolHandlerModule;
 import org.apache.james.modules.protocols.SMTPServerModule;
+import org.apache.james.modules.server.CamelMailetContainerModule;
 import org.apache.james.modules.server.DataRoutesModules;
 import org.apache.james.modules.server.JMXServerModule;
 import org.apache.james.modules.server.MailboxRoutesModule;
 import org.apache.james.modules.server.MemoryMailQueueModule;
+import org.apache.james.modules.server.RawPostDequeueDecoratorModule;
 import org.apache.james.modules.server.SwaggerRoutesModule;
 import org.apache.james.modules.server.WebAdminServerModule;
 
@@ -66,6 +69,19 @@ public class MemoryJamesServerMain {
         new MemoryMailQueueModule(),
         new MailboxModule());
 
+    public static final Module SMTP_ONLY_MODULE = Modules.combine(
+        MemoryJamesServerMain.IN_MEMORY_SERVER_MODULE,
+        new ProtocolHandlerModule(),
+        new SMTPServerModule(),
+        new RawPostDequeueDecoratorModule(),
+        binder -> binder.bind(CamelMailetContainerModule.DefaultProcessorsConfigurationSupplier.class)
+            .toInstance(DefaultConfigurationBuilder::new));
+
+
+    public static final Module SMTP_AND_IMAP_MODULE = Modules.combine(
+        SMTP_ONLY_MODULE,
+        new IMAPServerModule());
+
     public static final Module IN_MEMORY_SERVER_AGGREGATE_MODULE = Modules.combine(
         IN_MEMORY_SERVER_MODULE,
         PROTOCOLS,

http://git-wip-us.apache.org/repos/asf/james-project/blob/cc9707f3/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
index 374d7bf..5d7d498 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
@@ -22,14 +22,14 @@ package org.apache.james.mailets;
 import static com.jayway.restassured.RestAssured.when;
 import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
 import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+import static org.apache.james.MemoryJamesServerMain.SMTP_AND_IMAP_MODULE;
+import static org.apache.james.MemoryJamesServerMain.SMTP_ONLY_MODULE;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasSize;
 
 import java.net.InetAddress;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.commons.configuration.DefaultConfigurationBuilder;
-import org.apache.james.MemoryJamesServerMain;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.dnsservice.api.InMemoryDNSService;
 import org.apache.james.mailbox.model.MailboxConstants;
@@ -37,11 +37,6 @@ import org.apache.james.mailets.configuration.CommonProcessors;
 import org.apache.james.mailets.configuration.MailetConfiguration;
 import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.mailets.configuration.ProcessorConfiguration;
-import org.apache.james.modules.protocols.IMAPServerModule;
-import org.apache.james.modules.protocols.ProtocolHandlerModule;
-import org.apache.james.modules.protocols.SMTPServerModule;
-import org.apache.james.modules.server.CamelMailetContainerModule;
-import org.apache.james.modules.server.RawPostDequeueDecoratorModule;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.transport.mailets.LocalDelivery;
 import org.apache.james.transport.mailets.Null;
@@ -66,8 +61,6 @@ import org.junit.rules.TemporaryFolder;
 import org.testcontainers.containers.wait.HostPortWaitStrategy;
 
 import com.google.common.base.Charsets;
-import com.google.inject.Module;
-import com.google.inject.util.Modules;
 import com.jayway.awaitility.Awaitility;
 import com.jayway.awaitility.Duration;
 import com.jayway.awaitility.core.ConditionFactory;
@@ -86,16 +79,6 @@ public class GatewayRemoteDeliveryIntegrationTest {
 
     private static final String FROM = "from@" + JAMES_APACHE_ORG;
     private static final String RECIPIENT = "touser@" + JAMES_ANOTHER_DOMAIN;
-    private static final Module SMTP_ONLY_MODULE = Modules.combine(
-        MemoryJamesServerMain.IN_MEMORY_SERVER_MODULE,
-        new ProtocolHandlerModule(),
-        new SMTPServerModule(),
-        new RawPostDequeueDecoratorModule(),
-        binder -> binder.bind(CamelMailetContainerModule.DefaultProcessorsConfigurationSupplier.class)
-            .toInstance(DefaultConfigurationBuilder::new));
-    private static final Module SMTP_AND_IMAP_MODULE = Modules.combine(
-        SMTP_ONLY_MODULE,
-        new IMAPServerModule());
 
     @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[03/13] james-project git commit: JAMES-2246 ProcessorConfiguration builder JMX enabled default value is not explicit

Posted by bt...@apache.org.
JAMES-2246 ProcessorConfiguration builder JMX enabled default value is not explicit


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/31f473f2
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/31f473f2
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/31f473f2

Branch: refs/heads/master
Commit: 31f473f234753de4db019a1cfafd29b9d2006796
Parents: 1fb731a
Author: benwa <bt...@linagora.com>
Authored: Fri Dec 1 10:02:15 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:33:49 2017 +0700

----------------------------------------------------------------------
 .../james/mailets/configuration/ProcessorConfiguration.java  | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/31f473f2/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/ProcessorConfiguration.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/ProcessorConfiguration.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/ProcessorConfiguration.java
index cf95d69..e1de38d 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/ProcessorConfiguration.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/ProcessorConfiguration.java
@@ -20,6 +20,8 @@
 
 package org.apache.james.mailets.configuration;
 
+import java.util.Optional;
+
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
@@ -37,7 +39,7 @@ public class ProcessorConfiguration implements SerializableAsXml {
     public static class Builder {
 
         private String state;
-        private boolean enableJmx;
+        private Optional<Boolean> enableJmx = Optional.empty();
         private ImmutableList.Builder<MailetConfiguration> mailets;
 
         private Builder() {
@@ -50,7 +52,7 @@ public class ProcessorConfiguration implements SerializableAsXml {
         }
 
         public Builder enableJmx(boolean enableJmx) {
-            this.enableJmx = enableJmx;
+            this.enableJmx = Optional.of(enableJmx);
             return this;
         }
 
@@ -61,7 +63,7 @@ public class ProcessorConfiguration implements SerializableAsXml {
 
         public ProcessorConfiguration build() {
             Preconditions.checkState(!Strings.isNullOrEmpty(state), "'state' is mandatory");
-            return new ProcessorConfiguration(state, enableJmx, mailets.build());
+            return new ProcessorConfiguration(state, enableJmx.orElse(false), mailets.build());
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[07/13] james-project git commit: JAMES−2246 Avoiding delayed checks for validating an action did not happened

Posted by bt...@apache.org.
JAMES−2246 Avoiding delayed checks for validating an action did not happened

To avoid a negative assertion, we create an alternative action. In the case of
mailet pipelines, to better fit in an integration test scenario, this action
can be storing an undelivered email in a repository.


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/8eb8bf1a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/8eb8bf1a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/8eb8bf1a

Branch: refs/heads/master
Commit: 8eb8bf1a4303da9ba23714b0e0aea5a60b513eb1
Parents: 51b8f72
Author: benwa <bt...@linagora.com>
Authored: Wed Dec 6 09:33:49 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:34:45 2017 +0700

----------------------------------------------------------------------
 .../server/MailStoreRepositoryModule.java       |  3 ++
 .../james/utils/MailRepositoryProbeImpl.java    | 45 ++++++++++++++++++++
 .../mailets/NetworkMatcherIntegrationTest.java  | 35 +++++++--------
 .../james/mailets/SmtpAuthIntegrationTest.java  | 13 +++---
 4 files changed, 73 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8eb8bf1a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/MailStoreRepositoryModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/MailStoreRepositoryModule.java b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/MailStoreRepositoryModule.java
index 4ed9cd7..6a8e3b2 100644
--- a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/MailStoreRepositoryModule.java
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/server/MailStoreRepositoryModule.java
@@ -28,7 +28,9 @@ import org.apache.james.mailrepository.api.MailRepositoryStore;
 import org.apache.james.mailrepository.file.FileMailRepository;
 import org.apache.james.utils.ConfigurationPerformer;
 import org.apache.james.utils.ConfigurationProvider;
+import org.apache.james.utils.GuiceProbe;
 import org.apache.james.utils.InMemoryMailRepositoryStore;
+import org.apache.james.utils.MailRepositoryProbeImpl;
 import org.apache.james.utils.MailRepositoryProvider;
 
 import com.google.common.base.Throwables;
@@ -49,6 +51,7 @@ public class MailStoreRepositoryModule extends AbstractModule {
         Multibinder<MailRepositoryProvider> multibinder = Multibinder.newSetBinder(binder(), MailRepositoryProvider.class);
         multibinder.addBinding().to(FileMailRepositoryProvider.class);
         Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(MailRepositoryStoreModuleConfigurationPerformer.class);
+        Multibinder.newSetBinder(binder(), GuiceProbe.class).addBinding().to(MailRepositoryProbeImpl.class);
     }
 
     public static class FileMailRepositoryProvider implements MailRepositoryProvider {

http://git-wip-us.apache.org/repos/asf/james-project/blob/8eb8bf1a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
new file mode 100644
index 0000000..baac629
--- /dev/null
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
@@ -0,0 +1,45 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.utils;
+
+import javax.inject.Inject;
+
+import org.apache.james.mailrepository.api.MailRepositoryStore;
+
+import com.google.common.collect.Iterators;
+
+public class MailRepositoryProbeImpl implements GuiceProbe {
+
+    private final MailRepositoryStore repositoryStore;
+
+    @Inject
+    public MailRepositoryProbeImpl(MailRepositoryStore repositoryStore) {
+        this.repositoryStore = repositoryStore;
+    }
+
+    /**
+     * Get the count of email currently stored in a given repository
+     */
+    public int getRepositoryMailCount(String url) throws Exception {
+        return Iterators.size(repositoryStore.select(url)
+            .list());
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/8eb8bf1a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
index b317ae8..e70d4e4 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
@@ -21,8 +21,6 @@ package org.apache.james.mailets;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.util.concurrent.TimeUnit;
-
 import org.apache.james.MemoryJamesServerMain;
 import org.apache.james.mailets.configuration.CommonProcessors;
 import org.apache.james.mailets.configuration.MailetConfiguration;
@@ -30,14 +28,15 @@ import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.mailets.configuration.ProcessorConfiguration;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.transport.mailets.LocalDelivery;
-import org.apache.james.transport.mailets.Null;
 import org.apache.james.transport.mailets.RemoveMimeHeader;
 import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.mailets.ToRepository;
 import org.apache.james.transport.matchers.All;
 import org.apache.james.transport.matchers.RemoteAddrInNetwork;
 import org.apache.james.transport.matchers.RemoteAddrNotInNetwork;
 import org.apache.james.utils.DataProbeImpl;
 import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.MailRepositoryProbeImpl;
 import org.apache.james.utils.SMTPMessageSender;
 import org.junit.After;
 import org.junit.Before;
@@ -57,6 +56,7 @@ public class NetworkMatcherIntegrationTest {
 
     private static final String JAMES_APACHE_ORG = "james.org";
     private static final String FROM = "fromuser@" + JAMES_APACHE_ORG;
+    private static final String DROPPED_MAILS = "file://var/mail/dropped-mails/";
 
     @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -109,10 +109,11 @@ public class NetworkMatcherIntegrationTest {
             .build();
     }
 
-    private MailetConfiguration dropAllMailMailet() {
+    private MailetConfiguration toRepository() {
         return MailetConfiguration.builder()
             .matcher(All.class)
-            .mailet(Null.class)
+            .mailet(ToRepository.class)
+            .addProperty("repositoryPath", DROPPED_MAILS)
             .build();
     }
 
@@ -131,7 +132,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
@@ -155,7 +156,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
@@ -179,7 +180,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
@@ -203,7 +204,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
@@ -227,7 +228,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
@@ -251,7 +252,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
@@ -275,7 +276,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
@@ -285,8 +286,8 @@ public class NetworkMatcherIntegrationTest {
 
             calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
 
-            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
-
+            MailRepositoryProbeImpl repositoryProbe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> repositoryProbe.getRepositoryMailCount(DROPPED_MAILS) == 1);
             assertThat(imapMessageReader.userReceivedMessage(FROM, PASSWORD)).isFalse();
         }
     }
@@ -301,7 +302,7 @@ public class NetworkMatcherIntegrationTest {
                 .mailet(ToProcessor.class)
                 .addProperty("processor", "transport")
                 .build())
-            .addMailet(dropAllMailMailet()));
+            .addMailet(toRepository()));
 
         try (SMTPMessageSender messageSender =
                  SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
@@ -311,8 +312,8 @@ public class NetworkMatcherIntegrationTest {
 
             calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
 
-            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
-
+            MailRepositoryProbeImpl repositoryProbe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> repositoryProbe.getRepositoryMailCount(DROPPED_MAILS) == 1);
             assertThat(imapMessageReader.userReceivedMessage(FROM, PASSWORD)).isFalse();
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/8eb8bf1a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
index 8b6160e..070eb09 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/SmtpAuthIntegrationTest.java
@@ -21,8 +21,6 @@ package org.apache.james.mailets;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.util.concurrent.TimeUnit;
-
 import org.apache.james.MemoryJamesServerMain;
 import org.apache.james.mailets.configuration.CommonProcessors;
 import org.apache.james.mailets.configuration.MailetConfiguration;
@@ -30,13 +28,14 @@ import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.mailets.configuration.ProcessorConfiguration;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.transport.mailets.LocalDelivery;
-import org.apache.james.transport.mailets.Null;
 import org.apache.james.transport.mailets.RemoveMimeHeader;
 import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.mailets.ToRepository;
 import org.apache.james.transport.matchers.All;
 import org.apache.james.transport.matchers.SMTPAuthSuccessful;
 import org.apache.james.utils.DataProbeImpl;
 import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.MailRepositoryProbeImpl;
 import org.apache.james.utils.SMTPMessageSender;
 import org.junit.After;
 import org.junit.Before;
@@ -56,6 +55,7 @@ public class SmtpAuthIntegrationTest {
 
     private static final String JAMES_APACHE_ORG = "james.org";
     private static final String FROM = "fromuser@" + JAMES_APACHE_ORG;
+    private static final String DROPPED_MAILS = "file://var/mail/dropped-mails/";
 
 
     @Rule
@@ -128,7 +128,8 @@ public class SmtpAuthIntegrationTest {
             .enableJmx(true)
             .addMailet(MailetConfiguration.builder()
                 .matcher(All.class)
-                .mailet(Null.class)
+                .mailet(ToRepository.class)
+                .addProperty("repositoryPath", DROPPED_MAILS)
                 .build())
             .build();
     }
@@ -162,8 +163,8 @@ public class SmtpAuthIntegrationTest {
 
             calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
 
-            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
-
+            MailRepositoryProbeImpl repositoryProbe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> repositoryProbe.getRepositoryMailCount(DROPPED_MAILS) == 1);
             assertThat(imapMessageReader.userReceivedMessage(FROM, PASSWORD)).isFalse();
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[09/13] james-project git commit: JAMES-2246 Add SMTP authorized addresses tests

Posted by bt...@apache.org.
JAMES-2246 Add SMTP authorized addresses tests

The authorized address parameter is only used at the SMTP level (for aborting the SMTP connnection if a non authenticated, not allowed user try to relay an email).

We miss a mechanism for allowed user to actually relay emails (in MailetContainer). SMTPAuthSuccessful is not expressive enough.


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4b5c18ce
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4b5c18ce
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4b5c18ce

Branch: refs/heads/master
Commit: 4b5c18cedde57d106fb3a6af25f05612d405d0a2
Parents: b64f2b2
Author: benwa <bt...@linagora.com>
Authored: Mon Dec 4 11:15:38 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:35:50 2017 +0700

----------------------------------------------------------------------
 .../james/smtp/SmtpAuthorizedAddressesTest.java | 256 +++++++++++++++++++
 .../AddDefaultAttributesMessageHook.java        |   4 +-
 2 files changed, 258 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4b5c18ce/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
new file mode 100644
index 0000000..3c9ea9f
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
@@ -0,0 +1,256 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.smtp;
+
+import static com.jayway.restassured.RestAssured.when;
+import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
+import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.TemporaryJamesServer;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.mailets.configuration.SmtpConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.RemoteDelivery;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.RecipientIsLocal;
+import org.apache.james.transport.matchers.SMTPIsAuthNetwork;
+import org.apache.james.util.streams.SwarmGenericContainer;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TemporaryFolder;
+import org.testcontainers.containers.wait.HostPortWaitStrategy;
+
+import com.google.common.base.Charsets;
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.builder.RequestSpecBuilder;
+import com.jayway.restassured.http.ContentType;
+
+public class SmtpAuthorizedAddressesTest {
+    private static final String DEFAULT_DOMAIN = "james.org";
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    public static final int IMAP_PORT = 1143;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String FROM = "fromuser@" + JAMES_APACHE_ORG;
+    private static final String TO = "to@any.com";
+
+    private final TemporaryFolder smtpFolder = new TemporaryFolder();
+    private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer("weave/rest-smtp-sink:latest")
+        .withExposedPorts(25)
+        .withAffinityToContainer()
+        .waitingFor(new HostPortWaitStrategy());
+
+    @Rule
+    public final RuleChain chain = RuleChain.outerRule(smtpFolder).around(fakeSmtp);
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+        calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> fakeSmtp.tryConnect(25));
+
+        RestAssured.requestSpecification = new RequestSpecBuilder()
+            .setContentType(ContentType.JSON)
+            .setAccept(ContentType.JSON)
+            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
+            .setPort(80)
+            .setBaseUri("http://" + fakeSmtp.getContainerIp())
+            .build();
+    }
+
+    private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception {
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + DEFAULT_DOMAIN)
+            .threads(5)
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("root")
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "transport")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("transport")
+                .enableJmx(true)
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(RemoveMimeHeader.class)
+                    .addProperty("name", "bcc")
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(RecipientIsLocal.class)
+                    .mailet(LocalDelivery.class)
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(SMTPIsAuthNetwork.class)
+                    .mailet(RemoteDelivery.class)
+                    .addProperty("outgoingQueue", "outgoing")
+                    .addProperty("delayTime", "5000, 100000, 500000")
+                    .addProperty("maxRetries", "25")
+                    .addProperty("maxDnsProblemRetries", "0")
+                    .addProperty("deliveryThreads", "10")
+                    .addProperty("sendpartial", "true")
+                    .addProperty("bounceProcessor", "bounces")
+                    .addProperty("gateway", fakeSmtp.getContainerIp())
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "bounces")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.localAddressError())
+            .addProcessor(CommonProcessors.relayDenied())
+            .addProcessor(CommonProcessors.bounces())
+            .build();
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_AND_IMAP_MODULE)
+            .withSmtpConfiguration(smtpConfiguration.build())
+            .build(temporaryFolder, mailetContainer);
+
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+    }
+
+    @After
+    public void tearDown() {
+        if (jamesServer != null) {
+            jamesServer.shutdown();
+        }
+    }
+
+    @Test
+    public void userShouldBeAbleToRelayMessagesWhenInAcceptedNetwork() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .withAutorizedAddresses("127.0.0.0/8"));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+
+            messageSender.sendMessage(FROM, TO);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    @Test
+    public void userShouldNotBeAbleToRelayMessagesWhenOutOfAcceptedNetwork() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .withAutorizedAddresses("172.0.0.0/8"));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG)) {
+
+            messageSender.sendMessage(FROM, TO);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageSendingFailed);
+        }
+    }
+
+    @Test
+    public void userShouldBeAbleToRelayMessagesWhenOutOfAcceptedNetworkButAuthenticated() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .withAutorizedAddresses("172.0.0.0/8"));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD)) {
+
+            messageSender.sendMessage(FROM, TO);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(this::messageIsReceivedByTheSmtpServer);
+        }
+    }
+
+    @Test
+    public void localDeliveryShouldBePossibleFromNonAuthenticatedNonAuthorizedSender() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireAuthentication()
+            .withAutorizedAddresses("172.0.0.0/8"));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(TO, FROM);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    private boolean messageIsReceivedByTheSmtpServer() {
+        try {
+            when()
+                .get("/api/email")
+            .then()
+                .statusCode(200)
+                .body("", hasSize(1))
+                .body("[0].from", equalTo(FROM))
+                .body("[0].subject", equalTo("test"));
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/4b5c18ce/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/AddDefaultAttributesMessageHook.java
----------------------------------------------------------------------
diff --git a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/AddDefaultAttributesMessageHook.java b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/AddDefaultAttributesMessageHook.java
index 8cd5d67..04aa165 100644
--- a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/AddDefaultAttributesMessageHook.java
+++ b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/AddDefaultAttributesMessageHook.java
@@ -20,10 +20,10 @@ package org.apache.james.smtpserver;
 
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationException;
-import org.apache.james.server.core.MailImpl;
 import org.apache.james.protocols.smtp.SMTPSession;
 import org.apache.james.protocols.smtp.hook.HookResult;
 import org.apache.james.protocols.smtp.hook.HookReturnCode;
+import org.apache.james.server.core.MailImpl;
 import org.apache.mailet.Mail;
 
 /**
@@ -34,7 +34,7 @@ public class AddDefaultAttributesMessageHook implements JamesMessageHook {
     /**
      * The mail attribute which get set if the client is allowed to relay
      */
-    private final static String SMTP_AUTH_NETWORK_NAME = "org.apache.james.SMTPIsAuthNetwork";
+    public static final String SMTP_AUTH_NETWORK_NAME = "org.apache.james.SMTPIsAuthNetwork";
 
     @Override
     public void init(Configuration config) throws ConfigurationException {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[04/13] james-project git commit: JAMES-2246 Provide integration tests for RemoteAddress matcher

Posted by bt...@apache.org.
JAMES-2246 Provide integration tests for RemoteAddress matcher


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/51b8f727
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/51b8f727
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/51b8f727

Branch: refs/heads/master
Commit: 51b8f7277e3ad74e75187375d54a959ff4875cc4
Parents: b8c7067
Author: benwa <bt...@linagora.com>
Authored: Fri Dec 1 10:35:34 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:33:50 2017 +0700

----------------------------------------------------------------------
 .../mailets/NetworkMatcherIntegrationTest.java  | 320 +++++++++++++++++++
 1 file changed, 320 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/51b8f727/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
new file mode 100644
index 0000000..b317ae8
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/NetworkMatcherIntegrationTest.java
@@ -0,0 +1,320 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.mailets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.Null;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.RemoteAddrInNetwork;
+import org.apache.james.transport.matchers.RemoteAddrNotInNetwork;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class NetworkMatcherIntegrationTest {
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final int IMAP_PORT = 1143;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String FROM = "fromuser@" + JAMES_APACHE_ORG;
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+    }
+
+    private TemporaryJamesServer createJamesServerWithRootProcessor(ProcessorConfiguration.Builder rootProcessor) throws Exception {
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + JAMES_APACHE_ORG)
+            .threads(5)
+            .addProcessor(rootProcessor.build())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(deliverOnlyTransport())
+            .build();
+
+        TemporaryJamesServer temporaryJamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_AND_IMAP_MODULE)
+            .build(temporaryFolder, mailetContainer);
+
+        DataProbe dataProbe = temporaryJamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(FROM, PASSWORD);
+        return temporaryJamesServer;
+    }
+
+    private ProcessorConfiguration deliverOnlyTransport() {
+        return ProcessorConfiguration.builder()
+            .state("transport")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(RemoveMimeHeader.class)
+                .addProperty("name", "bcc")
+                .build())
+            .addMailet(MailetConfiguration.builder()
+                .matcher(All.class)
+                .mailet(LocalDelivery.class)
+                .build())
+            .build();
+    }
+
+    private MailetConfiguration dropAllMailMailet() {
+        return MailetConfiguration.builder()
+            .matcher(All.class)
+            .mailet(Null.class)
+            .build();
+    }
+
+    @After
+    public void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    public void mailsFromAuthorizedNetworksShouldBeDeliveredWithRemoteAddrInNetwork() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrInNetwork.class)
+                .matcherCondition("127.0.0.0/8")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void mailsFromAuthorizedNetworksShouldBeDeliveredWithRemoteAddrNotInNetwork() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrNotInNetwork.class)
+                .matcherCondition("172.0.0.0/8")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void remoteAddrInNetworkShouldSupportLargerMask() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrInNetwork.class)
+                .matcherCondition("127.0.0.0/2")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void remoteAddrInNetworkShouldSupportRangesDefinedByAMiddleIp() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrInNetwork.class)
+                .matcherCondition("127.0.4.108/8")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void remoteAddrInNetworkShouldSupportRangesDefinedByEndingIp() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrInNetwork.class)
+                .matcherCondition("127.255.255.255/8")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void remoteAddrInNetworkShouldSupportRangesWithNonEightMultipleSubMasks() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrInNetwork.class)
+                .matcherCondition("126.0.0.0/4")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, FROM, PASSWORD);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE)
+                .until(() -> imapMessageReader.userReceivedMessage(FROM, PASSWORD));
+        }
+    }
+
+    @Test
+    public void mailsFromNonAuthorizedNetworksShouldNotBeDeliveredWithRemoteAddrInNetwork() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrInNetwork.class)
+                .matcherCondition("172.0.0.0/8")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+
+            assertThat(imapMessageReader.userReceivedMessage(FROM, PASSWORD)).isFalse();
+        }
+    }
+
+    @Test
+    public void mailsFromNonAuthorizedNetworksShouldNotBeDeliveredWithRemoteAddrNotInNetwork() throws Exception {
+        jamesServer = createJamesServerWithRootProcessor(ProcessorConfiguration.builder()
+            .state("root")
+            .addMailet(MailetConfiguration.builder()
+                .matcher(RemoteAddrNotInNetwork.class)
+                .matcherCondition("127.0.0.0/8")
+                .mailet(ToProcessor.class)
+                .addProperty("processor", "transport")
+                .build())
+            .addMailet(dropAllMailMailet()));
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+
+            messageSender.sendMessage(FROM, FROM);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+
+            assertThat(imapMessageReader.userReceivedMessage(FROM, PASSWORD)).isFalse();
+        }
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[12/13] james-project git commit: JAMES-2246 Update configurations

Posted by bt...@apache.org.
JAMES-2246 Update configurations


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0ce56b7e
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0ce56b7e
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0ce56b7e

Branch: refs/heads/master
Commit: 0ce56b7e5a9648251e8199913061e1155ad9bb8b
Parents: 4b5c18c
Author: benwa <bt...@linagora.com>
Authored: Wed Dec 6 17:45:57 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:35:50 2017 +0700

----------------------------------------------------------------------
 .../guice/cassandra-ldap/destination/conf/mailetcontainer.xml  | 3 +++
 .../run/guice/cassandra-ldap/destination/conf/smtpserver.xml   | 6 +++---
 .../run/guice/cassandra/destination/conf/mailetcontainer.xml   | 3 +++
 .../run/guice/cassandra/destination/conf/smtpserver.xml        | 6 +++---
 dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml | 3 +++
 dockerfiles/run/guice/jpa/destination/conf/smtpserver.xml      | 6 +++---
 dockerfiles/run/spring/destination/conf/mailetcontainer.xml    | 3 +++
 dockerfiles/run/spring/destination/conf/smtpserver.xml         | 6 +++---
 mpt/impl/smtp/cassandra/src/test/resources/smtpserver.xml      | 3 ---
 .../apache/james/smtp/scripts/data_starts_with_starttls.test   | 2 ++
 .../org/apache/james/smtp/scripts/data_with_starttls.test      | 2 ++
 .../org/apache/james/smtp/scripts/rcpt_with_starttls.test      | 2 ++
 .../main/resources/org/apache/james/smtp/scripts/starttls.test | 2 ++
 .../org/apache/james/smtp/scripts/starttls_with_injection.test | 2 ++
 .../guice/cassandra-guice/src/test/resources/smtpserver.xml    | 3 ---
 .../cassandra-ldap-guice/src/test/resources/smtpserver.xml     | 3 ---
 .../guice/jpa-guice/src/test/resources/smtpserver.xml          | 3 ---
 .../container/guice/jpa-smtp/src/test/resources/smtpserver.xml | 3 ---
 .../guice/memory-guice/src/test/resources/mailetcontainer.xml  | 3 +++
 .../guice/memory-guice/src/test/resources/smtpserver.xml       | 3 ---
 .../src/test/resources/mailetcontainer.xml                     | 3 +++
 .../src/test/resources/smtpserver.xml                          | 3 ---
 .../src/test/resources/mailetcontainer.xml                     | 3 +++
 .../src/test/resources/smtpserver.xml                          | 3 ---
 24 files changed, 43 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml b/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
index 5a45d3e..f1f7de4 100644
--- a/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/guice/cassandra-ldap/destination/conf/mailetcontainer.xml
@@ -73,6 +73,9 @@
             <mailet match="SMTPAuthSuccessful" class="ToProcessor">
                 <processor>relay</processor>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="ToProcessor">
+                <processor>relay</processor>
+            </mailet>
             <mailet match="org.apache.james.jmap.mailet.SentByJmap" class="ToProcessor">
                 <processor>relay</processor>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/guice/cassandra-ldap/destination/conf/smtpserver.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/cassandra-ldap/destination/conf/smtpserver.xml b/dockerfiles/run/guice/cassandra-ldap/destination/conf/smtpserver.xml
index bc609be..e464ca6 100644
--- a/dockerfiles/run/guice/cassandra-ldap/destination/conf/smtpserver.xml
+++ b/dockerfiles/run/guice/cassandra-ldap/destination/conf/smtpserver.xml
@@ -34,7 +34,7 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +61,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +89,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml b/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
index d766643..f453b77 100644
--- a/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/guice/cassandra/destination/conf/mailetcontainer.xml
@@ -73,6 +73,9 @@
             <mailet match="SMTPAuthSuccessful" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="SetMailAttribute">
+                <RelayAllowed>true</RelayAllowed>
+            </mailet>
             <mailet match="SentByMailet" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/guice/cassandra/destination/conf/smtpserver.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/cassandra/destination/conf/smtpserver.xml b/dockerfiles/run/guice/cassandra/destination/conf/smtpserver.xml
index bc609be..e464ca6 100644
--- a/dockerfiles/run/guice/cassandra/destination/conf/smtpserver.xml
+++ b/dockerfiles/run/guice/cassandra/destination/conf/smtpserver.xml
@@ -34,7 +34,7 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +61,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +89,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml b/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
index cc18299..3872dd8 100644
--- a/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/guice/jpa/destination/conf/mailetcontainer.xml
@@ -72,6 +72,9 @@
             <mailet match="SMTPAuthSuccessful" class="ToProcessor">
                 <processor>relay</processor>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="ToProcessor">
+                <processor>relay</processor>
+            </mailet>
             <mailet match="SentByMailet" class="ToProcessor">
                 <processor>relay</processor>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/guice/jpa/destination/conf/smtpserver.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/guice/jpa/destination/conf/smtpserver.xml b/dockerfiles/run/guice/jpa/destination/conf/smtpserver.xml
index bc609be..e464ca6 100644
--- a/dockerfiles/run/guice/jpa/destination/conf/smtpserver.xml
+++ b/dockerfiles/run/guice/jpa/destination/conf/smtpserver.xml
@@ -34,7 +34,7 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +61,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +89,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/spring/destination/conf/mailetcontainer.xml b/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
index 2998503..4793203 100644
--- a/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
+++ b/dockerfiles/run/spring/destination/conf/mailetcontainer.xml
@@ -72,6 +72,9 @@
             <mailet match="SMTPAuthSuccessful" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="SetMailAttribute">
+                <RelayAllowed>true</RelayAllowed>
+            </mailet>
             <mailet match="SentByMailet" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/dockerfiles/run/spring/destination/conf/smtpserver.xml
----------------------------------------------------------------------
diff --git a/dockerfiles/run/spring/destination/conf/smtpserver.xml b/dockerfiles/run/spring/destination/conf/smtpserver.xml
index bc609be..e464ca6 100644
--- a/dockerfiles/run/spring/destination/conf/smtpserver.xml
+++ b/dockerfiles/run/spring/destination/conf/smtpserver.xml
@@ -34,7 +34,7 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +61,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +89,7 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/mpt/impl/smtp/cassandra/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/cassandra/src/test/resources/smtpserver.xml b/mpt/impl/smtp/cassandra/src/test/resources/smtpserver.xml
index c2b31b5..b5a4608 100644
--- a/mpt/impl/smtp/cassandra/src/test/resources/smtpserver.xml
+++ b/mpt/impl/smtp/cassandra/src/test/resources/smtpserver.xml
@@ -35,7 +35,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -62,7 +61,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -90,7 +88,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_starts_with_starttls.test
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_starts_with_starttls.test b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_starts_with_starttls.test
index 22b1fff..88c656f 100644
--- a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_starts_with_starttls.test
+++ b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_starts_with_starttls.test
@@ -4,6 +4,8 @@ C: mail from:<ma...@yopmail.com>
 C: rcpt to:<bo...@mydomain.tld>
 C: data
 S: 250.*
+S: 250-AUTH LOGIN PLAIN
+S: 250-AUTH=LOGIN PLAIN
 S: 250-PIPELINING
 S: 250-ENHANCEDSTATUSCODES
 S: 250-8BITMIME

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_with_starttls.test
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_with_starttls.test b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_with_starttls.test
index edd3a40..c25db1b 100644
--- a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_with_starttls.test
+++ b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/data_with_starttls.test
@@ -4,6 +4,8 @@ C: mail from:<ma...@yopmail.com>
 C: rcpt to:<bo...@mydomain.tld>
 C: data
 S: 250.*
+S: 250-AUTH LOGIN PLAIN
+S: 250-AUTH=LOGIN PLAIN
 S: 250-PIPELINING
 S: 250-ENHANCEDSTATUSCODES
 S: 250-8BITMIME

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/rcpt_with_starttls.test
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/rcpt_with_starttls.test b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/rcpt_with_starttls.test
index efe719b..5b6de69 100644
--- a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/rcpt_with_starttls.test
+++ b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/rcpt_with_starttls.test
@@ -4,6 +4,8 @@ C: mail from:<ma...@yopmail.com>
 C: rcpt to:<st...@mydomain.tld>
 C: data
 S: 250.*
+S: 250-AUTH LOGIN PLAIN
+S: 250-AUTH=LOGIN PLAIN
 S: 250-PIPELINING
 S: 250-ENHANCEDSTATUSCODES
 S: 250-8BITMIME

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls.test
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls.test b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls.test
index abcb5c9..d71186b 100644
--- a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls.test
+++ b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls.test
@@ -2,6 +2,8 @@ S: 220 mydomain.tld smtp
 
 C: ehlo yopmail.com
 S: 250.*
+S: 250-AUTH LOGIN PLAIN
+S: 250-AUTH=LOGIN PLAIN
 S: 250-PIPELINING
 S: 250-ENHANCEDSTATUSCODES
 S: 250-8BITMIME

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls_with_injection.test
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls_with_injection.test b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls_with_injection.test
index 5c09717..90068f6 100644
--- a/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls_with_injection.test
+++ b/mpt/impl/smtp/core/src/main/resources/org/apache/james/smtp/scripts/starttls_with_injection.test
@@ -2,6 +2,8 @@ S: 220 mydomain.tld smtp
 
 C: ehlo yopmail.com
 S: 250.*
+S: 250-AUTH LOGIN PLAIN
+S: 250-AUTH=LOGIN PLAIN
 S: 250-PIPELINING
 S: 250-ENHANCEDSTATUSCODES
 S: 250-8BITMIME

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/container/guice/cassandra-guice/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/resources/smtpserver.xml b/server/container/guice/cassandra-guice/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/container/guice/cassandra-guice/src/test/resources/smtpserver.xml
+++ b/server/container/guice/cassandra-guice/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/container/guice/cassandra-ldap-guice/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-ldap-guice/src/test/resources/smtpserver.xml b/server/container/guice/cassandra-ldap-guice/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/container/guice/cassandra-ldap-guice/src/test/resources/smtpserver.xml
+++ b/server/container/guice/cassandra-ldap-guice/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/container/guice/jpa-guice/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/jpa-guice/src/test/resources/smtpserver.xml b/server/container/guice/jpa-guice/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/container/guice/jpa-guice/src/test/resources/smtpserver.xml
+++ b/server/container/guice/jpa-guice/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/container/guice/jpa-smtp/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/jpa-smtp/src/test/resources/smtpserver.xml b/server/container/guice/jpa-smtp/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/container/guice/jpa-smtp/src/test/resources/smtpserver.xml
+++ b/server/container/guice/jpa-smtp/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/container/guice/memory-guice/src/test/resources/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/memory-guice/src/test/resources/mailetcontainer.xml b/server/container/guice/memory-guice/src/test/resources/mailetcontainer.xml
index 04ffa3e..887dab3 100644
--- a/server/container/guice/memory-guice/src/test/resources/mailetcontainer.xml
+++ b/server/container/guice/memory-guice/src/test/resources/mailetcontainer.xml
@@ -64,6 +64,9 @@
             <mailet match="SMTPAuthSuccessful" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="SetMailAttribute">
+                <RelayAllowed>true</RelayAllowed>
+            </mailet>
             <mailet match="SentByMailet" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/container/guice/memory-guice/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/container/guice/memory-guice/src/test/resources/smtpserver.xml b/server/container/guice/memory-guice/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/container/guice/memory-guice/src/test/resources/smtpserver.xml
+++ b/server/container/guice/memory-guice/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/mailetcontainer.xml b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/mailetcontainer.xml
index b998dcd..dbcf81b 100644
--- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/mailetcontainer.xml
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/mailetcontainer.xml
@@ -61,6 +61,9 @@
             <mailet match="SMTPAuthSuccessful" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="SetMailAttribute">
+                <RelayAllowed>true</RelayAllowed>
+            </mailet>
             <mailet match="SentByMailet" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/smtpserver.xml b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/smtpserver.xml
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/mailetcontainer.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/mailetcontainer.xml b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/mailetcontainer.xml
index 7b421ca..fe07346 100644
--- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/mailetcontainer.xml
+++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/mailetcontainer.xml
@@ -61,6 +61,9 @@
             <mailet match="SMTPAuthSuccessful" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>
+            <mailet match="SMTPIsAuthNetwork" class="SetMailAttribute">
+                <RelayAllowed>true</RelayAllowed>
+            </mailet>
             <mailet match="SentByMailet" class="SetMailAttribute">
                 <RelayAllowed>true</RelayAllowed>
             </mailet>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0ce56b7e/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/smtpserver.xml b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/smtpserver.xml
index a3d4b8f..2f83c8e 100644
--- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/smtpserver.xml
+++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/resources/smtpserver.xml
@@ -34,7 +34,6 @@
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
         <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <verifyIdentity>true</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
         <addressBracketsEnforcement>true</addressBracketsEnforcement>
@@ -61,7 +60,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>
@@ -89,7 +87,6 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
         <maxmessagesize>0</maxmessagesize>


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[05/13] james-project git commit: JAMES-2246 Allow customizable SMTP configuration

Posted by bt...@apache.org.
JAMES-2246 Allow customizable SMTP configuration


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b64f2b2c
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b64f2b2c
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b64f2b2c

Branch: refs/heads/master
Commit: b64f2b2cbd1201060ff06e9743b1734678630a35
Parents: 4311b7d
Author: benwa <bt...@linagora.com>
Authored: Mon Dec 4 11:15:07 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:34:45 2017 +0700

----------------------------------------------------------------------
 server/mailet/integration-testing/pom.xml       |  15 ++
 .../mailets/TemporaryFilesystemModule.java      |   3 +-
 .../james/mailets/TemporaryJamesServer.java     |  24 ++-
 .../configuration/SerializableAsXml.java        |   4 +-
 .../configuration/SmtpConfiguration.java        | 149 ++++++++++++++++++
 .../configuration/SmtpConfigurationTest.java    | 155 +++++++++++++++++++
 .../src/test/resources/smtpserver.xml           |  28 ++--
 7 files changed, 363 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/pom.xml
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/pom.xml b/server/mailet/integration-testing/pom.xml
index 336e5b6..12352da 100644
--- a/server/mailet/integration-testing/pom.xml
+++ b/server/mailet/integration-testing/pom.xml
@@ -106,6 +106,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>com.github.spullara.mustache.java</groupId>
+            <artifactId>compiler</artifactId>
+            <version>0.9.5</version>
+        </dependency>
+        <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
         </dependency>
@@ -147,6 +152,16 @@
             <groupId>org.testcontainers</groupId>
             <artifactId>testcontainers</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.xmlunit</groupId>
+            <artifactId>xmlunit-core</artifactId>
+            <version>2.5.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.xmlunit</groupId>
+            <artifactId>xmlunit-matchers</artifactId>
+            <version>2.5.1</version>
+        </dependency>
     </dependencies>
 
     <profiles>

http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java
index 22eaac4..7770d1a 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java
@@ -30,10 +30,10 @@ import java.util.List;
 import java.util.function.Supplier;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.james.server.core.JamesServerResourceLoader;
 import org.apache.james.filesystem.api.FileSystem;
 import org.apache.james.filesystem.api.JamesDirectoriesProvider;
 import org.apache.james.modules.CommonServicesModule;
+import org.apache.james.server.core.JamesServerResourceLoader;
 import org.junit.rules.TemporaryFolder;
 
 import com.google.common.base.Throwables;
@@ -52,7 +52,6 @@ public class TemporaryFilesystemModule extends AbstractModule {
             "managesieveserver.xml",
             "pop3server.xml",
             "recipientrewritetable.xml",
-            "smtpserver.xml",
             "usersrepository.xml",
             "smime.p12");
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
index 0d32c46..ebbe397 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
@@ -36,6 +36,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.james.GuiceJamesServer;
 import org.apache.james.MemoryJamesServerMain;
 import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.SmtpConfiguration;
 import org.apache.james.modules.TestJMAPServerModule;
 import org.apache.james.utils.GuiceProbe;
 import org.apache.james.webadmin.WebAdminConfiguration;
@@ -50,10 +51,12 @@ public class TemporaryJamesServer {
     public static class Builder {
         private ImmutableList.Builder<Module> overrideModules;
         private Optional<Module> module;
+        private Optional<SmtpConfiguration> smtpConfiguration;
 
         private Builder() {
             overrideModules = ImmutableList.builder();
             module = Optional.empty();
+            smtpConfiguration = Optional.empty();
         }
 
         public Builder withBase(Module module) {
@@ -61,6 +64,11 @@ public class TemporaryJamesServer {
             return this;
         }
 
+        public Builder withSmtpConfiguration(SmtpConfiguration smtpConfiguration) {
+            this.smtpConfiguration = Optional.of(smtpConfiguration);
+            return this;
+        }
+
         public Builder withOverrides(Module... modules) {
             this.overrideModules.addAll(Arrays.asList(modules));
             return this;
@@ -70,6 +78,7 @@ public class TemporaryJamesServer {
             return new TemporaryJamesServer(
                 temporaryFolder,
                 mailetContainer,
+                smtpConfiguration.orElse(SmtpConfiguration.DEFAULT),
                 module.orElse(MemoryJamesServerMain.IN_MEMORY_SERVER_AGGREGATE_MODULE),
                 overrideModules.build());
         }
@@ -80,14 +89,16 @@ public class TemporaryJamesServer {
     }
 
     private static final String MAILETCONTAINER_CONFIGURATION_FILENAME = "mailetcontainer.xml";
+    private static final String SMTP_CONFIGURATION_FILENAME = "smtpserver.xml";
 
     private static final int LIMIT_TO_3_MESSAGES = 3;
 
     private final GuiceJamesServer jamesServer;
 
-    private TemporaryJamesServer(TemporaryFolder temporaryFolder, MailetContainer mailetContainer,
+    private TemporaryJamesServer(TemporaryFolder temporaryFolder, MailetContainer mailetContainer, SmtpConfiguration smtpConfiguration,
                                  Module serverBaseModule, List<Module> additionalModules) throws Exception {
         appendMailetConfigurations(temporaryFolder, mailetContainer);
+        appendSmtpConfigurations(temporaryFolder, smtpConfiguration);
 
         jamesServer = new GuiceJamesServer()
             .combineWith(serverBaseModule)
@@ -106,11 +117,22 @@ public class TemporaryJamesServer {
         }
     }
 
+    private void appendSmtpConfigurations(TemporaryFolder temporaryFolder, SmtpConfiguration smtpConfiguration) throws ConfigurationException, IOException {
+        try (OutputStream outputStream = createSmtpConfigurationFile(temporaryFolder)) {
+            IOUtils.write(smtpConfiguration.serializeAsXml(), outputStream, StandardCharsets.UTF_8);
+        }
+    }
+
     private FileOutputStream createMailetConfigurationFile(TemporaryFolder temporaryFolder) throws IOException {
         File configurationFolder = temporaryFolder.newFolder("conf");
         return new FileOutputStream(Paths.get(configurationFolder.getAbsolutePath(), MAILETCONTAINER_CONFIGURATION_FILENAME).toFile());
     }
 
+    private FileOutputStream createSmtpConfigurationFile(TemporaryFolder temporaryFolder) throws IOException {
+        File configurationFolder = temporaryFolder.getRoot().listFiles()[0];
+        return new FileOutputStream(Paths.get(configurationFolder.getAbsolutePath(), SMTP_CONFIGURATION_FILENAME).toFile());
+    }
+
     public void shutdown() {
         jamesServer.stop();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SerializableAsXml.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SerializableAsXml.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SerializableAsXml.java
index fcc9248..22dda03 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SerializableAsXml.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SerializableAsXml.java
@@ -20,7 +20,9 @@
 
 package org.apache.james.mailets.configuration;
 
+import java.io.IOException;
+
 public interface SerializableAsXml {
 
-    String serializeAsXml();
+    String serializeAsXml() throws IOException;
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfiguration.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfiguration.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfiguration.java
new file mode 100644
index 0000000..de9a23b
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfiguration.java
@@ -0,0 +1,149 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.mailets.configuration;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringReader;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Optional;
+
+import org.apache.commons.io.IOUtils;
+
+import com.github.mustachejava.DefaultMustacheFactory;
+import com.github.mustachejava.Mustache;
+import com.github.mustachejava.MustacheFactory;
+import com.google.common.base.Preconditions;
+
+public class SmtpConfiguration implements SerializableAsXml {
+    public static final boolean AUTH_REQUIRED = true;
+    public static final SmtpConfiguration DEFAULT = SmtpConfiguration.builder().build();
+
+    public static class Builder {
+        private static final int DEFAULT_DISABLED = 0;
+
+        private Optional<Boolean> authRequired;
+        private Optional<Integer> maxMessageSizeInKb;
+        private Optional<Boolean> verifyIndentity;
+        private Optional<Boolean> bracketEnforcement;
+        private Optional<String> authorizedAddresses;
+
+        public Builder() {
+            authorizedAddresses = Optional.empty();
+            authRequired = Optional.empty();
+            verifyIndentity = Optional.empty();
+            maxMessageSizeInKb = Optional.empty();
+            bracketEnforcement = Optional.empty();
+        }
+
+        public Builder withAutorizedAddresses(String authorizedAddresses) {
+            Preconditions.checkNotNull(authorizedAddresses);
+            this.authorizedAddresses = Optional.of(authorizedAddresses);
+            return this;
+        }
+
+        public Builder withMaxMessageSizeInKb(int sizeInKb) {
+            Preconditions.checkArgument(sizeInKb > 0);
+            this.maxMessageSizeInKb = Optional.of(sizeInKb);
+            return this;
+        }
+
+        public Builder requireAuthentication() {
+            this.authRequired = Optional.of(AUTH_REQUIRED);
+            return this;
+        }
+
+        public Builder requireBracketEnforcement() {
+            this.bracketEnforcement = Optional.of(true);
+            return this;
+        }
+
+        public Builder doNotRequireBracketEnforcement() {
+            this.bracketEnforcement = Optional.of(false);
+            return this;
+        }
+
+        public Builder verifyIdentity() {
+            this.verifyIndentity = Optional.of(true);
+            return this;
+        }
+
+        public Builder doNotVerifyIdentity() {
+            this.verifyIndentity = Optional.of(false);
+            return this;
+        }
+
+        public SmtpConfiguration build() {
+            return new SmtpConfiguration(authorizedAddresses,
+                authRequired.orElse(!AUTH_REQUIRED),
+                bracketEnforcement.orElse(true),
+                verifyIndentity.orElse(true),
+                maxMessageSizeInKb.orElse(DEFAULT_DISABLED));
+        }
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    private final Optional<String> authorizedAddresses;
+    private final boolean authRequired;
+    private final boolean bracketEnforcement;
+    private final boolean verifyIndentity;
+    private final int maxMessageSizeInKb;
+
+    public SmtpConfiguration(Optional<String> authorizedAddresses, boolean authRequired, boolean bracketEnforcement, boolean verifyIndentity, int maxMessageSizeInKb) {
+        this.authorizedAddresses = authorizedAddresses;
+        this.authRequired = authRequired;
+        this.bracketEnforcement = bracketEnforcement;
+        this.verifyIndentity = verifyIndentity;
+        this.maxMessageSizeInKb = maxMessageSizeInKb;
+    }
+
+    public String serializeAsXml() throws IOException {
+        HashMap<String, Object> scopes = new HashMap<>();
+        scopes.put("hasAuthorizedAddresses", authorizedAddresses.isPresent());
+        authorizedAddresses.ifPresent(value -> scopes.put("authorizedAddresses", value));
+        scopes.put("authRequired", authRequired);
+        scopes.put("verifyIdentity", verifyIndentity);
+        scopes.put("maxmessagesize", maxMessageSizeInKb);
+        scopes.put("bracketEnforcement", bracketEnforcement);
+
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        Writer writer = new OutputStreamWriter(byteArrayOutputStream);
+        MustacheFactory mf = new DefaultMustacheFactory();
+        Mustache mustache = mf.compile(getPatternReader(), "example");
+        mustache.execute(writer, scopes);
+        writer.flush();
+        return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
+    }
+
+    private StringReader getPatternReader() throws IOException {
+        InputStream patternStream = ClassLoader.getSystemResourceAsStream("smtpserver.xml");
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        IOUtils.copy(patternStream, byteArrayOutputStream);
+        String pattern = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
+        return new StringReader(pattern);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfigurationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfigurationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfigurationTest.java
new file mode 100644
index 0000000..abd0c3f
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/configuration/SmtpConfigurationTest.java
@@ -0,0 +1,155 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.mailets.configuration;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.xmlunit.matchers.EvaluateXPathMatcher.hasXPath;
+
+import java.io.IOException;
+
+import javax.xml.transform.Source;
+
+import org.assertj.core.api.Assertions;
+import org.junit.Test;
+import org.w3c.dom.Node;
+import org.xmlunit.builder.Input;
+import org.xmlunit.xpath.JAXPXPathEngine;
+import org.xmlunit.xpath.XPathEngine;
+
+public class SmtpConfigurationTest {
+    @Test
+    public void authenticationCanBeRequired() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .requireAuthentication()
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/authRequired/text()",
+                is("true")));
+    }
+
+    @Test
+    public void maxMessageSizeCanBeCustomized() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .withMaxMessageSizeInKb(36)
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/maxmessagesize/text()",
+                is("36")));
+    }
+
+    @Test
+    public void bracketEnforcementCanBeDisable() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .doNotRequireBracketEnforcement()
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/addressBracketsEnforcement/text()",
+                is("false")));
+    }
+
+    @Test
+    public void verifyIdentityEnforcementCanBeDisabled() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .doNotVerifyIdentity()
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/verifyIdentity/text()",
+                is("false")));
+    }
+
+    @Test
+    public void authenticationCanBeDisabled() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/authRequired/text()",
+                is("false")));
+    }
+
+    @Test
+    public void bracketEnforcementCanBeEnabled() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .requireBracketEnforcement()
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/addressBracketsEnforcement/text()",
+                is("true")));
+    }
+
+    @Test
+    public void verifyIdentityEnforcementCanBeEnabled() throws IOException {
+        assertThat(SmtpConfiguration.builder()
+                .verifyIdentity()
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/verifyIdentity/text()",
+                is("true")));
+    }
+
+    @Test
+    public void specificNetworkCanBeAuthorized() throws IOException {
+        String network = "172.0.0.0/24";
+        assertThat(SmtpConfiguration.builder()
+                .withAutorizedAddresses(network)
+                .build()
+                .serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/authorizedAddresses/text()",
+                is(network)));
+    }
+
+    @Test
+    public void defaultSmtpConfigurationShouldNotHaveAuthorizedNetwork() throws IOException {
+        String xmlFile = SmtpConfiguration.DEFAULT.serializeAsXml();
+        Source source = Input.fromString(xmlFile).build();
+        XPathEngine xpath = new JAXPXPathEngine();
+        Iterable<Node> allMatches = xpath.selectNodes("/smtpservers/smtpserver/authorizedAddresses", source);
+
+        Assertions.assertThat(allMatches).isEmpty();
+    }
+
+    @Test
+    public void authenticationShouldNotBeRequiredByDefault() throws IOException {
+        assertThat(SmtpConfiguration.DEFAULT.serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/authRequired/text()",
+                is("false")));
+    }
+
+    @Test
+    public void maxMessageSizeShouldBeDisabledByDefault() throws IOException {
+        assertThat(SmtpConfiguration.DEFAULT.serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/maxmessagesize/text()",
+                is("0")));
+    }
+
+    @Test
+    public void addressBracketsEnforcementShouldBeEnforcedByDefault() throws IOException {
+        assertThat(SmtpConfiguration.DEFAULT.serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/addressBracketsEnforcement/text()",
+                is("true")));
+    }
+
+    @Test
+    public void verifyIdentityShouldBeEnforcedByDefault() throws IOException {
+        assertThat(SmtpConfiguration.DEFAULT.serializeAsXml(),
+            hasXPath("/smtpservers/smtpserver/verifyIdentity/text()",
+                is("true")));
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b64f2b2c/server/mailet/integration-testing/src/test/resources/smtpserver.xml
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/resources/smtpserver.xml b/server/mailet/integration-testing/src/test/resources/smtpserver.xml
index a3d4b8f..30c56f8 100644
--- a/server/mailet/integration-testing/src/test/resources/smtpserver.xml
+++ b/server/mailet/integration-testing/src/test/resources/smtpserver.xml
@@ -33,11 +33,13 @@
         <connectiontimeout>360</connectiontimeout>
         <connectionLimit>0</connectionLimit>
         <connectionLimitPerIP>0</connectionLimitPerIP>
-        <authRequired>false</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
-        <verifyIdentity>true</verifyIdentity>
-        <maxmessagesize>0</maxmessagesize>
-        <addressBracketsEnforcement>true</addressBracketsEnforcement>
+        <authRequired>{{authRequired}}</authRequired>
+{{#hasAuthorizedAddresses}}
+        <authorizedAddresses>{{authorizedAddresses}}</authorizedAddresses>
+{{/hasAuthorizedAddresses}}
+        <verifyIdentity>{{verifyIdentity}}</verifyIdentity>
+        <maxmessagesize>{{maxmessagesize}}</maxmessagesize>
+        <addressBracketsEnforcement>{{bracketEnforcement}}</addressBracketsEnforcement>
         <smtpGreeting>JAMES Linagora's SMTP awesome Server</smtpGreeting>
         <handlerchain>
             <handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/>
@@ -61,11 +63,13 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        {{#hasAuthorizedAddresses}}
+        <authorizedAddresses>{{authorizedAddresses}}</authorizedAddresses>
+        {{/hasAuthorizedAddresses}}
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
-        <maxmessagesize>0</maxmessagesize>
-        <addressBracketsEnforcement>true</addressBracketsEnforcement>
+        <maxmessagesize>{{maxmessagesize}}</maxmessagesize>
+        <addressBracketsEnforcement>{{bracketEnforcement}}</addressBracketsEnforcement>
         <smtpGreeting>JAMES Linagora's SMTP awesome Server</smtpGreeting>
         <handlerchain>
             <handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/>
@@ -89,11 +93,13 @@
            Authorize only local users
         -->
         <authRequired>true</authRequired>
-        <authorizedAddresses>0.0.0.0/0</authorizedAddresses>
+        {{#hasAuthorizedAddresses}}
+        <authorizedAddresses>{{authorizedAddresses}}</authorizedAddresses>
+        {{/hasAuthorizedAddresses}}
         <!-- Trust authenticated users -->
         <verifyIdentity>false</verifyIdentity>
-        <maxmessagesize>0</maxmessagesize>
-        <addressBracketsEnforcement>true</addressBracketsEnforcement>
+        <maxmessagesize>{{maxmessagesize}}</maxmessagesize>
+        <addressBracketsEnforcement>{{bracketEnforcement}}</addressBracketsEnforcement>
         <smtpGreeting>JAMES Linagora's SMTP awesome Server</smtpGreeting>
         <handlerchain>
             <handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/>


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[11/13] james-project git commit: JAMES-2246 Adding tests for SMTP recipient brackets enforcement

Posted by bt...@apache.org.
JAMES-2246 Adding tests for SMTP recipient brackets enforcement


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2c55ee75
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2c55ee75
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2c55ee75

Branch: refs/heads/master
Commit: 2c55ee75c1bed8ca8416dcaf2f01d687b4221248
Parents: 27556e9
Author: benwa <bt...@linagora.com>
Authored: Tue Dec 5 15:12:38 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:35:50 2017 +0700

----------------------------------------------------------------------
 .../james/smtp/SmtpBracketEnforcementTest.java  | 176 +++++++++++++++++++
 .../apache/james/utils/SMTPMessageSender.java   |  15 ++
 2 files changed, 191 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2c55ee75/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpBracketEnforcementTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpBracketEnforcementTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpBracketEnforcementTest.java
new file mode 100644
index 0000000..3480a8e
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpBracketEnforcementTest.java
@@ -0,0 +1,176 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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 org.apache.james.smtp;
+
+import org.apache.james.MemoryJamesServerMain;
+import org.apache.james.mailets.TemporaryJamesServer;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetConfiguration;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.mailets.configuration.SmtpConfiguration;
+import org.apache.james.probe.DataProbe;
+import org.apache.james.transport.mailets.LocalDelivery;
+import org.apache.james.transport.mailets.RemoveMimeHeader;
+import org.apache.james.transport.mailets.ToProcessor;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.transport.matchers.RecipientIsLocal;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.SMTPMessageSender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class SmtpBracketEnforcementTest {
+    private static final String DEFAULT_DOMAIN = "james.org";
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int SMTP_PORT = 1025;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+    private static final String USER = "user@" + JAMES_APACHE_ORG;
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+
+    @Before
+    public void setup() throws Exception {
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
+            .pollInterval(slowPacedPollInterval)
+            .and()
+            .with()
+            .pollDelay(slowPacedPollInterval)
+            .await();
+    }
+
+    private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception {
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + DEFAULT_DOMAIN)
+            .threads(5)
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("root")
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "transport")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(ProcessorConfiguration.builder()
+                .state("transport")
+                .enableJmx(true)
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(RemoveMimeHeader.class)
+                    .addProperty("name", "bcc")
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(RecipientIsLocal.class)
+                    .mailet(LocalDelivery.class)
+                    .build())
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToProcessor.class)
+                    .addProperty("processor", "bounces")
+                    .build())
+                .build())
+            .addProcessor(CommonProcessors.localAddressError())
+            .addProcessor(CommonProcessors.relayDenied())
+            .addProcessor(CommonProcessors.bounces())
+            .build();
+        jamesServer = TemporaryJamesServer.builder()
+            .withBase(MemoryJamesServerMain.SMTP_ONLY_MODULE)
+            .withSmtpConfiguration(smtpConfiguration.build())
+            .build(temporaryFolder, mailetContainer);
+
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(JAMES_APACHE_ORG);
+        dataProbe.addUser(USER, PASSWORD);
+    }
+
+    @After
+    public void tearDown() {
+        if (jamesServer != null) {
+            jamesServer.shutdown();
+        }
+    }
+
+    @Test
+    public void recipientWithBracketsShouldBeAcceptedWhenNoBracketRequired() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .doNotRequireBracketEnforcement());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessage(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+        }
+    }
+
+    @Test
+    public void recipientWithNoBracketsShouldBeAcceptedWhenNoBracketRequired() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .doNotRequireBracketEnforcement());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessageNoBracket(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+        }
+    }
+
+    @Test
+    public void recipientWithBracketsShouldBeAcceptedWhenBracketRequired() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireBracketEnforcement());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessage(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+        }
+    }
+
+    @Test
+    public void recipientWithNoBracketsShouldBeRejectedWhenBracketRequired() throws Exception {
+        createJamesServer(SmtpConfiguration.builder()
+            .requireBracketEnforcement());
+
+        try (SMTPMessageSender messageSender =
+                 SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) {
+
+            messageSender.sendMessageNoBracket(USER, USER);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageSendingFailed);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/2c55ee75/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
index ef61f24..2fb175e 100644
--- a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
+++ b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
@@ -79,6 +79,21 @@ public class SMTPMessageSender implements Closeable {
         }
     }
 
+    public void sendMessageNoBracket(String from, String recipient) {
+        try {
+            smtpClient.helo(senderDomain);
+            smtpClient.setSender(from);
+            smtpClient.rcpt(recipient);
+            smtpClient.sendShortMessageData("FROM: " + from + "\r\n" +
+                "subject: test\r\n" +
+                "\r\n" +
+                "content\r\n" +
+                ".\r\n");
+        } catch (IOException e) {
+            throw Throwables.propagate(e);
+        }
+    }
+
     public void sendMessageWithHeaders(String from, String recipient, String message) {
         try {
             smtpClient.helo(senderDomain);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Re: [06/13] james-project git commit: JAMES−2246 Sanitize SMTPIsAuthNetwork

Posted by jackie issa <ja...@hotmail.com>.

Sent from Outlook Mobile<https://aka.ms/qtex0l>
________________________________
From: btellier@apache.org <bt...@apache.org>
Sent: Friday, December 8, 2017 12:40:01 PM
To: server-dev@james.apache.org
Subject: [06/13] james-project git commit: JAMES−2246 Sanitize SMTPIsAuthNetwork

JAMES???2246 Sanitize SMTPIsAuthNetwork


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4311b7d1
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4311b7d1
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4311b7d1

Branch: refs/heads/master
Commit: 4311b7d143061ff3de9bd30bc3ae5f3e55f84d37
Parents: 8eb8bf1
Author: benwa <bt...@linagora.com>
Authored: Wed Dec 6 17:27:53 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:34:45 2017 +0700

----------------------------------------------------------------------
 .../transport/matchers/SMTPIsAuthNetwork.java   | 19 +++--
 .../matchers/SMTPIsAuthNetworkTest.java         | 79 ++++++++------------
 2 files changed, 41 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4311b7d1/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
index bc9afa9..9773993 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
@@ -17,16 +17,16 @@
  * under the License.                                           *
  ****************************************************************/

-
-
 package org.apache.james.transport.matchers;

-import org.apache.mailet.Experimental;
-import org.apache.mailet.base.GenericMatcher;
-import org.apache.mailet.Mail;
+import java.util.Collection;
+import java.util.Objects;
+
 import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;

-import java.util.Collection;
+import com.google.common.collect.ImmutableList;

 /**
  * <P>
@@ -37,21 +37,20 @@ import java.util.Collection;
  * class=&quot;&lt;any-class&gt;&quot;&gt; </CODE></PRE>
  *
  */
-@Experimental
 public class SMTPIsAuthNetwork extends GenericMatcher {

     /**
      * The mail attribute which is set if the client is allowed to relay
      */
-    private final static String SMTP_AUTH_NETWORK_NAME = "org.apache.james.SMTPIsAuthNetwork";
+    public static final String SMTP_AUTH_NETWORK_NAME = "org.apache.james.SMTPIsAuthNetwork";

     public Collection<MailAddress> match(Mail mail) {
         String relayingAllowed = (String) mail
                 .getAttribute(SMTP_AUTH_NETWORK_NAME);
-        if (relayingAllowed != null && relayingAllowed.equals("true")) {
+        if (Objects.equals(relayingAllowed, "true")) {
             return mail.getRecipients();
         } else {
-            return null;
+            return ImmutableList.of();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/4311b7d1/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
index 8f90d00..3ce6235 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
@@ -17,71 +17,56 @@
  * under the License.                                           *
  ****************************************************************/

-
 package org.apache.james.transport.matchers;

-import java.util.Collection;
-
-import javax.mail.MessagingException;
+import static org.assertj.core.api.Assertions.assertThat;

 import org.apache.james.core.MailAddress;
-import org.apache.mailet.Matcher;
+import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.FakeMail;
-import org.apache.mailet.base.test.FakeMatcherConfig;
-import org.apache.mailet.base.test.MailUtil;
-import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;

 public class SMTPIsAuthNetworkTest {

-    private FakeMail mockedMail;
-
-    private Matcher matcher;
-
-    private boolean isAuthorized = false;
-
-    private void setIsAuthorized(boolean isAuthorized) {
-        this.isAuthorized = isAuthorized;
-    }
+    private SMTPIsAuthNetwork testee;
+    private MailAddress recipient;

-    private void setupMockedMail() throws MessagingException {
-        mockedMail = MailUtil.createMockMail2Recipients();
-        if (isAuthorized) {
-            String MAIL_ATTRIBUTE_NAME = "org.apache.james.SMTPIsAuthNetwork";
-            mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME, "true");
-        }
+    @Before
+    public void setUp() throws Exception {
+        testee = new SMTPIsAuthNetwork();
+        recipient = new MailAddress("recipient@domain.com");
     }

-    private void setupMatcher() throws MessagingException {
-        matcher = new SMTPIsAuthNetwork();
-        FakeMatcherConfig mci = FakeMatcherConfig.builder()
-                .matcherName("SMTPIsAuthNetwork")
-                .build();
+    @Test
+    public void matchShouldReturnEmptyWhenNoSmtpInformation() throws Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .build();

-        matcher.init(mci);
+        assertThat(testee.match(mail))
+            .isEmpty();
     }

     @Test
-    public void testIsAuthNetwork() throws MessagingException {
-        setIsAuthorized(true);
-        setupMockedMail();
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
-        Assert.assertNotNull(matchedRecipients);
-        Assert.assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
-                .size());
+    public void matchShouldReturnAddressesWhenAuthorizedNetwork() throws Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .attribute(SMTPIsAuthNetwork.SMTP_AUTH_NETWORK_NAME, "true")
+            .build();
+
+        assertThat(testee.match(mail))
+            .containsOnly(recipient);
     }

     @Test
-    public void testIsNotAuthNetwork() throws MessagingException {
-        setIsAuthorized(false);
-        setupMockedMail();
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
-        Assert.assertNull(matchedRecipients);
+    public void matchShouldReturnEmptyWhenNonAuthorizedNetwork() throws Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .attribute(SMTPIsAuthNetwork.SMTP_AUTH_NETWORK_NAME, "false")
+            .build();
+
+        assertThat(testee.match(mail))
+            .isEmpty();
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[06/13] james-project git commit: JAMES−2246 Sanitize SMTPIsAuthNetwork

Posted by bt...@apache.org.
JAMES−2246 Sanitize SMTPIsAuthNetwork


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4311b7d1
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4311b7d1
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4311b7d1

Branch: refs/heads/master
Commit: 4311b7d143061ff3de9bd30bc3ae5f3e55f84d37
Parents: 8eb8bf1
Author: benwa <bt...@linagora.com>
Authored: Wed Dec 6 17:27:53 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Dec 8 17:34:45 2017 +0700

----------------------------------------------------------------------
 .../transport/matchers/SMTPIsAuthNetwork.java   | 19 +++--
 .../matchers/SMTPIsAuthNetworkTest.java         | 79 ++++++++------------
 2 files changed, 41 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4311b7d1/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
index bc9afa9..9773993 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/SMTPIsAuthNetwork.java
@@ -17,16 +17,16 @@
  * under the License.                                           *
  ****************************************************************/
 
-
-
 package org.apache.james.transport.matchers;
 
-import org.apache.mailet.Experimental;
-import org.apache.mailet.base.GenericMatcher;
-import org.apache.mailet.Mail;
+import java.util.Collection;
+import java.util.Objects;
+
 import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
 
-import java.util.Collection;
+import com.google.common.collect.ImmutableList;
 
 /**
  * <P>
@@ -37,21 +37,20 @@ import java.util.Collection;
  * class=&quot;&lt;any-class&gt;&quot;&gt; </CODE></PRE>
  * 
  */
-@Experimental
 public class SMTPIsAuthNetwork extends GenericMatcher {
 
     /**
      * The mail attribute which is set if the client is allowed to relay
      */
-    private final static String SMTP_AUTH_NETWORK_NAME = "org.apache.james.SMTPIsAuthNetwork";
+    public static final String SMTP_AUTH_NETWORK_NAME = "org.apache.james.SMTPIsAuthNetwork";
 
     public Collection<MailAddress> match(Mail mail) {
         String relayingAllowed = (String) mail
                 .getAttribute(SMTP_AUTH_NETWORK_NAME);
-        if (relayingAllowed != null && relayingAllowed.equals("true")) {
+        if (Objects.equals(relayingAllowed, "true")) {
             return mail.getRecipients();
         } else {
-            return null;
+            return ImmutableList.of();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/4311b7d1/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
index 8f90d00..3ce6235 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SMTPIsAuthNetworkTest.java
@@ -17,71 +17,56 @@
  * under the License.                                           *
  ****************************************************************/
 
-
 package org.apache.james.transport.matchers;
 
-import java.util.Collection;
-
-import javax.mail.MessagingException;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import org.apache.james.core.MailAddress;
-import org.apache.mailet.Matcher;
+import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.FakeMail;
-import org.apache.mailet.base.test.FakeMatcherConfig;
-import org.apache.mailet.base.test.MailUtil;
-import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 public class SMTPIsAuthNetworkTest {
 
-    private FakeMail mockedMail;
-
-    private Matcher matcher;
-
-    private boolean isAuthorized = false;
-
-    private void setIsAuthorized(boolean isAuthorized) {
-        this.isAuthorized = isAuthorized;
-    }
+    private SMTPIsAuthNetwork testee;
+    private MailAddress recipient;
 
-    private void setupMockedMail() throws MessagingException {
-        mockedMail = MailUtil.createMockMail2Recipients();
-        if (isAuthorized) {
-            String MAIL_ATTRIBUTE_NAME = "org.apache.james.SMTPIsAuthNetwork";
-            mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME, "true");
-        }
+    @Before
+    public void setUp() throws Exception {
+        testee = new SMTPIsAuthNetwork();
+        recipient = new MailAddress("recipient@domain.com");
     }
 
-    private void setupMatcher() throws MessagingException {
-        matcher = new SMTPIsAuthNetwork();
-        FakeMatcherConfig mci = FakeMatcherConfig.builder()
-                .matcherName("SMTPIsAuthNetwork")
-                .build();
+    @Test
+    public void matchShouldReturnEmptyWhenNoSmtpInformation() throws Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .build();
 
-        matcher.init(mci);
+        assertThat(testee.match(mail))
+            .isEmpty();
     }
 
     @Test
-    public void testIsAuthNetwork() throws MessagingException {
-        setIsAuthorized(true);
-        setupMockedMail();
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
-        Assert.assertNotNull(matchedRecipients);
-        Assert.assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
-                .size());
+    public void matchShouldReturnAddressesWhenAuthorizedNetwork() throws Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .attribute(SMTPIsAuthNetwork.SMTP_AUTH_NETWORK_NAME, "true")
+            .build();
+
+        assertThat(testee.match(mail))
+            .containsOnly(recipient);
     }
 
     @Test
-    public void testIsNotAuthNetwork() throws MessagingException {
-        setIsAuthorized(false);
-        setupMockedMail();
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-
-        Assert.assertNull(matchedRecipients);
+    public void matchShouldReturnEmptyWhenNonAuthorizedNetwork() throws Exception {
+        Mail mail = FakeMail.builder()
+            .recipient(recipient)
+            .attribute(SMTPIsAuthNetwork.SMTP_AUTH_NETWORK_NAME, "false")
+            .build();
+
+        assertThat(testee.match(mail))
+            .isEmpty();
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org