Search in sources :

Example 21 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithJWTConditionInvert.

@Test
public void testGetThrottledInConditionWithJWTConditionInvert() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    throttleProperties.setEnableJwtConditions(true);
    String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3c28yLm9yZy9wcm9kdWN0cy9hbSIsImV4cCI6MTM0NTE4MzQ5MjE4MSwiaHR0cDov" + "L3dzbzIub3JnL2NsYWltcy9hYmMiOiJjZGUiLCJodHRwOi8vd3NvMi5vcmcvY2xhaW1zL2JjZCI6Inh5eiJ9.9zGU062DJ5mQ5hne" + "41h4IRpLbaY_b5thRxb3feebOcA";
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setCallerToken(jwt);
    ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
    MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
    Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
    conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getJWTCondition(true) }));
    conditionMap.put("default", Arrays.asList(new ConditionDto[] { getJWTCondition(true) }));
    String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, authenticationContext, conditionMap);
    Assert.assertEquals(condition, "default");
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) HashMap(java.util.HashMap) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) List(java.util.List) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Test(org.junit.Test)

Example 22 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluator method isThrottledWithinCondition.

private boolean isThrottledWithinCondition(MessageContext axis2MessageContext, AuthenticationContext authContext, List<ConditionDto> conditionDtoList) {
    ThrottleProperties throttleProperties = ServiceReferenceHolder.getInstance().getThrottleProperties();
    boolean status = true;
    for (ConditionDto condition : conditionDtoList) {
        status = true;
        if (condition.getIpCondition() != null) {
            if (!isMatchingIP(axis2MessageContext, condition.getIpCondition())) {
                status = false;
            }
        } else if (condition.getIpRangeCondition() != null) {
            if (!isWithinIP(axis2MessageContext, condition.getIpRangeCondition())) {
                status = false;
            }
        }
        if (condition.getHeaderConditions() != null && throttleProperties.isEnableHeaderConditions() && !condition.getHeaderConditions().getValues().isEmpty()) {
            if (!isHeaderPresent(axis2MessageContext, condition.getHeaderConditions())) {
                status = false;
            }
        }
        if (condition.getJwtClaimConditions() != null && throttleProperties.isEnableJwtConditions() && !condition.getJwtClaimConditions().getValues().isEmpty()) {
            if (!isJWTClaimPresent(authContext, condition.getJwtClaimConditions())) {
                status = false;
            }
        }
        if (condition.getQueryParameterConditions() != null && throttleProperties.isEnableQueryParamConditions() && !condition.getQueryParameterConditions().getValues().isEmpty()) {
            if (!isQueryParamPresent(axis2MessageContext, condition.getQueryParameterConditions())) {
                status = false;
            }
        }
        if (status) {
            break;
        }
    }
    return status;
}
Also used : ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Example 23 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class SubscriptionValidationDataUtil method fromApiPolicyToApiPolicyListDTO.

public static ApiPolicyListDTO fromApiPolicyToApiPolicyListDTO(List<APIPolicy> model) {
    ApiPolicyListDTO apiPolicyListDTO = new ApiPolicyListDTO();
    if (model != null) {
        for (APIPolicy apiPolicyModel : model) {
            ApiPolicyDTO policyDTO = new ApiPolicyDTO();
            policyDTO.setName(apiPolicyModel.getName());
            policyDTO.setQuotaType(apiPolicyModel.getQuotaType());
            policyDTO.setTenantId(apiPolicyModel.getTenantId());
            policyDTO.setTenantDomain(apiPolicyModel.getTenantDomain());
            policyDTO.setApplicableLevel(apiPolicyModel.getApplicableLevel());
            policyDTO.setDefaultLimit(getThrottleLimitDTO(apiPolicyModel));
            apiPolicyListDTO.getList().add(policyDTO);
            List<APIPolicyConditionGroup> retrievedGroups = apiPolicyModel.getConditionGroups();
            List<ApiPolicyConditionGroupDTO> condGroups = new ArrayList<ApiPolicyConditionGroupDTO>();
            for (APIPolicyConditionGroup retGroup : retrievedGroups) {
                ApiPolicyConditionGroupDTO group = new ApiPolicyConditionGroupDTO();
                group.setConditionGroupId(retGroup.getConditionGroupId());
                group.setQuotaType(retGroup.getQuotaType());
                group.setDefaultLimit(getThrottleLimitDTO(retGroup));
                group.setPolicyId(retGroup.getPolicyId());
                List<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO> condition = new ArrayList<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO>();
                List<ConditionDTO> retrievedConditions = retGroup.getConditionDTOS();
                for (ConditionDTO retrievedCondition : retrievedConditions) {
                    org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO conditionDTO = new org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO();
                    conditionDTO.setConditionType(retrievedCondition.getConditionType());
                    conditionDTO.setIsInverted(retrievedCondition.isInverted());
                    conditionDTO.setName(retrievedCondition.getConditionName());
                    conditionDTO.setValue(retrievedCondition.getConditionValue());
                    condition.add(conditionDTO);
                }
                group.setCondition(condition);
                condGroups.add(group);
            }
            policyDTO.setConditionGroups(condGroups);
        }
        apiPolicyListDTO.setCount(model.size());
    } else {
        apiPolicyListDTO.setCount(0);
    }
    return apiPolicyListDTO;
}
Also used : APIPolicyConditionGroup(org.wso2.carbon.apimgt.api.model.subscription.APIPolicyConditionGroup) ArrayList(java.util.ArrayList) ApiPolicyConditionGroupDTO(org.wso2.carbon.apimgt.internal.service.dto.ApiPolicyConditionGroupDTO) ApiPolicyDTO(org.wso2.carbon.apimgt.internal.service.dto.ApiPolicyDTO) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) APIPolicy(org.wso2.carbon.apimgt.api.model.subscription.APIPolicy) ApiPolicyListDTO(org.wso2.carbon.apimgt.internal.service.dto.ApiPolicyListDTO)

Example 24 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class PolicyMappingUtil method mapCondition.

/**
 * Map a org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition to a
 * org.wso2.carbon.apimgt.api.model.policy.Condition
 *
 * @param conditionDTO org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition
 * @return org.wso2.carbon.apimgt.api.model.policy.Condition object
 */
public static Condition mapCondition(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition conditionDTO) {
    switch(conditionDTO.getConditionType()) {
        case PolicyConstants.IP_RANGE_TYPE:
            IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
            ipRangeCondition.setInvertCondition(conditionDTO.isInverted());
            ipRangeCondition.setStartingIP(conditionDTO.getName());
            ipRangeCondition.setEndingIP(conditionDTO.getValue());
            return ipRangeCondition;
        case PolicyConstants.IP_SPECIFIC_TYPE:
            IPCondition ipSpecificCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
            ipSpecificCondition.setInvertCondition(conditionDTO.isInverted());
            ipSpecificCondition.setSpecificIP(conditionDTO.getValue());
            return ipSpecificCondition;
        case PolicyConstants.HEADER_TYPE:
            HeaderCondition headerCondition = new HeaderCondition();
            headerCondition.setInvertCondition(conditionDTO.isInverted());
            headerCondition.setHeader(conditionDTO.getName());
            headerCondition.setValue(conditionDTO.getValue());
            return headerCondition;
        case PolicyConstants.JWT_CLAIMS_TYPE:
            JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
            jwtClaimsCondition.setInvertCondition(conditionDTO.isInverted());
            jwtClaimsCondition.setClaimUrl(conditionDTO.getName());
            jwtClaimsCondition.setAttribute(conditionDTO.getValue());
            return jwtClaimsCondition;
        case PolicyConstants.QUERY_PARAMETER_TYPE:
            QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
            queryParameterCondition.setInvertCondition(conditionDTO.isInverted());
            queryParameterCondition.setParameter(conditionDTO.getName());
            queryParameterCondition.setValue(conditionDTO.getValue());
            return queryParameterCondition;
        default:
            return null;
    }
}
Also used : IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 25 with ConditionDto

use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testApplicabilityOfMatchingIPSpecificCondition.

@Test
public void testApplicabilityOfMatchingIPSpecificCondition() {
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("IPSpecificConditionGroup");
    ConditionDTO matchingCondition = new ConditionDTO();
    matchingCondition.setConditionType("IPSpecific");
    matchingCondition.setConditionValue("127.0.0.1");
    ConditionDTO[] conditionDTOS = { matchingCondition };
    conditionGroupDTO.setConditions(conditionDTOS);
    ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
    List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
    Assert.assertEquals(matchingConditionGroups.get(0).getConditionGroupId(), "IPSpecificConditionGroup");
}
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)

Aggregations

Test (org.junit.Test)45 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)40 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)33 MessageContext (org.apache.synapse.MessageContext)32 HashMap (java.util.HashMap)31 List (java.util.List)26 Map (java.util.Map)23 TreeMap (java.util.TreeMap)23 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)20 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)20 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)19 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)17 ArrayList (java.util.ArrayList)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)2 IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)2 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)2 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)2 APIPolicy (org.wso2.carbon.apimgt.api.model.subscription.APIPolicy)2