Skip to content

Commit

Permalink
V2 (#6)
Browse files Browse the repository at this point in the history
* Porting the code to make use of the AWS SDK for Java 2.0.
  • Loading branch information
msailes committed Aug 19, 2020
1 parent 07587a7 commit df627a5
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 209 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ You can download release builds through the [releases section of this](https://g
* **Minimum requirements** -- You'll need Java 8 (or later) and [Maven 3](http://maven.apache.org/).
* **Download** -- Download the [latest preview release](https://github.com/awslabs/large-payload-offloading-java-common-lib-for-aws/releases) or pick it up from Maven:

### Version 2.x
```xml
<dependency>
<groupId>software.amazon.payloadoffloading</groupId>
<artifactId>payloadoffloading-common</artifactId>
<version>2.0.0</version>
</dependency>
```

### Version 1.x
```xml
<dependency>
<groupId>software.amazon.payloadoffloading</groupId>
<artifactId>payloadoffloading-common</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
```

Expand Down
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>software.amazon.payloadoffloading</groupId>
<artifactId>payloadoffloading-common</artifactId>
<version>1.0.0</version>
<version>2.0.0</version>
<packaging>jar</packaging>
<name>Payload offloading common library for AWS</name>
<description>Common library between extended Amazon AWS clients to save payloads up to 2GB on Amazon S3.</description>
Expand Down Expand Up @@ -36,15 +36,21 @@
</developers>

<properties>
<aws-java-sdk.version>1.11.817</aws-java-sdk.version>
<aws-java-sdk.version>2.13.64</aws-java-sdk.version>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>utils</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/software/amazon/payloadoffloading/AwsManagedCmk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package software.amazon.payloadoffloading;

import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.ServerSideEncryption;

public class AwsManagedCmk implements ServerSideEncryptionStrategy {
@Override
public void decorate(PutObjectRequest.Builder putObjectRequestBuilder) {
putObjectRequestBuilder.serverSideEncryption(ServerSideEncryption.AWS_KMS);
}
}
18 changes: 18 additions & 0 deletions src/main/java/software/amazon/payloadoffloading/CustomerKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package software.amazon.payloadoffloading;

import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.ServerSideEncryption;

public class CustomerKey implements ServerSideEncryptionStrategy {
private final String awsKmsKeyId;

public CustomerKey(String awsKmsKeyId) {
this.awsKmsKeyId = awsKmsKeyId;
}

@Override
public void decorate(PutObjectRequest.Builder putObjectRequestBuilder) {
putObjectRequestBuilder.serverSideEncryption(ServerSideEncryption.AWS_KMS);
putObjectRequestBuilder.ssekmsKeyId(awsKmsKeyId);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package software.amazon.payloadoffloading;

import com.amazonaws.AmazonClientException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.core.exception.SdkClientException;

/**
* This class is used for carrying pointer to Amazon S3 objects which contain payloads.
*/
public class PayloadS3Pointer {
private static final Log LOG = LogFactory.getLog(PayloadS3Pointer.class);
private static final Logger LOG = LoggerFactory.getLogger(PayloadS3Pointer.class);
private String s3BucketName;
private String s3Key;

Expand Down Expand Up @@ -38,7 +38,7 @@ public String toJson() {
} catch (Exception e) {
String errorMessage = "Failed to convert S3 object pointer to text.";
LOG.error(errorMessage, e);
throw new AmazonClientException(errorMessage, e);
throw SdkClientException.create(errorMessage, e);
}
return s3PointerStr;
}
Expand All @@ -52,7 +52,7 @@ public static PayloadS3Pointer fromJson(String s3PointerJson) {
} catch (Exception e) {
String errorMessage = "Failed to read the S3 object pointer from given string.";
LOG.error(errorMessage, e);
throw new AmazonClientException(errorMessage, e);
throw SdkClientException.create(errorMessage, e);
}
return s3Pointer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,61 @@
package software.amazon.payloadoffloading;

import com.amazonaws.AmazonClientException;
import com.amazonaws.annotation.NotThreadSafe;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.SSEAwsKeyManagementParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.NotThreadSafe;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.services.s3.S3Client;

/**
* Amazon payload storage configuration options such as Amazon S3 client,
* bucket name, and payload size threshold for payloads.
* <p>Amazon payload storage configuration options such as Amazon S3 client,
* bucket name, and payload size threshold for payloads.</p>
*
* <p>Server side encryption is optional and can be enabled using with {@link #withServerSideEncryption(ServerSideEncryptionStrategy)}
* or {@link #setServerSideEncryptionStrategy(ServerSideEncryptionStrategy)}</p>
*
* <p>There are two possible options for server side encrption. This can be using a customer managed key or AWS managed CMK.</p>
*
* Example usage:
*
* <pre>
* withServerSideEncryption(ServerSideEncrptionFactory.awsManagedCmk())
* </pre>
*
* or
*
* <pre>
* withServerSideEncryption(ServerSideEncrptionFactory.customerKey(YOUR_CUSTOMER_ID))
* </pre>
*
* @see software.amazon.payloadoffloading.ServerSideEncryptionFactory
*/
@NotThreadSafe
public class PayloadStorageConfiguration {
private static final Log LOG = LogFactory.getLog(PayloadStorageConfiguration.class);
private static final Logger LOG = LoggerFactory.getLogger(PayloadStorageConfiguration.class);

private AmazonS3 s3;
private S3Client s3;
private String s3BucketName;
private int payloadSizeThreshold = 0;
private boolean alwaysThroughS3 = false;
private boolean payloadSupport = false;
/**
* This field is optional, it is set only when we want to configure S3 Server Side Encryption with KMS.
*/
private SSEAwsKeyManagementParams sseAwsKeyManagementParams;
private ServerSideEncryptionStrategy serverSideEncryptionStrategy;

public PayloadStorageConfiguration() {
s3 = null;
s3BucketName = null;
sseAwsKeyManagementParams = null;
serverSideEncryptionStrategy = null;
}

public PayloadStorageConfiguration(PayloadStorageConfiguration other) {
this.s3 = other.getAmazonS3Client();
this.s3 = other.getS3Client();
this.s3BucketName = other.getS3BucketName();
this.sseAwsKeyManagementParams = other.getSSEAwsKeyManagementParams();
this.payloadSupport = other.isPayloadSupportEnabled();
this.alwaysThroughS3 = other.isAlwaysThroughS3();
this.payloadSizeThreshold = other.getPayloadSizeThreshold();
this.serverSideEncryptionStrategy = other.getServerSideEncryptionStrategy();
}

/**
Expand All @@ -47,11 +65,11 @@ public PayloadStorageConfiguration(PayloadStorageConfiguration other) {
* @param s3BucketName Name of the bucket which is going to be used for storing payload.
* The bucket must be already created and configured in s3.
*/
public void setPayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
public void setPayloadSupportEnabled(S3Client s3, String s3BucketName) {
if (s3 == null || s3BucketName == null) {
String errorMessage = "S3 client and/or S3 bucket name cannot be null.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
throw SdkClientException.create(errorMessage);
}
if (isPayloadSupportEnabled()) {
LOG.warn("Payload support is already enabled. Overwriting AmazonS3Client and S3BucketName.");
Expand All @@ -70,7 +88,7 @@ public void setPayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
* The bucket must be already created and configured in s3.
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withPayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
public PayloadStorageConfiguration withPayloadSupportEnabled(S3Client s3, String s3BucketName) {
setPayloadSupportEnabled(s3, s3BucketName);
return this;
}
Expand Down Expand Up @@ -109,7 +127,7 @@ public boolean isPayloadSupportEnabled() {
*
* @return Reference to the Amazon S3 client which is being used.
*/
public AmazonS3 getAmazonS3Client() {
public S3Client getS3Client() {
return s3;
}

Expand All @@ -122,35 +140,6 @@ public String getS3BucketName() {
return s3BucketName;
}

/**
* Gets the S3 SSE-KMS encryption params of S3 objects under configured S3 bucket name.
*
* @return The S3 SSE-KMS params used for encryption.
*/
public SSEAwsKeyManagementParams getSSEAwsKeyManagementParams() {
return sseAwsKeyManagementParams;
}

/**
* Sets the the S3 SSE-KMS encryption params of S3 objects under configured S3 bucket name.
*
* @param sseAwsKeyManagementParams The S3 SSE-KMS params used for encryption.
*/
public void setSSEAwsKeyManagementParams(SSEAwsKeyManagementParams sseAwsKeyManagementParams) {
this.sseAwsKeyManagementParams = sseAwsKeyManagementParams;
}

/**
* Sets the the S3 SSE-KMS encryption params of S3 objects under configured S3 bucket name.
*
* @param sseAwsKeyManagementParams The S3 SSE-KMS params used for encryption.
* @return the updated PayloadStorageConfiguration object
*/
public PayloadStorageConfiguration withSSEAwsKeyManagementParams(SSEAwsKeyManagementParams sseAwsKeyManagementParams) {
setSSEAwsKeyManagementParams(sseAwsKeyManagementParams);
return this;
}

/**
* Sets the payload size threshold for storing payloads in Amazon S3.
*
Expand Down Expand Up @@ -212,4 +201,38 @@ public boolean isAlwaysThroughS3() {
public void setAlwaysThroughS3(boolean alwaysThroughS3) {
this.alwaysThroughS3 = alwaysThroughS3;
}

/**
* Sets which method of server side encryption should be used, if required.
*
* This is optional, it is set only when you want to configure S3 server side encryption with KMS.
*
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withServerSideEncryption(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
setServerSideEncryptionStrategy(serverSideEncryptionStrategy);
return this;
}

/**
* Sets which method of server side encryption should be use, if required.
*
* This is optional, it is set only when you want to configure S3 Server Side Encryption with KMS.
*
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
*/
public void setServerSideEncryptionStrategy(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
this.serverSideEncryptionStrategy = serverSideEncryptionStrategy;
}

/**
* The method of service side encryption which should be used, if required.
*
* @return The server side encryption method required. Default null.
*/
public ServerSideEncryptionStrategy getServerSideEncryptionStrategy() {
return this.serverSideEncryptionStrategy;
}

}
19 changes: 9 additions & 10 deletions src/main/java/software/amazon/payloadoffloading/PayloadStore.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package software.amazon.payloadoffloading;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.services.s3.model.S3Exception;

/**
* An AWS storage service that supports saving high payload sizes.
Expand All @@ -11,26 +11,25 @@ public interface PayloadStore {
* Stores payload in a store that has higher payload size limit than that is supported by original payload store.
*
* @param payload
* @param payloadContentSize
* @return a pointer that must be used to retrieve the original payload later.
* @throws AmazonClientException If any internal errors are encountered on the client side while
* @throws SdkClientException If any internal errors are encountered on the client side while
* attempting to make the request or handle the response. For example
* if a network connection is not available.
* @throws AmazonServiceException If an error response is returned by actual PayloadStore indicating
* @throws S3Exception If an error response is returned by actual PayloadStore indicating
* either a problem with the data in the request, or a server side issue.
*/
String storeOriginalPayload(String payload, Long payloadContentSize);
String storeOriginalPayload(String payload);

/**
* Retrieves the original payload using the given payloadPointer. The pointer must
* have been obtained using {@link storeOriginalPayload}
*
* @param payloadPointer
* @return original payload
* @throws AmazonClientException If any internal errors are encountered on the client side while
* @throws SdkClientException If any internal errors are encountered on the client side while
* attempting to make the request or handle the response. For example
* if payloadPointer is invalid or a network connection is not available.
* @throws AmazonServiceException If an error response is returned by actual PayloadStore indicating
* @throws S3Exception If an error response is returned by actual PayloadStore indicating
* a server side issue.
*/
String getOriginalPayload(String payloadPointer);
Expand All @@ -40,10 +39,10 @@ public interface PayloadStore {
* have been obtained using {@link storeOriginalPayload}
*
* @param payloadPointer
* @throws AmazonClientException If any internal errors are encountered on the client side while
* @throws SdkClientException If any internal errors are encountered on the client side while
* attempting to make the request or handle the response to/from PayloadStore.
* For example, if payloadPointer is invalid or a network connection is not available.
* @throws AmazonServiceException If an error response is returned by actual PayloadStore indicating
* @throws S3Exception If an error response is returned by actual PayloadStore indicating
* a server side issue.
*/
void deleteOriginalPayload(String payloadPointer);
Expand Down
Loading

0 comments on commit df627a5

Please sign in to comment.