Search in sources :

Example 6 with JSONConfig

use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.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.ballerina.threatprotection.configurations.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.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.

the class ConfigureJsonAnalyzer method execute.

@Override
public BValue[] execute(Context context) {
    String event = getStringArgument(context, 0);
    // configure json analyzer
    BStruct jsonInfo = ((BStruct) getRefArgument(context, 0));
    if (jsonInfo != null) {
        String jsonPolicyId = jsonInfo.getStringField(0);
        switch(event) {
            case THREAT_PROTECTION_POLICY_ADD:
            case THREAT_PROTECTION_POLICY_UPDATE:
                String name = jsonInfo.getStringField(1);
                int propertyCount = (int) jsonInfo.getIntField(0);
                int stringLength = (int) jsonInfo.getIntField(1);
                int arrayElementCount = (int) jsonInfo.getIntField(2);
                int keyLength = (int) jsonInfo.getIntField(3);
                int maxJSONDepth = (int) jsonInfo.getIntField(4);
                JSONConfig jsonConfig = new JSONConfig();
                jsonConfig.setName(name);
                jsonConfig.setMaxPropertyCount(propertyCount);
                jsonConfig.setMaxStringLength(stringLength);
                jsonConfig.setMaxArrayElementCount(arrayElementCount);
                jsonConfig.setMaxKeyLength(keyLength);
                jsonConfig.setMaxJsonDepth(maxJSONDepth);
                // put into ConfigurationHolder
                ConfigurationHolder.addJsonConfig(jsonPolicyId, jsonConfig);
                break;
            case THREAT_PROTECTION_POLICY_DELETE:
                ConfigurationHolder.removeJsonConfig(jsonPolicyId);
                break;
            default:
                log.warn("Unknown event type for Threat Protection Policy. Event: " + event);
                break;
        }
    }
    return getBValues(new BBoolean(true));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BBoolean(org.ballerinalang.model.values.BBoolean) JSONConfig(org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig)

Example 9 with JSONConfig

use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.

the class JSONAnalyzerTestCase method testMaxFieldLengthPass.

@Test
public void testMaxFieldLengthPass() throws Exception {
    JSONConfig config = Mockito.mock(JSONConfig.class);
    Mockito.when(config.getMaxKeyLength()).thenReturn(5);
    JSONAnalyzer analyzer = new JSONAnalyzer();
    analyzer.configure(config);
    String jsonString = "{\"abcde\": [1, 2, 3, 4, 5]}";
    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 10 with JSONConfig

use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.

the class JSONAnalyzerTestCase method testMaxArrayElementCountFail.

@Test(expectedExceptions = APIMThreatAnalyzerException.class)
public void testMaxArrayElementCountFail() throws Exception {
    JSONConfig config = Mockito.mock(JSONConfig.class);
    Mockito.when(config.getMaxArrayElementCount()).thenReturn(5);
    JSONAnalyzer analyzer = new JSONAnalyzer();
    analyzer.configure(config);
    String jsonString = "{\"a\": [1, 2, 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)

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 BBoolean (org.ballerinalang.model.values.BBoolean)1 BStruct (org.ballerinalang.model.values.BStruct)1 BeforeTest (org.testng.annotations.BeforeTest)1 APIMThreatAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.APIMThreatAnalyzer)1 XMLAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.XMLAnalyzer)1 XMLConfig (org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.XMLConfig)1