Search in sources :

Example 91 with RestRequest

use of com.github.ambry.rest.RestRequest in project ambry by linkedin.

the class AmbryUrlSigningServiceTest method verifySignedUrl.

/**
 * Verifies that a signed URL contains parameters as provided and passes verification.
 * @param signer the {@link AmbryUrlSigningService} to use.
 * @param url the signed URL.
 * @param restMethod the {@link RestMethod} intended by {@code url}.
 * @param randomHeaderVal the expected value of {@link #RANDOM_AMBRY_HEADER}.
 * @param maxUploadSize the expected value of {@link RestUtils.Headers#MAX_UPLOAD_SIZE}.
 * @param chunkUploadSet {@code true} if the signed URL should be a chunk upload URL.
 * @throws Exception
 */
private void verifySignedUrl(AmbryUrlSigningService signer, String url, RestMethod restMethod, String randomHeaderVal, long maxUploadSize, long blobTtl, boolean chunkUploadSet) throws Exception {
    RestRequest signedRequest = getRequestFromUrl(restMethod, url);
    assertTrue("Request should be declared as signed", signer.isRequestSigned(signedRequest));
    signer.verifySignedRequest(signedRequest);
    Map<String, Object> args = signedRequest.getArgs();
    assertEquals("URL type not as expected", restMethod.name(), args.get(RestUtils.Headers.URL_TYPE).toString());
    assertEquals("Random header value is not as expected", randomHeaderVal, args.get(RANDOM_AMBRY_HEADER).toString());
    Object blobTtlVal = args.get(RestUtils.Headers.TTL);
    Object chunkUploadVal = args.get(RestUtils.Headers.CHUNK_UPLOAD);
    Object sessionVal = args.get(RestUtils.Headers.SESSION);
    if (restMethod.equals(RestMethod.POST)) {
        assertEquals("Max upload size not as expected", maxUploadSize, Long.parseLong(args.get(RestUtils.Headers.MAX_UPLOAD_SIZE).toString()));
        if (blobTtl != Utils.Infinite_Time) {
            assertEquals("Blob TTL not as expected", blobTtl, Long.parseLong(blobTtlVal.toString()));
        }
        if (chunkUploadSet) {
            assertTrue("Chunk upload should be set", Boolean.valueOf(chunkUploadVal.toString()));
            assertNotNull("Session should be set", sessionVal);
            // ensure that the x-ambry-chunk-upload-session value is a valid UUID.
            UUID.fromString(sessionVal.toString());
        } else {
            assertNull("Chunk upload should not be set", chunkUploadVal);
            assertNull("Session should not be set", sessionVal);
        }
    } else {
        assertNull("Blob TTL should not be set", blobTtlVal);
        assertNull("Chunk upload should not be set", chunkUploadVal);
        assertNull("Session should not be set", sessionVal);
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject)

Example 92 with RestRequest

use of com.github.ambry.rest.RestRequest in project ambry by linkedin.

the class AmbryUrlSigningServiceTest method signFailuresTest.

/**
 * Tests for failure scenarios when trying to generate signed URLs.
 * @throws Exception
 */
private void signFailuresTest() throws Exception {
    AmbryUrlSigningService signer = getUrlSignerWithDefaults(new MockTime());
    // RestMethod not present
    RestRequest request = getUrlSignRequest(null, Utils.Infinite_Time, null, -1L, false);
    ensureSignedUrlCreationFailure(signer, request, RestServiceErrorCode.MissingArgs);
    // unknown RestMethod
    request = getUrlSignRequest(null, Utils.Infinite_Time, null, -1L, false);
    request.setArg(RestUtils.Headers.URL_TYPE, "@@unknown@@");
    ensureSignedUrlCreationFailure(signer, request, RestServiceErrorCode.InvalidArgs);
    // RestMethod not supported
    request = getUrlSignRequest(RestMethod.DELETE, Utils.Infinite_Time, null, -1L, false);
    ensureSignedUrlCreationFailure(signer, request, RestServiceErrorCode.InvalidArgs);
    // url ttl not long
    request = getUrlSignRequest(RestMethod.POST, Utils.Infinite_Time, null, -1L, false);
    request.setArg(RestUtils.Headers.URL_TTL, "@@notlong@@");
    ensureSignedUrlCreationFailure(signer, request, RestServiceErrorCode.InvalidArgs);
    // max upload size not long
    request = getUrlSignRequest(RestMethod.POST, Utils.Infinite_Time, null, -1L, false);
    request.setArg(RestUtils.Headers.MAX_UPLOAD_SIZE, "@@notlong@@");
    ensureSignedUrlCreationFailure(signer, request, RestServiceErrorCode.InvalidArgs);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockTime(com.github.ambry.utils.MockTime)

Example 93 with RestRequest

use of com.github.ambry.rest.RestRequest in project ambry by linkedin.

the class GetStatsReportHandlerTest method handleGoodCaseTest.

@Test
public void handleGoodCaseTest() throws Exception {
    AggregatedAccountStorageStats aggregatedAccountStorageStats = new AggregatedAccountStorageStats(StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 1, 10, 10, 1000L, 2, 100));
    doAnswer(invocation -> {
        String clusterName = invocation.getArgument(0);
        if (clusterName.equals(CLUSTER_NAME)) {
            return aggregatedAccountStorageStats;
        } else {
            return null;
        }
    }).when(accountStatsStore).queryAggregatedAccountStorageStatsByClusterName(anyString());
    RestRequest restRequest = createRestRequest(CLUSTER_NAME, StatsReportType.ACCOUNT_REPORT.name());
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
    assertNotNull("There should be a response", channel);
    assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
    assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    assertEquals("Storage stats mismatch", aggregatedAccountStorageStats.getStorageStats(), mapper.readValue(RestTestUtils.getResponseBody(channel), AggregatedAccountStorageStats.class).getStorageStats());
    AggregatedPartitionClassStorageStats aggregatedPartitionClassStorageStats = new AggregatedPartitionClassStorageStats(StorageStatsUtilTest.generateRandomAggregatedPartitionClassStorageStats(new String[] { "default", "newClass" }, (short) 1, 10, 10, 1000L, 2, 100));
    doAnswer(invocation -> {
        String clusterName = invocation.getArgument(0);
        if (clusterName.equals(CLUSTER_NAME)) {
            return aggregatedPartitionClassStorageStats;
        } else {
            return null;
        }
    }).when(accountStatsStore).queryAggregatedPartitionClassStorageStatsByClusterName(anyString());
    restRequest = createRestRequest(CLUSTER_NAME, StatsReportType.PARTITION_CLASS_REPORT.name());
    restResponseChannel = new MockRestResponseChannel();
    channel = sendRequestGetResponse(restRequest, restResponseChannel);
    assertNotNull("There should be a response", channel);
    assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
    assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    assertEquals("Storage stats mismatch", aggregatedPartitionClassStorageStats.getStorageStats(), mapper.readValue(RestTestUtils.getResponseBody(channel), AggregatedPartitionClassStorageStats.class).getStorageStats());
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) AggregatedAccountStorageStats(com.github.ambry.server.storagestats.AggregatedAccountStorageStats) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 94 with RestRequest

use of com.github.ambry.rest.RestRequest in project ambry by linkedin.

the class PostAccountContainersHandlerTest method badRequestsTest.

/**
 * Test bad request cases.
 * @throws Exception
 */
@Test
public void badRequestsTest() throws Exception {
    ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
        TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
    };
    String accountName = theAccount.getName();
    // Empty container list should fail
    String emptyContainers = new String(AccountCollectionSerde.serializeContainersInJson(Collections.emptyList()));
    RestRequest request = createRestRequest(emptyContainers, accountName, null);
    testAction.accept(request, RestServiceErrorCode.BadRequest);
    // non json input
    request = createRestRequest("ABC", accountName, null);
    testAction.accept(request, RestServiceErrorCode.BadRequest);
    // invalid json
    String invalidJson = new JSONObject().append("accounts", "ABC").toString();
    request = createRestRequest(invalidJson, accountName, null);
    testAction.accept(request, RestServiceErrorCode.BadRequest);
    // No account specified
    String oneContainer = new String(AccountCollectionSerde.serializeContainersInJson(Collections.singleton(accountService.getRandomContainer(theAccount.getId()))));
    request = createRestRequest(oneContainer, null, null);
    testAction.accept(request, RestServiceErrorCode.BadRequest);
    // AccountService update failure
    accountService.setShouldUpdateSucceed(false);
    request = createRestRequest(oneContainer, accountName, null);
    testAction.accept(request, RestServiceErrorCode.InternalServerError);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) FutureResult(com.github.ambry.router.FutureResult) ContainerBuilder(com.github.ambry.account.ContainerBuilder) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) ArrayList(java.util.ArrayList) 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) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) 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) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) 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) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test)

