This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new bc91a33777 JAMES-2601 Implement a LDAP healthCheck (#1453)
bc91a33777 is described below

commit bc91a337778961dbfac1df6c5dd2d099e9ee6084
Author: Houssem Nasri <[email protected]>
AuthorDate: Fri Feb 24 15:13:05 2023 +0100

    JAMES-2601 Implement a LDAP healthCheck (#1453)
---
 .../james/data/LdapUsersRepositoryModule.java      |  4 ++
 .../apache/james/user/ldap/LdapHealthCheck.java    | 58 +++++++++++++++++
 .../james/user/ldap/LdapHealthCheckTest.java       | 75 ++++++++++++++++++++++
 3 files changed, 137 insertions(+)

diff --git 
a/server/container/guice/data-ldap/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
 
b/server/container/guice/data-ldap/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
index c7e35b74f3..006d9ce49f 100644
--- 
a/server/container/guice/data-ldap/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
+++ 
b/server/container/guice/data-ldap/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
@@ -20,9 +20,11 @@ package org.apache.james.data;
 
 import org.apache.commons.configuration2.ex.ConfigurationException;
 import org.apache.james.adapter.mailbox.UserRepositoryAuthorizator;
+import org.apache.james.core.healthcheck.HealthCheck;
 import org.apache.james.mailbox.Authorizator;
 import org.apache.james.server.core.configuration.ConfigurationProvider;
 import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.ldap.LdapHealthCheck;
 import org.apache.james.user.ldap.LdapRepositoryConfiguration;
 import org.apache.james.user.ldap.ReadOnlyUsersLDAPRepository;
 import org.apache.james.utils.InitializationOperation;
@@ -32,6 +34,7 @@ import com.google.inject.AbstractModule;
 import com.google.inject.Provides;
 import com.google.inject.Scopes;
 import com.google.inject.Singleton;
+import com.google.inject.multibindings.Multibinder;
 import com.google.inject.multibindings.ProvidesIntoSet;
 
 public class LdapUsersRepositoryModule extends AbstractModule {
@@ -40,6 +43,7 @@ public class LdapUsersRepositoryModule extends AbstractModule 
{
         bind(ReadOnlyUsersLDAPRepository.class).in(Scopes.SINGLETON);
         bind(UsersRepository.class).to(ReadOnlyUsersLDAPRepository.class);
         bind(Authorizator.class).to(UserRepositoryAuthorizator.class);
+        Multibinder.newSetBinder(binder(), 
HealthCheck.class).addBinding().to(LdapHealthCheck.class);
     }
 
     @Provides
diff --git 
a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapHealthCheck.java
 
b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapHealthCheck.java
new file mode 100644
index 0000000000..7d5ca43891
--- /dev/null
+++ 
b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapHealthCheck.java
@@ -0,0 +1,58 @@
+/****************************************************************
+ * 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.user.ldap;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
+
+public class LdapHealthCheck implements HealthCheck {
+    public static final ComponentName COMPONENT_NAME = new ComponentName("LDAP 
User Server");
+    public static final Username LDAP_TEST_USER = Username.of("ldap-test");
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(LdapHealthCheck.class);
+
+    private final ReadOnlyUsersLDAPRepository ldapUserRepository;
+
+    @Inject
+    public LdapHealthCheck(ReadOnlyUsersLDAPRepository ldapUserRepository) {
+        this.ldapUserRepository = ldapUserRepository;
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return COMPONENT_NAME;
+    }
+
+    @Override
+    public Mono<Result> check() {
+        return Mono.fromCallable(() -> 
ldapUserRepository.getUserByName(LDAP_TEST_USER))
+            .thenReturn(Result.healthy(COMPONENT_NAME))
+            .doOnError(e -> LOGGER.error("Error in LDAP server", e))
+            .onErrorResume(e -> Mono.just(Result.unhealthy(COMPONENT_NAME, 
"Error checking LDAP server!", e)))
+            .subscribeOn(Schedulers.boundedElastic());
+    }
+}
diff --git 
a/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/LdapHealthCheckTest.java
 
b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/LdapHealthCheckTest.java
new file mode 100644
index 0000000000..61c2d8f3c0
--- /dev/null
+++ 
b/server/data/data-ldap/src/test/java/org/apache/james/user/ldap/LdapHealthCheckTest.java
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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.user.ldap;
+
+import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN_PASSWORD;
+import static org.apache.james.user.ldap.DockerLdapSingleton.DOMAIN;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.domainlist.api.mock.SimpleDomainList;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class LdapHealthCheckTest {
+    static LdapGenericContainer ldapContainer = LdapGenericContainer.builder()
+        .domain(DOMAIN)
+        .password(ADMIN_PASSWORD)
+        .build();
+
+    LdapHealthCheck ldapHealthCheck;
+
+    @BeforeAll
+    static void setUpAll() {
+        ldapContainer.start();
+    }
+
+    @AfterAll
+    static void afterAll() {
+        ldapContainer.stop();
+    }
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        ReadOnlyUsersLDAPRepository ldapUserRepository = new 
ReadOnlyUsersLDAPRepository(new SimpleDomainList());
+        
ldapUserRepository.configure(ReadOnlyUsersLDAPRepositoryTest.ldapRepositoryConfigurationWithVirtualHosting(ldapContainer));
+        ldapUserRepository.init();
+        ldapHealthCheck = new LdapHealthCheck(ldapUserRepository);
+    }
+
+    @Test
+    void checkShouldReturnUnhealthyIfLdapIsDown() {
+        ldapContainer.pause();
+
+        try {
+            Result checkResult = ldapHealthCheck.check().block();
+            assertThat(checkResult.isUnHealthy()).isTrue();
+        } finally {
+            ldapContainer.unpause();
+        }
+    }
+
+    @Test
+    void checkShouldReturnHealthyIfLdapIsRunning() {
+        Result checkResult = ldapHealthCheck.check().block();
+        assertThat(checkResult.isHealthy()).isTrue();
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to