Search in sources :

Example 16 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testApplicabilityOfInvertedIPSpecificCondition.

@Test
public void testApplicabilityOfInvertedIPSpecificCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("IPSpecificConditionGroup");
    ConditionDTO invertedIPRangeCondition = new ConditionDTO();
    invertedIPRangeCondition.setConditionType("IPSpecific");
    invertedIPRangeCondition.setConditionValue("127.0.0.1");
    invertedIPRangeCondition.isInverted(true);
    ConditionDTO[] conditionDTOS = { invertedIPRangeCondition };
    conditionGroupDTO.setConditions(conditionDTOS);
    ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
    List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
    Assert.assertNull(matchingConditionGroups.get(0));
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) Test(org.junit.Test)

Example 17 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluator method getApplicableConditions.

/**
 * When called, provides a list of Applicable Condition Groups for the current request.
 *
 * @param synapseContext        Message Context of the incoming request.
 * @param authenticationContext AuthenticationContext populated by {@code APIAuthenticationHandler}
 * @param inputConditionGroups  All Condition Groups Attached with the resource/API being invoked.
 * @return List of ConditionGroups applicable for the current request.
 */
public List<ConditionGroupDTO> getApplicableConditions(org.apache.synapse.MessageContext synapseContext, AuthenticationContext authenticationContext, ConditionGroupDTO[] inputConditionGroups) {
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>(inputConditionGroups.length);
    ConditionGroupDTO defaultGroup = null;
    for (ConditionGroupDTO conditionGroup : inputConditionGroups) {
        if (APIConstants.THROTTLE_POLICY_DEFAULT.equals(conditionGroup.getConditionGroupId())) {
            defaultGroup = conditionGroup;
        } else if (isConditionGroupApplicable(synapseContext, authenticationContext, conditionGroup)) {
            matchingConditions.add(conditionGroup);
        }
    }
    // If no matching ConditionGroups are present, apply the default group.
    if (matchingConditions.isEmpty()) {
        matchingConditions.add(defaultGroup);
    }
    return matchingConditions;
}
Also used : ArrayList(java.util.ArrayList) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)

Example 18 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgThrottleOutWhenAPILevelIsThrottled.

@Test
public void testMsgThrottleOutWhenAPILevelIsThrottled() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    // Set conditional group
    verbInfo.setConditionGroups(conditionGroupDTOs);
    messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
    authenticationContext.setApiTier(throttlingTier);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
    matchingConditions.add(conditionGroupDTO);
    String combinedResourceLevelThrottleKey = apiLevelThrottleKey + conditionGroupDTO.getConditionGroupId();
    throttleDataHolder.addThrottledAPIKey(apiLevelThrottleKey, System.currentTimeMillis() + 10000);
    throttleDataHolder.addThrottleData(combinedResourceLevelThrottleKey, System.currentTimeMillis() + 10000);
    Mockito.when(throttleEvaluator.getApplicableConditions(messageContext, authenticationContext, conditionGroupDTOs)).thenReturn(matchingConditions);
    // Should throttle out and discontinue message flow, when api level is throttled out
    Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgThrottleOutWhenApplicationLevelIsThrottled.

@Test
public void testMsgThrottleOutWhenApplicationLevelIsThrottled() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
    authenticationContext.setApiTier(throttlingTier);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    verbInfo.setConditionGroups(conditionGroupDTOs);
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
    matchingConditions.add(conditionGroupDTO);
    String applicationLevelThrottleKey = authenticationContext.getApplicationId() + ":" + authenticationContext.getUsername() + "@" + throttleHandler.getTenantDomain();
    // Set application level throttled out
    throttleDataHolder.addThrottleData(applicationLevelThrottleKey, System.currentTimeMillis() + 10000);
    // Should discontinue message flow, when application level is throttled
    Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgThrottleOutWithUserBlockingConditions.

@Test
public void testMsgThrottleOutWithUserBlockingConditions() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
    verbInfo.setConditionGroups(conditionGroupDTOs);
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
    // Adding a user blocking condition
    throttleDataHolder.addUserBlockingCondition(blockedUserWithTenantDomain, blockedUserWithTenantDomain);
    matchingConditions.add(conditionGroupDTO);
    authenticationContext.setApiTier("Unlimited");
    // When a blocked user is invoking
    authenticationContext.setUsername(blockedUserWithTenantDomain);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    throttleDataHolder.addThrottledAPIKey(resourceLevelThrottleKey, System.currentTimeMillis() + 10000);
    Assert.assertFalse(throttleHandler.handleRequest(messageContext));
    // When an unblocked user is invoking
    authenticationContext.setUsername(userWithTenantDomain);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    throttleDataHolder.addThrottledAPIKey(resourceLevelThrottleKey, System.currentTimeMillis() + 10000);
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
    // When a blocked user without tenant domain in the username is invoking
    authenticationContext.setUsername(blockedUserWithOutTenantDomain);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    throttleDataHolder.addThrottledAPIKey(resourceLevelThrottleKey, System.currentTimeMillis() + 10000);
    Assert.assertFalse(throttleHandler.handleRequest(messageContext));
    // Remove the user block condition and use blocked user to invoke
    throttleDataHolder.removeUserBlockingCondition(blockedUserWithTenantDomain);
    authenticationContext.setUsername(blockedUserWithTenantDomain);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    throttleDataHolder.addThrottledAPIKey(resourceLevelThrottleKey, System.currentTimeMillis() + 10000);
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)37 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)31 Test (org.junit.Test)29 MessageContext (org.apache.synapse.MessageContext)21 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)21 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)19 ArrayList (java.util.ArrayList)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ThrottleDataHolder (org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)12 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)6 HashMap (java.util.HashMap)3 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Before (org.junit.Before)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)2 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)2 Timer (org.wso2.carbon.metrics.manager.Timer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1