Example 95 with RestRequest

use of com.github.ambry.rest.RestRequest in project ambry by linkedin.

the class PostBlobHandlerTest method doTtlRequiredEnforcementTest.

// ttlRequiredEnforcementTest() helpers
/**
 * Does the TTL required enforcement test by selecting the right verification methods based on container and frontend
 * config
 * @param container the {@link Container} to upload to
 * @param blobTtlSecs the TTL to set for the blob
 * @throws Exception
 */
private void doTtlRequiredEnforcementTest(Container container, long blobTtlSecs) throws Exception {
    JSONObject headers = new JSONObject();
    FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, blobTtlSecs, !container.isCacheable(), SERVICE_ID, CONTENT_TYPE, OWNER_ID, REF_ACCOUNT.getName(), container.getName(), null);
    byte[] content = TestUtils.getRandomBytes(1024);
    RestRequest request = getRestRequest(headers, "/", content);
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    FutureResult<Void> future = new FutureResult<>();
    postBlobHandler.handle(request, restResponseChannel, future::done);
    if (container.isTtlRequired() && (blobTtlSecs == Utils.Infinite_Time || blobTtlSecs > frontendConfig.maxAcceptableTtlSecsIfTtlRequired)) {
        if (frontendConfig.failIfTtlRequiredButNotProvided) {
            try {
                future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
                fail("Post should have failed");
            } catch (ExecutionException e) {
                RestServiceException rootCause = (RestServiceException) Utils.getRootCause(e);
                assertNotNull("Root cause should be a RestServiceException", rootCause);
                assertEquals("Incorrect RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, rootCause.getErrorCode());
            }
        } else {
            verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, true);
        }
    } else {
        verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, false);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) FutureResult(com.github.ambry.router.FutureResult) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

RestRequest (com.github.ambry.rest.RestRequest)102 MockRestRequest (com.github.ambry.rest.MockRestRequest)82 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)54 JSONObject (org.json.JSONObject)50 Test (org.junit.Test)46 RestServiceException (com.github.ambry.rest.RestServiceException)34 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)26 RestMethod (com.github.ambry.rest.RestMethod)23 Account (com.github.ambry.account.Account)18 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)18 ExecutionException (java.util.concurrent.ExecutionException)18 ByteBuffer (java.nio.ByteBuffer)17 RestUtils (com.github.ambry.rest.RestUtils)16 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)16 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)16 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 Container (com.github.ambry.account.Container)14 RequestPath (com.github.ambry.rest.RequestPath)14 FutureResult (com.github.ambry.router.FutureResult)14