Search in sources :

Example 1 with Plan

use of io.gravitee.gateway.handlers.api.definition.Plan in project gravitee-gateway by gravitee-io.

the class ApiManagerTest method add_simpleApi.

@Test
public void add_simpleApi() {
    Api api = new ApiDefinitionBuilder().name("api-test").proxy(new ProxyDefinitionBuilder().contextPath("/team").target("default", "http://localhost/target").build()).build();
    api.setPlans(Collections.singletonList(new Plan()));
    apiManager.deploy(api);
    verify(eventManager, only()).publishEvent(ReactorEvent.DEPLOY, api);
}
Also used : ProxyDefinitionBuilder(io.gravitee.gateway.handlers.api.builders.ProxyDefinitionBuilder) ApiDefinitionBuilder(io.gravitee.gateway.handlers.api.builders.ApiDefinitionBuilder) Api(io.gravitee.gateway.handlers.api.definition.Api) Plan(io.gravitee.gateway.handlers.api.definition.Plan) Test(org.junit.Test)

Example 2 with Plan

use of io.gravitee.gateway.handlers.api.definition.Plan in project gravitee-gateway by gravitee-io.

the class PlanPolicyChainResolver method calculate.

@Override
protected List<Policy> calculate(StreamType streamType, Request request, Response response, ExecutionContext executionContext) {
    if (streamType == StreamType.ON_REQUEST) {
        String plan = (String) executionContext.getAttribute(ExecutionContext.ATTR_PLAN);
        String application = (String) executionContext.getAttribute(ExecutionContext.ATTR_APPLICATION);
        // String user = (String) executionContext.getAttribute(ExecutionContext.ATTR_USER_ID);
        // request.metrics().setUserId(user);
        request.metrics().setPlan(plan);
        request.metrics().setApplication(application);
        Plan apiPlan = api.getPlan(plan);
        // The call is probably not relative to the same API.
        if (plan != null && apiPlan != null) {
            Map<String, Path> paths = api.getPlan(plan).getPaths();
            if (paths != null && !paths.isEmpty()) {
                // For 1.0.0, there is only a single root path defined
                // Must be reconsidered when user will be able to manage policies at the plan level by himself
                Path rootPath = paths.values().iterator().next();
                return rootPath.getRules().stream().filter(rule -> rule.isEnabled() && rule.getMethods().contains(request.method())).map(rule -> create(streamType, rule.getPolicy().getName(), rule.getPolicy().getConfiguration())).filter(Objects::nonNull).collect(Collectors.toList());
            }
        } else {
            logger.warn("No plan has been selected to process request {}. Returning an unauthorized HTTP status (401)", request.id());
            return null;
        }
    }
    return Collections.emptyList();
}
Also used : Path(io.gravitee.definition.model.Path) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) Autowired(org.springframework.beans.factory.annotation.Autowired) Plan(io.gravitee.gateway.handlers.api.definition.Plan) ResponsePolicyChain(io.gravitee.gateway.policy.impl.ResponsePolicyChain) Collectors(java.util.stream.Collectors) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Objects(java.util.Objects) List(java.util.List) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Map(java.util.Map) Api(io.gravitee.gateway.handlers.api.definition.Api) Path(io.gravitee.definition.model.Path) io.gravitee.gateway.policy(io.gravitee.gateway.policy) PolicyResult(io.gravitee.policy.api.PolicyResult) Collections(java.util.Collections) Plan(io.gravitee.gateway.handlers.api.definition.Plan)

Example 3 with Plan

use of io.gravitee.gateway.handlers.api.definition.Plan in project gravitee-gateway by gravitee-io.

the class ApiManagerImpl method deploy.

@Override
public void deploy(Api api) {
    MDC.put("api", api.getId());
    logger.info("Deployment of {}", api);
    // Deploy the API only if there is at least one plan
    if (!api.getPlans().isEmpty()) {
        logger.info("Deploying {} plan(s) for API {}:", api.getPlans().size(), api.getId());
        for (Plan plan : api.getPlans()) {
            logger.info("\t- {}", plan.getName());
        }
        try {
            validator.validate(api);
            apis.put(api.getId(), api);
            if (api.isEnabled()) {
                eventManager.publishEvent(ReactorEvent.DEPLOY, api);
            } else {
                logger.debug("{} is not enabled. Skip deployment.", api);
            }
        } catch (ValidationException ve) {
            logger.error("API {} can't be deployed because of validation errors", api, ve);
        }
    } else {
        logger.warn("There is no published plan associated to this API, skipping deployment...");
    }
    MDC.remove("api");
}
Also used : ValidationException(io.gravitee.gateway.handlers.api.validator.ValidationException) Plan(io.gravitee.gateway.handlers.api.definition.Plan)

Example 4 with Plan

use of io.gravitee.gateway.handlers.api.definition.Plan in project gravitee-gateway by gravitee-io.

the class ApiKeyRepositoryRefresherTest method shouldRefreshWithRevokedApiKey.

