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);
}
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();
}
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");
}
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;
}
}));
}
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));
}
Aggregations