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");
}
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);
}
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;
}
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;
}
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;
}
Aggregations