Adapt to Spring Boot's actuator API changes.
Use CompositeHealthIndicatorConfiguration to monitor multiple VaultOperations beans. Provide Vault version via health details. Remove deprecated VaultBootstrapHealthIndicatorConfiguration. Closes gh-155.
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017 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
|
||||
*
|
||||
* 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.springframework.cloud.vault.config;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
|
||||
* Auto-configuration} for Vault providing beans for the application context.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Stuart Ingram
|
||||
* @deprecated since 1.1, use {@link VaultHealthIndicatorConfiguration}.
|
||||
*/
|
||||
@Deprecated
|
||||
public class VaultBootstrapHealthIndicatorConfiguration extends
|
||||
VaultHealthIndicatorConfiguration {
|
||||
}
|
||||
@@ -17,6 +17,8 @@ package org.springframework.cloud.vault.config;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.core.VaultOperations;
|
||||
import org.springframework.vault.support.VaultHealth;
|
||||
|
||||
@@ -41,22 +43,34 @@ public class VaultHealthIndicator implements HealthIndicator {
|
||||
|
||||
VaultHealth vaultHealthResponse = vaultOperations.opsForSys().health();
|
||||
|
||||
if (!vaultHealthResponse.isInitialized()) {
|
||||
return Health.down().withDetail("state", "Vault uninitialized").build();
|
||||
Builder healthBuilder = getHealthBuilder(vaultHealthResponse);
|
||||
|
||||
if (StringUtils.hasText(vaultHealthResponse.getVersion())) {
|
||||
healthBuilder = healthBuilder.withDetail("version",
|
||||
vaultHealthResponse.getVersion());
|
||||
}
|
||||
|
||||
if (vaultHealthResponse.isSealed()) {
|
||||
return Health.down().withDetail("state", "Vault sealed").build();
|
||||
}
|
||||
|
||||
if (vaultHealthResponse.isStandby()) {
|
||||
return Health.up().withDetail("state", "Vault in standby").build();
|
||||
}
|
||||
|
||||
return Health.up().build();
|
||||
return healthBuilder.build();
|
||||
}
|
||||
catch (Exception e) {
|
||||
return Health.down(e).build();
|
||||
}
|
||||
}
|
||||
|
||||
private Builder getHealthBuilder(VaultHealth vaultHealthResponse) {
|
||||
|
||||
if (!vaultHealthResponse.isInitialized()) {
|
||||
return Health.down().withDetail("state", "Vault uninitialized");
|
||||
}
|
||||
|
||||
if (vaultHealthResponse.isSealed()) {
|
||||
return Health.down().withDetail("state", "Vault sealed");
|
||||
}
|
||||
|
||||
if (vaultHealthResponse.isStandby()) {
|
||||
return Health.up().withDetail("state", "Vault in standby");
|
||||
}
|
||||
|
||||
return Health.up();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2017 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.
|
||||
@@ -15,14 +15,15 @@
|
||||
*/
|
||||
package org.springframework.cloud.vault.config;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthIndicatorConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
|
||||
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -40,15 +41,21 @@ import org.springframework.vault.core.VaultOperations;
|
||||
@Configuration
|
||||
@ConditionalOnClass(HealthIndicator.class)
|
||||
@ConditionalOnBean(VaultBootstrapConfiguration.class)
|
||||
@ConditionalOnEnabledHealthIndicator("vault")
|
||||
@ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true)
|
||||
@ConditionalOnExpression("${health.vault.enabled:true}")
|
||||
@AutoConfigureBefore({ EndpointAutoConfiguration.class })
|
||||
@AutoConfigureAfter(HealthIndicatorAutoConfiguration.class)
|
||||
public class VaultHealthIndicatorConfiguration {
|
||||
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
|
||||
public class VaultHealthIndicatorConfiguration extends
|
||||
CompositeHealthIndicatorConfiguration<VaultHealthIndicator, VaultOperations> {
|
||||
|
||||
private final Map<String, VaultOperations> vaultTemplates;
|
||||
|
||||
public VaultHealthIndicatorConfiguration(Map<String, VaultOperations> vaultTemplates) {
|
||||
this.vaultTemplates = vaultTemplates;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "vaultHealthIndicator")
|
||||
public HealthIndicator vaultHealthIndicator(VaultOperations vaultOperations) {
|
||||
return new VaultHealthIndicator(vaultOperations);
|
||||
@ConditionalOnMissingBean(name = { "vaultHealthIndicator" })
|
||||
public HealthIndicator vaultHealthIndicator() {
|
||||
return this.createHealthIndicator(this.vaultTemplates);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user