Skip to content

Commit 5ed9aa3

Browse files
committed
Support using profile-based credentials for S3 binary storage
1 parent f803c23 commit 5ed9aa3

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

Diff for: deploy/jbossas/modeshape-jbossas-subsystem/src/main/java/org/modeshape/jboss/subsystem/AddS3BinaryStorage.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,24 @@ protected void writeBinaryStorageConfiguration( String repositoryName,
3535
ModelNode model,
3636
EditableDocument binaries ) throws OperationFailedException {
3737
binaries.set(FieldName.TYPE, FieldValue.BINARY_STORAGE_TYPE_S3);
38-
String username = ModelAttributes.S3_USERNAME.resolveModelAttribute(context, model).asString();
39-
binaries.setString(FieldName.USER_NAME, username);
40-
String password = ModelAttributes.S3_PASSWORD.resolveModelAttribute(context, model).asString();
41-
binaries.setString(FieldName.USER_PASSWORD, password);
42-
String bucketName = ModelAttributes.S3_BUCKET_NAME.resolveModelAttribute(context, model).asString();
43-
binaries.setString(FieldName.BUCKET_NAME, bucketName);
44-
ModelNode node = ModelAttributes.S3_ENDPOINT_URL.resolveModelAttribute(context, model);
45-
String endPoint = node.isDefined() ? node.asString() : null; //check if the node exist before getting the value
46-
binaries.setString(FieldName.ENDPOINT_URL, endPoint);
38+
39+
ModelNode usernameNode = ModelAttributes.S3_USERNAME.resolveModelAttribute(context, model);
40+
if (usernameNode.isDefined()) {
41+
binaries.setString(FieldName.USER_NAME, usernameNode.asString());
42+
}
43+
44+
ModelNode passwordNode = ModelAttributes.S3_PASSWORD.resolveModelAttribute(context, model);
45+
if (passwordNode.isDefined()) {
46+
binaries.setString(FieldName.USER_PASSWORD, passwordNode.asString());
47+
}
48+
49+
ModelNode bucketNameNode = ModelAttributes.S3_BUCKET_NAME.resolveModelAttribute(context, model);
50+
binaries.setString(FieldName.BUCKET_NAME, bucketNameNode.asString());
51+
52+
ModelNode endPointNode = ModelAttributes.S3_ENDPOINT_URL.resolveModelAttribute(context, model);
53+
if (endPointNode.isDefined()) {
54+
binaries.setString(FieldName.ENDPOINT_URL, endPointNode.asString());
55+
}
4756
}
4857

4958
@Override

