Search in sources :

Example 6 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class AmbrySecurityService method processRequest.

@Override
public void processRequest(RestRequest restRequest, Callback<Void> callback) {
    Exception exception = null;
    frontendMetrics.securityServiceProcessRequestRate.mark();
    long startTimeMs = System.currentTimeMillis();
    if (!isOpen) {
        exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable);
    } else if (restRequest == null) {
        throw new IllegalArgumentException("RestRequest is null");
    }
    frontendMetrics.securityServiceProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs);
    callback.onCompletion(null, exception);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RestServiceException(com.github.ambry.rest.RestServiceException)

Example 7 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class AmbryUrlSigningService method verifySignedRequest.

@Override
public void verifySignedRequest(RestRequest restRequest) throws RestServiceException {
    if (!isRequestSigned(restRequest)) {
        throw new RestServiceException("Request is not signed - method should not have been called", RestServiceErrorCode.InternalServerError);
    }
    Map<String, Object> args = restRequest.getArgs();
    long expiryTimeSecs = RestUtils.getLongHeader(args, LINK_EXPIRY_TIME, true);
    if (time.seconds() > expiryTimeSecs) {
        throw new RestServiceException("Signed URL has expired", RestServiceErrorCode.Unauthorized);
    }
    RestMethod restMethodInUrl = RestMethod.valueOf(RestUtils.getHeader(args, RestUtils.Headers.URL_TYPE, true));
    if (!restRequest.getRestMethod().equals(restMethodInUrl)) {
        throw new RestServiceException("Type of request being made not compatible with signed URL", RestServiceErrorCode.Unauthorized);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RestMethod(com.github.ambry.rest.RestMethod)

Example 8 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class AmbryUrlSigningService method getSignedUrl.

@Override
public String getSignedUrl(RestRequest restRequest) throws RestServiceException {
    Map<String, Object> args = restRequest.getArgs();
    String restMethodInSignedUrlStr = RestUtils.getHeader(args, RestUtils.Headers.URL_TYPE, true);
    RestMethod restMethodInSignedUrl;
    try {
        restMethodInSignedUrl = RestMethod.valueOf(restMethodInSignedUrlStr);
    } catch (IllegalArgumentException e) {
        throw new RestServiceException("Unrecognized RestMethod: " + restMethodInSignedUrlStr, RestServiceErrorCode.InvalidArgs);
    }
    StringBuilder urlBuilder = new StringBuilder();
    switch(restMethodInSignedUrl) {
        case GET:
            urlBuilder.append(downloadEndpoint).append(QUERY_STRING_START);
            break;
        case POST:
            urlBuilder.append(uploadEndpoint).append(QUERY_STRING_START);
            break;
        default:
            throw new RestServiceException("Signing request for " + restMethodInSignedUrl + " is not supported", RestServiceErrorCode.InvalidArgs);
    }
    long urlTtlSecs = defaultUrlTtlSecs;
    long maxUploadSize = defaultMaxUploadSize;
    for (Map.Entry<String, Object> entry : args.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        if (name.regionMatches(true, 0, AMBRY_PARAMETERS_PREFIX, 0, AMBRY_PARAMETERS_PREFIX.length()) && value instanceof String) {
            switch(name) {
                case RestUtils.Headers.URL_TTL:
                    urlTtlSecs = Math.min(maxUrlTtlSecs, RestUtils.getLongHeader(args, RestUtils.Headers.URL_TTL, true));
                    break;
                case RestUtils.Headers.MAX_UPLOAD_SIZE:
                    maxUploadSize = RestUtils.getLongHeader(args, RestUtils.Headers.MAX_UPLOAD_SIZE, true);
                    break;
                default:
                    urlBuilder.append(name).append(PARAMETER_ASSIGN).append(value).append(PARAMETER_SEPARATOR);
                    break;
            }
        }
    }
    if (RestMethod.POST.equals(restMethodInSignedUrl)) {
        urlBuilder.append(RestUtils.Headers.MAX_UPLOAD_SIZE).append(PARAMETER_ASSIGN).append(maxUploadSize).append(PARAMETER_SEPARATOR);
    }
    urlBuilder.append(LINK_EXPIRY_TIME).append(PARAMETER_ASSIGN).append(time.seconds() + urlTtlSecs);
    // since all strings came from the URL, they are assumed to be url encodable.
    return urlBuilder.toString();
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Map(java.util.Map) RestMethod(com.github.ambry.rest.RestMethod)

Example 9 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class GetPeersHandler method getDataNodeId.

/**
 * Gets the {@link DataNodeId} based on query parameters in the {@code restRequest}. Return is always non-null.
 * @param restRequest the {@link RestRequest} containing the parameters of the request.
 * @return the {@link DataNodeId} based on query parameters in the {@code restRequest}. Return is always non-null.
 * @throws RestServiceException if either {@link #NAME_QUERY_PARAM} or {@link #PORT_QUERY_PARAM} is missing, if
 * {@link #PORT_QUERY_PARAM} is not an {@link Integer} or if there is no datanode that corresponds to the given
 * parameters.
 */
private DataNodeId getDataNodeId(RestRequest restRequest) throws RestServiceException {
    String name = (String) restRequest.getArgs().get(NAME_QUERY_PARAM);
    String portStr = (String) restRequest.getArgs().get(PORT_QUERY_PARAM);
    if (name == null || portStr == null) {
        throw new RestServiceException("Missing name and/or port of data node", RestServiceErrorCode.MissingArgs);
    }
    int port;
    try {
        port = Integer.parseInt(portStr);
    } catch (NumberFormatException e) {
        throw new RestServiceException("Port " + "[" + portStr + "] could not parsed into a number", RestServiceErrorCode.InvalidArgs);
    }
    DataNodeId dataNodeId = clusterMap.getDataNodeId(name, port);
    if (dataNodeId == null) {
        metrics.unknownDatanodeError.inc();
        throw new RestServiceException("No datanode found for parameters " + name + ":" + port, RestServiceErrorCode.NotFound);
    }
    return dataNodeId;
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) DataNodeId(com.github.ambry.clustermap.DataNodeId)

Example 10 with RestServiceException

use of com.github.ambry.rest.RestServiceException in project ambry by linkedin.

the class GetReplicasHandler method getReplicas.

/**
 * Extracts the blob ID provided by the client and figures out the partition that the blob ID would belong to
 * based on the cluster map. Using the partition information, returns the list of replicas as a part of a JSONObject.
 * @param blobId the blob ID whose replicas are required.
 * @return A {@link JSONObject} that wraps the replica list.
 * @throws RestServiceException if there were missing or invalid arguments or if there was a {@link JSONException}
 *                                or any other while building the response
 */
private JSONObject getReplicas(String blobId) throws RestServiceException {
    try {
        PartitionId partitionId = new BlobId(blobId, clusterMap).getPartition();
        if (partitionId == null) {
            metrics.invalidBlobIdError.inc();
            logger.warn("Partition for blob id {} is null. The blob id might be invalid", blobId);
            throw new RestServiceException("Partition for blob id " + blobId + " is null. The id might be invalid", RestServiceErrorCode.NotFound);
        }
        return packageResult(partitionId.getReplicaIds());
    } catch (IllegalArgumentException e) {
        metrics.invalidBlobIdError.inc();
        throw new RestServiceException("Invalid blob id received for getReplicasForBlob request - " + blobId, e, RestServiceErrorCode.NotFound);
    } catch (IOException | JSONException e) {
        metrics.responseConstructionError.inc();
        throw new RestServiceException("Could not create response for GET of replicas for " + blobId, e, RestServiceErrorCode.InternalServerError);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) JSONException(org.json.JSONException) IOException(java.io.IOException) PartitionId(com.github.ambry.clustermap.PartitionId) BlobId(com.github.ambry.commons.BlobId)

Aggregations

RestServiceException (com.github.ambry.rest.RestServiceException)35 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)15 Test (org.junit.Test)12 MockRestRequest (com.github.ambry.rest.MockRestRequest)11 RestRequest (com.github.ambry.rest.RestRequest)10 UtilsTest (com.github.ambry.utils.UtilsTest)8 JSONObject (org.json.JSONObject)8 RestMethod (com.github.ambry.rest.RestMethod)7 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)6 Account (com.github.ambry.account.Account)5 Container (com.github.ambry.account.Container)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 ExecutionException (java.util.concurrent.ExecutionException)4 BlobId (com.github.ambry.commons.BlobId)3 RestUtils (com.github.ambry.rest.RestUtils)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 PartitionId (com.github.ambry.clustermap.PartitionId)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)2