Repository: spring-cloud/spring-cloud-config
Issue: Spring Cloud Vault Health Check Fails

Hey guys hope all is well.

I had a quick question about the health check for config server’s vault backend. 

I seem to be having some trouble with it – I’m not able to access health for vault repo without an exception being thrown “No thread-bound request found”.

To illustrate the problem, I connect to vault backend. I created a ScheduledExecutorService that calls health on every HealthIndicator, every 30 seconds. The health check to vault repo fails.

On the other hand if I curl the actuator health endpoint the health check to vault repo, the health check succeeds. (curl localhost:8888/actuator/health). 

I think this may have to do with ThreadLocal usage of RequestAttributes from ObjectProvider(HttpServletRequest) request called from AbstractVaultEnvironmentRepository::getWatchState.

I am using all the latest versions of projects: spring-cloud-config-server 3.1.2, spring-cloud-starter-bootstrap 3.1.2 and spring-boot-starter-actuator 2.6.7.

I’ve attached my simple, repeatable project here: https://github.com/speedo72/spring-cloud-config-vault-health
Thanks for any help.


Changed files:
spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractVaultEnvironmentRepository.java

diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractVaultEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractVaultEnvironmentRepository.java
index 3f4e4a30..5fb70bb8 100644
--- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractVaultEnvironmentRepository.java
+++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractVaultEnvironmentRepository.java
@@ -25,6 +25,9 @@ import java.util.Properties;
 import javax.servlet.http.HttpServletRequest;
 import javax.validation.constraints.NotEmpty;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import org.springframework.beans.factory.ObjectProvider;
 import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
 import org.springframework.cloud.config.environment.Environment;
@@ -44,6 +47,8 @@ import static org.springframework.cloud.config.client.ConfigClientProperties.STA
  */
 public abstract class AbstractVaultEnvironmentRepository implements EnvironmentRepository, Ordered {
 
+	private static Log log = LogFactory.getLog(AbstractVaultEnvironmentRepository.class);
+
 	// TODO: move to watchState:String on findOne?
 	protected final ObjectProvider<HttpServletRequest> request;
 
@@ -104,8 +109,14 @@ public abstract class AbstractVaultEnvironmentRepository implements EnvironmentR
 	private String getWatchState() {
 		HttpServletRequest servletRequest = this.request.getIfAvailable();
 		if (servletRequest != null) {
-			String state = servletRequest.getHeader(STATE_HEADER);
-			return this.watch.watch(state);
+			try {
+				String state = servletRequest.getHeader(STATE_HEADER);
+				return this.watch.watch(state);
+			}
+			catch (IllegalStateException e) {
+				log.debug("Could not get state.", e);
+				return null;
+			}
 		}
 		return null;
 	}