Search in sources :

Example 86 with Container

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

the class AccountAndContainerInjector method injectAccountAndContainerForNamedBlob.

/**
 * Injects target {@link Account} and {@link Container} for named blob requests. This will treat the request path as
 * a named blob path that includes the account and container names.
 * @param restRequest The Put {@link RestRequest}.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException
 */
public void injectAccountAndContainerForNamedBlob(RestRequest restRequest, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    accountAndContainerSanityCheck(restRequest);
    NamedBlobPath namedBlobPath = NamedBlobPath.parse(getRequestPath(restRequest), restRequest.getArgs());
    String accountName = namedBlobPath.getAccountName();
    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 = namedBlobPath.getContainerName();
    Container targetContainer;
    try {
        targetContainer = accountService.getContainerByName(accountName, containerName);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container " + containerName + " from account " + accountName + " in put request with account and container headers.", RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    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);
    }
    if (targetContainer.getNamedBlobMode() == Container.NamedBlobMode.DISABLED) {
        throw new RestServiceException("Named blob APIs disabled for this container. account=" + accountName + ", container=" + containerName, RestServiceErrorCode.BadRequest);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 87 with Container

use of com.github.ambry.account.Container 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.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if either of {@link Account} or {@link Container} object could not be found.
 */
private void injectAccountAndContainerUsingServiceId(RestRequest restRequest, String serviceId, boolean isPrivate, RestRequestMetricsGroup metricsGroup) 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;
    short containerId = isPrivate ? Container.DEFAULT_PRIVATE_CONTAINER_ID : Container.DEFAULT_PUBLIC_CONTAINER_ID;
    try {
        targetContainer = accountService.getContainerById(targetAccount.getId(), containerId);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container with Id= " + containerId + " from account " + targetAccount.getName() + "for put request; ServiceId=" + serviceId + ", isPrivate=" + isPrivate, RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        throw new RestServiceException("Invalid account or container to inject; serviceId=" + serviceId + ", isPrivate=" + isPrivate, RestServiceErrorCode.InternalServerError);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 88 with Container

use of com.github.ambry.account.Container 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.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if either of {@link Account} or {@link Container} object could not be found.
 */
private void injectAccountAndContainerUsingAccountAndContainerHeaders(RestRequest restRequest, RestRequestMetricsGroup metricsGroup) 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;
    try {
        targetContainer = accountService.getContainerByName(accountName, containerName);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container " + containerName + " from account " + accountName + " for put request with account and container headers.", RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    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, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 89 with Container

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

the class AmbrySecurityService method setCacheHeaders.

/**
 * Sets headers that provide directions to proxies and caches.
 * @param restRequest the {@link RestRequest} that was received.
 * @param restResponseChannel the channel that the response will be sent over
 * @throws RestServiceException if there is any problem setting the headers
 */
private void setCacheHeaders(RestRequest restRequest, RestResponseChannel restResponseChannel) throws RestServiceException {
    Container container = RestUtils.getContainerFromArgs(restRequest.getArgs());
    if (container.isCacheable()) {
        restResponseChannel.setHeader(RestUtils.Headers.EXPIRES, new Date(System.currentTimeMillis() + frontendConfig.cacheValiditySeconds * Time.MsPerSec));
        restResponseChannel.setHeader(RestUtils.Headers.CACHE_CONTROL, "max-age=" + frontendConfig.cacheValiditySeconds);
    } else {
        restResponseChannel.setHeader(RestUtils.Headers.EXPIRES, restResponseChannel.getHeader(RestUtils.Headers.DATE));
        restResponseChannel.setHeader(RestUtils.Headers.CACHE_CONTROL, "private, no-cache, no-store, proxy-revalidate");
        restResponseChannel.setHeader(RestUtils.Headers.PRAGMA, "no-cache");
    }
}
Also used : Container(com.github.ambry.account.Container) Date(java.util.Date)

Example 90 with Container

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

the class AmbrySecurityService method setAccountAndContainerHeaders.

/**
 * Adds the account and container details to the response headers if the {@code restRequest} contains the target
 * account and container and they are not generic unknowns.
 * @param restRequest the {@link RestRequest} that contains the {@link Account} and {@link Container} details.
 * @param restResponseChannel the {@link RestResponseChannel} where headers need to be set.
 * @throws RestServiceException if headers cannot be set.
 */
private void setAccountAndContainerHeaders(RestRequest restRequest, RestResponseChannel restResponseChannel) throws RestServiceException {
    Account account = RestUtils.getAccountFromArgs(restRequest.getArgs());
    Container container = RestUtils.getContainerFromArgs(restRequest.getArgs());
    if (account.getId() != Account.UNKNOWN_ACCOUNT_ID) {
        restResponseChannel.setHeader(RestUtils.Headers.TARGET_ACCOUNT_NAME, account.getName());
        restResponseChannel.setHeader(RestUtils.Headers.TARGET_CONTAINER_NAME, container.getName());
    }
    restResponseChannel.setHeader(RestUtils.Headers.PRIVATE, !container.isCacheable());
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Aggregations

Container (com.github.ambry.account.Container)119 Account (com.github.ambry.account.Account)88 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)30 RestServiceException (com.github.ambry.rest.RestServiceException)20 ContainerBuilder (com.github.ambry.account.ContainerBuilder)17 JSONObject (org.json.JSONObject)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashSet (java.util.HashSet)15 HashMap (java.util.HashMap)14 Properties (java.util.Properties)14 AccountBuilder (com.github.ambry.account.AccountBuilder)13 RestRequest (com.github.ambry.rest.RestRequest)13 ByteBuffer (java.nio.ByteBuffer)13 Map (java.util.Map)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 TestUtils (com.github.ambry.utils.TestUtils)12 Collections (java.util.Collections)12 List (java.util.List)12 Assert (org.junit.Assert)12