Skip to content

Commit

Permalink
Merge pull request wso2#1 from ujitha/support-9.0.174.edcast
Browse files Browse the repository at this point in the history
Organization level throttleing changes
  • Loading branch information
ujitha committed Feb 17, 2022
2 parents c706acd + 4b34cae commit a3a6fc0
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.wso2.carbon.apimgt.gateway.handlers.security;

import org.wso2.carbon.apimgt.gateway.MethodStats;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Contains some context information related to an authenticated request. This can be used
Expand All @@ -37,6 +40,7 @@ public class AuthenticationContext {
private String applicationId;
private String applicationUUID;
private String applicationName;
private Set<String> applicationGroupIds;
private String consumerKey;
private String subscriber;
private List<String> throttlingDataList;
Expand Down Expand Up @@ -253,4 +257,12 @@ public String getApplicationUUID() {
public void setApplicationUUID(String applicationUUID) {
this.applicationUUID = applicationUUID;
}

public Set<String> getApplicationGroupIds() {
return applicationGroupIds;
}

public void setApplicationGroupIds(Set<String> applicationGroupIds) {
this.applicationGroupIds = applicationGroupIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public AuthenticationResponse authenticate(MessageContext synCtx) throws APIMana
authContext.setCallerToken(info.getEndUserToken());
authContext.setApplicationId(info.getApplicationId());
authContext.setApplicationUUID(info.getApplicationUUID());
authContext.setApplicationGroupIds(info.getApplicationGroupIds());
authContext.setApplicationName(info.getApplicationName());
authContext.setApplicationTier(info.getApplicationTier());
authContext.setSubscriber(info.getSubscriber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder;
import org.wso2.carbon.apimgt.keymgt.model.entity.Application;
import org.wso2.carbon.apimgt.keymgt.model.entity.GroupId;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.util.List;
import java.util.stream.Collectors;

/**
* This mediator would load the subscriber's information from the subscribers list according to the index of the list.
Expand Down Expand Up @@ -174,6 +176,7 @@ private void populateAuthContext(String tenantDomain, int appId, AuthenticationC
authContext.setApplicationName(app.getName());
authContext.setSubscriber(app.getSubName());
authContext.setKeyType(app.getTokenType());
authContext.setApplicationGroupIds(app.getGroupIds().stream().map(GroupId::getGroupId).collect(Collectors.toSet()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ public static AuthenticationContext generateAuthenticationContext(String jti,
authContext.setKeyType(apiKeyValidationInfoDTO.getType());
authContext.setApplicationId(apiKeyValidationInfoDTO.getApplicationId());
authContext.setApplicationUUID(apiKeyValidationInfoDTO.getApplicationUUID());
authContext.setApplicationGroupIds(apiKeyValidationInfoDTO.getApplicationGroupIds());
authContext.setApplicationName(apiKeyValidationInfoDTO.getApplicationName());
authContext.setApplicationTier(apiKeyValidationInfoDTO.getApplicationTier());
authContext.setSubscriber(apiKeyValidationInfoDTO.getSubscriber());
Expand Down Expand Up @@ -648,6 +649,7 @@ public static AuthenticationContext generateAuthenticationContext(String tokenSi
String.valueOf(applicationObj.getAsNumber(APIConstants.JwtTokenConstants.APPLICATION_ID)));
authContext.setApplicationUUID(
String.valueOf(applicationObj.getAsString(APIConstants.JwtTokenConstants.APPLICATION_UUID)));

authContext.setApplicationName(applicationObj.getAsString(APIConstants.JwtTokenConstants.APPLICATION_NAME));
authContext.setApplicationTier(applicationObj.getAsString(APIConstants.JwtTokenConstants.APPLICATION_TIER));
authContext.setSubscriber(applicationObj.getAsString(APIConstants.JwtTokenConstants.APPLICATION_OWNER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ private void addToApplicationList(List<Application> list, ResultSet resultSet) t
application.addAttribute(attributeName, attributeValue);
}
// todo read from the aplication_group_mapping table and make it a set
// application.addGroupId(resultSet.getString("GROUP_ID"));

String groupId = resultSet.getString("GROUP_ID");
if (StringUtils.isNotEmpty(groupId)) {
application.addGroupId(groupId);
}

list.add(application);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ public class SubscriptionValidationSQLConstants {
" APP.TOKEN_TYPE AS TOKEN_TYPE," +
" SUB.USER_ID AS SUB_NAME," +
" ATTRIBUTES.NAME AS ATTRIBUTE_NAME," +
" ATTRIBUTES.VALUE AS ATTRIBUTE_VALUE" +
" ATTRIBUTES.VALUE AS ATTRIBUTE_VALUE," +
" GROUP_MAP.GROUP_ID AS GROUP_ID" +
" FROM " +
" AM_SUBSCRIBER SUB," +
" AM_APPLICATION APP" +
" LEFT OUTER JOIN AM_APPLICATION_ATTRIBUTES ATTRIBUTES" +
" ON APP.APPLICATION_ID = ATTRIBUTES.APPLICATION_ID" +
" LEFT OUTER JOIN AM_APPLICATION_GROUP_MAPPING GROUP_MAP" +
" ON APP.APPLICATION_ID = GROUP_MAP.APPLICATION_ID" +
" WHERE " +
" APP.SUBSCRIBER_ID = SUB.SUBSCRIBER_ID AND" +
" SUB.TENANT_ID = ? ";
Expand All @@ -66,12 +69,15 @@ public class SubscriptionValidationSQLConstants {
" APP.TOKEN_TYPE AS TOKEN_TYPE," +
" SUB.USER_ID AS SUB_NAME," +
" ATTRIBUTES.NAME AS ATTRIBUTE_NAME," +
" ATTRIBUTES.VALUE AS ATTRIBUTE_VALUE" +
" ATTRIBUTES.VALUE AS ATTRIBUTE_VALUE," +
" GROUP_MAP.GROUP_ID AS GROUP_ID" +
" FROM " +
" AM_SUBSCRIBER SUB," +
" AM_APPLICATION APP" +
" LEFT OUTER JOIN AM_APPLICATION_ATTRIBUTES ATTRIBUTES " +
"ON APP.APPLICATION_ID = ATTRIBUTES.APPLICATION_ID" +
" LEFT OUTER JOIN AM_APPLICATION_GROUP_MAPPING GROUP_MAP" +
" ON APP.APPLICATION_ID = GROUP_MAP.APPLICATION_ID" +
" WHERE " +
" APP.SUBSCRIBER_ID = SUB.SUBSCRIBER_ID AND" +
" APP.APPLICATION_ID = ? ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.carbon.apimgt.impl.dto;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class APIKeyValidationInfoDTO implements Serializable {
private int graphQLMaxComplexity;
private String apiVersion;
private String applicationUUID;
private Set<String> applicationGroupIds = new HashSet<>();
private Map<String, String> appAttributes;

public List<String> getThrottlingDataList() {
Expand Down Expand Up @@ -380,6 +382,14 @@ public void setApplicationUUID(String applicationUUID) {
this.applicationUUID = applicationUUID;
}

public Set<String> getApplicationGroupIds() {
return applicationGroupIds;
}

public void setApplicationGroupIds(Set<String> applicationGroupIds) {
this.applicationGroupIds = applicationGroupIds;
}

public Map<String, String> getAppAttributes() {

return appAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.wso2.carbon.apimgt.keymgt.handlers;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIManagementException;
Expand All @@ -34,6 +33,7 @@
import org.wso2.carbon.apimgt.keymgt.model.entity.Application;
import org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping;
import org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy;
import org.wso2.carbon.apimgt.keymgt.model.entity.GroupId;
import org.wso2.carbon.apimgt.keymgt.model.entity.Subscription;
import org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy;
import org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException;
Expand All @@ -46,6 +46,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractKeyValidationHandler implements KeyValidationHandler {

Expand Down Expand Up @@ -388,6 +389,7 @@ private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String
infoDTO.setApplicationName(app.getName());
infoDTO.setApplicationTier(app.getPolicy());
infoDTO.setApplicationUUID(app.getUUID());
infoDTO.setApplicationGroupIds(app.getGroupIds().stream().map(GroupId::getGroupId).collect(Collectors.toSet()));
infoDTO.setAppAttributes(app.getAttributes());
infoDTO.setType(type);

Expand Down Expand Up @@ -497,6 +499,7 @@ private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String
infoDTO.setApplicationName(app.getName());
infoDTO.setApplicationTier(app.getPolicy());
infoDTO.setApplicationUUID(app.getUUID());
infoDTO.setApplicationGroupIds(app.getGroupIds().stream().map(GroupId::getGroupId).collect(Collectors.toSet()));
infoDTO.setAppAttributes(app.getAttributes());
infoDTO.setType(type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Application implements CacheableEntity<Integer> {
private String subName = null;
private String policy = null;
private String tokenType = null;
private List<String> groupIds = new ArrayList<>();
private List<GroupId> groupIds = new ArrayList<>();
private Map<String, String> attributes = new ConcurrentHashMap<>();

public Integer getId() {
Expand Down Expand Up @@ -95,16 +95,21 @@ public Integer getCacheKey() {
return getId();
}

public List<String> getGroupIds() {
public List<GroupId> getGroupIds() {

return groupIds;
}

public void addGroupId(String groupId) {
public void addGroupId(GroupId groupId) {

this.groupIds.add(groupId);
}

public void setGroupIds(List<GroupId> groupIds) {

this.groupIds = groupIds;
}

public void removeGroupId(String groupId) {

this.groupIds.remove(groupId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.wso2.carbon.apimgt.keymgt.model.entity;

/**
* Entity for keeping GroupId and application id details
*/
public class GroupId {
private String groupId = null;
private Integer applicationId = null;

public String getGroupId() {
return groupId;
}

public void setGroupId(String groupId) {
this.groupId = groupId;
}

public Integer getApplicationId() {
return applicationId;
}

public void setApplicationId(Integer applicationId) {
this.applicationId = applicationId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.apimgt.keymgt.service;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.impl.keymgt.KeyManagerDataService;
Expand All @@ -37,10 +38,15 @@
import org.wso2.carbon.apimgt.keymgt.model.entity.Application;
import org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping;
import org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy;
import org.wso2.carbon.apimgt.keymgt.model.entity.GroupId;
import org.wso2.carbon.apimgt.keymgt.model.entity.Scope;
import org.wso2.carbon.apimgt.keymgt.model.entity.Subscription;
import org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class KeyManagerDataServiceImpl implements KeyManagerDataService {

private static final Log log = LogFactory.getLog(KeyManagerDataServiceImpl.class);
Expand Down Expand Up @@ -348,6 +354,18 @@ private Application getApplicationFromApplicationEvent(ApplicationEvent event) {
application.setUUID(event.getUuid());
event.getAttributes().forEach(application::addAttribute);
application.setSubName(event.getSubscriber());
//TODO add group ids list to application
if (!StringUtils.isEmpty(event.getGroupId())) {
String[] groupIdArray = event.getGroupId().split(",");
List<GroupId> groupIdList = Arrays.asList(groupIdArray).stream().map(id -> {
GroupId groupId = new GroupId();
groupId.setApplicationId(event.getApplicationId());
groupId.setGroupId(id);
return groupId;
}).collect(Collectors.toList());


}
if (log.isDebugEnabled()) {
log.debug("Event: " + event.toString());
log.debug("Converted : " + application.toString());
Expand Down

0 comments on commit a3a6fc0

Please sign in to comment.