Search in sources :

Example 1 with HttpEndpoint

use of io.gravitee.definition.model.endpoint.HttpEndpoint in project gravitee-management-rest-api by gravitee-io.

the class ApiServiceImpl method create.

@Override
public ApiEntity create(NewApiEntity newApiEntity, String userId) throws ApiAlreadyExistsException {
    UpdateApiEntity apiEntity = new UpdateApiEntity();
    apiEntity.setName(newApiEntity.getName());
    apiEntity.setDescription(newApiEntity.getDescription());
    apiEntity.setVersion(newApiEntity.getVersion());
    // check the existence of groups
    if (newApiEntity.getGroups() != null && !newApiEntity.getGroups().isEmpty()) {
        try {
            groupService.findByIds(newApiEntity.getGroups());
        } catch (GroupsNotFoundException gnfe) {
            throw new InvalidDataException("Groups [" + newApiEntity.getGroups() + "] does not exist");
        }
    }
    apiEntity.setGroups(newApiEntity.getGroups());
    Proxy proxy = new Proxy();
    proxy.setContextPath(newApiEntity.getContextPath());
    proxy.setEndpoints(new LinkedHashSet<>());
    proxy.getEndpoints().add(new HttpEndpoint("default", newApiEntity.getEndpoint()));
    apiEntity.setProxy(proxy);
    List<String> declaredPaths = (newApiEntity.getPaths() != null) ? newApiEntity.getPaths() : new ArrayList<>();
    if (!declaredPaths.contains("/")) {
        declaredPaths.add(0, "/");
    }
    // Initialize with a default path and provided paths
    Map<String, Path> paths = declaredPaths.stream().map(sPath -> {
        Path path = new Path();
        path.setPath(sPath);
        return path;
    }).collect(Collectors.toMap(Path::getPath, path -> path));
    apiEntity.setPaths(paths);
    return create0(apiEntity, userId);
}
Also used : Path(io.gravitee.definition.model.Path) PlanQuery(io.gravitee.management.model.plan.PlanQuery) java.util(java.util) HookScope(io.gravitee.management.service.notification.HookScope) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) MembershipRepository(io.gravitee.repository.management.api.MembershipRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) PageQuery(io.gravitee.management.model.documentation.PageQuery) Visibility(io.gravitee.repository.management.model.Visibility) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Proxy(io.gravitee.definition.model.Proxy) Value(org.springframework.beans.factory.annotation.Value) io.gravitee.management.service.exceptions(io.gravitee.management.service.exceptions) UUID(io.gravitee.common.utils.UUID) EventType(io.gravitee.management.model.EventType) ApiSynchronizationProcessor(io.gravitee.management.service.processor.ApiSynchronizationProcessor) AuditEvent(io.gravitee.repository.management.model.Api.AuditEvent) ApiRepository(io.gravitee.repository.management.api.ApiRepository) io.gravitee.management.model(io.gravitee.management.model) GenericNotificationConfigEntity(io.gravitee.management.model.notification.GenericNotificationConfigEntity) SystemRole(io.gravitee.management.model.permissions.SystemRole) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Lifecycle(io.gravitee.common.component.Lifecycle) SearchableUser(io.gravitee.management.idp.api.identity.SearchableUser) Logger(org.slf4j.Logger) ApiHook(io.gravitee.management.service.notification.ApiHook) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) Component(org.springframework.stereotype.Component) io.gravitee.repository.management.model(io.gravitee.repository.management.model) io.gravitee.management.service(io.gravitee.management.service) Path(io.gravitee.definition.model.Path) PageType(io.gravitee.management.model.PageType) DatatypeConverter(javax.xml.bind.DatatypeConverter) Proxy(io.gravitee.definition.model.Proxy) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint)

Example 2 with HttpEndpoint

use of io.gravitee.definition.model.endpoint.HttpEndpoint in project gravitee-gateway by gravitee-io.

the class ApiDeployerStatement method loadApi.

