use of org.wso2.carbon.identity.configuration.mgt.core.search.constant.ConditionType in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addBlockConditions.
@Override
public String addBlockConditions(BlockConditions blockConditions) throws APIMgtDAOException {
boolean status = false;
boolean valid = false;
String uuid = null;
String conditionType = blockConditions.getConditionType();
String conditionValue = blockConditions.getConditionValue();
try {
String query = "INSERT INTO AM_BLOCK_CONDITIONS (TYPE, VALUE, ENABLED, UUID) VALUES (?,?,?,?)";
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API.equals(conditionType)) {
if (isValidContext(conditionValue)) {
valid = true;
} else {
throw new APIMgtDAOException("Couldn't Save Block Condition Due to Invalid API Context : " + conditionValue, ExceptionCodes.BLOCK_CONDITION_UNSUPPORTED_API_CONTEXT);
}
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION.equals(conditionType)) {
String[] appArray = conditionValue.split(":");
if (appArray.length > 1) {
String appUuid = appArray[0];
String appName = appArray[1];
if (isValidApplication(appName, appUuid)) {
valid = true;
} else {
throw new APIMgtDAOException("Couldn't Save Block Condition Due to Invalid Application : " + appName + ", UUID :" + appUuid, ExceptionCodes.BLOCK_CONDITION_UNSUPPORTED_APP_ID_NAME);
}
}
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER.equals(conditionType)) {
valid = true;
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(conditionType)) {
valid = true;
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(conditionType)) {
valid = isIPRangeConditionValid(blockConditions.getStartingIP(), blockConditions.getEndingIP());
}
if (valid) {
try (Connection connection = DAOUtil.getConnection();
PreparedStatement insertPreparedStatement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
if (!isBlockConditionExist(blockConditions)) {
uuid = UUID.randomUUID().toString();
insertPreparedStatement.setString(1, conditionType);
insertPreparedStatement.setString(2, conditionValue);
insertPreparedStatement.setBoolean(3, blockConditions.isEnabled());
insertPreparedStatement.setString(4, uuid);
insertPreparedStatement.execute();
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(conditionType)) {
String ipConditionQuery = "INSERT INTO AM_IP_RANGE_CONDITION " + "(STARTING_IP, ENDING_IP, UUID) VALUES (?, ?, ?)";
try (PreparedStatement ipStatement = connection.prepareStatement(ipConditionQuery)) {
ipStatement.setString(1, blockConditions.getStartingIP());
ipStatement.setString(2, blockConditions.getEndingIP());
ipStatement.setString(3, uuid);
ipStatement.execute();
} catch (SQLException e) {
connection.rollback();
}
}
connection.commit();
} else {
throw new APIMgtDAOException("Condition with type: " + conditionType + ", value: " + conditionValue + " already exists", ExceptionCodes.BLOCK_CONDITION_ALREADY_EXISTS);
}
} catch (SQLException e) {
connection.rollback();
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding block condition: " + conditionType + " and " + conditionValue, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
}
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding block condition: " + conditionType + " and " + conditionValue, e);
}
return uuid;
}
use of org.wso2.carbon.identity.configuration.mgt.core.search.constant.ConditionType in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultBlockCondition.
public static BlockConditions createDefaultBlockCondition(String conditionType) {
BlockConditions blockConditions = new BlockConditions();
blockConditions.setConditionType(conditionType);
blockConditions.setEnabled(true);
if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP)) {
blockConditions.setConditionValue(SAMPLE_IP_1);
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
blockConditions.setStartingIP(SAMPLE_IP_1);
blockConditions.setEndingIP(SAMPLE_IP_2);
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API)) {
try {
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.build();
DAOFactory.getApiDAO().addAPI(api);
blockConditions.setConditionValue(api.getContext());
} catch (APIMgtDAOException e) {
log.error("Error while adding default api in default block condition", e);
}
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION)) {
try {
Application app = createDefaultApplication();
DAOFactory.getApplicationDAO().addApplication(app);
blockConditions.setConditionValue(app.getId() + ":" + app.getName());
} catch (APIMgtDAOException e) {
log.error("Error while adding default app in default block condition", e);
}
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER)) {
blockConditions.setConditionValue(ADMIN);
}
return blockConditions;
}
use of org.wso2.carbon.identity.configuration.mgt.core.search.constant.ConditionType in project carbon-apimgt by wso2.
the class APIProviderImpl method addBlockCondition.
@Override
public String addBlockCondition(String conditionType, String conditionValue) throws APIManagementException {
if (APIConstants.BLOCKING_CONDITIONS_USER.equals(conditionType)) {
conditionValue = MultitenantUtils.getTenantAwareUsername(conditionValue);
conditionValue = conditionValue + "@" + tenantDomain;
}
BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
blockConditionsDTO.setConditionType(conditionType);
blockConditionsDTO.setConditionValue(conditionValue);
blockConditionsDTO.setTenantDomain(tenantDomain);
blockConditionsDTO.setEnabled(true);
blockConditionsDTO.setUUID(UUID.randomUUID().toString());
BlockConditionsDTO createdBlockConditionsDto = apiMgtDAO.addBlockConditions(blockConditionsDTO);
if (createdBlockConditionsDto != null) {
publishBlockingEvent(createdBlockConditionsDto, "true");
}
return createdBlockConditionsDto.getUUID();
}
use of org.wso2.carbon.identity.configuration.mgt.core.search.constant.ConditionType in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOListToConditionList.
/**
* Converts a list of Throttle Condition DTOs into a list of Condition model objects
*
* @param throttleConditionDTOs list of Throttle Condition DTOs
* @return Derived list of Condition model objects from Throttle Condition DTOs
* @throws UnsupportedThrottleConditionTypeException
*/
public static List<Condition> fromDTOListToConditionList(List<ThrottleConditionDTO> throttleConditionDTOs) throws UnsupportedThrottleConditionTypeException {
List<Condition> conditions = new ArrayList<>();
String errorMessage;
if (throttleConditionDTOs != null) {
for (ThrottleConditionDTO dto : throttleConditionDTOs) {
ThrottleConditionDTO.TypeEnum conditionType = dto.getType();
if (conditionType != null) {
switch(conditionType) {
case HEADERCONDITION:
{
if (dto.getHeaderCondition() != null) {
conditions.add(fromDTOToHeaderCondition(dto.getHeaderCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.HEADERCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
case IPCONDITION:
{
if (dto.getIpCondition() != null) {
conditions.add(fromDTOToIPCondition(dto.getIpCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.IPCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
case QUERYPARAMETERCONDITION:
{
if (dto.getQueryParameterCondition() != null) {
conditions.add(fromDTOToQueryParameterCondition(dto.getQueryParameterCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.QUERYPARAMETERCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
case JWTCLAIMSCONDITION:
{
if (dto.getJwtClaimsCondition() != null) {
conditions.add(fromDTOToJWTClaimsCondition(dto.getJwtClaimsCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.JWTCLAIMSCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
default:
return null;
}
} else {
errorMessage = "Condition item 'type' property has not been specified\n" + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
}
}
return conditions;
}
use of org.wso2.carbon.identity.configuration.mgt.core.search.constant.ConditionType in project carbon-apimgt by wso2.
the class ApiMgtDAO method addBlockConditions.
/**
* Add a block condition
*
* @return uuid of the block condition if successfully added
* @throws APIManagementException
*/
public BlockConditionsDTO addBlockConditions(BlockConditionsDTO blockConditionsDTO) throws APIManagementException {
Connection connection = null;
PreparedStatement insertPreparedStatement = null;
boolean status = false;
boolean valid = false;
ResultSet rs = null;
String uuid = blockConditionsDTO.getUUID();
String conditionType = blockConditionsDTO.getConditionType();
String conditionValue = blockConditionsDTO.getConditionValue();
String tenantDomain = blockConditionsDTO.getTenantDomain();
String conditionStatus = String.valueOf(blockConditionsDTO.isEnabled());
try {
String query = SQLConstants.ThrottleSQLConstants.ADD_BLOCK_CONDITIONS_SQL;
if (APIConstants.BLOCKING_CONDITIONS_API.equals(conditionType)) {
String extractedTenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(conditionValue);
if (extractedTenantDomain == null) {
extractedTenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
if (tenantDomain.equals(extractedTenantDomain) && isValidContext(conditionValue)) {
valid = true;
} else {
throw new APIManagementException("Couldn't Save Block Condition Due to Invalid API Context " + conditionValue);
}
} else if (APIConstants.BLOCKING_CONDITIONS_APPLICATION.equals(conditionType)) {
String[] appArray = conditionValue.split(":");
if (appArray.length > 1) {
String appOwner = appArray[0];
String appName = appArray[1];
if ((MultitenantUtils.getTenantDomain(appOwner).equals(tenantDomain)) && isValidApplication(appOwner, appName)) {
valid = true;
} else {
throw new APIManagementException("Couldn't Save Block Condition Due to Invalid Application " + "name " + appName + " from Application " + "Owner " + appOwner);
}
}
} else if (APIConstants.BLOCKING_CONDITIONS_USER.equals(conditionType)) {
if (MultitenantUtils.getTenantDomain(conditionValue).equals(tenantDomain)) {
valid = true;
} else {
throw new APIManagementException("Invalid User in Tenant Domain " + tenantDomain);
}
} else if (APIConstants.BLOCKING_CONDITIONS_IP.equals(conditionType) || APIConstants.BLOCK_CONDITION_IP_RANGE.equals(conditionType)) {
valid = true;
} else if (APIConstants.BLOCKING_CONDITIONS_SUBSCRIPTION.equals(conditionType)) {
/* ATM this condition type will be used internally to handle subscription blockings for JWT type access
tokens.
*/
String[] conditionsArray = conditionValue.split(":");
if (conditionsArray.length > 0) {
String apiContext = conditionsArray[0];
String applicationIdentifier = conditionsArray[2];
String[] app = applicationIdentifier.split("-", 2);
String appOwner = app[0];
String appName = app[1];
// Check whether the given api context exists in tenant
String extractedTenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(apiContext);
if (extractedTenantDomain == null) {
extractedTenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
if (tenantDomain.equals(extractedTenantDomain) && isValidContext(apiContext)) {
valid = true;
} else {
throw new APIManagementException("Couldn't Save Subscription Block Condition Due to Invalid API Context " + apiContext);
}
// Check whether the given application is valid
if ((MultitenantUtils.getTenantDomain(appOwner).equals(tenantDomain)) && isValidApplication(appOwner, appName)) {
valid = true;
} else {
throw new APIManagementException("Couldn't Save Subscription Block Condition Due to Invalid Application " + "name " + appName + " from Application " + "Owner " + appOwner);
}
} else {
throw new APIManagementException("Invalid subscription block condition with insufficient data : " + conditionValue);
}
}
if (valid) {
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(false);
if (!isBlockConditionExist(conditionType, conditionValue, tenantDomain, connection)) {
String dbProductName = connection.getMetaData().getDatabaseProductName();
insertPreparedStatement = connection.prepareStatement(query, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "CONDITION_ID") });
insertPreparedStatement.setString(1, conditionType);
insertPreparedStatement.setString(2, conditionValue);
insertPreparedStatement.setString(3, conditionStatus);
insertPreparedStatement.setString(4, tenantDomain);
insertPreparedStatement.setString(5, uuid);
insertPreparedStatement.execute();
ResultSet generatedKeys = insertPreparedStatement.getGeneratedKeys();
if (generatedKeys != null && generatedKeys.next()) {
blockConditionsDTO.setConditionId(generatedKeys.getInt(1));
}
connection.commit();
status = true;
} else {
throw new BlockConditionAlreadyExistsException("Condition with type: " + conditionType + ", value: " + conditionValue + " already exists");
}
}
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
handleException("Failed to rollback adding Block condition : " + conditionType + " and " + conditionValue, ex);
}
}
handleException("Failed to add Block condition : " + conditionType + " and " + conditionValue, e);
} finally {
APIMgtDBUtil.closeAllConnections(insertPreparedStatement, connection, null);
}
if (status) {
return blockConditionsDTO;
} else {
return null;
}
}
Aggregations