@Test public void checkAuthKeyOcspStatusShouldFailWhenEndEntityCertNotAvailable() { CertChain mockCertChain = PowerMockito.mock(CertChain.class); when(mockCertChain.getEndEntityCert()).thenReturn(null); AuthKey authKey = new AuthKey(mockCertChain, null); KeyConfProvider mockKeyConfProvider = mock(KeyConfProvider.class); when(mockKeyConfProvider.getAuthKey()).thenReturn(authKey); KeyConf.reload(mockKeyConfProvider); HealthCheckProvider testedProvider = HealthChecks.checkAuthKeyOcspStatus(); HealthCheckResult checkedResult = testedProvider.get(); assertTrue("health check result should be a failure", !checkedResult.isOk()); assertThat(checkedResult.getErrorMessage(), containsString("No end entity certificate available")); }
public static HealthCheckProvider checkAuthKeyOcspStatus() { return () -> { AuthKey authKey = KeyConf.getAuthKey(); if (authKey == null) { return failure("No authentication key available. Signer might be down."); } CertChain certChain = authKey.getCertChain(); if (certChain == null) { return failure("No certificate chain available in authentication key."); } X509Certificate certificate = certChain.getEndEntityCert(); if (certificate == null) { return failure("No end entity certificate available for authentication key."); } int ocspStatus; try { ocspStatus = KeyConf.getOcspResponse(certificate).getStatus(); } catch (Exception e) { log.error("Getting OCSP response for authentication key failed, got exception", e); return failure("Getting OCSP response for authentication key failed."); } return new HealthCheckResult(OCSPResp.SUCCESSFUL == ocspStatus, "Authentication key OCSP response status " + ocspStatus); }; }
HealthChecks { public static HealthCheckProvider checkAuthKeyOcspStatus() { return () -> { AuthKey authKey = KeyConf.getAuthKey(); if (authKey == null) { return failure("No authentication key available. Signer might be down."); } CertChain certChain = authKey.getCertChain(); if (certChain == null) { return failure("No certificate chain available in authentication key."); } X509Certificate certificate = certChain.getEndEntityCert(); if (certificate == null) { return failure("No end entity certificate available for authentication key."); } int ocspStatus; try { ocspStatus = KeyConf.getOcspResponse(certificate).getStatus(); } catch (Exception e) { log.error("Getting OCSP response for authentication key failed, got exception", e); return failure("Getting OCSP response for authentication key failed."); } return new HealthCheckResult(OCSPResp.SUCCESSFUL == ocspStatus, "Authentication key OCSP response status " + ocspStatus); }; } }
HealthChecks { public static HealthCheckProvider checkAuthKeyOcspStatus() { return () -> { AuthKey authKey = KeyConf.getAuthKey(); if (authKey == null) { return failure("No authentication key available. Signer might be down."); } CertChain certChain = authKey.getCertChain(); if (certChain == null) { return failure("No certificate chain available in authentication key."); } X509Certificate certificate = certChain.getEndEntityCert(); if (certificate == null) { return failure("No end entity certificate available for authentication key."); } int ocspStatus; try { ocspStatus = KeyConf.getOcspResponse(certificate).getStatus(); } catch (Exception e) { log.error("Getting OCSP response for authentication key failed, got exception", e); return failure("Getting OCSP response for authentication key failed."); } return new HealthCheckResult(OCSPResp.SUCCESSFUL == ocspStatus, "Authentication key OCSP response status " + ocspStatus); }; } private  HealthChecks(); }
HealthChecks { public static HealthCheckProvider checkAuthKeyOcspStatus() { return () -> { AuthKey authKey = KeyConf.getAuthKey(); if (authKey == null) { return failure("No authentication key available. Signer might be down."); } CertChain certChain = authKey.getCertChain(); if (certChain == null) { return failure("No certificate chain available in authentication key."); } X509Certificate certificate = certChain.getEndEntityCert(); if (certificate == null) { return failure("No end entity certificate available for authentication key."); } int ocspStatus; try { ocspStatus = KeyConf.getOcspResponse(certificate).getStatus(); } catch (Exception e) { log.error("Getting OCSP response for authentication key failed, got exception", e); return failure("Getting OCSP response for authentication key failed."); } return new HealthCheckResult(OCSPResp.SUCCESSFUL == ocspStatus, "Authentication key OCSP response status " + ocspStatus); }; } private  HealthChecks(); static HealthCheckProvider checkAuthKeyOcspStatus(); static HealthCheckProvider checkServerConfDatabaseStatus(); static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor(
            int resultValidFor, int errorResultValidFor, TimeUnit timeUnit); static HealthCheckProvider cacheResultOnce(HealthCheckProvider provider,
                                                      HealthCheckResult cachedOnceResult); }
HealthChecks { public static HealthCheckProvider checkAuthKeyOcspStatus() { return () -> { AuthKey authKey = KeyConf.getAuthKey(); if (authKey == null) { return failure("No authentication key available. Signer might be down."); } CertChain certChain = authKey.getCertChain(); if (certChain == null) { return failure("No certificate chain available in authentication key."); } X509Certificate certificate = certChain.getEndEntityCert(); if (certificate == null) { return failure("No end entity certificate available for authentication key."); } int ocspStatus; try { ocspStatus = KeyConf.getOcspResponse(certificate).getStatus(); } catch (Exception e) { log.error("Getting OCSP response for authentication key failed, got exception", e); return failure("Getting OCSP response for authentication key failed."); } return new HealthCheckResult(OCSPResp.SUCCESSFUL == ocspStatus, "Authentication key OCSP response status " + ocspStatus); }; } private  HealthChecks(); static HealthCheckProvider checkAuthKeyOcspStatus(); static HealthCheckProvider checkServerConfDatabaseStatus(); static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor(
            int resultValidFor, int errorResultValidFor, TimeUnit timeUnit); static HealthCheckProvider cacheResultOnce(HealthCheckProvider provider,
                                                      HealthCheckResult cachedOnceResult);  }