private Api loadApi(String apiDescriptorPath) throws Exception {
    URL jsonFile = ApiDeployerStatement.class.getResource(apiDescriptorPath);
    Api api = new GraviteeMapper().readValue(jsonFile, Api.class);
    if (ApiLoaderInterceptor.class.isAssignableFrom(target.getClass())) {
        ApiLoaderInterceptor loader = (ApiLoaderInterceptor) target;
        loader.before(api);
    }
    boolean enhanceHttpPort = target.getClass().getAnnotation(ApiDescriptor.class).enhanceHttpPort();
    if (enhanceHttpPort) {
        List<Endpoint> endpoints = new ArrayList<>(api.getProxy().getEndpoints());
        List<Integer> bindPorts = SocketUtils.getBindPorts();
        for (int i = 0; i < bindPorts.size(); i++) {
            int port = SocketUtils.getBindPorts().get(i);
            if (i < endpoints.size()) {
                Endpoint edpt = endpoints.get(i);
                URL target = new URL(edpt.getTarget());
                URL newTarget = new URL(target.getProtocol(), target.getHost(), port, target.getFile());
                edpt.setTarget(newTarget.toString());
                edpt.setName(UUID.random().toString());
            } else {
                // Use the first defined endpoint as reference
                HttpEndpoint first = (HttpEndpoint) endpoints.get(0);
                URL target = new URL(first.getTarget());
                URL newTarget = new URL(target.getProtocol(), target.getHost(), port, target.getFile());
                HttpEndpoint edpt = new HttpEndpoint(UUID.random().toString(), newTarget.toString());
                edpt.setHttpClientOptions(first.getHttpClientOptions());
                api.getProxy().getEndpoints().add(edpt);
            }
        }
    }
    if (ApiLoaderInterceptor.class.isAssignableFrom(target.getClass())) {
        ApiLoaderInterceptor loader = (ApiLoaderInterceptor) target;
        loader.after(api);
    }
    return api;
}
Also used : ApiLoaderInterceptor(io.gravitee.gateway.standalone.ApiLoaderInterceptor) ApiDescriptor(io.gravitee.gateway.standalone.junit.annotation.ApiDescriptor) ArrayList(java.util.ArrayList) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) URL(java.net.URL) Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) GraviteeMapper(io.gravitee.definition.jackson.datatype.GraviteeMapper) Api(io.gravitee.gateway.handlers.api.definition.Api)

Example 3 with HttpEndpoint

use of io.gravitee.definition.model.endpoint.HttpEndpoint in project gravitee-gateway by gravitee-io.

the class ProxyValidatorTest method validate_proxy_correctContextPathAndTarget.

/*
    @Test(expected = ValidationException.class)
    public void validate_proxy_badTarget() {
        Proxy proxyDefinition = new Proxy();
        proxyDefinition.setContextPath("/");
        proxyDefinition.setEndpoint("toto");

        Api definition = new Api();
        definition.setProxy(proxyDefinition);

        new ProxyValidator().validate(definition);
    }
    */
@Test
public void validate_proxy_correctContextPathAndTarget() {
    Proxy proxyDefinition = new Proxy();
    proxyDefinition.setContextPath("/context-path");
    proxyDefinition.setEndpoints(new LinkedHashSet<>());
    proxyDefinition.getEndpoints().add(new HttpEndpoint("name", "http://localhost"));
    Api definition = new Api();
    definition.setProxy(proxyDefinition);
    new ProxyValidator().validate(definition);
}
Also used : Proxy(io.gravitee.definition.model.Proxy) Api(io.gravitee.gateway.handlers.api.definition.Api) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Test(org.junit.Test)

Example 4 with HttpEndpoint

use of io.gravitee.definition.model.endpoint.HttpEndpoint in project gravitee-gateway by gravitee-io.

the class EndpointHealthcheckResolver method resolve.

/**
 * Returns a {@link Stream} of {@link Endpoint} which have to be health-checked.
 *
 * @param api
 * @return
 */
