use of org.apache.synapse.commons.throttle.core.ThrottleException 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;
}
use of org.apache.synapse.commons.throttle.core.ThrottleException in project carbon-apimgt by wso2.
the class ThrottleHandler method isSubscriptionLevelSpike.
/**
* This method will check if coming request is hitting subscription level spikes.
*
* @param synCtx synapse message context which contains message data
* @param throttleKey subscription level throttle key.
* @return true if message is throttled else false
*/
public boolean isSubscriptionLevelSpike(MessageContext synCtx, String throttleKey) {
ThrottleContext subscriptionLevelSpikeArrestThrottleContext = throttle.getThrottleContext(throttleKey);
try {
AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(synCtx);
if (subscriptionLevelSpikeArrestThrottleContext != null && authContext.getKeyType() != null) {
org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
ConfigurationContext cc = axis2MC.getConfigurationContext();
subscriptionLevelSpikeArrestThrottleContext.setConfigurationContext(cc);
subscriptionLevelSpikeArrestThrottleContext.setThrottleId(id + APIThrottleConstants.SUBSCRIPTION_BURST_LIMIT);
AccessInformation info = getAccessInformation(subscriptionLevelSpikeArrestThrottleContext, throttleKey, throttleKey);
if (log.isDebugEnabled()) {
log.debug("Throttle by subscription level burst limit " + throttleKey);
log.debug("Allowed = " + (info != null ? info.isAccessAllowed() : "false"));
}
if (info != null && !info.isAccessAllowed()) {
synCtx.setProperty(APIThrottleConstants.THROTTLED_OUT_REASON, APIThrottleConstants.SUBSCRIPTON_BURST_LIMIT_EXCEEDED);
log.debug("Subscription level burst control limit exceeded for key " + throttleKey);
return true;
}
}
} catch (ThrottleException e) {
log.warn("Exception occurred while performing role " + "based throttling", e);
synCtx.setProperty(APIThrottleConstants.THROTTLED_OUT_REASON, APIThrottleConstants.HARD_LIMIT_EXCEEDED);
return false;
}
return false;
}
use of org.apache.synapse.commons.throttle.core.ThrottleException in project carbon-apimgt by wso2.
the class ThrottleHandler method initThrottleForSubscriptionLevelSpikeArrest.
/**
* This method will intialize subscription level throttling context and throttle object.
* This method need to be called for each and every request of spike arrest is enabled.
* If throttle context for incoming message is already created method will do nothing. Else
* it will create throttle object and context.
*
* @param synCtx synapse messaginitThrottleForSubscriptionLevelSpikeArreste context which contains message data
*/
private void initThrottleForSubscriptionLevelSpikeArrest(MessageContext synCtx, AuthenticationContext authenticationContext) {
policyKey = authenticationContext.getTier();
String apiContext = (String) synCtx.getProperty(RESTConstants.REST_API_CONTEXT);
String apiVersion = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
String subscriptionLevelThrottleKey = getSubscriptionLevelThrottleKey(policyKey, authenticationContext, apiContext, apiVersion);
int maxRequestCount = authenticationContext.getSpikeArrestLimit();
if (maxRequestCount != 0) {
String unitTime = authenticationContext.getSpikeArrestUnit();
int spikeArrestWindowUnitTime;
if (APIThrottleConstants.MIN.equalsIgnoreCase(unitTime)) {
spikeArrestWindowUnitTime = 60000;
} else {
spikeArrestWindowUnitTime = 1000;
}
try {
synchronized (this) {
if (throttle == null) {
OMElement spikeArrestSubscriptionLevelPolicy = createSpikeArrestSubscriptionLevelPolicy(subscriptionLevelThrottleKey, maxRequestCount, spikeArrestWindowUnitTime);
if (spikeArrestSubscriptionLevelPolicy != null) {
throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(spikeArrestSubscriptionLevelPolicy));
}
} else {
boolean createSpikeArrestSubscriptionLevelPolicy = false;
if (throttle.getThrottleContext(subscriptionLevelThrottleKey) == null) {
createSpikeArrestSubscriptionLevelPolicy = true;
} else {
CallerConfiguration existingCallerConfig = throttle.getThrottleContext(subscriptionLevelThrottleKey).getThrottleConfiguration().getCallerConfiguration(subscriptionLevelThrottleKey);
if (existingCallerConfig.getMaximumRequestPerUnitTime() != maxRequestCount || existingCallerConfig.getUnitTime() != spikeArrestWindowUnitTime) {
createSpikeArrestSubscriptionLevelPolicy = true;
}
}
if (createSpikeArrestSubscriptionLevelPolicy) {
OMElement spikeArrestSubscriptionLevelPolicy = createSpikeArrestSubscriptionLevelPolicy(subscriptionLevelThrottleKey, maxRequestCount, spikeArrestWindowUnitTime);
if (spikeArrestSubscriptionLevelPolicy != null) {
Throttle tempThrottle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(spikeArrestSubscriptionLevelPolicy));
ThrottleConfiguration newThrottleConfig = tempThrottle.getThrottleConfiguration(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
ThrottleContext subscriptionLevelSpikeThrottle = ThrottleContextFactory.createThrottleContext(ThrottleConstants.ROLE_BASE, newThrottleConfig);
throttle.addThrottleContext(subscriptionLevelThrottleKey, subscriptionLevelSpikeThrottle);
}
}
}
}
} catch (ThrottleException e) {
log.error("Error while initializing throttling object for subscription level spike arrest policy" + e.getMessage());
}
}
}
use of org.apache.synapse.commons.throttle.core.ThrottleException in project carbon-apimgt by wso2.
the class ThrottleHandler method initThrottleForHardLimitThrottling.
/**
* This method will intialize subscription level throttling context and throttle object.
* This method need to be called for each and every request of spike arrest is enabled.
* If throttle context for incoming message is already created method will do nothing. Else
* it will create throttle object and context.
*/
private void initThrottleForHardLimitThrottling() {
OMElement hardThrottlingPolicy = createHardThrottlingPolicy();
if (hardThrottlingPolicy != null) {
Throttle tempThrottle;
try {
tempThrottle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(hardThrottlingPolicy));
ThrottleConfiguration newThrottleConfig = tempThrottle.getThrottleConfiguration(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
ThrottleContext hardThrottling = ThrottleContextFactory.createThrottleContext(ThrottleConstants.ROLE_BASE, newThrottleConfig);
tempThrottle.addThrottleContext(APIThrottleConstants.HARD_THROTTLING_CONFIGURATION, hardThrottling);
if (throttle != null) {
throttle.addThrottleContext(APIThrottleConstants.HARD_THROTTLING_CONFIGURATION, hardThrottling);
} else {
throttle = tempThrottle;
}
} catch (ThrottleException e) {
log.error("Error occurred while creating policy file for Hard Throttling.", e);
}
}
}
use of org.apache.synapse.commons.throttle.core.ThrottleException in project carbon-apimgt by wso2.
the class APIThrottleHandler method throttleByAccessRate.
private boolean throttleByAccessRate(org.apache.axis2.context.MessageContext axisMC, ConfigurationContext cc) {
resolveTenantId();
String callerId = null;
boolean canAccess = true;
// remote ip of the caller
String remoteIP = (String) ((TreeMap) axisMC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).get(APIMgtGatewayConstants.X_FORWARDED_FOR);
if (remoteIP != null) {
if (remoteIP.indexOf(',') > 0) {
remoteIP = remoteIP.substring(0, remoteIP.indexOf(','));
}
} else {
remoteIP = (String) axisMC.getProperty(org.apache.axis2.context.MessageContext.REMOTE_ADDR);
}
// domain name of the caller
String domainName = (String) axisMC.getPropertyNonReplicable(NhttpConstants.REMOTE_HOST);
// this domain name ,then throttling will occur according to that configuration
if (domainName != null) {
// do the domain based throttling
if (log.isTraceEnabled()) {
log.trace("The Domain Name of the caller is :" + domainName);
}
// loads the DomainBasedThrottleContext
ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY);
if (context != null) {
// loads the DomainBasedThrottleConfiguration
ThrottleConfiguration config = context.getThrottleConfiguration();
if (config != null) {
// checks the availability of a policy configuration for this domain name
callerId = config.getConfigurationKeyOfCaller(domainName);
if (callerId != null) {
// If this is a clustered env.
if (isClusteringEnable) {
context.setConfigurationContext(cc);
context.setThrottleId(id);
}
try {
// Checks for access state
AccessInformation accessInformation = accessController.canAccess(context, callerId, ThrottleConstants.DOMAIN_BASE);
canAccess = accessInformation.isAccessAllowed();
if (log.isDebugEnabled()) {
log.debug("Access " + (canAccess ? "allowed" : "denied") + " for Domain Name : " + domainName);
}
// if the access has denied by rate based throttling
if (!canAccess && concurrentAccessController != null) {
concurrentAccessController.incrementAndGet();
if (isClusteringEnable) {
cc.setProperty(key, concurrentAccessController);
}
}
} catch (ThrottleException e) {
handleException("Error occurred during throttling", e);
}
}
}
}
} else {
log.debug("The Domain name of the caller cannot be found");
}
// therefore trying to find a configuration policy based on remote caller ip
if (callerId == null) {
// do the IP-based throttling
if (remoteIP == null) {
if (log.isDebugEnabled()) {
log.debug("The IP address of the caller cannot be found");
}
canAccess = true;
} else {
if (log.isDebugEnabled()) {
log.debug("The IP Address of the caller is :" + remoteIP);
}
try {
// Loads the IPBasedThrottleContext
ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
if (context != null) {
// Loads the IPBasedThrottleConfiguration
ThrottleConfiguration config = context.getThrottleConfiguration();
if (config != null) {
// Checks the availability of a policy configuration for this ip
callerId = config.getConfigurationKeyOfCaller(remoteIP);
if (callerId != null) {
// For clustered env.
if (isClusteringEnable) {
context.setConfigurationContext(cc);
context.setThrottleId(id);
}
// Checks access state
AccessInformation accessInformation = accessController.canAccess(context, callerId, ThrottleConstants.IP_BASE);
canAccess = accessInformation.isAccessAllowed();
if (log.isDebugEnabled()) {
log.debug("Access " + (canAccess ? "allowed" : "denied") + " for IP : " + remoteIP);
}
// if the access has denied by rate based throttling
if (!canAccess && concurrentAccessController != null) {
concurrentAccessController.incrementAndGet();
if (isClusteringEnable) {
cc.setProperty(key, concurrentAccessController);
}
}
}
}
}
} catch (ThrottleException e) {
handleException("Error occurred during throttling", e);
}
}
}
return canAccess;
}
Aggregations