Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CannedAccessControlList as a configuration option #8

Merged
merged 1 commit into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ src_managed/
project/boot/
project/plugins/project/


# Idea
.idea/
91 changes: 49 additions & 42 deletions src/main/java/ohnosequences/ivy/S3Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,50 @@

/**
* A repository the allows you to upload and download from an S3 repository.
*
*
* @author Ben Hale
* @author Evdokim Kovach
*/
public class S3Repository extends AbstractRepository {

private String accessKey;

private String secretKey;

private AmazonS3Client s3Client;

private AWSCredentialsProvider credentialsProvider;
private AmazonS3Client s3Client;

private Map<String, S3Resource> resourceCache = new HashMap<String, S3Resource>();

private Region region;

private boolean overwrite;

private CannedAccessControlList acl;

public S3Repository(String accessKey, String secretKey, boolean overwrite, Region region) {
credentialsProvider = new InstanceProfileCredentialsProvider();
this(accessKey, secretKey, overwrite, region, CannedAccessControlList.Private);
}

public S3Repository(String accessKey, String secretKey, boolean overwrite, Region region, CannedAccessControlList acl) {
AWSCredentialsProvider provider = new InstanceProfileCredentialsProvider();
try {
credentialsProvider.getCredentials();
provider.getCredentials();
} catch (AmazonClientException e1) {
credentialsProvider = new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
provider = new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
}
s3Client = new AmazonS3Client(credentialsProvider);

s3Client = new AmazonS3Client(provider);
this.overwrite = overwrite;
this.region = region;
this.acl = acl;
}

public S3Repository(AWSCredentialsProvider provider, boolean overwrite, Region region) {
this(provider, overwrite, region, CannedAccessControlList.Private);
}

public S3Repository(AWSCredentialsProvider provider, boolean overwrite, Region region) {
s3Client = new AmazonS3Client(provider);
this.overwrite = overwrite;
this.region = region;
}
public S3Repository(AWSCredentialsProvider provider, boolean overwrite, Region region, CannedAccessControlList acl) {
s3Client = new AmazonS3Client(provider);
this.overwrite = overwrite;
this.region = region;
this.acl = acl;
}

public void get(String source, File destination) {
//System.out.println("get source=" + source + " dst=" + destination.getPath());
Expand Down Expand Up @@ -111,18 +118,18 @@ public Resource getResource(String source) {
return resourceCache.get(source);
}

@Override
@Override
public List<String> list(String parent) {
try {
String marker = null;
List<String> keys = new ArrayList<String>();

do {
ListObjectsRequest request = new ListObjectsRequest()
.withBucketName(S3Utils.getBucket(parent))
.withPrefix(S3Utils.getKey(parent))
.withDelimiter("/") // RFC 2396
.withMarker(marker);
.withBucketName(S3Utils.getBucket(parent))
.withPrefix(S3Utils.getKey(parent))
.withDelimiter("/") // RFC 2396
.withMarker(marker);

ObjectListing listing = getS3Client().listObjects(request);

Expand Down Expand Up @@ -153,18 +160,18 @@ private boolean createBucket(String name, Region region) {
try {
attempt++;

getS3Client().createBucket(name, region);
getS3Client().createBucket(name, region);
if(getS3Client().doesBucketExist(name)) {
return true;
return true;
}

} catch(AmazonS3Exception s3e) {
try {
Message.warn(s3e.toString());
try {
Message.warn(s3e.toString());
Thread.sleep(timeout);
} catch (InterruptedException e) {
}
}
}
}
return false;
}
Expand All @@ -174,25 +181,25 @@ protected void put(File source, String destination, boolean overwrite) {
//System.out.print("parent> ");
String bucket = S3Utils.getBucket(destination);
String key = S3Utils.getKey(destination);
// System.out.println("publishing: bucket=" + bucket + " key=" + key);
PutObjectRequest request = new PutObjectRequest(bucket ,key, source);
request = request.withCannedAcl(CannedAccessControlList.Private);

if (!getS3Client().doesBucketExist(bucket)) {
if(!createBucket(bucket, region)) {
throw new Error("couldn't create bucket");
}
}
// System.out.println("publishing: bucket=" + bucket + " key=" + key);
PutObjectRequest request = new PutObjectRequest(bucket ,key, source);
request = request.withCannedAcl(acl);

if (!getS3Client().doesBucketExist(bucket)) {
if(!createBucket(bucket, region)) {
throw new Error("couldn't create bucket");
}
}

if (!this.overwrite && !getS3Client().listObjects(bucket, key).getObjectSummaries().isEmpty()) {
throw new Error(destination + " exists but overwriting is disabled");
}
getS3Client().putObject(request);
if (!this.overwrite && !getS3Client().listObjects(bucket, key).getObjectSummaries().isEmpty()) {
throw new Error(destination + " exists but overwriting is disabled");
}
getS3Client().putObject(request);

}

public AmazonS3Client getS3Client() {
return s3Client;
return s3Client;
}

}
23 changes: 17 additions & 6 deletions src/main/java/ohnosequences/ivy/S3Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,37 @@
package ohnosequences.ivy;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import org.apache.ivy.plugins.resolver.RepositoryResolver;
import com.amazonaws.services.s3.model.Region;

/**
* A dependency resolver that looks to an S3 repository to resolve dependencies.
*
*
* @author Ben Hale
* @author Evdokim Kovach
*/
public class S3Resolver extends RepositoryResolver {

public S3Resolver(String name, String accessKey, String secretKey, boolean overwrite, Region region) {
setName(name);
setRepository(new S3Repository(accessKey, secretKey, overwrite, region));
}

public S3Resolver(String name, AWSCredentialsProvider credentialsProvider, boolean overwrite, Region region) {
setName(name);
setRepository(new S3Repository(credentialsProvider, overwrite, region));
}
public S3Resolver(String name, String accessKey, String secretKey, boolean overwrite, Region region, CannedAccessControlList acl) {
setName(name);
setRepository(new S3Repository(accessKey, secretKey, overwrite, region, acl));
}

public S3Resolver(String name, AWSCredentialsProvider credentialsProvider, boolean overwrite, Region region) {
setName(name);
setRepository(new S3Repository(credentialsProvider, overwrite, region));
}

public S3Resolver(String name, AWSCredentialsProvider credentialsProvider, boolean overwrite, Region region, CannedAccessControlList acl) {
setName(name);
setRepository(new S3Repository(credentialsProvider, overwrite, region, acl));
}

public String getTypeName() {
return "s3";
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.7.0-SNAPSHOT"
version in ThisBuild := "0.7.1-SNAPSHOT"