Search in sources :

Example 1 with PolicyFailureType

use of io.apiman.gateway.engine.beans.PolicyFailureType 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 PolicyFailureType

use of io.apiman.gateway.engine.beans.PolicyFailureType 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 PolicyFailureType

use of io.apiman.gateway.engine.beans.PolicyFailureType 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 PolicyFailureType

use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.

the class AuthorizationPolicyTest method doTest.

private void doTest(String json, HashSet<String> userRoles, String verb, String path, boolean shouldSucceed) {
    AuthorizationPolicy policy = new AuthorizationPolicy();
    Object config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType(verb);
    request.setDestination(path);
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    Mockito.when(context.getAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, (HashSet<String>) null)).thenReturn(userRoles);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            failure.setFailureCode(failureCode);
            failure.setType(type);
            failure.setMessage(message);
            return failure;
        }
    });
    // Success
    policy.apply(request, context, config, chain);
    if (shouldSucceed) {
        Mockito.verify(chain).doApply(request);
    } else {
        Mockito.verify(chain).doFailure(failure);
    }
}
Also used : 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)

Example 5 with PolicyFailureType

use of io.apiman.gateway.engine.beans.PolicyFailureType in project apiman by apiman.

the class LdapTestMixin method doTest.

// pass null if you expect success
default void doTest(String json, String username, String password, Integer expectedFailureCode, Set<String> expectedRoles, ILdapComponent ldapComponentUnderTest) {
    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);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn((PolicyFailureType type, int failureCode, String message) -> {
        failure.setType(type);
        failure.setFailureCode(failureCode);
        failure.setMessage(message);
        return failure;
    });
    // The LDAP stuff we're testing!
    Mockito.when(context.getComponent(ILdapComponent.class)).thenReturn(ldapComponentUnderTest);
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    if (username != null) {
        request.getHeaders().put("Authorization", createBasicAuthorization(username, password));
    }
    if (expectedFailureCode == null) {
        policy.apply(request, context, config, chain);
        Mockito.verify(chain).doApply(request);
    } else {
        policy.apply(request, context, config, chain);
        Mockito.verify(chain).doFailure(failure);
        Assert.assertEquals(expectedFailureCode.intValue(), failure.getFailureCode());
    }
    if (expectedRoles != null && expectedFailureCode == null) {
        Mockito.verify(context).setAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, expectedRoles);
    }
}
Also used : IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) BasicAuthenticationPolicy(io.apiman.gateway.engine.policies.BasicAuthenticationPolicy) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) BasicAuthenticationConfig(io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest)

Aggregations

PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)9 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)9 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)8 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)8 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)8 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)6 Test (org.junit.Test)5 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)2 IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)2 DefaultJdbcComponent (io.apiman.gateway.engine.impl.DefaultJdbcComponent)1 InMemoryRateLimiterComponent (io.apiman.gateway.engine.impl.InMemoryRateLimiterComponent)1 BasicAuthenticationPolicy (io.apiman.gateway.engine.policies.BasicAuthenticationPolicy)1