Search in sources :

Example 6 with JSONConfig

use of org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig in project carbon-apimgt by wso2.

the class JSONAnalyzerTestCase method testMaxStringLengthInsideAnArrayFail.

@Test(expectedExceptions = APIMThreatAnalyzerException.class)
public void testMaxStringLengthInsideAnArrayFail() throws Exception {
    JSONConfig config = Mockito.mock(JSONConfig.class);
    Mockito.when(config.getMaxStringLength()).thenReturn(5);
    JSONAnalyzer analyzer = new JSONAnalyzer();
    analyzer.configure(config);
    String jsonString = "{\"abcdef\": [1, \"123456\", 3, 4, 5, 6]}";
    analyzer.analyze(jsonString, "/foo");
}
Also used : JSONAnalyzer(org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.JSONAnalyzer) JSONConfig(org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig) Test(org.testng.annotations.Test)

Example 7 with JSONConfig

use of org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig in project carbon-apimgt by wso2.

the class XMLAnalyzerTestCase method testConfigureAnalyzerException.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testConfigureAnalyzerException() throws Exception {
    XMLAnalyzer analyzer = new XMLAnalyzer();
    JSONConfig config = new JSONConfig();
    analyzer.configure(config);
}
Also used : JSONConfig(org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig) XMLAnalyzer(org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.XMLAnalyzer) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 8 with JSONConfig

use of org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig in project carbon-apimgt by wso2.

the class EnvironmentSpecificAPIPropertyDAO method getMGEnvironmentSpecificAPIPropertiesOfAPIs.

/**
 * Getting the api configs related MGs
 *
 * @param apiUuidS
 * @return
 * @throws APIManagementException
 */
