Search in sources :

Example 1 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class OpMonitoringServiceHandlerImpl method sendRequest.

private void sendRequest(HttpServletRequest servletRequest, ProxyMessage proxyRequestMessage, OpMonitoringData opMonitoringData) throws Exception {
    log.trace("sendRequest {}", OP_MONITOR_ADDRESS);
    URI opMonitorUri;
    try {
        opMonitorUri = getOpMonitorUri();
    } catch (URISyntaxException e) {
        log.error("Malformed operational monitoring daemon address '{}'", OP_MONITOR_ADDRESS, e);
        throw new CodedException(X_INTERNAL_ERROR, "Malformed operational monitoring daemon address");
    }
    log.info("Sending request to {}", opMonitorUri);
    try (InputStream in = proxyRequestMessage.getSoapContent()) {
        opMonitoringData.setRequestOutTs(getEpochMillisecond());
        sender.doPost(opMonitorUri, in, AbstractHttpSender.CHUNKED_LENGTH, servletRequest.getHeader(MimeUtils.HEADER_ORIGINAL_CONTENT_TYPE));
        opMonitoringData.setResponseInTs(getEpochMillisecond());
    } catch (Exception ex) {
        if (ex instanceof CodedException) {
            opMonitoringData.setResponseInTs(getEpochMillisecond());
        }
        throw translateException(ex).withPrefix(X_SERVICE_FAILED_X);
    }
}
Also used : CodedException(ee.ria.xroad.common.CodedException) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) CodedException(ee.ria.xroad.common.CodedException) ErrorCodes.translateException(ee.ria.xroad.common.ErrorCodes.translateException)

Example 2 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class RestMetadataServiceHandlerImpl method handleGetOpenApi.

