Search in sources :

Example 11 with HealthCheckResult

use of org.xipki.common.HealthCheckResult in project xipki by xipki.

the class HealthCheckResultTest method test2.

@Test
public void test2() {
    String encoded = "{\"healthy\":true,\"checks\":{\"childcheck\":{\"healthy\":false," + "\"checks\":{\"childChildCheck\":{\"healthy\":false}}}," + "\"childcheck2\":{\"healthy\":false}}}";
    HealthCheckResult result = HealthCheckResult.getInstanceFromJsonMessage("default", encoded);
    String noPrettyJson = "{\"healthy\":true,\"checks\":{\"childcheck\":{\"healthy\":false," + "\"checks\":{\"childChildCheck\":{\"healthy\":false}}}," + "\"childcheck2\":{\"healthy\":false}}}";
    String prettyJson = "{\n" + "    \"healthy\":true,\n" + "    \"checks\":{\n" + "        \"childcheck\":{\n" + "            \"healthy\":false,\n" + "            \"checks\":{\n" + "                \"childChildCheck\":{\n" + "                    \"healthy\":false\n" + "                }\n" + "            }\n" + "        },\n" + "        \"childcheck2\":{\n" + "            \"healthy\":false\n" + "        }\n" + "    }\n" + "}";
    check(result, noPrettyJson, prettyJson);
}
Also used : HealthCheckResult(org.xipki.common.HealthCheckResult) Test(org.junit.Test)

Example 12 with HealthCheckResult

use of org.xipki.common.HealthCheckResult in project xipki by xipki.

