Skip to content

Commit

Permalink
cleanup error handling for instance provider (AthenZ#473)
Browse files Browse the repository at this point in the history
* cleanup error handling for instance provider

* cleanup error handling for instance provider
  • Loading branch information
havetisyan authored May 21, 2018
1 parent 918d04b commit 84a523b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ public class ResourceError {
private int code;
private String message;

public ResourceError code(int code) {
public void setCode(int code) {
this.code = code;
return this;
}
public ResourceError message(String message) {

public void setMessage(String message) {
this.message = message;
return this;
}

public int getCode() {
return code;
}

public String getMessage() {
return message;
}

public String toString() {
return "{code: " + code + ", message: \"" + message + "\"}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,13 @@
*/
package com.yahoo.athenz.instance.provider;

@SuppressWarnings("WeakerAccess")
public class ResourceException extends RuntimeException {
public final static int OK = 200;
public final static int CREATED = 201;
public final static int ACCEPTED = 202;
public final static int NO_CONTENT = 204;
public final static int MOVED_PERMANENTLY = 301;
public final static int FOUND = 302;
public final static int SEE_OTHER = 303;
public final static int NOT_MODIFIED = 304;
public final static int TEMPORARY_REDIRECT = 307;
public final static int BAD_REQUEST = 400;
public final static int UNAUTHORIZED = 401;

public final static int FORBIDDEN = 403;
public final static int NOT_FOUND = 404;
public final static int CONFLICT = 409;
public final static int GONE = 410;
public final static int PRECONDITION_FAILED = 412;
public final static int UNSUPPORTED_MEDIA_TYPE = 415;
public final static int PRECONDITION_REQUIRED = 428;
public final static int TOO_MANY_REQUESTS = 429;
public final static int REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
public final static int INTERNAL_SERVER_ERROR = 500;
public final static int NOT_IMPLEMENTED = 501;
public final static int SERVICE_UNAVAILABLE = 503;
public final static int NETWORK_AUTHENTICATION_REQUIRED = 511;

public static String codeToString(int code) {
switch (code) {
case OK: return "OK";
case CREATED: return "Created";
case ACCEPTED: return "Accepted";
case NO_CONTENT: return "No Content";
case MOVED_PERMANENTLY: return "Moved Permanently";
case FOUND: return "Found";
case SEE_OTHER: return "See Other";
case NOT_MODIFIED: return "Not Modified";
case TEMPORARY_REDIRECT: return "Temporary Redirect";
case BAD_REQUEST: return "Bad Request";
case UNAUTHORIZED: return "Unauthorized";
case FORBIDDEN: return "Forbidden";
case NOT_FOUND: return "Not Found";
case CONFLICT: return "Conflict";
case GONE: return "Gone";
case PRECONDITION_FAILED: return "Precondition Failed";
case UNSUPPORTED_MEDIA_TYPE: return "Unsupported Media Type";
case PRECONDITION_REQUIRED: return "Precondition Required";
case TOO_MANY_REQUESTS: return "Too Many Requests";
case REQUEST_HEADER_FIELDS_TOO_LARGE: return "Request Header Fields Too Large";
case INTERNAL_SERVER_ERROR: return "Internal Server Error";
case NOT_IMPLEMENTED: return "Not Implemented";
case SERVICE_UNAVAILABLE: return "Service Unavailable";
case NETWORK_AUTHENTICATION_REQUIRED: return "Network Authentication Required";
default: return "" + code;
}
}

final int code;
final Object data;

public ResourceException(int code) {
this(code, new ResourceError().code(code).message(codeToString(code)));
}
final private int code;
final private Object data;

public ResourceException(int code, Object data) {
super("ResourceException (" + code + "): " + data);
Expand All @@ -96,5 +40,4 @@ public Object getData() {
public <T> T getData(Class<T> cl) {
return cl.cast(data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.testng.annotations.Test;

import com.yahoo.athenz.auth.util.Crypto;
import com.yahoo.athenz.instance.provider.ProviderHostnameVerifier;

public class ProviderHostnameVerifierTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class ResourceErrorTest {

@Test
public void testResourceError() {
ResourceError error = new ResourceError().code(401).message("unauthorized");
ResourceError error = new ResourceError();
error.setCode(401);
error.setMessage("unauthorized");
assertEquals(error.toString(), "{code: 401, message: \"unauthorized\"}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,13 @@

public class ResourceExceptionTest {

@Test
public void testCodeToString() {

assertEquals("OK", ResourceException.codeToString(200));
assertEquals("Created", ResourceException.codeToString(201));
assertEquals("Accepted", ResourceException.codeToString(202));
assertEquals("No Content", ResourceException.codeToString(204));
assertEquals("Moved Permanently", ResourceException.codeToString(301));
assertEquals("Found", ResourceException.codeToString(302));
assertEquals("See Other", ResourceException.codeToString(303));
assertEquals("Not Modified", ResourceException.codeToString(304));
assertEquals("Temporary Redirect", ResourceException.codeToString(307));
assertEquals("Bad Request", ResourceException.codeToString(400));
assertEquals("Unauthorized", ResourceException.codeToString(401));
assertEquals("Forbidden", ResourceException.codeToString(403));
assertEquals("Not Found", ResourceException.codeToString(404));
assertEquals("Conflict", ResourceException.codeToString(409));
assertEquals("Gone", ResourceException.codeToString(410));
assertEquals("Precondition Failed", ResourceException.codeToString(412));
assertEquals("Unsupported Media Type", ResourceException.codeToString(415));
assertEquals("Precondition Required", ResourceException.codeToString(428));
assertEquals("Too Many Requests", ResourceException.codeToString(429));
assertEquals("Request Header Fields Too Large", ResourceException.codeToString(431));
assertEquals("Internal Server Error", ResourceException.codeToString(500));
assertEquals("Not Implemented", ResourceException.codeToString(501));
assertEquals("Service Unavailable", ResourceException.codeToString(503));
assertEquals("Network Authentication Required", ResourceException.codeToString(511));
assertEquals("1001", ResourceException.codeToString(1001));
}

@Test
public void testCodeOnly() {

ResourceException exc = new ResourceException(400);

ResourceError resError = new ResourceError();
resError.setCode(400);
resError.setMessage("Bad Request");
ResourceException exc = new ResourceException(400, resError);
assertEquals(exc.getData().toString(), "{code: 400, message: \"Bad Request\"}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ public void testVerifyInstanceIdentityException() {
MockInstanceAWSProvider provider = new MockInstanceAWSProvider();
provider.setIdentitySuper(true);
AWSSecurityTokenServiceClient mockClient = Mockito.mock(AWSSecurityTokenServiceClient.class);
Mockito.when(mockClient.getCallerIdentity(ArgumentMatchers.any())).thenThrow(new ResourceException(101));
Mockito.when(mockClient.getCallerIdentity(ArgumentMatchers.any()))
.thenThrow(new ResourceException(101, "invaliderror"));
provider.setStsClient(mockClient);

AWSAttestationData info = new AWSAttestationData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4670,7 +4670,7 @@ public void testPostInstanceRefreshInformationForbidden() throws IOException {
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);

Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient);
Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(403));
Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(403, "Forbidden"));

X509CertRecord certRecord = new X509CertRecord();
certRecord.setInstanceId("1001");
Expand Down Expand Up @@ -4737,7 +4737,7 @@ public void testPostInstanceRefreshInformationNotFound() throws IOException {
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);

Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient);
Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(404));
Mockito.when(providerClient.refreshInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(404, "Not Found"));

X509CertRecord certRecord = new X509CertRecord();
certRecord.setInstanceId("1001");
Expand Down

0 comments on commit 84a523b

Please sign in to comment.