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