add testing credentials for file storage

This commit is contained in:
Fredrik Teschke
2015-01-23 15:01:26 +01:00
parent 5fd5c67cd3
commit b18632dda6
7 changed files with 86 additions and 45 deletions
@@ -6,7 +6,8 @@ Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: com.sap.sse.filestorage,
com.sap.sse.filestorage.impl
com.sap.sse.filestorage.impl,
com.sap.sse.filestorage.testsupport
Import-Package: org.osgi.framework;version="1.8.0"
Bundle-Activator: com.sap.sse.filestorage.impl.Activator
Require-Bundle: org.apache.servicemix.bundles.aws-java-sdk;bundle-version="1.9.8",
@@ -17,13 +17,15 @@ public class InvalidPropertiesException extends Exception {
super(message, cause);
}
@SafeVarargs
public InvalidPropertiesException(String message,
@SuppressWarnings("unchecked") Pair<Property, String>... perPropertyMessages) {
Pair<Property, String>... perPropertyMessages) {
this(message, null, perPropertyMessages);
}
@SafeVarargs
public InvalidPropertiesException(String message, Throwable cause,
@SuppressWarnings("unchecked") Pair<Property, String>... perPropertyMessages) {
Pair<Property, String>... perPropertyMessages) {
super(message, cause);
for (Pair<Property, String> pair : perPropertyMessages) {
this.perPropertyMessages.put(pair.getA(), pair.getB());
@@ -18,6 +18,7 @@ import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.sap.sse.common.Util.Pair;
import com.sap.sse.filestorage.FileStorageService;
import com.sap.sse.filestorage.InvalidPropertiesException;
import com.sap.sse.filestorage.OperationFailedException;
@@ -40,10 +41,12 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
private static final String retrievalProtocol = "http";
// private static final String bucketName = "ftes-sap-sailing";
private final PropertyImpl accessId = new PropertyImpl("accessId", true, "Access ID");
private final PropertyImpl accessKey = new PropertyImpl("accessKey", true, "Secret Access Key");
private final PropertyImpl accessId = new PropertyImpl("accessId", false,
"Access ID (leave blank to use ~/.aws/credentials instead)");
private final PropertyImpl accessKey = new PropertyImpl("accessKey", false,
"Secret Access Key (leave blank to use ~/.aws/credentials instead)");
private final PropertyImpl bucketName = new PropertyImpl("bucketName", true,
"Name of Bucket to use (has to already exist)");
"Name of Bucket to use (has to already exist, and user needs sufficient permissions)");
private final Map<String, PropertyImpl> properties = new HashMap<>();
public AmazonS3FileStorageServiceImpl() {
@@ -56,36 +59,24 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
}
}
private void testCredentials(AWSCredentials credentials) throws Exception {
AmazonS3Client s3 = new AmazonS3Client(credentials);
s3.getS3AccountOwner(); // might throw exception
}
private AmazonS3Client createS3Client() throws InvalidPropertiesException {
AWSCredentials credentials = null;
AWSCredentials creds;
// first try to use properties
if (accessId.getValue() != null && accessKey.getValue() != null) {
credentials = new BasicAWSCredentials(accessId.getValue(), accessKey.getValue());
creds = new BasicAWSCredentials(accessId.getValue(), accessKey.getValue());
} else {
// if properties are empty, read credentials from ~/.aws/credentials
try {
testCredentials(credentials);
return new AmazonS3Client(credentials);
creds = new ProfileCredentialsProvider().getCredentials();
} catch (Exception e) {
throw new InvalidPropertiesException("Access ID and secret key and ID seem to be invalid", e);
throw new InvalidPropertiesException(
"credentials in ~/.aws/credentials seem to be invalid (tried this as fallback because properties were empty)",
e);
}
}
// if properties are empty, read credentials from ~/.aws/credentials
try {
credentials = new ProfileCredentialsProvider().getCredentials();
testCredentials(credentials);
return new AmazonS3Client(credentials);
} catch (Exception e) {
throw new InvalidPropertiesException(
"Credentials in ~/.aws/credentials seem to be invalid (tried this as fallback because properties were empty)",
e);
}
return new AmazonS3Client(creds);
}
private static String getKey(String originalFileName) {
@@ -163,6 +154,21 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
@Override
public void testProperties() throws InvalidPropertiesException {
createS3Client();
AmazonS3Client s3 = createS3Client();
// test if credentials are valid
// TODO seems to even work if credentials are not valid if bucket is publicly visible
try {
s3.doesBucketExist(bucketName.getValue());
} catch (Exception e) {
throw new InvalidPropertiesException("invalid credentials or not enough access rights for the bucket", e,
new Pair<>(accessId, "seems to be invalid"), new Pair<>(accessKey, "seems to be invalid"));
}
// test if bucket exists
if (!s3.doesBucketExist(bucketName.getValue())) {
throw new InvalidPropertiesException("invalid bucket", new Pair<>(bucketName,
"bucket does not exist"));
}
}
}
@@ -0,0 +1,19 @@
package com.sap.sse.filestorage.testsupport;
import com.sap.sse.filestorage.InvalidPropertiesException;
import com.sap.sse.filestorage.impl.AmazonS3FileStorageServiceImpl;
public class AmazonS3TestSupport {
public static final String s3AccessId = "AKIAJOX7PZ6ACI2FQU4A";
public static final String s3AccessKey = "NkijH2DfhWgb9fmESPjpeIbpUF+tC220KyTOfvGJ";
private static final String s3BucketName = "sapsailing-automatic-upload-test";
public static AmazonS3FileStorageServiceImpl createService() throws InvalidPropertiesException {
AmazonS3FileStorageServiceImpl service = new AmazonS3FileStorageServiceImpl();
service.setProperty("accessId", s3AccessId);
service.setProperty("accessKey", s3AccessKey);
service.setProperty("bucketName", s3BucketName);
service.testProperties();
return service;
}
}