Search in sources :

Example 1 with IPListConfig

use of io.apiman.gateway.engine.policies.config.IPListConfig in project apiman by apiman.

the class IPBlacklistPolicyTest method testApply.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApply() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    IPListConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failure
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Success
    request.setRemoteAddr("9.8.7.6");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Example 2 with IPListConfig

use of io.apiman.gateway.engine.policies.config.IPListConfig in project apiman by apiman.

the class IPBlacklistPolicyTest method testApplyWithWildcards.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApplyWithWildcards() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"10.0.*.*\"" + "  ]" + "}";
    IPListConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failures
    request.setRemoteAddr("10.0.123.87");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    request.setRemoteAddr("10.0.97.1");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Successes
    request.setRemoteAddr("9.8.7.6");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    request.setRemoteAddr("10.10.15.123");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Example 3 with IPListConfig

use of io.apiman.gateway.engine.policies.config.IPListConfig in project apiman by apiman.

the class IPBlacklistPolicyTest method testParseConfiguration.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testParseConfiguration() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    // Empty config test
    String config = "{}";
    Object parsed = policy.parseConfiguration(config);
    Assert.assertNotNull(parsed);
    Assert.assertEquals(IPListConfig.class, parsed.getClass());
    IPListConfig parsedConfig = (IPListConfig) parsed;
    Assert.assertNotNull(parsedConfig.getIpList());
    Assert.assertTrue(parsedConfig.getIpList().isEmpty());
    // Single IP address
    config = "{" + "  \"httpHeader\" : null," + "  \"ipList\" : [" + "    \"1.2.3.4\"" + "  ]" + "}";
    parsed = policy.parseConfiguration(config);
    parsedConfig = (IPListConfig) parsed;
    Assert.assertNotNull(parsedConfig.getIpList());
    Assert.assertEquals(1, parsedConfig.getIpList().size());
    Assert.assertNull(parsedConfig.getHttpHeader());
    Assert.assertEquals("1.2.3.4", parsedConfig.getIpList().iterator().next());
    // Multiple IP addresses
    config = "{" + "  \"httpHeader\" : \"X-Forwarded-For\"," + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    parsed = policy.parseConfiguration(config);
    parsedConfig = (IPListConfig) parsed;
    Assert.assertNotNull(parsedConfig.getIpList());
    Assert.assertEquals("X-Forwarded-For", parsedConfig.getHttpHeader());
    Assert.assertEquals(3, parsedConfig.getIpList().size());
}
Also used : IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) Test(org.junit.Test)

Example 4 with IPListConfig

use of io.apiman.gateway.engine.policies.config.IPListConfig in project apiman by apiman.

the class IPWhitelistPolicyTest method testParseConfiguration.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPWhitelistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testParseConfiguration() {
    IPWhitelistPolicy policy = new IPWhitelistPolicy();
    // Empty config test
    String config = "{}";
    Object parsed = policy.parseConfiguration(config);
    Assert.assertNotNull(parsed);
    Assert.assertEquals(IPListConfig.class, parsed.getClass());
    IPListConfig parsedConfig = (IPListConfig) parsed;
    Assert.assertNotNull(parsedConfig.getIpList());
    Assert.assertTrue(parsedConfig.getIpList().isEmpty());
    // Single IP address
    config = "{" + "  \"httpHeader\" : null," + "  \"responseCode\" : 403," + "  \"ipList\" : [" + "    \"1.2.3.4\"" + "  ]" + "}";
    parsed = policy.parseConfiguration(config);
    parsedConfig = (IPListConfig) parsed;
    Assert.assertNotNull(parsedConfig.getIpList());
    Assert.assertEquals(1, parsedConfig.getIpList().size());
    Assert.assertNull(parsedConfig.getHttpHeader());
    Assert.assertEquals("1.2.3.4", parsedConfig.getIpList().iterator().next());
    Assert.assertEquals(403, parsedConfig.getResponseCode());
    // Multiple IP addresses
    config = "{" + "  \"httpHeader\" : \"X-Forwarded-For\"," + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    parsed = policy.parseConfiguration(config);
    parsedConfig = (IPListConfig) parsed;
    Assert.assertNotNull(parsedConfig.getIpList());
    Assert.assertEquals("X-Forwarded-For", parsedConfig.getHttpHeader());
    Assert.assertEquals(3, parsedConfig.getIpList().size());
}
Also used : IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) Test(org.junit.Test)

Aggregations

IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)4 Test (org.junit.Test)4 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)2 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)2 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)2 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)2 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)2 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)2