Consider performance standby and recovery replication in healthcheck

Closes gh-501.
This commit is contained in:
Mark Paluch
2019-11-06 09:45:14 +01:00
parent 1ffcc79389
commit 41e7051481
4 changed files with 94 additions and 5 deletions

View File

@@ -51,6 +51,7 @@ import org.springframework.vault.support.VaultResponseSupport;
import org.springframework.vault.support.VaultToken;
import org.springframework.vault.support.VaultUnsealStatus;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestOperations;
/**
@@ -385,7 +386,6 @@ public class VaultSysTemplate implements VaultSysOperations {
}
}
}
}
private static class Health implements RestOperationsCallback<VaultHealth> {
@@ -398,7 +398,7 @@ public class VaultSysTemplate implements VaultSysOperations {
"sys/health", HttpMethod.GET, null, VaultHealthImpl.class);
return healthResponse.getBody();
}
catch (HttpStatusCodeException responseError) {
catch (RestClientResponseException responseError) {
try {
ObjectMapper mapper = new ObjectMapper();
@@ -524,20 +524,27 @@ public class VaultSysTemplate implements VaultSysOperations {
private final boolean initialized;
private final boolean sealed;
private final boolean standby;
private final boolean performanceStandby;
private final boolean replicationRecoverySecondary;
private final int serverTimeUtc;
@Nullable
private final String version;
private VaultHealthImpl(@JsonProperty("initialized") boolean initialized,
VaultHealthImpl(@JsonProperty("initialized") boolean initialized,
@JsonProperty("sealed") boolean sealed,
@JsonProperty("standby") boolean standby,
@JsonProperty("performance_standby") boolean performanceStandby,
@Nullable @JsonProperty("replication_dr_mode") String replicationRecoverySecondary,
@JsonProperty("server_time_utc") int serverTimeUtc,
@Nullable @JsonProperty("version") String version) {
this.initialized = initialized;
this.sealed = sealed;
this.standby = standby;
this.performanceStandby = performanceStandby;
this.replicationRecoverySecondary = replicationRecoverySecondary != null
&& !"disabled".equalsIgnoreCase(replicationRecoverySecondary);
this.serverTimeUtc = serverTimeUtc;
this.version = version;
}
@@ -554,6 +561,14 @@ public class VaultSysTemplate implements VaultSysOperations {
return this.standby;
}
public boolean isPerformanceStandby() {
return performanceStandby;
}
public boolean isRecoveryReplicationSecondary() {
return replicationRecoverySecondary;
}
public int getServerTimeUtc() {
return this.serverTimeUtc;
}
@@ -571,13 +586,17 @@ public class VaultSysTemplate implements VaultSysOperations {
return false;
VaultHealthImpl that = (VaultHealthImpl) o;
return initialized == that.initialized && sealed == that.sealed
&& standby == that.standby && serverTimeUtc == that.serverTimeUtc
&& standby == that.standby
&& performanceStandby == that.performanceStandby
&& replicationRecoverySecondary == that.replicationRecoverySecondary
&& serverTimeUtc == that.serverTimeUtc
&& Objects.equals(version, that.version);
}
@Override
public int hashCode() {
return Objects.hash(initialized, sealed, standby, serverTimeUtc, version);
return Objects.hash(initialized, sealed, standby, performanceStandby,
replicationRecoverySecondary, serverTimeUtc, version);
}
}
}

View File

@@ -44,6 +44,20 @@ public interface VaultHealth {
*/
boolean isStandby();
/**
* @return {@literal true} if the Vault instance is in performance standby mode,
* otherwise {@literal false}.
* @since 2.2
*/
boolean isPerformanceStandby();
/**
* @return {@literal true} if the Vault instance is a secondary node in data recovery
* replication mode, otherwise {@literal false}.
* @since 2.2
*/
boolean isRecoveryReplicationSecondary();
/**
* @return the server time in seconds, UTC.
*/

View File

@@ -30,6 +30,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.vault.support.Policy;
import org.springframework.vault.support.Policy.Rule;
import org.springframework.vault.support.VaultHealth;
import org.springframework.vault.support.VaultMount;
import org.springframework.vault.support.VaultResponse;
import org.springframework.vault.support.VaultUnsealStatus;
@@ -256,6 +257,19 @@ class VaultSysTemplateIntegrationTests extends IntegrationTestSupport {
assertThat(adminOperations.getPolicyNames()).doesNotContain("foo");
}
@Test
@RequiresVaultVersion("0.6.1")
void shouldReportHealth() {
VaultHealth health = adminOperations.health();
assertThat(health.isInitialized()).isTrue();
assertThat(health.isSealed()).isFalse();
assertThat(health.isPerformanceStandby()).isFalse();
assertThat(health.isRecoveryReplicationSecondary()).isFalse();
assertThat(health.isStandby()).isFalse();
}
@Test
void isInitializedShouldReturnTrue() {
assertThat(adminOperations.isInitialized()).isTrue();

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2019 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.vault.core;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link VaultSysTemplate}.
*
* @author Mark Paluch
*/
class VaultSysTemplateUnitTests {
@Test
void shouldReportRecoveryReplicationMode() {
VaultSysTemplate.VaultHealthImpl disabled = new VaultSysTemplate.VaultHealthImpl(
true, true, true, true, "disabled", 0, null);
assertThat(disabled.isRecoveryReplicationSecondary()).isFalse();
VaultSysTemplate.VaultHealthImpl enabled = new VaultSysTemplate.VaultHealthImpl(
true, true, true, true, "enabled", 0, null);
assertThat(enabled.isRecoveryReplicationSecondary()).isTrue();
}
}