Search in sources :

Example 26 with Account

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);
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 27 with Account

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);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 28 with Account

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);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test)

Example 29 with Account

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));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Account(com.github.ambry.account.Account) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Container(com.github.ambry.account.Container) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) NettyConfig(com.github.ambry.config.NettyConfig) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with Account

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);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Account (com.github.ambry.account.Account)114 Container (com.github.ambry.account.Container)87 Test (org.junit.Test)67 RestServiceException (com.github.ambry.rest.RestServiceException)24 ArrayList (java.util.ArrayList)22 RestRequest (com.github.ambry.rest.RestRequest)18 JSONObject (org.json.JSONObject)18 MockRestRequest (com.github.ambry.rest.MockRestRequest)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)15 AccountBuilder (com.github.ambry.account.AccountBuilder)14 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)14 ContainerBuilder (com.github.ambry.account.ContainerBuilder)13 Properties (java.util.Properties)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 InMemAccountService (com.github.ambry.account.InMemAccountService)12 ByteBuffer (java.nio.ByteBuffer)12 RestMethod (com.github.ambry.rest.RestMethod)11 Map (java.util.Map)11