@Test
public void shouldRefreshWithRevokedApiKey() throws TechnicalException {
    Mockito.when(plan.getSecurity()).thenReturn(io.gravitee.repository.management.model.Plan.PlanSecurityType.API_KEY.name());
    List<Plan> plans = Collections.singletonList(plan);
    Mockito.when(api.getPlans()).thenReturn(plans);
    refresher.initialize();
    refresher.run();
    refresher.run();
    InOrder inOrder = Mockito.inOrder(apiKeyRepository, apiKeyRepository);
    inOrder.verify(apiKeyRepository).findByCriteria(Matchers.argThat(new ArgumentMatcher<ApiKeyCriteria>() {

        @Override
        public boolean matches(Object arg) {
            ApiKeyCriteria criteria = (ApiKeyCriteria) arg;
            return !criteria.isIncludeRevoked() && criteria.getFrom() == 0 && criteria.getTo() == 0 && criteria.getPlans().size() == 1;
        }
    }));
    inOrder.verify(apiKeyRepository).findByCriteria(Matchers.argThat(new ArgumentMatcher<ApiKeyCriteria>() {

        @Override
        public boolean matches(Object arg) {
            ApiKeyCriteria criteria = (ApiKeyCriteria) arg;
            return criteria.isIncludeRevoked() && criteria.getFrom() != 0 && criteria.getTo() != 0 && criteria.getPlans().size() == 1;
        }
    }));
}
Also used : ApiKeyCriteria(io.gravitee.repository.management.api.search.ApiKeyCriteria) Plan(io.gravitee.gateway.handlers.api.definition.Plan) Test(org.junit.Test)

Example 5 with Plan

use of io.gravitee.gateway.handlers.api.definition.Plan in project gravitee-gateway by gravitee-io.

the class ApiKeyRepositoryRefresherTest method shouldRefreshWithRevokedApiKeyAndPutIntoCache.

@Test
public void shouldRefreshWithRevokedApiKeyAndPutIntoCache() throws TechnicalException {
    Mockito.when(plan.getSecurity()).thenReturn(io.gravitee.repository.management.model.Plan.PlanSecurityType.API_KEY.name());
    List<Plan> plans = Collections.singletonList(plan);
    Mockito.when(api.getPlans()).thenReturn(plans);
    ApiKey apiKey1 = Mockito.mock(ApiKey.class);
    Mockito.when(apiKey1.isRevoked()).thenReturn(false);
    Mockito.when(apiKeyRepository.findByCriteria(Mockito.any(ApiKeyCriteria.class))).thenReturn(Collections.singletonList(apiKey1));
    refresher.initialize();
    refresher.run();
    refresher.run();
    InOrder inOrder = Mockito.inOrder(apiKeyRepository, apiKeyRepository);
    inOrder.verify(apiKeyRepository).findByCriteria(Matchers.argThat(new ArgumentMatcher<ApiKeyCriteria>() {

        @Override
        public boolean matches(Object arg) {
            ApiKeyCriteria criteria = (ApiKeyCriteria) arg;
            return !criteria.isIncludeRevoked() && criteria.getFrom() == 0 && criteria.getTo() == 0 && criteria.getPlans().size() == 1;
        }
    }));
    inOrder.verify(apiKeyRepository).findByCriteria(Matchers.argThat(new ArgumentMatcher<ApiKeyCriteria>() {

        @Override
        public boolean matches(Object arg) {
            ApiKeyCriteria criteria = (ApiKeyCriteria) arg;
            return criteria.isIncludeRevoked() && criteria.getFrom() != 0 && criteria.getTo() != 0 && criteria.getPlans().size() == 1;
        }
    }));
    Mockito.verify(cache, Mockito.times(2)).put(Matchers.any(Element.class));
}
Also used : ApiKeyCriteria(io.gravitee.repository.management.api.search.ApiKeyCriteria) ApiKey(io.gravitee.repository.management.model.ApiKey) Element(net.sf.ehcache.Element) Plan(io.gravitee.gateway.handlers.api.definition.Plan) Test(org.junit.Test)

Aggregations

Plan (io.gravitee.gateway.handlers.api.definition.Plan)13 Test (org.junit.Test)8 ApiKeyCriteria (io.gravitee.repository.management.api.search.ApiKeyCriteria)5 Path (io.gravitee.definition.model.Path)2 Api (io.gravitee.gateway.handlers.api.definition.Api)2 ValidationException (io.gravitee.gateway.handlers.api.validator.ValidationException)2 AuthenticationHandler (io.gravitee.gateway.security.core.AuthenticationHandler)2 ApiKey (io.gravitee.repository.management.model.ApiKey)2 Element (net.sf.ehcache.Element)2 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)1 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1 Request (io.gravitee.gateway.api.Request)1 Response (io.gravitee.gateway.api.Response)1 ApiDefinitionBuilder (io.gravitee.gateway.handlers.api.builders.ApiDefinitionBuilder)1 ProxyDefinitionBuilder (io.gravitee.gateway.handlers.api.builders.ProxyDefinitionBuilder)1 io.gravitee.gateway.policy (io.gravitee.gateway.policy)1 PolicyChain (io.gravitee.gateway.policy.impl.PolicyChain)1 RequestPolicyChain (io.gravitee.gateway.policy.impl.RequestPolicyChain)1 ResponsePolicyChain (io.gravitee.gateway.policy.impl.ResponsePolicyChain)1 PolicyResult (io.gravitee.policy.api.PolicyResult)1