the class HealthCheckServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Access-Control-Allow-Origin", "*");
    ResponderManager responderManager = ServletHelper.getResponderManager();
    if (responderManager == null) {
        LOG.error("ServletHelper.responderManager not configured");
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    try {
        String caName = null;
        X509CaCmpResponder responder = null;
        String path = StringUtil.getRelativeRequestUri(req.getServletPath(), req.getRequestURI());
        if (path.length() > 1) {
            // skip the first char which is always '/'
            String caAlias = path.substring(1);
            caName = responderManager.getCaNameForAlias(caAlias);
            if (caName == null) {
                caName = caAlias.toLowerCase();
            }
            responder = responderManager.getX509CaResponder(caName);
        }
        if (caName == null || responder == null || !responder.isOnService()) {
            String auditMessage;
            if (caName == null) {
                auditMessage = "no CA is specified";
            } else if (responder == null) {
                auditMessage = "unknown CA '" + caName + "'";
            } else {
                auditMessage = "CA '" + caName + "' is out of service";
            }
            LOG.warn(auditMessage);
            sendError(resp, HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        HealthCheckResult healthResult = responder.healthCheck();
        int status = healthResult.isHealthy() ? HttpServletResponse.SC_OK : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        byte[] respBytes = healthResult.toJsonMessage(true).getBytes();
        resp.setStatus(status);
        resp.setContentLength(respBytes.length);
        resp.setContentType(CT_RESPONSE);
        resp.getOutputStream().write(respBytes);
    } catch (Throwable th) {
        if (th instanceof EOFException) {
            LogUtil.warn(LOG, th, "connection reset by peer");
        } else {
            LOG.error("Throwable thrown, this should not happen!", th);
        }
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
        resp.flushBuffer();
    }
}
Also used : X509CaCmpResponder(org.xipki.ca.server.api.X509CaCmpResponder) EOFException(java.io.EOFException) ResponderManager(org.xipki.ca.server.api.ResponderManager) HealthCheckResult(org.xipki.common.HealthCheckResult)

Example 13 with HealthCheckResult

use of org.xipki.common.HealthCheckResult in project xipki by xipki.

the class HealthCheckServlet method service0.

private FullHttpResponse service0(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession) {
    HttpVersion version = request.protocolVersion();
    HttpMethod method = request.method();
    if (method != HttpMethod.GET) {
        return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
    try {
        if (responderManager == null) {
            LOG.error("responderManager in servlet is not configured");
            return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        }
        String caName = null;
        X509CaCmpResponder responder = null;
        if (servletUri.getPath().length() > 1) {
            // skip the first char which is always '/'
            String caAlias = servletUri.getPath().substring(1);
            caName = responderManager.getCaNameForAlias(caAlias);
            if (caName == null) {
                caName = caAlias.toLowerCase();
            }
            responder = responderManager.getX509CaResponder(caName);
        }
        if (caName == null || responder == null || !responder.isOnService()) {
            String auditMessage;
            if (caName == null) {
                auditMessage = "no CA is specified";
            } else if (responder == null) {
                auditMessage = "unknown CA '" + caName + "'";
            } else {
                auditMessage = "CA '" + caName + "' is out of service";
            }
            LOG.warn(auditMessage);
            return createErrorResponse(version, HttpResponseStatus.NOT_FOUND);
        }
        HealthCheckResult healthResult = responder.healthCheck();
        HttpResponseStatus status = healthResult.isHealthy() ? HttpResponseStatus.OK : HttpResponseStatus.INTERNAL_SERVER_ERROR;
        byte[] respBytes = healthResult.toJsonMessage(true).getBytes();
        return createResponse(version, status, HealthCheckServlet.CT_RESPONSE, respBytes);
    } catch (Throwable th) {
        if (th instanceof EOFException) {
            LogUtil.warn(LOG, th, "connection reset by peer");
        } else {
            LOG.error("Throwable thrown, this should not happen!", th);
        }
        return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : X509CaCmpResponder(org.xipki.ca.server.api.X509CaCmpResponder) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) EOFException(java.io.EOFException) HealthCheckResult(org.xipki.common.HealthCheckResult) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 14 with HealthCheckResult

use of org.xipki.common.HealthCheckResult in project xipki by xipki.

the class CaClientImpl method getHealthCheckResult.

@Override
public HealthCheckResult getHealthCheckResult(String caName) throws CaClientException {
    caName = ParamUtil.requireNonNull("caName", caName).toLowerCase();
    String name = "X509CA";
    HealthCheckResult healthCheckResult = new HealthCheckResult(name);
    try {
        init0(false);
    } catch (CaClientException ex) {
        LogUtil.error(LOG, ex, "could not initialize CaCleint");
        healthCheckResult.setHealthy(false);
        return healthCheckResult;
    }
    CaConf ca = casMap.get(caName);
    if (ca == null) {
        throw new IllegalArgumentException("unknown CA " + caName);
    }
    String healthUrlStr = ca.getHealthUrl();
    URL serverUrl;
    try {
        serverUrl = new URL(healthUrlStr);
    } catch (MalformedURLException ex) {
        throw new CaClientException("invalid URL '" + healthUrlStr + "'");
    }
    try {
        HttpURLConnection httpUrlConnection = IoUtil.openHttpConn(serverUrl);
        InputStream inputStream = httpUrlConnection.getInputStream();
        int responseCode = httpUrlConnection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_INTERNAL_ERROR) {
            inputStream.close();
            throw new IOException(String.format("bad response: code='%s', message='%s'", httpUrlConnection.getResponseCode(), httpUrlConnection.getResponseMessage()));
        }
        String responseContentType = httpUrlConnection.getContentType();
        boolean isValidContentType = false;
        if (responseContentType != null) {
            if ("application/json".equalsIgnoreCase(responseContentType)) {
                isValidContentType = true;
            }
        }
        if (!isValidContentType) {
            inputStream.close();
            throw new IOException("bad response: mime type " + responseContentType + " not supported!");
        }
        byte[] responseBytes = IoUtil.read(inputStream);
        if (responseBytes.length == 0) {
            healthCheckResult.setHealthy(responseCode == HttpURLConnection.HTTP_OK);
        } else {
            String response = new String(responseBytes);
            try {
                healthCheckResult = HealthCheckResult.getInstanceFromJsonMessage(name, response);
            } catch (IllegalArgumentException ex) {
                LogUtil.error(LOG, ex, "IOException while parsing the health json message");
                if (LOG.isDebugEnabled()) {
                    LOG.debug("json message: {}", response);
                }
                healthCheckResult.setHealthy(false);
            }
        }
    } catch (IOException ex) {
        LogUtil.error(LOG, ex, "IOException while fetching the URL " + healthUrlStr);
        healthCheckResult.setHealthy(false);
    }
    return healthCheckResult;
}
Also used : MalformedURLException(java.net.MalformedURLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HealthCheckResult(org.xipki.common.HealthCheckResult) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) CaClientException(org.xipki.ca.client.api.CaClientException)

Aggregations

HealthCheckResult (org.xipki.common.HealthCheckResult)14 Test (org.junit.Test)5 EOFException (java.io.EOFException)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 HttpVersion (io.netty.handler.codec.http.HttpVersion)2 X509CaCmpResponder (org.xipki.ca.server.api.X509CaCmpResponder)2 ResponderAndPath (org.xipki.ocsp.api.ResponderAndPath)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 CaClientException (org.xipki.ca.client.api.CaClientException)1 ResponderManager (org.xipki.ca.server.api.ResponderManager)1 IllegalCmdParamException (org.xipki.console.karaf.IllegalCmdParamException)1 OcspServer (org.xipki.ocsp.api.OcspServer)1 OcspStore (org.xipki.ocsp.api.OcspStore)1 ConcurrentContentSigner (org.xipki.security.ConcurrentContentSigner)1