use of com.github.ambry.account.Account in project ambry by linkedin.
the class RestUtils method accountAndContainerNamePreconditionCheck.
/**
* Check preconditions for request if the {@code restRequest} contains the target account and container.
* @param restRequest the {@link RestRequest} that contains the {@link Account} and {@link Container} details.
* @throws RestServiceException if preconditions check failed.
*/
public static void accountAndContainerNamePreconditionCheck(RestRequest restRequest) throws RestServiceException {
String accountNameFromHeader = getHeader(restRequest.getArgs(), Headers.TARGET_ACCOUNT_NAME, false);
String containerNameFromHeader = getHeader(restRequest.getArgs(), Headers.TARGET_CONTAINER_NAME, false);
if (accountNameFromHeader != null) {
Account targetAccount = getAccountFromArgs(restRequest.getArgs());
String accountNameFromBlobId = targetAccount.getName();
if (!accountNameFromHeader.equals(accountNameFromBlobId)) {
throw new RestServiceException("Account name: " + accountNameFromHeader + " from request doesn't match the account name from Blob id : " + accountNameFromBlobId, RestServiceErrorCode.PreconditionFailed);
}
if (containerNameFromHeader != null) {
Container targetContainer = getContainerFromArgs(restRequest.getArgs());
String containerNameFromBlobId = targetContainer.getName();
if (!containerNameFromHeader.equals(containerNameFromBlobId)) {
throw new RestServiceException("Container name: " + containerNameFromHeader + "from request doesn't match the container name from Blob id : " + containerNameFromBlobId, RestServiceErrorCode.PreconditionFailed);
}
}
} else if (containerNameFromHeader != null) {
throw new RestServiceException("Only container name is set in request with no corresponding account name is not allowed.", RestServiceErrorCode.BadRequest);
}
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class AmbrySecurityServiceTest method testGetBlob.
/**
* Tests {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for a Get blob
* with the passed in {@link BlobInfo} and {@link ByteRange}
* @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
* @param range the {@link ByteRange} for the {@link RestRequest}
* @throws Exception
*/
private void testGetBlob(BlobInfo blobInfo, ByteRange range) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
JSONObject headers = range != null ? new JSONObject().put(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range)) : null;
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", headers);
Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
Assert.assertEquals("ProcessResponse status should have been set", range == null ? ResponseStatus.Ok : ResponseStatus.PartialContent, restResponseChannel.getStatus());
verifyHeadersForGetBlob(restRequest, blobInfo, accountAndContainer, range, restResponseChannel);
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class FrontendIntegrationTest method accountApiTest.
/**
* Tests for the account/container get/update API.
*/
@Test
public void accountApiTest() throws Exception {
verifyGetAccountsAndContainer();
// update and add accounts
Map<Short, Account> accountsById = ACCOUNT_SERVICE.getAllAccounts().stream().collect(Collectors.toMap(Account::getId, Function.identity()));
Account editedAccount = accountsById.values().stream().findAny().get();
Container editedContainer = editedAccount.getAllContainers().stream().findAny().get();
editedContainer = new ContainerBuilder(editedContainer).setDescription("new description abcdefgh").build();
editedAccount = new AccountBuilder(editedAccount).addOrUpdateContainer(editedContainer).build();
updateAccountsAndVerify(ACCOUNT_SERVICE, editedAccount, ACCOUNT_SERVICE.generateRandomAccount());
verifyGetAccountsAndContainer();
// Test adding a container to the account
Container newContainer = ACCOUNT_SERVICE.getRandomContainer(editedAccount.getId());
updateContainersAndVerify(editedAccount, newContainer);
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class FrontendIntegrationTest method multipartPostGetHeadUpdateDeleteUndeleteTest.
/**
* Tests multipart POST and verifies it via GET operations.
* @throws Exception
*/
@Test
public void multipartPostGetHeadUpdateDeleteUndeleteTest() throws Exception {
Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
Container refContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
doPostGetHeadUpdateDeleteUndeleteTest(0, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
doPostGetHeadUpdateDeleteUndeleteTest((int) FRONTEND_CONFIG.chunkedGetResponseThresholdInBytes * 3, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
// failure case
// size of content being POSTed is higher than what is allowed via multipart/form-data
long maxAllowedSizeBytes = new NettyConfig(FRONTEND_VERIFIABLE_PROPS).nettyMultipartPostMaxSizeBytes;
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes((int) maxAllowedSizeBytes + 1));
HttpHeaders headers = new DefaultHttpHeaders();
setAmbryHeadersForPut(headers, TTL_SECS, !refContainer.isCacheable(), refAccount.getName(), "application/octet-stream", null, refAccount.getName(), refContainer.getName());
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, ByteBuffer.allocate(0));
ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
HttpResponse response = getHttpResponse(responseParts);
assertEquals("Unexpected response status", HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());
assertTrue("No Date header", response.headers().getTimeMillis(HttpHeaderNames.DATE, -1) != -1);
assertFalse("Channel should not be active", HttpUtil.isKeepAlive(response));
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class FrontendIntegrationTest method stitchedUploadTest.
/**
* Test the stitched (multipart) upload flow. This includes generating signed chunk upload URLs, uploading chunks to
* that URL, calling the /stitch API to create a stitched blob, and performing get/head/ttlUpdate/delete operations on
* the stitched blob.
* @throws Exception
*/
@Test
public void stitchedUploadTest() throws Exception {
Account account = ACCOUNT_SERVICE.createAndAddRandomAccount();
Container container = account.getContainerById(Container.DEFAULT_PRIVATE_CONTAINER_ID);
Pair<List<String>, byte[]> idsAndContent = uploadDataChunksAndVerify(account, container, 50, 50, 50, 50, 17);
stitchBlobAndVerify(account, container, idsAndContent.getFirst(), idsAndContent.getSecond(), 217);
idsAndContent = uploadDataChunksAndVerify(account, container, 167);
stitchBlobAndVerify(account, container, idsAndContent.getFirst(), idsAndContent.getSecond(), 167);
}
Aggregations