Skip to content

Commit 0ca7b16

Browse files
author
James Barnett
committed
Add support for canned acls when putting object to S3 for cross account resource scenarios
1 parent df0c625 commit 0ca7b16

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

src/main/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,9 @@ private void storeTextInS3(String s3Key, String messageContentStr, Long messageC
13341334
messageContentStreamMetadata.setContentLength(messageContentSize);
13351335
PutObjectRequest putObjectRequest = new PutObjectRequest(clientConfiguration.getS3BucketName(), s3Key,
13361336
messageContentStream, messageContentStreamMetadata);
1337+
if (clientConfiguration.isCannedAccessControlListDefined()) {
1338+
putObjectRequest.withCannedAcl(clientConfiguration.getCannedAccessControlList());
1339+
}
13371340
try {
13381341
clientConfiguration.getAmazonS3Client().putObject(putObjectRequest);
13391342
} catch (AmazonServiceException e) {

src/main/java/com/amazon/sqs/javamessaging/ExtendedClientConfiguration.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.amazonaws.AmazonClientException;
1919
import com.amazonaws.services.s3.AmazonS3;
20+
import com.amazonaws.services.s3.model.CannedAccessControlList;
2021
import org.apache.commons.logging.Log;
2122
import org.apache.commons.logging.LogFactory;
2223
import com.amazonaws.annotation.NotThreadSafe;
@@ -36,6 +37,7 @@ public class ExtendedClientConfiguration {
3637
private boolean largePayloadSupport = false;
3738
private boolean alwaysThroughS3 = false;
3839
private int messageSizeThreshold = SQSExtendedClientConstants.DEFAULT_MESSAGE_SIZE_THRESHOLD;
40+
private CannedAccessControlList cannedAccessControlList;
3941

4042
public ExtendedClientConfiguration() {
4143
s3 = null;
@@ -48,6 +50,7 @@ public ExtendedClientConfiguration(ExtendedClientConfiguration other) {
4850
this.largePayloadSupport = other.largePayloadSupport;
4951
this.alwaysThroughS3 = other.alwaysThroughS3;
5052
this.messageSizeThreshold = other.messageSizeThreshold;
53+
this.cannedAccessControlList = other.cannedAccessControlList;
5154
}
5255

5356
/**
@@ -214,4 +217,39 @@ public ExtendedClientConfiguration withAlwaysThroughS3(boolean alwaysThroughS3)
214217
public boolean isAlwaysThroughS3() {
215218
return alwaysThroughS3;
216219
}
220+
221+
/**
222+
* Configures the ACL to apply to the Amazon S3 putObject request.
223+
* @param cannedAccessControlList
224+
* The ACL to be used when storing objects in Amazon S3
225+
*/
226+
public void setCannedAccessControlList(CannedAccessControlList cannedAccessControlList) {
227+
this.cannedAccessControlList = cannedAccessControlList;
228+
}
229+
230+
/**
231+
* Configures the ACL to apply to the Amazon S3 putObject request.
232+
* @param cannedAccessControlList
233+
* The ACL to be used when storing objects in Amazon S3
234+
*/
235+
public ExtendedClientConfiguration withCannedAccessControlList(CannedAccessControlList cannedAccessControlList) {
236+
setCannedAccessControlList(cannedAccessControlList);
237+
return this;
238+
}
239+
240+
/**
241+
* Checks whether an ACL have been configured for storing objects in Amazon S3.
242+
* @return True if ACL is defined
243+
*/
244+
public boolean isCannedAccessControlListDefined() {
245+
return null != cannedAccessControlList;
246+
}
247+
248+
/**
249+
* Gets the AWS ACL to apply to the Amazon S3 putObject request.
250+
* @return Amazon S3 object ACL
251+
*/
252+
public CannedAccessControlList getCannedAccessControlList() {
253+
return cannedAccessControlList;
254+
}
217255
}

src/test/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClientTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222

2323
import com.amazonaws.services.s3.AmazonS3;
24+
import com.amazonaws.services.s3.model.CannedAccessControlList;
2425
import com.amazonaws.services.s3.model.PutObjectRequest;
2526
import com.amazonaws.services.sqs.AmazonSQS;
2627
import com.amazonaws.services.sqs.AmazonSQSClient;
@@ -209,7 +210,7 @@ public void testWhenSmallMessageIsSentThenNoAttributeIsAdded() {
209210
}
210211

211212
@Test
212-
public void testWhenLargeMessgaeIsSentThenAttributeWithPayloadSizeIsAdded() {
213+
public void testWhenLargeMessageIsSentThenAttributeWithPayloadSizeIsAdded() {
213214
int messageLength = MORE_THAN_SQS_SIZE_LIMIT;
214215
String messageBody = generateStringWithLength(messageLength);
215216

@@ -224,6 +225,24 @@ public void testWhenLargeMessgaeIsSentThenAttributeWithPayloadSizeIsAdded() {
224225
Assert.assertEquals(messageLength, (int)Integer.valueOf(attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).getStringValue()));
225226
}
226227

228+
@Test
229+
public void testWhenSendMessageWIthCannedAccessControlListDefined() {
230+
CannedAccessControlList expected = CannedAccessControlList.BucketOwnerFullControl;
231+
String messageBody = generateStringWithLength(MORE_THAN_SQS_SIZE_LIMIT);
232+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
233+
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withCannedAccessControlList(expected);
234+
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
235+
236+
SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
237+
sqsExtended.sendMessage(messageRequest);
238+
239+
ArgumentCaptor<PutObjectRequest> captor = ArgumentCaptor.forClass(PutObjectRequest.class);
240+
241+
verify(mockS3).putObject(captor.capture());
242+
243+
Assert.assertEquals(expected, captor.getValue().getCannedAcl());
244+
}
245+
227246
private String generateStringWithLength(int messageLength) {
228247
char[] charArray = new char[messageLength];
229248
Arrays.fill(charArray, 'x');

src/test/java/com/amazon/sqs/javamessaging/ExtendedClientConfigurationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.amazon.sqs.javamessaging;
1717

1818
import com.amazonaws.services.s3.AmazonS3;
19+
import com.amazonaws.services.s3.model.CannedAccessControlList;
1920
import com.amazonaws.services.s3.model.PutObjectRequest;
2021
import junit.framework.Assert;
2122
import org.junit.Before;
@@ -116,5 +117,18 @@ public void testMessageSizeThreshold() {
116117

117118
}
118119

120+
@Test
121+
public void testCannedAccessControlList() {
122+
123+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();
124+
125+
Assert.assertFalse(extendedClientConfiguration.isCannedAccessControlListDefined());
126+
127+
final CannedAccessControlList accessControlList = CannedAccessControlList.BucketOwnerFullControl;
128+
extendedClientConfiguration.withCannedAccessControlList(accessControlList);
129+
Assert.assertTrue(extendedClientConfiguration.isCannedAccessControlListDefined());
130+
Assert.assertEquals(accessControlList, extendedClientConfiguration.getCannedAccessControlList());
131+
132+
}
119133

120134
}

0 commit comments

Comments
 (0)