Search in sources :

Example 16 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class AbstractPolicy method onRequest.

public void onRequest(Object... args) throws PolicyException {
    ExecutionContext executionContext = getParameterAssignableTo(ExecutionContext.class, args);
    PolicyChain policyChain = getParameterAssignableTo(PolicyChain.class, args);
    Request request = getParameterAssignableTo(Request.class, args);
    Response response = getParameterAssignableTo(Response.class, args);
    this.onRequest(request, response, policyChain, executionContext);
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request)

Example 17 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class ApiReactorHandler method handleClientRequest.

private void handleClientRequest(Request serverRequest, Response serverResponse, ExecutionContext executionContext, Handler<Response> handler) {
    serverRequest.pause();
    // Apply request policies
    RequestPolicyChainProcessor requestPolicyChain = new RequestPolicyChainProcessor(policyResolvers);
    requestPolicyChain.setResultHandler(requestPolicyChainResult -> {
        if (requestPolicyChainResult.isFailure()) {
            sendPolicyFailure(requestPolicyChainResult.getPolicyResult(), serverResponse);
            handler.handle(serverResponse);
        } else {
            // Call an invoker to get a proxy connection (connection to an underlying backend, mainly HTTP)
            Invoker upstreamInvoker = (Invoker) executionContext.getAttribute(ExecutionContext.ATTR_INVOKER);
            long serviceInvocationStart = System.currentTimeMillis();
            AtomicReference<ProxyConnection> proxyConnection = new AtomicReference<>();
            Request invokeRequest = upstreamInvoker.invoke(executionContext, serverRequest, requestPolicyChainResult.getPolicyChain(), connection -> {
                proxyConnection.set(connection);
                connection.responseHandler(proxyResponse -> handleProxyResponse(serverRequest, serverResponse, executionContext, proxyResponse, serviceInvocationStart, handler));
                requestPolicyChain.setStreamErrorHandler(result -> {
                    connection.cancel();
                    sendPolicyFailure(result.getPolicyResult(), serverResponse);
                    handler.handle(serverResponse);
                });
            });
            // Plug server request stream to request policy chain stream
            invokeRequest.bodyHandler(chunk -> requestPolicyChainResult.getPolicyChain().write(chunk)).endHandler(aVoid -> requestPolicyChainResult.getPolicyChain().end());
        }
    });
    requestPolicyChain.execute(serverRequest, serverResponse, executionContext);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) CorsHandler(io.gravitee.gateway.handlers.api.cors.CorsHandler) HttpHeaders(io.gravitee.common.http.HttpHeaders) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) LoggerFactory(org.slf4j.LoggerFactory) HttpHeadersValues(io.gravitee.common.http.HttpHeadersValues) Autowired(org.springframework.beans.factory.annotation.Autowired) Invoker(io.gravitee.gateway.api.Invoker) PolicyManager(io.gravitee.gateway.policy.PolicyManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) LoggableClientResponse(io.gravitee.gateway.handlers.api.logging.LoggableClientResponse) RequestPolicyChainProcessor(io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Response(io.gravitee.gateway.api.Response) StreamType(io.gravitee.gateway.policy.StreamType) Reactable(io.gravitee.gateway.reactor.Reactable) Request(io.gravitee.gateway.api.Request) DefaultInvoker(io.gravitee.gateway.http.core.invoker.DefaultInvoker) Logger(org.slf4j.Logger) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) PolicyChainResolver(io.gravitee.gateway.policy.PolicyChainResolver) TemplateContext(io.gravitee.gateway.api.expression.TemplateContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) Throwables(com.google.common.base.Throwables) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) Buffer(io.gravitee.gateway.api.buffer.Buffer) Handler(io.gravitee.gateway.api.handler.Handler) CorsResponseHandler(io.gravitee.gateway.handlers.api.cors.CorsResponseHandler) EndpointLifecycleManager(io.gravitee.gateway.http.core.endpoint.EndpointLifecycleManager) ResourceLifecycleManager(io.gravitee.gateway.resource.ResourceLifecycleManager) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) ProxyResponse(io.gravitee.gateway.api.proxy.ProxyResponse) ExecutionContextFactory(io.gravitee.gateway.handlers.api.context.ExecutionContextFactory) Cors(io.gravitee.definition.model.Cors) TemplateVariableProvider(io.gravitee.gateway.api.expression.TemplateVariableProvider) PlanPolicyChainResolver(io.gravitee.gateway.handlers.api.policy.plan.PlanPolicyChainResolver) Api(io.gravitee.gateway.handlers.api.definition.Api) AbstractReactorHandler(io.gravitee.gateway.reactor.handler.AbstractReactorHandler) PolicyResult(io.gravitee.policy.api.PolicyResult) SecurityPolicyChainResolver(io.gravitee.gateway.security.core.SecurityPolicyChainResolver) ApiPolicyChainResolver(io.gravitee.gateway.handlers.api.policy.api.ApiPolicyChainResolver) Invoker(io.gravitee.gateway.api.Invoker) DefaultInvoker(io.gravitee.gateway.http.core.invoker.DefaultInvoker) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Request(io.gravitee.gateway.api.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestPolicyChainProcessor(io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor)

Example 18 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_onException.

@Test
public void shouldReturnUnauthorized_onException() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenThrow(TechnicalException.class);
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Test(org.junit.Test)

Example 19 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_badClient.

@Test
public void shouldReturnUnauthorized_badClient() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    Subscription subscription = mock(Subscription.class);
    when(subscription.getClientId()).thenReturn("my-bad-client-id");
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenReturn(Collections.singletonList(subscription));
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 20 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class KeylessAuthenticationHandlerTest method shouldReturnPolicies.

@Test
public void shouldReturnPolicies() {
    ExecutionContext executionContext = mock(ExecutionContext.class);
    List<AuthenticationPolicy> keylessProviderPolicies = authenticationHandler.handle(executionContext);
    Assert.assertEquals(1, keylessProviderPolicies.size());
    PluginAuthenticationPolicy policy = (PluginAuthenticationPolicy) keylessProviderPolicies.iterator().next();
    Assert.assertEquals(policy.name(), KeylessAuthenticationHandler.KEYLESS_POLICY);
}
Also used : ExecutionContext(io.gravitee.gateway.api.ExecutionContext) AuthenticationPolicy(io.gravitee.gateway.security.core.AuthenticationPolicy) PluginAuthenticationPolicy(io.gravitee.gateway.security.core.PluginAuthenticationPolicy) PluginAuthenticationPolicy(io.gravitee.gateway.security.core.PluginAuthenticationPolicy) Test(org.junit.Test)

Aggregations

ExecutionContext (io.gravitee.gateway.api.ExecutionContext)28 Request (io.gravitee.gateway.api.Request)22 Test (org.junit.Test)21 Response (io.gravitee.gateway.api.Response)19 PolicyChain (io.gravitee.gateway.policy.impl.PolicyChain)12 Handler (io.gravitee.gateway.api.handler.Handler)9 PolicyChain (io.gravitee.policy.api.PolicyChain)9 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 BufferedReadWriteStream (io.gravitee.gateway.api.stream.BufferedReadWriteStream)6 ReadWriteStream (io.gravitee.gateway.api.stream.ReadWriteStream)6 ResponsePolicyChain (io.gravitee.gateway.policy.impl.ResponsePolicyChain)6 RequestPolicyChain (io.gravitee.gateway.policy.impl.RequestPolicyChain)5 Subscription (io.gravitee.repository.management.model.Subscription)5 AuthenticationPolicy (io.gravitee.gateway.security.core.AuthenticationPolicy)4 PluginAuthenticationPolicy (io.gravitee.gateway.security.core.PluginAuthenticationPolicy)4 InOrder (org.mockito.InOrder)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 HttpHeaders (io.gravitee.common.http.HttpHeaders)3 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)3