Move LDAP health auto-configuration into spring-boot-ldap

This commit is contained in:
Andy Wilkinson
2025-05-19 08:05:00 +01:00
committed by Phillip Webb
parent df7afc5aac
commit ec48e45c62
9 changed files with 19 additions and 16 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.ldap.actuate.health.autoconfigure;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.ldap.actuate.health.LdapHealthIndicator;
import org.springframework.boot.ldap.autoconfigure.LdapAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.ldap.core.LdapOperations;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link LdapHealthIndicator}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @since 2.0.0
*/
@AutoConfiguration(after = LdapAutoConfiguration.class)
@ConditionalOnClass({ LdapOperations.class, LdapHealthIndicator.class, ConditionalOnEnabledHealthIndicator.class })
@ConditionalOnBean(LdapOperations.class)
@ConditionalOnEnabledHealthIndicator("ldap")
public class LdapHealthContributorAutoConfiguration
extends CompositeHealthContributorConfiguration<LdapHealthIndicator, LdapOperations> {
public LdapHealthContributorAutoConfiguration() {
super(LdapHealthIndicator::new);
}
@Bean
@ConditionalOnMissingBean(name = { "ldapHealthIndicator", "ldapHealthContributor" })
public HealthContributor ldapHealthContributor(ConfigurableListableBeanFactory beanFactory) {
return createContributor(beanFactory, LdapOperations.class);
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed 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
*
* https://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.
*/
/**
* Auto-configuration for LDAP health integration.
*/
package org.springframework.boot.ldap.actuate.health.autoconfigure;

View File

@@ -0,0 +1,10 @@
{
"properties": [
{
"name": "management.health.ldap.enabled",
"type": "java.lang.Boolean",
"description": "Whether to enable LDAP health check.",
"defaultValue": true
}
]
}

View File

@@ -1,2 +1,3 @@
org.springframework.boot.ldap.actuate.health.autoconfigure.LdapHealthContributorAutoConfiguration
org.springframework.boot.ldap.autoconfigure.LdapAutoConfiguration
org.springframework.boot.ldap.autoconfigure.embedded.EmbeddedLdapAutoConfiguration

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.ldap.actuate.health.autoconfigure;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.ldap.actuate.health.LdapHealthIndicator;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.ldap.core.LdapOperations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link LdapHealthContributorAutoConfiguration}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
class LdapHealthContributorAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(LdapOperations.class, () -> mock(LdapOperations.class))
.withConfiguration(AutoConfigurations.of(LdapHealthContributorAutoConfiguration.class,
HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(LdapHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.ldap.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(LdapHealthIndicator.class));
}
}