diff options
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.java | 74 | 
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; +       +      } +    } +  } +    } | 