private void handleGetOpenApi(ProxyMessage requestProxyMessage) throws IOException, HttpClientCreator.HttpClientCreatorException, URISyntaxException {
    List<NameValuePair> pairs = URLEncodedUtils.parse(requestProxyMessage.getRest().getQuery(), Charset.forName("UTF-8"));
    String targetServiceCode = null;
    for (NameValuePair pair : pairs) {
        log.trace("{} : {}", pair.getName(), pair.getValue());
        if (pair.getName().equalsIgnoreCase(QUERY_PARAM_SERVICECODE)) {
            targetServiceCode = pair.getValue();
        }
    }
    if (targetServiceCode == null || targetServiceCode.isEmpty()) {
        throw new CodedException(X_INVALID_REQUEST, "Missing serviceCode in message body");
    }
    ServiceId targetServiceId = ServiceId.create(requestProxyMessage.getRest().getServiceId().getClientId(), targetServiceCode);
    log.trace("targetServiceId={}", targetServiceId);
    DescriptionType descriptionType = ServerConf.getDescriptionType(targetServiceId);
    if (descriptionType == null) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Service not found: %s", targetServiceId.toString()));
    }
    if (descriptionType != DescriptionType.OPENAPI3) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Invalid service type: %s", descriptionType.toString()));
    }
    String serviceDescriptionURL = ServerConf.getServiceDescriptionURL(targetServiceId);
    HttpClient client = httpClientCreator.getHttpClient();
    HttpContext httpContext = new BasicHttpContext();
    // ServerMessageProcessor uses the same method to pass the ServiceId to CustomSSLSocketFactory
    httpContext.setAttribute(ServiceId.class.getName(), targetServiceId);
    URI uri = new URI(serviceDescriptionURL);
    HttpResponse response = client.execute(new HttpGet(uri), httpContext);
    StatusLine statusLine = response.getStatusLine();
    if (HttpStatus.SC_OK != statusLine.getStatusCode()) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Failed reading service description from %s. Status: %s Reason: %s", serviceDescriptionURL, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
    }
    InputStream responseContent = response.getEntity().getContent();
    try {
        OpenapiDescriptionFiletype filetype = getFileType(response, uri);
        Openapi3Anonymiser anonymiser = new Openapi3Anonymiser();
        if (OpenapiDescriptionFiletype.JSON.equals(filetype)) {
            anonymiser.anonymiseJson(responseContent, restResponseBody);
        } else {
            anonymiser.anonymiseYaml(responseContent, restResponseBody);
        }
    } catch (IOException e) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Failed overwriting origin URL for the openapi servers for %s", serviceDescriptionURL));
    }
    if (response.containsHeader(MimeUtils.HEADER_CONTENT_TYPE)) {
        restResponse.getHeaders().add(new BasicHeader(MimeUtils.HEADER_CONTENT_TYPE, response.getFirstHeader(MimeUtils.HEADER_CONTENT_TYPE).getValue()));
    } else {
        restResponse.getHeaders().add(new BasicHeader(MimeUtils.HEADER_CONTENT_TYPE, DEFAULT_GETOPENAPI_CONTENT_TYPE));
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URI(java.net.URI) ServiceId(ee.ria.xroad.common.identifier.ServiceId) StatusLine(org.apache.http.StatusLine) CodedException(ee.ria.xroad.common.CodedException) HttpClient(org.apache.http.client.HttpClient) OpenapiDescriptionFiletype(ee.ria.xroad.common.util.OpenapiDescriptionFiletype) BasicHeader(org.apache.http.message.BasicHeader)

Example 3 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class ProxyMonitorServiceHandlerImpl method verifyAccess.

private void verifyAccess() {
    final ClientId owner = ServerConf.getIdentifier().getOwner();
    final ClientId client = requestMessage.getSoap().getClient();
    if (owner.equals(client)) {
        return;
    }
    // Grant access for configured monitoring client (if any)
    ClientId monitoringClient = MonitoringConf.getInstance().getMonitoringClient();
    if (monitoringClient != null && monitoringClient.equals(client)) {
        return;
    }
    throw new CodedException(ErrorCodes.X_ACCESS_DENIED, "Request is not allowed: %s", requestMessage.getSoap().getService());
}
Also used : CodedException(ee.ria.xroad.common.CodedException) ClientId(ee.ria.xroad.common.identifier.ClientId)

Example 4 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class ManagementRequestHandler method verifyCertificate.

private static void verifyCertificate(X509Certificate memberCert, OCSPResp memberCertOcsp) throws Exception {
    try {
        memberCert.checkValidity();
    } catch (Exception e) {
        throw new CodedException(X_CERT_VALIDATION, "Member (owner/client) sign certificate is invalid: %s", e.getMessage());
    }
    X509Certificate issuer = GlobalConf.getCaCert(GlobalConf.getInstanceIdentifier(), memberCert);
    new OcspVerifier(GlobalConf.getOcspFreshnessSeconds(false), new OcspVerifierOptions(GlobalConfExtensions.getInstance().shouldVerifyOcspNextUpdate())).verifyValidityAndStatus(memberCertOcsp, memberCert, issuer);
}
Also used : CodedException(ee.ria.xroad.common.CodedException) OcspVerifierOptions(ee.ria.xroad.common.ocsp.OcspVerifierOptions) OcspVerifier(ee.ria.xroad.common.ocsp.OcspVerifier) ErrorCodes.translateException(ee.ria.xroad.common.ErrorCodes.translateException) CodedException(ee.ria.xroad.common.CodedException) X509Certificate(java.security.cert.X509Certificate)

Example 5 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class MonitorClient method getMetrics.

/**
 * Get monitoring metrics
 */
public MetricSetType getMetrics(List<String> metricNames, boolean isOwner) {
    try {
        final Future<Object> response = Patterns.ask(metricsProvider, new SystemMetricsRequest(metricNames, isOwner), Timeout.apply(TIMEOUT_REQUEST, TimeUnit.SECONDS));
        Object obj = Await.result(response, Duration.apply(TIMEOUT_AWAIT, TimeUnit.SECONDS));
        if (obj instanceof SystemMetricsResponse) {
            final SystemMetricsResponse result = (SystemMetricsResponse) obj;
            return MetricTypes.of(result.getMetrics());
        } else {
            throw new CodedException(ErrorCodes.X_INTERNAL_ERROR, "Unexpected response");
        }
    } catch (Exception e) {
        log.warn("Unable to read metrics", e);
        throw new CodedException(ErrorCodes.X_INTERNAL_ERROR, "Unable to read metrics");
    }
}
Also used : SystemMetricsResponse(ee.ria.xroad.monitor.common.SystemMetricsResponse) CodedException(ee.ria.xroad.common.CodedException) SystemMetricsRequest(ee.ria.xroad.monitor.common.SystemMetricsRequest) CodedException(ee.ria.xroad.common.CodedException)

Aggregations

CodedException (ee.ria.xroad.common.CodedException)131 X509Certificate (java.security.cert.X509Certificate)28 IOException (java.io.IOException)17 ErrorCodes.translateException (ee.ria.xroad.common.ErrorCodes.translateException)15 SignerNotReachableException (org.niis.xroad.restapi.service.SignerNotReachableException)14 TokenInfo (ee.ria.xroad.signer.protocol.dto.TokenInfo)12 OCSPResp (org.bouncycastle.cert.ocsp.OCSPResp)11 ServiceException (org.niis.xroad.restapi.service.ServiceException)11 ClientId (ee.ria.xroad.common.identifier.ClientId)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)8 InputStream (java.io.InputStream)8 URISyntaxException (java.net.URISyntaxException)7 Date (java.util.Date)7 SoapFault (ee.ria.xroad.common.message.SoapFault)6 ServiceId (ee.ria.xroad.common.identifier.ServiceId)5 Soap (ee.ria.xroad.common.message.Soap)5 SoapMessageImpl (ee.ria.xroad.common.message.SoapMessageImpl)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5