Search in sources :

Example 6 with ThrowingConsumer

use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.

the class GetAccountsHandlerTest method securityServiceDenialTest.

/**
 * Tests the case where the {@link SecurityService} denies the request.
 * @throws Exception
 */
@Test
public void securityServiceDenialTest() throws Exception {
    IllegalStateException injectedException = new IllegalStateException("@@expected");
    TestUtils.ThrowingRunnable testAction = () -> sendRequestGetResponse(createRestRequest(null, null, null, Operations.ACCOUNTS), new MockRestResponseChannel());
    ThrowingConsumer<IllegalStateException> errorChecker = e -> assertEquals("Wrong exception", injectedException, e);
    securityServiceFactory.exceptionToReturn = injectedException;
    securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.ProcessRequest;
    TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
    securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.PostProcessRequest;
    TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
    securityServiceFactory.exceptionToThrow = injectedException;
    securityServiceFactory.exceptionToReturn = null;
    securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.ProcessRequest;
    TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
    securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.PostProcessRequest;
    TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FutureResult(com.github.ambry.router.FutureResult) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) HashSet(java.util.HashSet) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) RestMethod(com.github.ambry.rest.RestMethod) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) TestUtils(com.github.ambry.utils.TestUtils) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 7 with ThrowingConsumer

use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.

the class AmbryServerSecurityServiceTest method validateConnectionTest.

/**
 * Tests for {@link AmbryServerSecurityService#validateConnection(SSLSession, Callback)}
 * @throws Exception
 */
@Test
public void validateConnectionTest() throws Exception {
    // sslSession is null
    TestUtils.assertException(IllegalArgumentException.class, () -> serverSecurityService.validateConnection(null).get(), null);
    // success case
    SSLSession sslSession = Mockito.mock(SSLSession.class);
    serverSecurityService.validateConnection(sslSession, (r, e) -> {
        Assert.assertNull("result not null", r);
        Assert.assertNull("exception not null", e);
    });
    // service is closed
    serverSecurityService.close();
    ThrowingConsumer<ExecutionException> errorAction = e -> {
        Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
        RestServiceException re = (RestServiceException) e.getCause();
        Assert.assertEquals("Unexpected RestServerErrorCode (Future)", RestServiceErrorCode.ServiceUnavailable, re.getErrorCode());
    };
    TestUtils.assertException(ExecutionException.class, () -> serverSecurityService.validateConnection(sslSession).get(), errorAction);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) ServerSecurityService(com.github.ambry.rest.ServerSecurityService) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) ServerConfig(com.github.ambry.config.ServerConfig) Test(org.junit.Test) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) ExecutionException(java.util.concurrent.ExecutionException) AmbryRequests(com.github.ambry.protocol.AmbryRequests) Mockito(org.mockito.Mockito) RestServiceException(com.github.ambry.rest.RestServiceException) SSLSession(javax.net.ssl.SSLSession) TestUtils(com.github.ambry.utils.TestUtils) ServerMetrics(com.github.ambry.commons.ServerMetrics) Callback(com.github.ambry.commons.Callback) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) RestServiceException(com.github.ambry.rest.RestServiceException) SSLSession(javax.net.ssl.SSLSession) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 8 with ThrowingConsumer

use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.

the class AmbrySecurityServiceTest method testExceptionCasesProcessResponse.

/**
 * Tests exception cases for
 * {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)}
 * @param restMethod the {@link RestMethod} of the request to be made
 * @param restResponseChannel the {@link RestResponseChannel} to write responses over.
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param expectedErrorCode the {@link RestServiceErrorCode} expected in the exception returned.
 * @throws Exception
 */