public List<EndpointRule> resolve(Api api) {
    HealthCheckService rootHealthCheck = api.getServices().get(HealthCheckService.class);
    Stream<Endpoint> endpoints = api.getProxy().getEndpoints().stream();
    // Only HTTP endpoint
    Stream<HttpEndpoint> httpEndpoints = endpoints.filter(endpoint -> endpoint.getType() == EndpointType.HTTP).map(endpoint -> (HttpEndpoint) endpoint);
    // Filtering endpoints according to tenancy configuration
    if (gatewayConfiguration.tenant().isPresent()) {
        String tenant = gatewayConfiguration.tenant().get();
        httpEndpoints = httpEndpoints.filter(endpoint -> endpoint.getTenants() != null && endpoint.getTenants().contains(tenant));
    }
    // Remove backup endpoints
    httpEndpoints = httpEndpoints.filter(endpoint -> !endpoint.isBackup());
    // Keep only endpoints where health-check is enabled or not settled (inherit from service)
    httpEndpoints = httpEndpoints.filter(endpoint -> (endpoint.getHealthCheck() == null) || (endpoint.getHealthCheck() != null && endpoint.getHealthCheck().isEnabled()));
    return httpEndpoints.map((Function<HttpEndpoint, EndpointRule>) endpoint -> new DefaultEndpointRule(api.getId(), endpoint, (endpoint.getHealthCheck() == null || endpoint.getHealthCheck().isInherit()) ? rootHealthCheck : endpoint.getHealthCheck())).collect(Collectors.toList());
}
Also used : Endpoint(io.gravitee.definition.model.Endpoint) List(java.util.List) Stream(java.util.stream.Stream) EndpointType(io.gravitee.definition.model.EndpointType) DefaultEndpointRule(io.gravitee.gateway.services.healthcheck.rule.DefaultEndpointRule) HealthCheckService(io.gravitee.definition.model.services.healthcheck.HealthCheckService) Autowired(org.springframework.beans.factory.annotation.Autowired) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Api(io.gravitee.definition.model.Api) GatewayConfiguration(io.gravitee.gateway.env.GatewayConfiguration) Function(java.util.function.Function) DefaultEndpointRule(io.gravitee.gateway.services.healthcheck.rule.DefaultEndpointRule) HealthCheckService(io.gravitee.definition.model.services.healthcheck.HealthCheckService) Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint)

Example 5 with HttpEndpoint

use of io.gravitee.definition.model.endpoint.HttpEndpoint in project gravitee-gateway by gravitee-io.

the class HttpEndpointRuleHandler method handle.