private Map<String, Map<String, Environment>> getMGEnvironmentSpecificAPIPropertiesOfAPIs(List<String> apiUuidS) throws APIManagementException {
    final String query = EnvironmentSpecificAPIPropertyConstants.GET_ENVIRONMENT_SPECIFIC_API_PROPERTIES_BY_APIS_SQL.replaceAll("_API_ID_LIST_", String.join(",", Collections.nCopies(apiUuidS.size(), "?")));
    Map<String, Map<String, Environment>> apiEnvironmentMap = new HashMap<>();
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(query)) {
        int index = 1;
        for (String apiId : apiUuidS) {
            preparedStatement.setString(index++, apiId);
        }
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String envId = resultSet.getString(1);
                String envName = resultSet.getString(2);
                String apiId = resultSet.getString(3);
                JsonObject jsonConfig = null;
                try (InputStream propertyConfigBlob = resultSet.getBinaryStream(4)) {
                    if (propertyConfigBlob != null) {
                        String apiJsonConfig = APIMgtDBUtil.getStringFromInputStream(propertyConfigBlob);
                        jsonConfig = new Gson().fromJson(apiJsonConfig, JsonObject.class);
                    }
                }
                Map<String, Environment> environmentMap;
                Environment environment;
                if (apiEnvironmentMap.containsKey(apiId)) {
                    environmentMap = apiEnvironmentMap.get(apiId);
                } else {
                    environmentMap = new HashMap<>();
                    apiEnvironmentMap.put(apiId, environmentMap);
                }
                environment = new Environment();
                environment.setEnvId(envId);
                environment.setEnvName(envName);
                environment.setConfigs(jsonConfig);
                environmentMap.put(envName, environment);
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error occurred when getting MG environment specific api properties", e);
    }
    return apiEnvironmentMap;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ResultSet(java.sql.ResultSet) Environment(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with JSONConfig

use of org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig in project carbon-apimgt by wso2.

the class EnvironmentSpecificAPIPropertyDAO method getDefaultEnvironmentSpecificAPIPropertiesOfAPIs.

/**
 * Getting the api configs related to default environments.
 *
 * @param apiUuidS
 * @param envIds
 * @param apiEnvironmentMap
 * @return
 * @throws APIManagementException
 */
private Map<String, Map<String, Environment>> getDefaultEnvironmentSpecificAPIPropertiesOfAPIs(List<String> apiUuidS, List<String> envIds, Map<String, Map<String, Environment>> apiEnvironmentMap) throws APIManagementException {
    final String query = EnvironmentSpecificAPIPropertyConstants.GET_ENVIRONMENT_SPECIFIC_API_PROPERTIES_BY_APIS_ENVS_SQL.replaceAll("_ENV_ID_LIST_", String.join(",", Collections.nCopies(envIds.size(), "?"))).replaceAll("_API_ID_LIST_", String.join(",", Collections.nCopies(apiUuidS.size(), "?")));
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(query)) {
        int index = 1;
        for (String envId : envIds) {
            preparedStatement.setString(index++, envId);
        }
        for (String apiId : apiUuidS) {
            preparedStatement.setString(index++, apiId);
        }
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String envId = resultSet.getString(1);
                // for default envs envId and envName is same
                String envName = envId;
                String apiId = resultSet.getString(2);
                JsonObject jsonConfig = null;
                try (InputStream propertyConfigBlob = resultSet.getBinaryStream(3)) {
                    if (propertyConfigBlob != null) {
                        String apiJsonConfig = APIMgtDBUtil.getStringFromInputStream(propertyConfigBlob);
                        jsonConfig = new Gson().fromJson(apiJsonConfig, JsonObject.class);
                    }
                }
                Map<String, Environment> environmentMap;
                Environment environment;
                if (apiEnvironmentMap.containsKey(apiId)) {
                    environmentMap = apiEnvironmentMap.get(apiId);
                } else {
                    environmentMap = new HashMap<>();
                    apiEnvironmentMap.put(apiId, environmentMap);
                }
                environment = new Environment();
                environment.setEnvId(envId);
                environment.setEnvName(envName);
                environment.setConfigs(jsonConfig);
                environmentMap.put(envName, environment);
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error occurred when getting default environment specific api properties", e);
    }
    return apiEnvironmentMap;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ResultSet(java.sql.ResultSet) Environment(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment)

Example 10 with JSONConfig

use of org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig in project carbon-apimgt by wso2.

the class JsonSchemaValidator method configureSchemaProperties.

/**
 * This method binds the properties of the json validator sequence with the JsonConfig object.
 *
 * @param messageContext This message context contains the request message properties of the relevant
 *                       API which was enabled the JSON_Validator message mediation in flow.
 * @return JSONConfig contains the json schema properties need to be validated.
 */
public JSONConfig configureSchemaProperties(MessageContext messageContext) {
    Object messageProperty;
    int propertyCount = 0;
    int stringLength = 0;
    int arrayElementCount = 0;
    int keyLength = 0;
    int maxJSONDepth = 0;
    messageProperty = messageContext.getProperty(ThreatProtectorConstants.MAX_PROPERTY_COUNT);
    if (messageProperty != null) {
        propertyCount = Integer.parseInt(messageProperty.toString());
    } else {
        String errorMessage = "Json schema maxProperty count is missing.";
        ThreatExceptionHandler.handleException(messageContext, errorMessage);
    }
    messageProperty = messageContext.getProperty(ThreatProtectorConstants.MAX_STRING_LENGTH);
    if (messageProperty != null) {
        stringLength = Integer.parseInt(messageProperty.toString());
    } else {
        String errorMessage = "Json schema Max String length is missing";
        ThreatExceptionHandler.handleException(messageContext, errorMessage);
    }
    messageProperty = messageContext.getProperty(ThreatProtectorConstants.MAX_ARRAY_ELEMENT_COUNT);
    if (messageProperty != null) {
        arrayElementCount = Integer.parseInt(messageProperty.toString());
    } else {
        String errorMessage = "Json schema max array element count is missing";
        ThreatExceptionHandler.handleException(messageContext, errorMessage);
    }
    messageProperty = messageContext.getProperty(ThreatProtectorConstants.MAX_KEY_LENGTH);
    if (messageProperty != null) {
        keyLength = Integer.parseInt(messageProperty.toString());
    } else {
        String errorMessage = "Json schema maximum key length is missing";
        ThreatExceptionHandler.handleException(messageContext, errorMessage);
    }
    messageProperty = messageContext.getProperty(ThreatProtectorConstants.MAX_JSON_DEPTH);
    if (messageProperty != null) {
        maxJSONDepth = Integer.parseInt(messageProperty.toString());
    } else {
        String errorMessage = "Json schema maximum JSON depth is missing";
        ThreatExceptionHandler.handleException(messageContext, errorMessage);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(("Max Priority count is:" + propertyCount) + ", " + "Max String length is: " + stringLength + ", " + "Max Array element count: " + arrayElementCount + ", " + "Max Key Length: " + keyLength + ", " + "Max JSON depth is:" + maxJSONDepth + ", ");
    }
    JSONConfig jsonConfig = new JSONConfig();
    jsonConfig.setMaxPropertyCount(propertyCount);
    jsonConfig.setMaxStringLength(stringLength);
    jsonConfig.setMaxArrayElementCount(arrayElementCount);
    jsonConfig.setMaxKeyLength(keyLength);
    jsonConfig.setMaxJsonDepth(maxJSONDepth);
    return jsonConfig;
}
Also used : JSONConfig(org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig)

Aggregations

JSONConfig (org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig)15 Test (org.testng.annotations.Test)13 JSONAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.JSONAnalyzer)12 JSONConfig (org.wso2.carbon.apimgt.gateway.threatprotection.configuration.JSONConfig)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 APIMThreatAnalyzer (org.wso2.carbon.apimgt.gateway.threatprotection.analyzer.APIMThreatAnalyzer)2 Environment (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment)2 BufferedInputStream (java.io.BufferedInputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 BBoolean (org.ballerinalang.model.values.BBoolean)1