Skip to content

Commit

Permalink
fixed problem with ParaClient where AWSv4 request signatures would br…
Browse files Browse the repository at this point in the history
…eak for PUT requests with large bodies
  • Loading branch information
albogdano committed Jan 9, 2021
1 parent c6a4b62 commit 6d045fc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import org.apache.commons.lang3.StringUtils;
import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -100,9 +100,7 @@ public ParaClient(String accessKey, String secretKey) {
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(GenericExceptionMapper.class);
clientConfig.register(new JacksonJsonProvider(mapper));
clientConfig.connectorProvider(new GrizzlyConnectorProvider((client, c, bldr) -> {
return bldr.setRequestTimeout(5 * 60 * 1000); // 5 min request timeout
}));
clientConfig.connectorProvider(new HttpUrlConnectorProvider().useSetMethodWorkaround());
SSLContext sslContext = SslConfigurator.newInstance().createSSLContext();
apiClient = ClientBuilder.newBuilder().
sslContext(sslContext).
Expand Down
7 changes: 3 additions & 4 deletions para-core/src/main/java/com/erudika/para/rest/Signer.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,16 @@ public Response invokeSignedRequest(Client apiClient, String accessKey, String s
public Map<String, String> signRequest(String accessKey, String secretKey,
String httpMethod, String endpointURL, String reqPath,
Map<String, String> headers, MultivaluedMap<String, String> params, byte[] jsonEntity) {

if (headers == null) {
headers = new HashMap<>();
}
if (StringUtils.isBlank(accessKey)) {
logger.error("Blank access key: {} {}", httpMethod, reqPath);
return headers;
}

if (StringUtils.isBlank(secretKey)) {
logger.debug("Anonymous request: {} {}", httpMethod, reqPath);
if (headers == null) {
headers = new HashMap<>();
}
headers.put(HttpHeaders.AUTHORIZATION, "Anonymous " + accessKey);
return headers;
}
Expand Down
34 changes: 0 additions & 34 deletions para-server/src/main/java/com/erudika/para/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import static com.erudika.para.validation.ValidationUtils.validateObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand Down Expand Up @@ -231,39 +230,6 @@ public static Response getEntity(InputStream is, Class<?> type) {
return Response.ok(entity).build();
}

/**
* Reads the bytes from an InputStream.
* @param in an InputStream
* @return byte[]
*/
public static byte[] readEntityBytes(InputStream in) {
byte[] jsonEntity = null;
try {
if (in != null && in.available() > 0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
baos.write(buf, 0, length);
}
jsonEntity = baos.toByteArray();
} else {
jsonEntity = new byte[0];
}
} catch (IOException ex) {
logger.error(null, ex);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
logger.error(null, ex);
}
}
return jsonEntity;
}

/**
* Process voting request and create vote object.
* @param object the object to cast vote on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,10 @@ public static boolean isValidSignature(HttpServletRequest incoming, String secre
String httpMethod = incoming.getMethod();
InputStream entity;
try {
entity = new BufferedRequestWrapper(incoming).getInputStream();
if (entity.available() <= 0) {
entity = null;
if (incoming instanceof BufferedRequestWrapper) {
entity = incoming.getInputStream();
} else {
entity = new BufferedRequestWrapper(incoming).getInputStream();
}
} catch (IOException ex) {
logger.error(null, ex);
Expand Down

0 comments on commit 6d045fc

Please sign in to comment.