Search in sources :

Example 11 with Condition

use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method isValidApplication.

/**
 * validate the blocking application.
 *
 * @param appName name of the application
 * @param uuid    uuid of the application
 * @return return true/false depends of the success
 * @throws APIMgtDAOException if failed validating application
 */
private boolean isValidApplication(String appName, String uuid) throws APIMgtDAOException {
    String query = "SELECT 1 FROM AM_APPLICATION WHERE UUID = ? AND NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        connection.setAutoCommit(false);
        statement.setString(1, uuid);
        statement.setString(2, appName);
        try (ResultSet resultSet = statement.executeQuery()) {
            return resultSet.next();
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "checking if Block condition with Application Name " + appName + " , Application ID = " + uuid + " exists", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 12 with Condition

use of org.wso2.carbon.apimgt.api.model.policy.Condition 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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 13 with Condition

use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getBlockConditionByUUID.

@Override
public BlockConditions getBlockConditionByUUID(String uuid) throws APIMgtDAOException {
    BlockConditions blockCondition = new BlockConditions();
    String query = "SELECT CONDITION_ID,TYPE,VALUE,ENABLED,UUID FROM AM_BLOCK_CONDITIONS WHERE UUID =?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement selectPreparedStatement = connection.prepareStatement(query)) {
        selectPreparedStatement.setString(1, uuid);
        try (ResultSet resultSet = selectPreparedStatement.executeQuery()) {
            if (resultSet.next()) {
                blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
                blockCondition.setConditionType(resultSet.getString("TYPE"));
                blockCondition.setConditionValue(resultSet.getString("VALUE"));
                blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
                blockCondition.setUuid(resultSet.getString("UUID"));
                if (blockCondition.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
                    String ipQuery = "SELECT STARTING_IP, ENDING_IP FROM AM_IP_RANGE_CONDITION WHERE UUID = ?";
                    try (PreparedStatement selectIpStatement = connection.prepareStatement(ipQuery)) {
                        selectIpStatement.setString(1, uuid);
                        try (ResultSet rs = selectIpStatement.executeQuery()) {
                            if (rs.next()) {
                                blockCondition.setStartingIP(rs.getString("STARTING_IP"));
                                blockCondition.setEndingIP(rs.getString("ENDING_IP"));
                            }
                        }
                    }
                }
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting block condition by uuid " + uuid, e);
    }
    return blockCondition;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 14 with Condition

use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method setJWTClaimConditions.

/**
 * Add JWT claim conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
 * provided {@link Condition} array
 *
 * @param pipelineId Id of the pipeline
 * @param conditions condition array to populate
 * @throws SQLException
 */
private void setJWTClaimConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
    final String query = "SELECT " + "CLAIM_URI, " + "CLAIM_ATTRIB , IS_CLAIM_MAPPING " + "FROM " + "AM_JWT_CLAIM_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setInt(1, pipelineId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
                jwtClaimsCondition.setClaimUrl(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CLAIM_URI));
                jwtClaimsCondition.setAttribute(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CLAIM_ATTRIBUTE));
                jwtClaimsCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_CLAIM_MAPPING));
                conditions.add(jwtClaimsCondition);
            }
        }
    }
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 15 with Condition

use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method setIPCondition.

/**
 * Retrieve IP condition from pipeline
 *
 * @param pipelineId id of the pipeline to get ip condition
 * @param conditions condition list to add each ip condition
 * @param connection connection to db
 * @throws SQLException If error occurred while getting ip condition form db
 */
private void setIPCondition(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
    final String sqlQuery = "SELECT " + "STARTING_IP, " + "ENDING_IP, " + "SPECIFIC_IP,WITHIN_IP_RANGE " + "FROM " + "" + "AM_IP_CONDITION " + "WHERE " + "CONDITION_GROUP_ID = ? ";
    String startingIP;
    String endingIP;
    String specificIP;
    boolean invert;
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setInt(1, pipelineId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                startingIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_STARTING_IP);
                endingIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_ENDING_IP);
                specificIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_SPECIFIC_IP);
                invert = resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_WITHIN_IP_RANGE);
                if (specificIP != null && !"".equals(specificIP)) {
                    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
                    ipCondition.setSpecificIP(specificIP);
                    ipCondition.setInvertCondition(invert);
                    conditions.add(ipCondition);
                } else if (startingIP != null && !"".equals(startingIP)) {
                    /*
                     Assumes availability of starting ip means ip range is enforced.
                     Therefore availability of ending ip is not checked.
                    */
                    IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
                    ipRangeCondition.setStartingIP(startingIP);
                    ipRangeCondition.setEndingIP(endingIP);
                    ipRangeCondition.setInvertCondition(invert);
                    conditions.add(ipRangeCondition);
                }
            }
        }
    }
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

HashMap (java.util.HashMap)39 Test (org.junit.Test)32 Test (org.testng.annotations.Test)31 ArrayList (java.util.ArrayList)30 List (java.util.List)26 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)26 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)26 MessageContext (org.apache.synapse.MessageContext)25 PreparedStatement (java.sql.PreparedStatement)23 Map (java.util.Map)22 ResultSet (java.sql.ResultSet)20 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 Connection (java.sql.Connection)16 SQLException (java.sql.SQLException)16 TreeMap (java.util.TreeMap)16 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)15 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)15 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)15 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)15