Search in sources :

Example 1 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class AccountAndContainerInjector method injectAccountAndContainerUsingAccountAndContainerHeaders.

/**
 * Injects {@link Account} and {@link Container} for the PUT requests that carry the target account and container headers.
 * @param restRequest The {@link RestRequest} to inject {@link Account} and {@link Container} object.
 * @throws RestServiceException if either of {@link Account} or {@link Container} object could not be found.
 */
private void injectAccountAndContainerUsingAccountAndContainerHeaders(RestRequest restRequest) throws RestServiceException {
    String accountName = getHeader(restRequest.getArgs(), Headers.TARGET_ACCOUNT_NAME, false);
    Account targetAccount = accountService.getAccountByName(accountName);
    if (targetAccount == null) {
        frontendMetrics.unrecognizedAccountNameCount.inc();
        throw new RestServiceException("Account cannot be found for accountName=" + accountName + " in put request with account and container headers.", RestServiceErrorCode.InvalidAccount);
    }
    ensureAccountNameMatch(targetAccount, restRequest);
    String containerName = getHeader(restRequest.getArgs(), Headers.TARGET_CONTAINER_NAME, false);
    Container targetContainer = targetAccount.getContainerByName(containerName);
    if (targetContainer == null) {
        frontendMetrics.unrecognizedContainerNameCount.inc();
        throw new RestServiceException("Container cannot be found for accountName=" + accountName + " and containerName=" + containerName + " in put request with account and container headers.", RestServiceErrorCode.InvalidContainer);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 2 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class AccountAndContainerInjector method ensureAccountAndContainerInjected.

/**
 * If a non-unknown {@link Account} and {@link Container} was not previously injected, inject them into the provided
 * {@link RestRequest}, based on the given {@link BlobProperties}' service ID and blob privacy setting. This is useful
 * for V1 blob IDs that do not directly encode the account/container ID.
 * @param restRequest The {@link RestRequest} to inject {@link Account} and {@link Container}.
 * @param blobProperties The {@link BlobProperties} that contains the service id and blob privacy setting.
 * @throws RestServiceException if no valid account or container cound be identified for re-injection.
 */
public void ensureAccountAndContainerInjected(RestRequest restRequest, BlobProperties blobProperties) throws RestServiceException {
    Account targetAccount = (Account) restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY);
    Container targetContainer = (Container) restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY);
    if (targetAccount == null || targetContainer == null) {
        throw new RestServiceException("Account and container were not injected by BlobStorageService", RestServiceErrorCode.InternalServerError);
    } else if (targetAccount.getId() == Account.UNKNOWN_ACCOUNT_ID) {
        // This should only occur for V1 blobs, where the blob ID does not contain the actual account and container IDs.
        String serviceId = blobProperties.getServiceId();
        boolean isPrivate = blobProperties.isPrivate();
        injectAccountAndContainerUsingServiceId(restRequest, serviceId, isPrivate);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 3 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class AccountAndContainerInjector method injectAccountAndContainerUsingServiceId.

/**
 * Inject {@link Account} and {@link Container} into a {@link RestRequest} based on a blob's service ID and
 * privacy setting.
 * @param restRequest The {@link RestRequest} to inject {@link Account} and {@link Container} object.
 * @param serviceId The service ID associated with the blob.
 * @param isPrivate The blob's privacy setting.
 * @throws RestServiceException if either of {@link Account} or {@link Container} object could not be found.
 */
private void injectAccountAndContainerUsingServiceId(RestRequest restRequest, String serviceId, boolean isPrivate) throws RestServiceException {
    // First, try to see if a migrated account exists for the service ID.
    Account targetAccount = accountService.getAccountByName(serviceId);
    if (targetAccount == null) {
        frontendMetrics.unrecognizedServiceIdCount.inc();
        logger.debug("Account cannot be found for put request with serviceId={}. Setting targetAccount to UNKNOWN_ACCOUNT", serviceId);
        // If a migrated account does not exist fall back to the UNKNOWN_ACCOUNT.
        targetAccount = accountService.getAccountById(Account.UNKNOWN_ACCOUNT_ID);
    }
    // Either the UNKNOWN_ACCOUNT, or the migrated account should contain default public/private containers
    Container targetContainer = targetAccount.getContainerById(isPrivate ? Container.DEFAULT_PRIVATE_CONTAINER_ID : Container.DEFAULT_PUBLIC_CONTAINER_ID);
    if (targetContainer == null) {
        throw new RestServiceException("Invalid account or container to inject; serviceId=" + serviceId + ", isPrivate=" + isPrivate, RestServiceErrorCode.InternalServerError);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 4 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class AccountAndContainerInjector method injectTargetAccountAndContainerFromBlobId.

/**
 * Obtains the target {@link Account} and {@link Container} id from the blobId string, queries the {@link AccountService}
 * to get the corresponding {@link Account} and {@link Container}, and injects the target {@link Account} and
 * {@link Container} into the {@link RestRequest}.
 * @param blobId The blobId to get the target {@link Account} and {@link Container} id.
 * @param restRequest The rest request to insert the target {@link Account} and {@link Container}.
 * @throws RestServiceException if 1) either {@link Account} or {@link Container} could not be found; or 2)
 *                              either {@link Account} or {@link Container} were explicitly specified as
 *                              {@link Account#UNKNOWN_ACCOUNT} or {@link Container#UNKNOWN_CONTAINER}.
 */
public void injectTargetAccountAndContainerFromBlobId(BlobId blobId, RestRequest restRequest) throws RestServiceException {
    Account targetAccount = accountService.getAccountById(blobId.getAccountId());
    if (targetAccount == null) {
        frontendMetrics.getHeadDeleteUnrecognizedAccountCount.inc();
        // @todo The check can be removed once HelixAccountService is running with UNKNOWN_ACCOUNT created.
        if (blobId.getAccountId() != Account.UNKNOWN_ACCOUNT_ID) {
            throw new RestServiceException("Account from blobId=" + blobId.getID() + "with accountId=" + blobId.getAccountId() + " cannot be recognized", RestServiceErrorCode.InvalidAccount);
        } else {
            logger.debug("Account cannot be found for blobId={} with accountId={}. Setting targetAccount to UNKNOWN_ACCOUNT", blobId.getID(), blobId.getAccountId());
            targetAccount = accountService.getAccountById(Account.UNKNOWN_ACCOUNT_ID);
        }
    }
    Container targetContainer = targetAccount.getContainerById(blobId.getContainerId());
    if (targetContainer == null) {
        frontendMetrics.getHeadDeleteUnrecognizedContainerCount.inc();
        throw new RestServiceException("Container from blobId=" + blobId.getID() + "with accountId=" + blobId.getAccountId() + " containerId=" + blobId.getContainerId() + " cannot be recognized", RestServiceErrorCode.InvalidContainer);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 5 with Account

use of com.github.ambry.account.Account in project ambry by linkedin.

the class AmbrySecurityServiceTest method testGetNotModifiedBlob.

/**
 * Tests {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for a Get blob
 * with the passed in {@link BlobInfo} for a not modified response
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param ifModifiedSinceMs the value (as a date string) of the {@link RestUtils.Headers#IF_MODIFIED_SINCE} header.
 * @throws Exception
 */
private void testGetNotModifiedBlob(BlobInfo blobInfo, long ifModifiedSinceMs) throws Exception {
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    JSONObject headers = new JSONObject();
    SimpleDateFormat dateFormat = new SimpleDateFormat(RestUtils.HTTP_DATE_FORMAT, Locale.ENGLISH);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = new Date(ifModifiedSinceMs);
    String dateStr = dateFormat.format(date);
    headers.put(RestUtils.Headers.IF_MODIFIED_SINCE, dateStr);
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/abc", headers);
    Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
    insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
    securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
    if (ifModifiedSinceMs >= blobInfo.getBlobProperties().getCreationTimeInMs()) {
        Assert.assertEquals("Not modified response expected", ResponseStatus.NotModified, restResponseChannel.getStatus());
        verifyHeadersForGetBlobNotModified(restResponseChannel, accountAndContainer.getSecond().isCacheable());
    } else {
        Assert.assertEquals("Not modified response should not be returned", ResponseStatus.Ok, restResponseChannel.getStatus());
        verifyHeadersForGetBlob(blobInfo.getBlobProperties(), null, 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) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

Account (com.github.ambry.account.Account)39 Container (com.github.ambry.account.Container)27 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)5 MockRestRequest (com.github.ambry.rest.MockRestRequest)4 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)4 RestRequest (com.github.ambry.rest.RestRequest)4 RestServiceException (com.github.ambry.rest.RestServiceException)4 UtilsTest (com.github.ambry.utils.UtilsTest)4 JSONObject (org.json.JSONObject)4 BlobProperties (com.github.ambry.messageformat.BlobProperties)2 ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2 AccountBuilder (com.github.ambry.account.AccountBuilder)1