private void testExceptionCasesProcessResponse(RestMethod restMethod, RestResponseChannel restResponseChannel, BlobInfo blobInfo, RestServiceErrorCode expectedErrorCode) throws Exception {
    ThrowingConsumer<ExecutionException> errorAction = e -> {
        Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
        RestServiceException re = (RestServiceException) e.getCause();
        Assert.assertEquals("Unexpected RestServerErrorCode (Future)", expectedErrorCode, re.getErrorCode());
    };
    RestRequest restRequest = createRestRequest(restMethod, "/", null);
    TestUtils.assertException(ExecutionException.class, () -> securityService.processResponse(restRequest, restResponseChannel, blobInfo).get(), errorAction);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) BlobProperties(com.github.ambry.messageformat.BlobProperties) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ResponseStatus(com.github.ambry.rest.ResponseStatus) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) HostThrottleConfig(com.github.ambry.config.HostThrottleConfig) TestUtils(com.github.ambry.utils.TestUtils) Locale(java.util.Locale) Map(java.util.Map) Container(com.github.ambry.account.Container) TimeZone(java.util.TimeZone) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Utils(com.github.ambry.utils.Utils) BlobInfo(com.github.ambry.messageformat.BlobInfo) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) QuotaTestUtils(com.github.ambry.quota.QuotaTestUtils) Account(com.github.ambry.account.Account) Callback(com.github.ambry.commons.Callback) RestUtils(com.github.ambry.rest.RestUtils) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InMemAccountService(com.github.ambry.account.InMemAccountService) ByteRanges(com.github.ambry.router.ByteRanges) SimpleDateFormat(java.text.SimpleDateFormat) AccountService(com.github.ambry.account.AccountService) HashMap(java.util.HashMap) RestTestUtils(com.github.ambry.rest.RestTestUtils) QuotaManager(com.github.ambry.quota.QuotaManager) QuotaConfig(com.github.ambry.config.QuotaConfig) RequestPath(com.github.ambry.rest.RequestPath) QuotaMode(com.github.ambry.quota.QuotaMode) HostLevelThrottler(com.github.ambry.commons.HostLevelThrottler) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Pair(com.github.ambry.utils.Pair) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ByteRange(com.github.ambry.router.ByteRange) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) InMemAccountServiceFactory(com.github.ambry.account.InMemAccountServiceFactory) AmbryQuotaManager(com.github.ambry.quota.AmbryQuotaManager) Mockito(org.mockito.Mockito) QuotaMetrics(com.github.ambry.quota.QuotaMetrics) SimpleQuotaRecommendationMergePolicy(com.github.ambry.quota.SimpleQuotaRecommendationMergePolicy) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with ThrowingConsumer

use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.

the class PostAccountsHandlerTest method validRequestsTest.

/**
 * Test valid request cases.
 * @throws Exception
 */
@Test
public void validRequestsTest() throws Exception {
    ThrowingConsumer<Collection<Account>> testAction = accountsToUpdate -> {
        String requestBody = new String(AccountCollectionSerde.serializeAccountsInJson(accountsToUpdate));
        RestResponseChannel restResponseChannel = new MockRestResponseChannel();
        sendRequestGetResponse(requestBody, restResponseChannel);
        assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
        assertEquals("Content-length is not as expected", 0, Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        for (Account account : accountsToUpdate) {
            assertEquals("Account in account service not as expected", account, accountService.getAccountById(account.getId()));
        }
    };
    testAction.accept(Collections.emptyList());
    // add new account
    testAction.accept(Collections.singleton(accountService.generateRandomAccount()));
    // update multiple accounts
    testAction.accept(IntStream.range(0, 3).mapToObj(i -> {
        Account account = accountService.createAndAddRandomAccount();
        return new AccountBuilder(account).name(account.getName() + i).build();
    }).collect(Collectors.toList()));
}
Also used : IntStream(java.util.stream.IntStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) FutureResult(com.github.ambry.router.FutureResult) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) LinkedList(java.util.LinkedList) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) AccountBuilder(com.github.ambry.account.AccountBuilder) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) Account(com.github.ambry.account.Account) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Collection(java.util.Collection) AccountBuilder(com.github.ambry.account.AccountBuilder) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 10 with ThrowingConsumer

use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.

the class FrontendRestRequestService method handleGet.