@Override
public void handle(Long timer) {
    HttpEndpoint endpoint = (HttpEndpoint) rule.endpoint();
    logger.debug("Running health-check for endpoint: {} [{}]", endpoint.getName(), endpoint.getTarget());
    // Run request for each step
    for (io.gravitee.definition.model.services.healthcheck.Step step : rule.steps()) {
        try {
            URI hcRequestUri = create(endpoint.getTarget(), step.getRequest());
            // Prepare HTTP client
            HttpClientOptions httpClientOptions = new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(false).setTcpKeepAlive(false);
            if (endpoint.getHttpClientOptions() != null) {
                httpClientOptions.setIdleTimeout((int) (endpoint.getHttpClientOptions().getIdleTimeout() / 1000)).setConnectTimeout((int) endpoint.getHttpClientOptions().getConnectTimeout()).setTryUseCompression(endpoint.getHttpClientOptions().isUseCompression());
            }
            // Configure HTTP proxy
            HttpProxy proxy = endpoint.getHttpProxy();
            if (proxy != null && proxy.isEnabled()) {
                ProxyOptions proxyOptions = new ProxyOptions().setHost(proxy.getHost()).setPort(proxy.getPort()).setUsername(proxy.getUsername()).setPassword(proxy.getPassword()).setType(ProxyType.valueOf(proxy.getType().name()));
                httpClientOptions.setProxyOptions(proxyOptions);
            }
            // Configure TLS if required
            HttpClientSslOptions sslOptions = endpoint.getHttpClientSslOptions();
            if (sslOptions != null && sslOptions.isEnabled()) {
                httpClientOptions.setSsl(sslOptions.isEnabled()).setVerifyHost(sslOptions.isHostnameVerifier()).setTrustAll(sslOptions.isTrustAll());
                if (sslOptions.getPem() != null && !sslOptions.getPem().isEmpty()) {
                    httpClientOptions.setPemTrustOptions(new PemTrustOptions().addCertValue(io.vertx.core.buffer.Buffer.buffer(sslOptions.getPem())));
                }
            } else if (HTTPS_SCHEME.equalsIgnoreCase(hcRequestUri.getScheme())) {
                // SSL is not configured but the endpoint scheme is HTTPS so let's enable the SSL on Vert.x HTTP client
                // automatically
                httpClientOptions.setSsl(true).setTrustAll(true);
            }
            HttpClient httpClient = vertx.createHttpClient(httpClientOptions);
            final int port = hcRequestUri.getPort() != -1 ? hcRequestUri.getPort() : (HTTPS_SCHEME.equals(hcRequestUri.getScheme()) ? 443 : 80);
            String relativeUri = (hcRequestUri.getRawQuery() == null) ? hcRequestUri.getRawPath() : hcRequestUri.getRawPath() + '?' + hcRequestUri.getRawQuery();
            // Run health-check
            HttpClientRequest healthRequest = httpClient.request(HttpMethod.valueOf(step.getRequest().getMethod().name().toUpperCase()), port, hcRequestUri.getHost(), relativeUri);
            // Set timeout on request
            if (endpoint.getHttpClientOptions() != null) {
                healthRequest.setTimeout(endpoint.getHttpClientOptions().getReadTimeout());
            }
            // Prepare request
            if (step.getRequest().getHeaders() != null) {
                step.getRequest().getHeaders().forEach(httpHeader -> healthRequest.headers().set(httpHeader.getName(), httpHeader.getValue()));
            }
            final EndpointStatus.Builder healthBuilder = EndpointStatus.forEndpoint(rule.api(), endpoint.getName()).on(System.currentTimeMillis());
            long startTime = System.currentTimeMillis();
            Request request = new Request();
            request.setMethod(step.getRequest().getMethod());
            request.setUri(hcRequestUri.toString());
            healthRequest.handler(response -> response.bodyHandler(buffer -> {
                long endTime = System.currentTimeMillis();
                logger.debug("Health-check endpoint returns a response with a {} status code", response.statusCode());
                String body = buffer.toString();
                EndpointStatus.StepBuilder stepBuilder = validateAssertions(step, new EvaluableHttpResponse(response, body));
                stepBuilder.request(request);
                stepBuilder.responseTime(endTime - startTime);
                Response healthResponse = new Response();
                healthResponse.setStatus(response.statusCode());
                // If validation fail, store request and response data
                if (!stepBuilder.isSuccess()) {
                    request.setBody(step.getRequest().getBody());
                    if (step.getRequest().getHeaders() != null) {
                        HttpHeaders reqHeaders = new HttpHeaders();
                        step.getRequest().getHeaders().forEach(httpHeader -> reqHeaders.put(httpHeader.getName(), Collections.singletonList(httpHeader.getValue())));
                        request.setHeaders(reqHeaders);
                    }
                    // Extract headers
                    HttpHeaders headers = new HttpHeaders();
                    response.headers().names().forEach(headerName -> headers.put(headerName, response.headers().getAll(headerName)));
                    healthResponse.setHeaders(headers);
                    // Store body
                    healthResponse.setBody(body);
                }
                stepBuilder.response(healthResponse);
                // Append step stepBuilder
                healthBuilder.step(stepBuilder.build());
                report(healthBuilder.build());
                // Close client
                httpClient.close();
            }));
            healthRequest.exceptionHandler(event -> {
                long endTime = System.currentTimeMillis();
                EndpointStatus.StepBuilder stepBuilder = EndpointStatus.forStep(step.getName());
                stepBuilder.fail(event.getMessage());
                Response healthResponse = new Response();
                // Extract request information
                request.setBody(step.getRequest().getBody());
                if (step.getRequest().getHeaders() != null) {
                    HttpHeaders reqHeaders = new HttpHeaders();
                    step.getRequest().getHeaders().forEach(httpHeader -> reqHeaders.put(httpHeader.getName(), Collections.singletonList(httpHeader.getValue())));
                    request.setHeaders(reqHeaders);
                }
                if (event instanceof ConnectTimeoutException) {
                    stepBuilder.fail(event.getMessage());
                    healthResponse.setStatus(HttpStatusCode.REQUEST_TIMEOUT_408);
                } else {
                    healthResponse.setStatus(HttpStatusCode.SERVICE_UNAVAILABLE_503);
                }
                Step result = stepBuilder.build();
                result.setResponse(healthResponse);
                result.setRequest(request);
                result.setResponseTime(endTime - startTime);
                // Append step result
                healthBuilder.step(result);
                report(healthBuilder.build());
                try {
                    // Close client
                    httpClient.close();
                } catch (IllegalStateException ise) {
                // Do not take care about exception when closing client
                }
            });
            // Send request
            logger.debug("Execute health-check request: {}", healthRequest);
            if (step.getRequest().getBody() != null && !step.getRequest().getBody().isEmpty()) {
                healthRequest.end(step.getRequest().getBody());
            } else {
                healthRequest.end();
            }
        } catch (Exception ex) {
            logger.error("An unexpected error occurs", ex);
        }
    }
}
Also used : Step(io.gravitee.reporter.api.health.Step) HttpHeaders(io.gravitee.common.http.HttpHeaders) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) EndpointStatus(io.gravitee.reporter.api.health.EndpointStatus) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) EvaluationException(io.gravitee.gateway.services.healthcheck.eval.EvaluationException) HttpClientSslOptions(io.gravitee.definition.model.HttpClientSslOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Response(io.gravitee.reporter.api.common.Response) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) EndpointRule(io.gravitee.gateway.services.healthcheck.EndpointRule) EndpointStatusDecorator(io.gravitee.gateway.services.healthcheck.EndpointStatusDecorator) HttpClientOptions(io.vertx.core.http.HttpClientOptions) URI(java.net.URI) PemTrustOptions(io.vertx.core.net.PemTrustOptions) Request(io.gravitee.reporter.api.common.Request) ProxyOptions(io.vertx.core.net.ProxyOptions) Logger(org.slf4j.Logger) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) Iterator(java.util.Iterator) HttpProxy(io.gravitee.definition.model.HttpProxy) Vertx(io.vertx.core.Vertx) EvaluableHttpResponse(io.gravitee.gateway.services.healthcheck.http.el.EvaluableHttpResponse) ProxyType(io.vertx.core.net.ProxyType) AssertionEvaluation(io.gravitee.gateway.services.healthcheck.eval.assertion.AssertionEvaluation) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) HttpHeaders(io.gravitee.common.http.HttpHeaders) ProxyOptions(io.vertx.core.net.ProxyOptions) Step(io.gravitee.reporter.api.health.Step) URI(java.net.URI) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Request(io.gravitee.reporter.api.common.Request) HttpClientSslOptions(io.gravitee.definition.model.HttpClientSslOptions) EvaluableHttpResponse(io.gravitee.gateway.services.healthcheck.http.el.EvaluableHttpResponse) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) URISyntaxException(java.net.URISyntaxException) EvaluationException(io.gravitee.gateway.services.healthcheck.eval.EvaluationException) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) HttpProxy(io.gravitee.definition.model.HttpProxy) EndpointStatus(io.gravitee.reporter.api.health.EndpointStatus) Response(io.gravitee.reporter.api.common.Response) EvaluableHttpResponse(io.gravitee.gateway.services.healthcheck.http.el.EvaluableHttpResponse) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException)

Aggregations

HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)6 Endpoint (io.gravitee.definition.model.Endpoint)2 Proxy (io.gravitee.definition.model.Proxy)2 HealthCheckService (io.gravitee.definition.model.services.healthcheck.HealthCheckService)2 Api (io.gravitee.gateway.handlers.api.definition.Api)2 DefaultEndpointRule (io.gravitee.gateway.services.healthcheck.rule.DefaultEndpointRule)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Lifecycle (io.gravitee.common.component.Lifecycle)1 HttpHeaders (io.gravitee.common.http.HttpHeaders)1 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)1 UUID (io.gravitee.common.utils.UUID)1 GraviteeMapper (io.gravitee.definition.jackson.datatype.GraviteeMapper)1 Api (io.gravitee.definition.model.Api)1 EndpointType (io.gravitee.definition.model.EndpointType)1