HDDS-705. OS3Exception resource name should be the actual resource name.

Contributed by Bharat Viswanadham.

Recommitting after making sure that patch is clean.
This commit is contained in:
Nanda kumar 2018-10-22 15:48:36 +05:30
parent c696419f3e
commit 97a41b3dbe
8 changed files with 24 additions and 64 deletions

View File

@ -199,11 +199,11 @@ public class BucketEndpoint extends EndpointBase {
} catch (IOException ex) {
if (ex.getMessage().contains("BUCKET_NOT_EMPTY")) {
OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable
.BUCKET_NOT_EMPTY, S3ErrorTable.Resource.BUCKET);
.BUCKET_NOT_EMPTY, bucketName);
throw os3Exception;
} else if (ex.getMessage().contains("BUCKET_NOT_FOUND")) {
OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable
.NO_SUCH_BUCKET, S3ErrorTable.Resource.BUCKET);
.NO_SUCH_BUCKET, bucketName);
throw os3Exception;
} else {
throw ex;

View File

@ -28,7 +28,6 @@ import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneVolume;
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
import org.apache.hadoop.ozone.s3.exception.S3ErrorTable;
import org.apache.hadoop.ozone.s3.exception.S3ErrorTable.Resource;
import org.apache.hadoop.ozone.s3.header.AuthorizationHeaderV2;
import org.apache.hadoop.ozone.s3.header.AuthorizationHeaderV4;
@ -61,7 +60,7 @@ public class EndpointBase {
LOG.error("Error occurred is {}", ex);
if (ex.getMessage().contains("NOT_FOUND")) {
OS3Exception oex =
S3ErrorTable.newError(S3ErrorTable.NO_SUCH_BUCKET, Resource.BUCKET);
S3ErrorTable.newError(S3ErrorTable.NO_SUCH_BUCKET, bucketName);
throw oex;
} else {
throw ex;
@ -80,7 +79,7 @@ public class EndpointBase {
LOG.error("Error occurred is {}", ex);
if (ex.getMessage().contains("NOT_FOUND")) {
OS3Exception oex =
S3ErrorTable.newError(S3ErrorTable.NO_SUCH_BUCKET, Resource.BUCKET);
S3ErrorTable.newError(S3ErrorTable.NO_SUCH_BUCKET, bucketName);
throw oex;
} else {
throw ex;
@ -187,7 +186,7 @@ public class EndpointBase {
if (auth == null) {
throw S3ErrorTable
.newError(S3ErrorTable.MALFORMED_HEADER, Resource.HEADER);
.newError(S3ErrorTable.MALFORMED_HEADER, auth);
}
String userName;

View File

@ -150,7 +150,7 @@ public class ObjectEndpoint extends EndpointBase {
} catch (IOException ex) {
if (ex.getMessage().contains("NOT_FOUND")) {
OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable
.NO_SUCH_OBJECT, S3ErrorTable.Resource.OBJECT);
.NO_SUCH_KEY, keyPath);
throw os3Exception;
} else {
throw ex;
@ -176,9 +176,8 @@ public class ObjectEndpoint extends EndpointBase {
} catch (IOException ex) {
LOG.error("Exception occurred in HeadObject", ex);
if (ex.getMessage().contains("KEY_NOT_FOUND")) {
OS3Exception os3Exception = S3ErrorTable.newError(S3ErrorTable
.NO_SUCH_OBJECT, S3ErrorTable.Resource.OBJECT);
throw os3Exception;
// Just return 404 with no content
return Response.status(Status.NOT_FOUND).build();
} else {
throw ex;
}
@ -215,7 +214,7 @@ public class ObjectEndpoint extends EndpointBase {
} catch (IOException ex) {
if (ex.getMessage().contains("BUCKET_NOT_FOUND")) {
throw S3ErrorTable.newError(S3ErrorTable
.NO_SUCH_BUCKET, S3ErrorTable.Resource.BUCKET);
.NO_SUCH_BUCKET, bucketName);
} else if (!ex.getMessage().contains("NOT_FOUND")) {
throw ex;
}

View File

@ -45,52 +45,23 @@ public final class S3ErrorTable {
"BucketNotEmpty", "The bucket you tried to delete is not empty.",
HTTP_CONFLICT);
public static final OS3Exception NO_SUCH_OBJECT = new OS3Exception(
"NoSuchObject", "The specified object does not exist", HTTP_NOT_FOUND);
public static final OS3Exception MALFORMED_HEADER = new OS3Exception(
"AuthorizationHeaderMalformed", "The authorization header you provided " +
"is invalid.", HTTP_NOT_FOUND);
public static final OS3Exception NO_SUCH_KEY = new OS3Exception(
"NoSuchObject", "The specified key does not exist", HTTP_NOT_FOUND);
/**
* Create a new instance of Error.
* @param e Error Template
* @param resource Resource associated with this exception
* @return creates a new instance of error based on the template
*/
public static OS3Exception newError(OS3Exception e, Resource resource) {
public static OS3Exception newError(OS3Exception e, String resource) {
OS3Exception err = new OS3Exception(e.getCode(), e.getErrorMessage(),
e.getHttpCode());
err.setResource(resource.getResource());
err.setResource(resource);
return err;
}
/**
* Resources, which can be defined in OS3Exception.
*/
public enum Resource {
BUCKET("Bucket"),
OBJECT("Object"),
HEADER("header"),
VOLUME("Volume");
private final String resource;
/**
* Constructs resource.
* @param value
*/
Resource(String value) {
this.resource = value;
}
/**
* Get resource.
* @return string
*/
public String getResource() {
return this.resource;
}
}
}

View File

@ -52,28 +52,24 @@ public class AuthorizationHeaderV2 {
public void parseHeader() throws OS3Exception {
String[] split = authHeader.split(" ");
if (split.length != 2) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
identifier = split[0];
if (!IDENTIFIER.equals(identifier)) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
String[] remainingSplit = split[1].split(":");
if (remainingSplit.length != 2) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
accessKeyID = remainingSplit[0];
signature = remainingSplit[1];
if (isBlank(accessKeyID) || isBlank(signature)) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
}

View File

@ -64,8 +64,7 @@ public class AuthorizationHeaderV4 {
String[] split = authHeader.split(" ");
if (split.length != 4) {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
algorithm = split[0];
@ -78,24 +77,21 @@ public class AuthorizationHeaderV4 {
credential = credential.substring(CREDENTIAL.length(), credential
.length() - 1);
} else {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
if (signedHeaders.startsWith(SIGNEDHEADERS)) {
signedHeaders = signedHeaders.substring(SIGNEDHEADERS.length(),
signedHeaders.length() - 1);
} else {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
if (signature.startsWith(SIGNATURE)) {
signature = signature.substring(SIGNATURE.length(), signature
.length());
} else {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, authHeader);
}
// Parse credential. Other parts of header are not validated yet. When

View File

@ -63,8 +63,7 @@ public class Credential {
awsService = split[3];
awsRequest = split[4];
} else {
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, S3ErrorTable
.Resource.HEADER);
throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_HEADER, credential);
}
}

View File

@ -32,7 +32,7 @@ public class TestOS3Exception {
OS3Exception ex = new OS3Exception("AccessDenied", "Access Denied",
403);
String requestId = OzoneUtils.getRequestID();
ex = S3ErrorTable.newError(ex, S3ErrorTable.Resource.BUCKET);
ex = S3ErrorTable.newError(ex, "bucket");
ex.setRequestId(requestId);
String val = ex.toXml();
String formatString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +