Search in sources :

Example 16 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class ApplicationThrottleControllerTest method testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceRetrievalFailed.

@Test(expected = SynapseException.class)
public void testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceRetrievalFailed() throws UserStoreException, RegistryException {
    Mockito.when(throttleDataHolder.getThrottleContext(applicationId)).thenReturn(null);
    PowerMockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(tenantID)).thenReturn(registry);
    PowerMockito.when(registry.resourceExists(RESOURCE_PATH)).thenReturn(true);
    PowerMockito.doThrow(new RegistryException("Error while fetching resource ")).when(registry).get(RESOURCE_PATH);
    ApplicationThrottleController.getApplicationThrottleContext(messageContext, throttleDataHolder, applicationId, THROTTLE_POLICY_KEY);
}
Also used : RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class ApplicationThrottleControllerTest method testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceContentRetrievalFailed.

@Test(expected = SynapseException.class)
public void testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceContentRetrievalFailed() throws UserStoreException, RegistryException {
    Mockito.when(throttleDataHolder.getThrottleContext(applicationId)).thenReturn(null);
    PowerMockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
    PowerMockito.when(registryService.getGovernanceSystemRegistry(tenantID)).thenReturn(registry);
    PowerMockito.when(registry.resourceExists(RESOURCE_PATH)).thenReturn(true);
    PowerMockito.when(registry.get(RESOURCE_PATH)).thenReturn(throttlingPolicyResource);
    PowerMockito.doThrow(new RegistryException("Error while retrieving resource content")).when(throttlingPolicyResource).getContent();
    ApplicationThrottleController.getApplicationThrottleContext(messageContext, throttleDataHolder, applicationId, THROTTLE_POLICY_KEY);
}
Also used : RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class SseUtils method isRequestBlocked.

public static boolean isRequestBlocked(AuthenticationContext authContext, String apiContext, String apiVersion, String authorizedUser, String clientIp, String apiTenantDomain) {
    ThrottleDataHolder throttleDataHolder = ServiceReferenceHolder.getInstance().getThrottleDataHolder();
    if (throttleDataHolder.isBlockingConditionsPresent()) {
        String appLevelBlockingKey = authContext.getSubscriber() + ":" + authContext.getApplicationName();
        String subscriptionLevelBlockingKey = apiContext + ":" + apiVersion + ":" + authContext.getSubscriber() + "-" + authContext.getApplicationName() + ":" + authContext.getKeyType();
        return throttleDataHolder.isRequestBlocked(apiContext, appLevelBlockingKey, authorizedUser, clientIp, apiTenantDomain, subscriptionLevelBlockingKey);
    }
    return false;
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)

Example 19 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class ApplicationThrottleController method createThrottleContext.

private static ThrottleContext createThrottleContext(MessageContext synCtx, ThrottleDataHolder dataHolder, String applicationId, String policyKeyApplication) {
    // Object entryValue = synCtx.getEntry(APPLICATION_THROTTLE_POLICY_KEY);
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
    AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(synCtx);
    // extract the subscriber username from the auth Context
    String subscriber = authContext.getSubscriber();
    // get the tenant Domain from the subscriber
    String tenantDomain = MultitenantUtils.getTenantDomain(subscriber);
    int tenantId;
    // get the tenant domain id from the tenant domain name
    try {
        tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
    } catch (UserStoreException e) {
        handleException("Unable to Find the tenant ID using tenant: " + tenantDomain, e);
        return null;
    }
    Object entryValue = lookup(policyKeyApplication, tenantId);
    if (entryValue == null || !(entryValue instanceof OMElement)) {
        handleException("Unable to load throttling policy using key: " + policyKeyApplication);
    }
    try {
        Throttle throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy((OMElement) entryValue));
        ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
        dataHolder.addThrottleContext(applicationId, context);
        return context;
    } catch (ThrottleException e) {
        handleException("Error processing the throttling policy", e);
    }
    return null;
}
Also used : ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) RealmService(org.wso2.carbon.user.core.service.RealmService) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Throttle(org.apache.synapse.commons.throttle.core.Throttle)

Example 20 with ThrottleDataHolder

use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.

the class KeyTemplateRetrieverTest method run.

@Test
public void run() throws Exception {
    Map map = new HashMap();
    map.put("$userId", "$userId");
    map.put("$apiContext", "$apiContext");
    map.put("$apiVersion", "$apiVersion");
    String content = "[\"$userId\",\"$apiContext\",\"$apiVersion\"]";
    PowerMockito.mockStatic(APIUtil.class);
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
    BasicHttpEntity httpEntity = new BasicHttpEntity();
    httpEntity.setContent(new ByteArrayInputStream(content.getBytes()));
    Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
    Mockito.when(httpClient.execute(Mockito.any(HttpGet.class))).thenReturn(httpResponse);
    StatusLine status = Mockito.mock(StatusLine.class);
    Mockito.when(status.getStatusCode()).thenReturn(200);
    Mockito.when(httpResponse.getStatusLine()).thenReturn(status);
    BDDMockito.given(APIUtil.getHttpClient(Mockito.anyInt(), Mockito.anyString())).willReturn(httpClient);
    EventHubConfigurationDto eventHubConfigurationDto = new EventHubConfigurationDto();
    eventHubConfigurationDto.setUsername("admin");
    eventHubConfigurationDto.setPassword("admin".toCharArray());
    eventHubConfigurationDto.setEnabled(true);
    eventHubConfigurationDto.setServiceUrl("http://localhost:18084/internal/data/v1");
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    KeyTemplateRetriever keyTemplateRetriever = new KeyTemplateRetrieverWrapper(eventHubConfigurationDto, throttleDataHolder);
    keyTemplateRetriever.run();
    Map<String, String> keyTemplateMap = throttleDataHolder.getKeyTemplateMap();
    Assert.assertNotNull(keyTemplateMap);
    Assert.assertEquals(map, keyTemplateMap);
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) StatusLine(org.apache.http.StatusLine) EventHubConfigurationDto(org.wso2.carbon.apimgt.impl.dto.EventHubConfigurationDto) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 ThrottleDataHolder (org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)27 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)21 MessageContext (org.apache.synapse.MessageContext)20 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)17 ArrayList (java.util.ArrayList)12 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)12 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)6 ThrottleDataPublisher (org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher)3 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpResponse (org.apache.http.HttpResponse)2 StatusLine (org.apache.http.StatusLine)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)2 ThrottleContext (org.apache.synapse.commons.throttle.core.ThrottleContext)2 ThrottleException (org.apache.synapse.commons.throttle.core.ThrottleException)2 EventHubConfigurationDto (org.wso2.carbon.apimgt.impl.dto.EventHubConfigurationDto)2