vttranlina commented on code in PR #1824: URL: https://github.com/apache/james-project/pull/1824#discussion_r1441410197
########## server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IpConnectionCheck.java: ########## @@ -0,0 +1,47 @@ +/**************************************************************** + * 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.imapserver.netty; + +import java.net.InetSocketAddress; +import java.util.Set; + +import org.apache.james.imap.api.ConnectionCheck; +import org.reactivestreams.Publisher; + +import reactor.core.publisher.Mono; + +public class IpConnectionCheck implements ConnectionCheck { + private Set<String> bannedIps = Set.of(); + + @Override + public Publisher<Void> validate(InetSocketAddress remoteAddress) { + String ip = remoteAddress.getAddress().getHostAddress(); + // check against bannedIps + if (bannedIps.stream().anyMatch(bannedIp -> bannedIp.equals(ip))) { + return Mono.error(() -> new RuntimeException("Banned")); + } else { + return Mono.empty(); + } + } + + public void setBannedIps(Set<String> bannedIps) { Review Comment: Not sure understand what logic but it looks a bit weird, The `bannedIps` should be immutable? ########## server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/IMAPServerFactory.java: ########## @@ -36,37 +39,45 @@ import org.apache.james.protocols.lib.netty.AbstractServerFactory; import com.github.fge.lambdas.functions.ThrowingFunction; +import com.google.common.collect.ImmutableSet; public class IMAPServerFactory extends AbstractServerFactory { protected final FileSystem fileSystem; protected final ThrowingFunction<HierarchicalConfiguration<ImmutableNode>, ImapSuite> imapSuiteProvider; protected final ImapMetrics imapMetrics; protected final GaugeRegistry gaugeRegistry; + protected final ConnectionCheckFactory connectionCheckFactory; @Inject @Deprecated public IMAPServerFactory(FileSystem fileSystem, ImapDecoder decoder, ImapEncoder encoder, ImapProcessor processor, - MetricFactory metricFactory, GaugeRegistry gaugeRegistry) { + MetricFactory metricFactory, GaugeRegistry gaugeRegistry, ConnectionCheckFactory connectionCheckFactory) { this.fileSystem = fileSystem; + this.connectionCheckFactory = connectionCheckFactory; this.imapSuiteProvider = any -> new ImapSuite(decoder, encoder, processor); this.imapMetrics = new ImapMetrics(metricFactory); this.gaugeRegistry = gaugeRegistry; } public IMAPServerFactory(FileSystem fileSystem, ThrowingFunction<HierarchicalConfiguration<ImmutableNode>, ImapSuite> imapSuiteProvider, - MetricFactory metricFactory, GaugeRegistry gaugeRegistry) { + MetricFactory metricFactory, GaugeRegistry gaugeRegistry, ConnectionCheckFactory connectionCheckFactory) { this.fileSystem = fileSystem; this.imapSuiteProvider = imapSuiteProvider; this.imapMetrics = new ImapMetrics(metricFactory); this.gaugeRegistry = gaugeRegistry; + this.connectionCheckFactory = connectionCheckFactory; } protected IMAPServer createServer(HierarchicalConfiguration<ImmutableNode> config) { ImapSuite imapSuite = imapSuiteProvider.apply(config); - return new IMAPServer(imapSuite.getDecoder(), imapSuite.getEncoder(), imapSuite.getProcessor(), imapMetrics, gaugeRegistry); + ImmutableSet<String> connectionChecks = Arrays.stream(config.getStringArray("additionalConnectionChecks")).collect(ImmutableSet.toImmutableSet()); + + return new IMAPServer(imapSuite.getDecoder(), imapSuite.getEncoder(), imapSuite.getProcessor(), imapMetrics, gaugeRegistry, connectionCheckFactory.create(ImapConfiguration.builder() Review Comment: IMO ``` ImapConfiguration.builder() .connectionChecks(connectionChecks) .build() ``` it looks quite mock, - It should be full (real) "ImapConfiguration", not just a empty ImapConfiguration with only `connectionChecks` value - or the input of `ConnectionCheckFactory::create` should be `HierarchicalConfiguration<ImmutableNode> config` or another ########## third-party/crowdsec/src/main/java/org/apache/james/CrowdsecImapConnectionCheck.java: ########## @@ -0,0 +1,80 @@ +/**************************************************************** + * 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; + +import static org.apache.james.model.CrowdsecClientConfiguration.DEFAULT_TIMEOUT; + +import java.net.InetSocketAddress; +import java.util.concurrent.TimeoutException; + +import javax.inject.Inject; + +import org.apache.commons.net.util.SubnetUtils; +import org.apache.james.exception.CrowdsecException; +import org.apache.james.imap.api.ConnectionCheck; +import org.apache.james.model.CrowdsecClientConfiguration; +import org.apache.james.model.CrowdsecDecision; +import org.apache.james.model.CrowdsecHttpClient; +import org.reactivestreams.Publisher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import reactor.core.publisher.Mono; + +public class CrowdsecImapConnectionCheck implements ConnectionCheck { + private static final Logger LOGGER = LoggerFactory.getLogger(CrowdsecImapConnectionCheck.class); + + private final CrowdsecHttpClient client; + + @Inject + public CrowdsecImapConnectionCheck(CrowdsecClientConfiguration crowdsecClientConfiguration) { + this.client = new CrowdsecHttpClient(crowdsecClientConfiguration); + } + + @Override + public Publisher<Void> validate(InetSocketAddress remoteAddress) { + String ip = remoteAddress.getAddress().getHostAddress(); + + return client.getCrowdsecDecisions() Review Comment: I think we should have the cache here We can use memory cache by caffeine The "DEFAULT_TIMEOUT" is 5 seconds, looks like it is a heavy request, => the request to crowdsec for each request is not good performance -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
