summaryrefslogtreecommitdiff
path: root/eaaf-springboot-utils/src/main/java
diff options
context:
space:
mode:
authorThomas <>2021-10-22 11:44:37 +0200
committerThomas <>2021-10-22 11:44:37 +0200
commit9eca51b62ef6f69788d5af3a11ffd3191965cf65 (patch)
tree01f1b6eceac84a22688a1e7616b523b6ca138bb7 /eaaf-springboot-utils/src/main/java
parent405cd6a73cf0c31a15ca0e8c254ac4f431a15fe2 (diff)
downloadEAAF-Components-9eca51b62ef6f69788d5af3a11ffd3191965cf65.tar.gz
EAAF-Components-9eca51b62ef6f69788d5af3a11ffd3191965cf65.tar.bz2
EAAF-Components-9eca51b62ef6f69788d5af3a11ffd3191965cf65.zip
add deadline into HSM-Facade Health-Check that fails this test if HSM-Facade does not responde
Diffstat (limited to 'eaaf-springboot-utils/src/main/java')
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/actuator/HsmFacadeProviderHealthCheck.java74
1 files changed, 60 insertions, 14 deletions
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/actuator/HsmFacadeProviderHealthCheck.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/actuator/HsmFacadeProviderHealthCheck.java
index d2406552..3b2e3fe7 100644
--- a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/actuator/HsmFacadeProviderHealthCheck.java
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/actuator/HsmFacadeProviderHealthCheck.java
@@ -1,10 +1,17 @@
package at.gv.egiz.eaaf.utils.springboot.actuator;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Service;
+import at.gv.egiz.eaaf.core.api.idp.IConfiguration;
import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory;
import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory.HsmFacadeStatus;
import lombok.extern.slf4j.Slf4j;
@@ -19,27 +26,27 @@ import lombok.extern.slf4j.Slf4j;
@Service("HsmFacadeProvider")
public class HsmFacadeProviderHealthCheck implements HealthIndicator {
+ private static final String CONFIG_PROP_HEALTHCHECK_DEADLINE = "security.hsmfacade.healthcheck.deadline";
+ private static final int DEFAULT_HEALTHCHECK_DEADLINE = 10;
+
@Autowired(required = false) EaafKeyStoreFactory factory;
-
+ @Autowired(required = false) IConfiguration basicConfig;
+
@Override
public Health health() {
if (factory != null && factory.isHsmFacadeInitialized()) {
+ int deadline = getIntegerFromConfig(CONFIG_PROP_HEALTHCHECK_DEADLINE, DEFAULT_HEALTHCHECK_DEADLINE);
+ CompletableFuture<Health> asynchTestOperation = new CompletableFuture<>();
+ Executors.newCachedThreadPool().submit(() -> runHsmTest(asynchTestOperation));
try {
- HsmFacadeStatus status = factory.checkHsmFacadeStatus();
- log.trace("Current HSM-Facade status: {}", status);
- if (HsmFacadeStatus.UP.equals(status)) {
- return Health.up().build();
-
- } else if (HsmFacadeStatus.DOWN.equals(status)) {
- return Health.down().build();
-
- }
+ return asynchTestOperation.get(deadline, TimeUnit.SECONDS);
- } catch (Exception e) {
- log.warn("HSM-Facaden Health-Check has an error", e);
- return Health.down(e).build();
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {
+ log.warn("Receive no respose from Health-Check after {} seconds.", deadline, e);
+ return Health.outOfService().withException(e).build();
- }
+ }
+
} else {
log.trace("No {} or HSM-Facade is not initialized. Skipping healthCheck ...",
@@ -51,4 +58,43 @@ public class HsmFacadeProviderHealthCheck implements HealthIndicator {
}
+ private void runHsmTest(CompletableFuture<Health> completableFuture) {
+ try {
+ HsmFacadeStatus status = factory.checkHsmFacadeStatus();
+ log.trace("Current HSM-Facade status: {}", status);
+ if (HsmFacadeStatus.UP.equals(status)) {
+ completableFuture.complete(Health.up().build());
+
+ } else if (HsmFacadeStatus.DOWN.equals(status)) {
+ completableFuture.complete(Health.down().build());
+
+ }
+
+ } catch (Exception e) {
+ log.warn("HSM-Facaden Health-Check has an error", e);
+ completableFuture.complete(Health.down(e).build());
+
+ }
+
+ }
+
+ private int getIntegerFromConfig(String key, int defaultValue) {
+ if (basicConfig == null) {
+ log.info("Using default-value: {} for Config. Property: {}", defaultValue, key);
+ return defaultValue;
+
+ } else {
+ String value = basicConfig.getBasicConfiguration(key, String.valueOf(defaultValue));
+ try {
+ return Integer.parseInt(value);
+
+ } catch (NumberFormatException e) {
+ log.warn("Config. Property: {} with value: {} is NO valid Integer", key, value, e);
+ log.info("Using default-value: {} for Config. Property: {}", defaultValue, key);
+ return defaultValue;
+
+ }
+ }
+ }
+
}