Search in sources :

Example 66 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class AmbrySecurityServiceTest method preProcessRequestTest.

/**
 * Tests for {@link AmbrySecurityService#preProcessRequest(RestRequest, Callback)}
 * @throws Exception
 */
@Test
public void preProcessRequestTest() throws Exception {
    RestMethod[] methods = new RestMethod[] { RestMethod.POST, RestMethod.GET, RestMethod.DELETE, RestMethod.HEAD, RestMethod.OPTIONS, RestMethod.PUT };
    for (RestMethod restMethod : methods) {
        // add a header that is prohibited
        JSONObject headers = new JSONObject();
        headers.put(RestUtils.InternalKeys.KEEP_ALIVE_ON_ERROR_HINT, true);
        RestRequest restRequest = createRestRequest(restMethod, "/", headers);
        try {
            securityService.preProcessRequest(restRequest).get(1, TimeUnit.SECONDS);
            Assert.fail("Should have failed because the request contains a prohibited header: " + RestUtils.InternalKeys.KEEP_ALIVE_ON_ERROR_HINT);
        } catch (ExecutionException e) {
            RestServiceException rse = (RestServiceException) Utils.getRootCause(e);
            Assert.assertEquals("Should be a bad request", RestServiceErrorCode.BadRequest, rse.getErrorCode());
        }
    }
    // verify request args regarding to tracking is set accordingly
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null);
    securityService.preProcessRequest(restRequest).get();
    Assert.assertTrue("The arg with key: ambry-internal-keys-send-tracking-info should be set to true", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_TRACKING_INFO));
    Properties properties = new Properties();
    properties.setProperty("frontend.attach.tracking.info", "false");
    FrontendConfig frontendConfig = new FrontendConfig(new VerifiableProperties(properties));
    SecurityService securityServiceWithTrackingDisabled = new AmbrySecurityService(frontendConfig, new FrontendMetrics(new MetricRegistry()), URL_SIGNING_SERVICE_FACTORY.getUrlSigningService(), hostLevelThrottler, QUOTA_MANAGER);
    restRequest = createRestRequest(RestMethod.GET, "/", null);
    securityServiceWithTrackingDisabled.preProcessRequest(restRequest);
    Assert.assertFalse("The arg with key: ambry-internal-keys-send-tracking-info should be set to false", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_TRACKING_INFO));
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) BlobProperties(com.github.ambry.messageformat.BlobProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RestServiceException(com.github.ambry.rest.RestServiceException) FrontendConfig(com.github.ambry.config.FrontendConfig) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) ExecutionException(java.util.concurrent.ExecutionException) RestMethod(com.github.ambry.rest.RestMethod) Test(org.junit.Test)

Example 67 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class HostLevelThrottlerTest method quotaTest.

/**
 * Test to make sure {@link HostLevelThrottler#shouldThrottle(RestRequest)} works as expected.
 */
