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));
}
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));
}
}
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);
}
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
}
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()));
}
Aggregations