@Override
public void handleGet(final RestRequest restRequest, final RestResponseChannel restResponseChannel) {
    ThrowingConsumer<RequestPath> routingAction = requestPath -> {
        if (requestPath.matchesOperation(Operations.GET_PEERS)) {
            getPeersHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
        } else if (requestPath.matchesOperation(Operations.GET_CLUSTER_MAP_SNAPSHOT)) {
            getClusterMapSnapshotHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
        } else if (requestPath.matchesOperation(Operations.GET_SIGNED_URL)) {
            getSignedUrlHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
        } else if (requestPath.matchesOperation(Operations.ACCOUNTS)) {
            getAccountsHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
        } else if (requestPath.matchesOperation(Operations.STATS_REPORT)) {
            getStatsReportHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
        } else if (requestPath.matchesOperation(Operations.NAMED_BLOB) && NamedBlobPath.parse(requestPath, restRequest.getArgs()).getBlobName() == null) {
            listNamedBlobsHandler.handle(restRequest, restResponseChannel, ((result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception)));
        } else {
            SubResource subResource = requestPath.getSubResource();
            GetBlobOptions options = buildGetBlobOptions(restRequest.getArgs(), subResource, getGetOption(restRequest, frontendConfig.defaultRouterGetOption), restRequest, requestPath.getBlobSegmentIdx());
            GetCallback routerCallback = new GetCallback(restRequest, restResponseChannel, subResource, options);
            SecurityProcessRequestCallback securityCallback = new SecurityProcessRequestCallback(restRequest, restResponseChannel, routerCallback);
            if (subResource == SubResource.Replicas) {
                securityCallback = new SecurityProcessRequestCallback(restRequest, restResponseChannel);
            }
            RestRequestMetricsGroup metricsGroup = getMetricsGroupForGet(frontendMetrics, subResource);
            RestRequestMetrics restRequestMetrics = metricsGroup.getRestRequestMetrics(restRequest.isSslUsed(), false);
            restRequest.getMetricsTracker().injectMetrics(restRequestMetrics);
            // named blob requests have their account/container in the URI, so checks can be done prior to ID conversion.
            if (requestPath.matchesOperation(Operations.NAMED_BLOB)) {
                accountAndContainerInjector.injectAccountAndContainerForNamedBlob(restRequest, metricsGroup);
            }
            securityService.processRequest(restRequest, securityCallback);
        }
    };
    preProcessAndRouteRequest(restRequest, restResponseChannel, frontendMetrics.getPreProcessingMetrics, routingAction);
}
Also used : RequestPath(com.github.ambry.rest.RequestPath) Histogram(com.codahale.metrics.Histogram) GetOption(com.github.ambry.protocol.GetOption) FrontendConfig(com.github.ambry.config.FrontendConfig) ResponseStatus(com.github.ambry.rest.ResponseStatus) LoggerFactory(org.slf4j.LoggerFactory) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) AccountService(com.github.ambry.account.AccountService) QuotaManager(com.github.ambry.quota.QuotaManager) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) RequestPath(com.github.ambry.rest.RequestPath) NamedBlobDb(com.github.ambry.named.NamedBlobDb) RestRequestService(com.github.ambry.rest.RestRequestService) RestRequestMetrics(com.github.ambry.rest.RestRequestMetrics) SystemTime(com.github.ambry.utils.SystemTime) Router(com.github.ambry.router.Router) RouterErrorCode(com.github.ambry.router.RouterErrorCode) RestResponseHandler(com.github.ambry.rest.RestResponseHandler) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) Logger(org.slf4j.Logger) GregorianCalendar(java.util.GregorianCalendar) RestMethod(com.github.ambry.rest.RestMethod) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) InternalKeys(com.github.ambry.rest.RestUtils.InternalKeys) ClusterMap(com.github.ambry.clustermap.ClusterMap) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) GetBlobOptions(com.github.ambry.router.GetBlobOptions) RouterException(com.github.ambry.router.RouterException) BlobInfo(com.github.ambry.messageformat.BlobInfo) QuotaUtils(com.github.ambry.quota.QuotaUtils) AccountStatsStore(com.github.ambry.accountstats.AccountStatsStore) RestServiceException(com.github.ambry.rest.RestServiceException) GetBlobResult(com.github.ambry.router.GetBlobResult) Callback(com.github.ambry.commons.Callback) RestUtils(com.github.ambry.rest.RestUtils) AsyncOperationTracker(com.github.ambry.utils.AsyncOperationTracker) RestRequest(com.github.ambry.rest.RestRequest) GetBlobOptionsBuilder(com.github.ambry.router.GetBlobOptionsBuilder) BlobId(com.github.ambry.commons.BlobId) RestRequestMetrics(com.github.ambry.rest.RestRequestMetrics) GetBlobOptions(com.github.ambry.router.GetBlobOptions)

Aggregations

RestRequest (com.github.ambry.rest.RestRequest)11 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)11 RestServiceException (com.github.ambry.rest.RestServiceException)11 ThrowingConsumer (com.github.ambry.utils.ThrowingConsumer)11 MetricRegistry (com.codahale.metrics.MetricRegistry)9 RequestPath (com.github.ambry.rest.RequestPath)9 RestMethod (com.github.ambry.rest.RestMethod)9 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)9 RestUtils (com.github.ambry.rest.RestUtils)9 TestUtils (com.github.ambry.utils.TestUtils)9 ExecutionException (java.util.concurrent.ExecutionException)9 Assert (org.junit.Assert)9 Test (org.junit.Test)9 FrontendConfig (com.github.ambry.config.FrontendConfig)8 VerifiableProperties (com.github.ambry.config.VerifiableProperties)8 ByteBuffer (java.nio.ByteBuffer)8 Properties (java.util.Properties)8 Account (com.github.ambry.account.Account)7 InMemAccountService (com.github.ambry.account.InMemAccountService)7 MockRestRequest (com.github.ambry.rest.MockRestRequest)7