@Test
public void quotaTest() throws Exception {
    Properties props = new Properties();
    props.setProperty(HostThrottleConfig.REST_REQUEST_QUOTA_STRING, "{\"PUT\": \"20\",\"GET\": \"20\",\"POST\": \"20\",\"HEAD\": \"20\",\"OPTIONS\": \"20\",\"DELETE\": \"20\"}");
    HostThrottleConfig hostThrottleConfig = new HostThrottleConfig(new VerifiableProperties(props));
    MockClock clock = new MockClock();
    HostLevelThrottler quotaManager = new HostLevelThrottler(createQuotaMock(hostThrottleConfig, clock), new HashMap<>(), null);
    // Issue new requests. Since MockClock tick doesn't change, rate is 0.
    for (int i = 0; i < 100; i++) {
        for (RestMethod restMethod : RestMethod.values()) {
            RestRequest restRequest = createRestRequest(restMethod, "http://www.linkedin.com/");
            Assert.assertFalse("Should not throttle", quotaManager.shouldThrottle(restRequest));
        }
    }
    // Move MockClock ahead to 5 seconds later. Rate = 20. New requests should be denied unless its quota is not defined.
    clock.tick(5);
    for (RestMethod restMethod : RestMethod.values()) {
        RestRequest restRequest = createRestRequest(restMethod, "http://www.linkedin.com/");
        if (restMethod == RestMethod.UNKNOWN) {
            Assert.assertFalse("Should not throttle.", quotaManager.shouldThrottle(restRequest));
        } else {
            Assert.assertTrue("Should throttle", quotaManager.shouldThrottle(restRequest));
        }
    }
    // Clock tick to another 5 seconds later, rate < 20. Accept new requests.
    clock.tick(5);
    for (RestMethod restMethod : RestMethod.values()) {
        RestRequest restRequest = createRestRequest(restMethod, "http://www.linkedin.com/");
        Assert.assertFalse("Should not throttle", quotaManager.shouldThrottle(restRequest));
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) HostThrottleConfig(com.github.ambry.config.HostThrottleConfig) MockClock(com.github.ambry.utils.MockClock) RestMethod(com.github.ambry.rest.RestMethod) Test(org.junit.Test)

Example 68 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class PostBlobHandlerTest method initPostBlobHandler.

// helpers
// general
/**
 * Initates a {@link PostBlobHandler}
 * @param properties the properties to use to init the {@link PostBlobHandler}
 */
private void initPostBlobHandler(Properties properties) {
    VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
    frontendConfig = new FrontendConfig(verifiableProperties);
    postBlobHandler = new PostBlobHandler(securityServiceFactory.getSecurityService(), idConverterFactory.getIdConverter(), idSigningService, router, injector, time, frontendConfig, metrics, CLUSTER_NAME, QUOTA_MANAGER);
}
Also used : FrontendConfig(com.github.ambry.config.FrontendConfig) VerifiableProperties(com.github.ambry.config.VerifiableProperties)

Example 69 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method runtimeExceptionRouterTest.

/**
 * Checks reactions of all methods in {@link FrontendRestRequestService} to a {@link Router} that throws
 * {@link RuntimeException}.
 * @throws Exception
 */
@Test
public void runtimeExceptionRouterTest() throws Exception {
    // set InMemoryRouter up to throw RuntimeException
    Properties properties = new Properties();
    properties.setProperty(InMemoryRouter.OPERATION_THROW_EARLY_RUNTIME_EXCEPTION, "true");
    router.setVerifiableProperties(new VerifiableProperties(properties));
    doRuntimeExceptionRouterTest(RestMethod.GET);
    doRuntimeExceptionRouterTest(RestMethod.POST);
    doRuntimeExceptionRouterTest(RestMethod.DELETE);
    doRuntimeExceptionRouterTest(RestMethod.HEAD);
// PUT is tested in the individual handlers
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) BlobProperties(com.github.ambry.messageformat.BlobProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 70 with VerifiableProperties

use of com.github.ambry.config.VerifiableProperties in project ambry by linkedin.

the class UndeleteHandlerTest method routerFailureTest.

/**
 * Tests the case where the {@link com.github.ambry.router.Router} fails or returns an exception
 * @throws Exception
 */
private void routerFailureTest() throws Exception {
    setupBlob();
    // get the router to throw a RuntimeException
    Properties properties = new Properties();
    properties.setProperty(InMemoryRouter.OPERATION_THROW_EARLY_RUNTIME_EXCEPTION, "true");
    router.setVerifiableProperties(new VerifiableProperties(properties));
    verifyFailureWithMsg(InMemoryRouter.OPERATION_THROW_EARLY_RUNTIME_EXCEPTION);
    // get the router to return a RuntimeException
    properties = new Properties();
    properties.setProperty(InMemoryRouter.OPERATION_THROW_LATE_RUNTIME_EXCEPTION, "true");
    router.setVerifiableProperties(new VerifiableProperties(properties));
    verifyFailureWithMsg(InMemoryRouter.OPERATION_THROW_LATE_RUNTIME_EXCEPTION);
    router.setVerifiableProperties(new VerifiableProperties(new Properties()));
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) BlobProperties(com.github.ambry.messageformat.BlobProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties)

Aggregations

VerifiableProperties (com.github.ambry.config.VerifiableProperties)335 Properties (java.util.Properties)219 Test (org.junit.Test)192 MetricRegistry (com.codahale.metrics.MetricRegistry)131 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)80 ArrayList (java.util.ArrayList)62 BlobProperties (com.github.ambry.messageformat.BlobProperties)61 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)52 StoreConfig (com.github.ambry.config.StoreConfig)51 IOException (java.io.IOException)47 File (java.io.File)39 RouterConfig (com.github.ambry.config.RouterConfig)37 ClusterMap (com.github.ambry.clustermap.ClusterMap)36 JSONObject (org.json.JSONObject)34 HashMap (java.util.HashMap)33 BlobId (com.github.ambry.commons.BlobId)31 LoggingNotificationSystem (com.github.ambry.commons.LoggingNotificationSystem)31 CountDownLatch (java.util.concurrent.CountDownLatch)31 Map (java.util.Map)28 InMemAccountService (com.github.ambry.account.InMemAccountService)26