Search in sources :

Example 16 with PolicyFailure

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

the class TransferQuotaPolicy method limitExceededFailure.

protected PolicyFailure limitExceededFailure(IPolicyFailureFactoryComponent failureFactory) {
    PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, // $NON-NLS-1$
    Messages.i18n.format("TransferQuotaPolicy.RateExceeded"));
    failure.setResponseCode(429);
    return failure;
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure)

Example 17 with PolicyFailure

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

the class BasicAuthenticationPolicyTest method testStatic.

@Test
@Configuration("{\r\n" + "    \"realm\" : \"TestRealm\",\r\n" + "    \"forwardIdentityHttpHeader\" : \"X-Authenticated-Identity\",\r\n" + "    \"staticIdentity\" : {\r\n" + "      \"identities\" : [\r\n" + "        { \"username\" : \"ckent\", \"password\" : \"ckent123!\" },\r\n" + "        { \"username\" : \"bwayne\", \"password\" : \"bwayne123!\" },\r\n" + "        { \"username\" : \"dprince\", \"password\" : \"dprince123!\" }\r\n" + "      ]\r\n" + "    }\r\n" + "}")
public void testStatic() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    // Failure
    try {
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authentication, policyFailure.getType());
        Assert.assertEquals(10004, policyFailure.getFailureCode());
    }
    // Failure
    try {
        request.basicAuth("ckent", "invalid_password");
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authentication, policyFailure.getType());
        Assert.assertEquals(10003, policyFailure.getFailureCode());
    }
    // Success
    request.basicAuth("ckent", "ckent123!");
    PolicyTestResponse response = send(request);
    Assert.assertEquals(200, response.code());
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    String header = echo.getHeaders().get("X-Authenticated-Identity");
    Assert.assertNotNull(header);
    Assert.assertEquals("ckent", header);
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 18 with PolicyFailure

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

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

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

Aggregations

PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)54 Test (org.junit.Test)26 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)19 ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)19 Configuration (io.apiman.test.policies.Configuration)19 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)19 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)19 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)11 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)11 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)11 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)9 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)8 EchoResponse (io.apiman.test.common.mock.EchoResponse)7 HashSet (java.util.HashSet)6 BackEndApi (io.apiman.test.policies.BackEndApi)4 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)4 IRateLimiterComponent (io.apiman.gateway.engine.components.IRateLimiterComponent)2 RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)2 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)2 IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)2