Diff for: deploy/jbossas/modeshape-jbossas-subsystem/src/main/java/org/modeshape/jboss/subsystem/ModelAttributes.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -961,15 +961,15 @@ public class ModelAttributes {
961961
new MappedAttributeDefinitionBuilder(Attribute.USERNAME.getLocalName(), ModelType.STRING,
962962
FieldName.STORAGE, FieldName.BINARY_STORAGE, FieldName.USER_NAME)
963963
.setAllowExpression(true)
964-
.setAllowNull(false)
964+
.setAllowNull(true)
965965
.setFlags(AttributeAccess.Flag.RESTART_RESOURCE_SERVICES)
966966
.build();
967967

968968
public static final MappedSimpleAttributeDefinition S3_PASSWORD =
969969
new MappedAttributeDefinitionBuilder(Attribute.PASSWORD.getLocalName(), ModelType.STRING,
970970
FieldName.STORAGE, FieldName.BINARY_STORAGE, FieldName.USER_PASSWORD)
971971
.setAllowExpression(true)
972-
.setAllowNull(false)
972+
.setAllowNull(true)
973973
.setFlags(AttributeAccess.Flag.RESTART_RESOURCE_SERVICES)
974974
.build();
975975

Diff for: deploy/jbossas/modeshape-jbossas-subsystem/src/test/resources/org/modeshape/jboss/subsystem/modeshape-s3-binary-storage.xml

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
<s3-binary-storage mime-type-detection="content" min-string-size="10" min-value-size="4096"
44
username="test" password="test" bucket-name="test" />
55
</repository>
6+
<repository name="sample-using-profile">
7+
<s3-binary-storage mime-type-detection="content" min-string-size="10" min-value-size="4096"
8+
bucket-name="test" />
9+
</repository>
610
</subsystem>

Diff for: modeshape-jcr/src/main/java/org/modeshape/jcr/RepositoryConfiguration.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -1211,14 +1211,7 @@ public BinaryStore getBinaryStore() throws Exception {
12111211
String password = binaryStorage.getString(FieldName.USER_PASSWORD);
12121212
String bucketName = binaryStorage.getString(FieldName.BUCKET_NAME);
12131213
String endPoint = binaryStorage.getString(FieldName.ENDPOINT_URL);
1214-
1215-
//Use S3 provided endpoints
1216-
if (endPoint != null) {
1217-
store = new S3BinaryStore(username, password, bucketName, endPoint);
1218-
}
1219-
else { //Use default AWS endpoint
1220-
store = new S3BinaryStore(username, password, bucketName);
1221-
}
1214+
store = new S3BinaryStore(username, password, bucketName, endPoint);
12221215
}
12231216

12241217
if (store == null) store = TransientBinaryStore.get();

Diff for: modeshape-jcr/src/main/java/org/modeshape/jcr/value/binary/S3BinaryStore.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,11 @@
1515
*/
1616
package org.modeshape.jcr.value.binary;
1717

18-
import java.io.IOException;
19-
import java.io.InputStream;
20-
import java.util.Collections;
21-
import java.util.Date;
22-
import java.util.Iterator;
23-
import java.util.Map;
24-
import java.util.concurrent.TimeUnit;
25-
26-
import javax.jcr.RepositoryException;
27-
2818
import com.amazonaws.AmazonClientException;
19+
import com.amazonaws.auth.AWSCredentialsProvider;
2920
import com.amazonaws.auth.BasicAWSCredentials;
21+
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
22+
import com.amazonaws.internal.StaticCredentialsProvider;
3023
import com.amazonaws.services.s3.AmazonS3Client;
3124
import com.amazonaws.services.s3.iterable.S3Objects;
3225
import com.amazonaws.services.s3.model.CopyObjectRequest;
@@ -38,6 +31,15 @@
3831
import org.modeshape.jcr.value.BinaryKey;
3932
import org.modeshape.jcr.value.BinaryValue;
4033

34+
import javax.jcr.RepositoryException;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.util.Collections;
38+
import java.util.Date;
39+
import java.util.Iterator;
40+
import java.util.Map;
41+
import java.util.concurrent.TimeUnit;
42+
4143
/**
4244
* Binary storage option which manages the storage of files to Amazon S3
4345
*
@@ -93,7 +95,14 @@ public S3BinaryStore(String accessKey, String secretKey, String bucketName) thro
9395
*/
9496
public S3BinaryStore(String accessKey, String secretKey, String bucketName, String endPoint) throws BinaryStoreException {
9597
this.bucketName = bucketName;
96-
this.s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
98+
99+
AWSCredentialsProvider credentialsProvider;
100+
if (accessKey == null && secretKey == null) {
101+
credentialsProvider = new ProfileCredentialsProvider();
102+
} else {
103+
credentialsProvider = new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
104+
}
105+
this.s3Client = new AmazonS3Client(credentialsProvider);
97106

98107
// Support for compatible S3 storage systems
99108
if(endPoint != null)

Diff for: modeshape-jcr/src/main/resources/org/modeshape/jcr/repository-config-schema.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,12 @@
476476
"username" : {
477477
"type" : "string",
478478
"description" : "The access key ID for the connection to Amazon S3.",
479-
"required" : true
479+
"required" : false
480480
},
481481
"password" : {
482482
"type" : "string",
483483
"description" : "The secret key for the connection to Amazon S3.",
484-
"required" : true
484+
"required" : false
485485
},
486486
"bucketName" : {
487487
"type" : "string",

0 commit comments

Comments
 (0)