From 6f19dac38d6cf098778e941babe3d3b41033b3ec Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sat, 27 May 2017 23:25:51 +0200 Subject: [PATCH] Allow Vault standby node to pass health check. We now accept Vault standby nodes as available. Requests to standby nodes are redirected by Vault to the master node. Communication with a standby node allows using Vault without functional restrictions. Related pull request: gh-113. Fixes gh-112. --- .../vault/config/VaultHealthIndicator.java | 8 +++-- .../config/VaultHealthIndicatorUnitTests.java | 35 ++++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicator.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicator.java index a1132990..7d17dba2 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicator.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicator.java @@ -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. @@ -22,7 +22,10 @@ import org.springframework.vault.core.VaultOperations; import org.springframework.vault.support.VaultHealth; /** + * Simple health indicator reporting Vault's availability. + * * @author Stuart Ingram + * @author Mark Paluch */ public class VaultHealthIndicator implements HealthIndicator { @@ -45,8 +48,7 @@ public class VaultHealthIndicator implements HealthIndicator { } if (vaultHealthResponse.isStandby()) { - return Health.outOfService().withDetail("state", "Vault in standby") - .build(); + return Health.up().withDetail("state", "Vault in standby").build(); } return Health.up().build(); diff --git a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorUnitTests.java b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorUnitTests.java index a2391bc0..b3fa7e0e 100644 --- a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorUnitTests.java +++ b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorUnitTests.java @@ -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,15 +15,6 @@ */ package org.springframework.cloud.vault.config; -import org.springframework.boot.actuate.health.Health; -import org.springframework.boot.actuate.health.Status; -import org.springframework.vault.core.VaultOperations; -import org.springframework.vault.core.VaultSysOperations; -import org.springframework.vault.support.VaultHealth; - -import static org.assertj.core.api.Assertions.*; -import static org.mockito.Mockito.*; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,6 +22,16 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.vault.core.VaultOperations; +import org.springframework.vault.core.VaultSysOperations; +import org.springframework.vault.support.VaultHealth; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.when; + /** * Unit tests for {@link VaultHealthIndicator}. * @@ -52,14 +53,14 @@ public class VaultHealthIndicatorUnitTests { VaultHealth healthResponse; @Before - public void before() throws Exception { + public void before() { when(vaultOperations.opsForSys()).thenReturn(vaultSysOperations); when(vaultSysOperations.health()).thenReturn(healthResponse); } @Test - public void shouldReportHealthyService() throws Exception { + public void shouldReportHealthyService() { when(healthResponse.isInitialized()).thenReturn(true); when(vaultOperations.opsForSys()).thenReturn(vaultSysOperations); @@ -70,7 +71,7 @@ public class VaultHealthIndicatorUnitTests { } @Test - public void shouldReportSealedService() throws Exception { + public void shouldReportSealedService() { when(healthResponse.isInitialized()).thenReturn(true); when(healthResponse.isSealed()).thenReturn(true); @@ -82,7 +83,7 @@ public class VaultHealthIndicatorUnitTests { } @Test - public void shouldReportUninitializedService() throws Exception { + public void shouldReportUninitializedService() { Health health = healthIndicator.health(); @@ -91,19 +92,19 @@ public class VaultHealthIndicatorUnitTests { } @Test - public void shouldReportStandbyService() throws Exception { + public void shouldReportStandbyService() { when(healthResponse.isInitialized()).thenReturn(true); when(healthResponse.isStandby()).thenReturn(true); Health health = healthIndicator.health(); - assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); + assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getDetails()).containsEntry("state", "Vault in standby"); } @Test - public void exceptionsShouldReportDownStatus() throws Exception { + public void exceptionsShouldReportDownStatus() { reset(vaultSysOperations); when(vaultSysOperations.health()).thenThrow(new IllegalStateException());