Search in sources :

Example 1 with IPolicyChain

use of io.apiman.gateway.engine.policy.IPolicyChain 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 IPolicyChain

use of io.apiman.gateway.engine.policy.IPolicyChain 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 IPolicyChain

use of io.apiman.gateway.engine.policy.IPolicyChain in project apiman by apiman.

the class IPWhitelistPolicyTest method testApply.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPWhitelistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApply() {
    IPWhitelistPolicy policy = new IPWhitelistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    Object 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);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Success
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    // Failure
    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;
        }
    });
    chain = Mockito.mock(IPolicyChain.class);
    request.setRemoteAddr("9.8.7.6");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
}
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) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Example 4 with IPolicyChain

use of io.apiman.gateway.engine.policy.IPolicyChain in project apiman by apiman.

the class TimeRestrictedAccessPolicyTest method testApply.

// @Test
public void testApply() throws Exception {
    TimeRestrictedAccessPolicy policy = new TimeRestrictedAccessPolicy();
    ApiRequest request = new ApiRequest();
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination(path);
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    final PolicyFailure failure = createFailurePolicyObject(context);
    TimeRestrictedAccess rule = new TimeRestrictedAccess();
    ArrayList<TimeRestrictedAccess> elements = new ArrayList<>(2);
    TimeRestrictedAccessConfig configObj = new TimeRestrictedAccessConfig();
    elements.add(rule);
    configObj.setRules(elements);
    int dayOfWeek = new DateTime().getDayOfWeek();
    rule.setDayEnd(7);
    rule.setDayStart(dayOfWeek);
    rule.setTimeEnd(new DateTime().plusHours(2).toDate());
    rule.setTimeStart(new DateTime().minusHours(2).toDate());
    rule.setPathPattern("PathNotListed");
    configObj.setRules(elements);
    Object config = updateConfig(policy, configObj);
    // Successful requests
    policy.apply(request, context, config, chain);
    rule.setPathPattern(path);
    config = updateConfig(policy, configObj);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain, Mockito.times(2)).doApply(request);
    Mockito.verify(chain, Mockito.never()).doFailure(Mockito.<PolicyFailure>any());
    chain = Mockito.mock(IPolicyChain.class);
    // Failed requests
    rule.setDayEnd(dayOfWeek + 1);
    rule.setDayStart(dayOfWeek - 1);
    rule.setPathPattern(path);
    rule.setTimeEnd(new DateTime().plusHours(1).toDate());
    rule.setTimeStart(new Date());
    request.setDestination(path);
    config = updateConfig(policy, configObj);
    policy.apply(request, context, config, chain);
    rule.setDayEnd(1);
    rule.setDayStart(7);
    rule.setTimeEnd(new DateTime().plusHours(2).toDate());
    rule.setTimeStart(new DateTime().plusHours(1).toDate());
    config = updateConfig(policy, configObj);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain, Mockito.times(2)).doFailure(failure);
}
Also used : TimeRestrictedAccess(io.apiman.gateway.engine.policies.config.TimeRestrictedAccess) ArrayList(java.util.ArrayList) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) TimeRestrictedAccessConfig(io.apiman.gateway.engine.policies.config.TimeRestrictedAccessConfig) DateTime(org.joda.time.DateTime) Date(java.util.Date) IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure)

Example 5 with IPolicyChain

use of io.apiman.gateway.engine.policy.IPolicyChain in project apiman by apiman.

the class BasicAuthJDBCTest method testApplyJdbcWithRoles.

public void testApplyJdbcWithRoles(String json) throws Exception {
    // A live LDAP server is required to run this test!
    BasicAuthenticationPolicy policy = new BasicAuthenticationPolicy();
    BasicAuthenticationConfig 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);
    Mockito.when(context.getComponent(IJdbcComponent.class)).thenReturn(new DefaultJdbcComponent());
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Success
    request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, JDBC_PASSWORD));
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    Set<String> expectedRoles = new HashSet<>();
    expectedRoles.add("admin");
    expectedRoles.add("user");
    Mockito.verify(context).setAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, expectedRoles);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) BasicAuthenticationConfig(io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig) DefaultJdbcComponent(io.apiman.gateway.engine.impl.DefaultJdbcComponent) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) HashSet(java.util.HashSet)

Aggregations

ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)9 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)9 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)9 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)8 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)6 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)6 Test (org.junit.Test)6 DefaultJdbcComponent (io.apiman.gateway.engine.impl.DefaultJdbcComponent)2 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)2 IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)2 ArrayList (java.util.ArrayList)2 InMemoryRateLimiterComponent (io.apiman.gateway.engine.impl.InMemoryRateLimiterComponent)1 IgnoredResource (io.apiman.gateway.engine.policies.config.IgnoredResource)1 IgnoredResourcesConfig (io.apiman.gateway.engine.policies.config.IgnoredResourcesConfig)1 TimeRestrictedAccess (io.apiman.gateway.engine.policies.config.TimeRestrictedAccess)1 TimeRestrictedAccessConfig (io.apiman.gateway.engine.policies.config.TimeRestrictedAccessConfig)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DateTime (org.joda.time.DateTime)1