Skip to content

Commit

Permalink
Merge pull request #14 from americanexpress/add_option_for_http_headers
Browse files Browse the repository at this point in the history
Resolves #13 Add support for custom headers
  • Loading branch information
myniva authored Jan 26, 2019
2 parents aff90f4 + aea61cd commit aa4b0d2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The AWS S3 build cache implementation has a few configuration options:
| `path` | The path under which all cache objects should be stored. | no | |
| `reducedRedundancy` | Whether or not to use [reduced redundancy](https://aws.amazon.com/s3/reduced-redundancy/). | no | true |
| `endpoint` | Alternative S3 compatible endpoint | no | |
| `headers` | A map with HTTP headers to be added to each request (nulls are ignored). e.g. `[ 'x-header-name': 'header-value' ]` | no | |
| `awsAccessKeyId` | The AWS access key id | no | from DefaultAWSCredentialsProviderChain |
| `awsSecretKey` | The AWS secret key | no | from DefaultAWSCredentialsProviderChain |

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/ch/myniva/gradle/caching/s3/AwsS3BuildCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package ch.myniva.gradle.caching.s3;

import java.util.HashMap;
import java.util.Map;
import org.gradle.caching.configuration.AbstractBuildCache;

public class AwsS3BuildCache extends AbstractBuildCache {
Expand All @@ -24,6 +26,7 @@ public class AwsS3BuildCache extends AbstractBuildCache {
private String path;
private boolean reducedRedundancy = true;
private String endpoint;
private Map<String, String> headers;
private String awsAccessKeyId;
private String awsSecretKey;

Expand Down Expand Up @@ -67,6 +70,14 @@ public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public Map<String, String> getHeaders() {
return headers;
}

public void setHeaders(final Map<String, String> headers) {
this.headers = headers != null ? new HashMap<>(headers) : null;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

import static com.amazonaws.util.StringUtils.isNullOrEmpty;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import java.util.Map;
import org.gradle.api.GradleException;
import org.gradle.caching.BuildCacheService;
import org.gradle.caching.BuildCacheServiceFactory;
Expand Down Expand Up @@ -78,6 +79,9 @@ private AmazonS3 createS3Client(AwsS3BuildCache config) {
s3Builder.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(config.getAwsAccessKeyId(), config.getAwsSecretKey())));
}

addHttpHeaders(s3Builder, config);

if (isNullOrEmpty(config.getEndpoint())) {
s3Builder.withRegion(config.getRegion());
} else {
Expand All @@ -91,4 +95,17 @@ private AmazonS3 createS3Client(AwsS3BuildCache config) {
}
return s3;
}

private void addHttpHeaders(final AmazonS3ClientBuilder s3Builder, final AwsS3BuildCache config) {
final Map<String, String> headers = config.getHeaders();
if (headers != null) {
final ClientConfiguration clientConfiguration = new ClientConfiguration();
for (Map.Entry<String, String> header : headers.entrySet()) {
if(header.getKey() != null && header.getValue() != null) {
clientConfiguration.addHeader(header.getKey(), header.getValue());
}
}
s3Builder.setClientConfiguration(clientConfiguration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.junit.Assert.assertNotNull;

import ch.myniva.gradle.caching.s3.AwsS3BuildCache;
import java.util.HashMap;
import java.util.Map;
import org.gradle.caching.BuildCacheService;
import org.gradle.caching.BuildCacheServiceFactory.Describer;
import org.junit.Before;
Expand Down Expand Up @@ -58,6 +60,49 @@ public void testPath() {
assertNotNull(service);
}

@Test
public void testNullHeaders() {
AwsS3BuildCache conf = new AwsS3BuildCache();
conf.setRegion("us-west-1");
conf.setBucket("my-bucket");
conf.setHeaders(null);

BuildCacheService service = subject.createBuildCacheService(conf, buildCacheDescriber);

assertNotNull(service);
}


@Test
public void testNullHeaderName() {
AwsS3BuildCache conf = new AwsS3BuildCache();
conf.setRegion("us-west-1");
conf.setBucket("my-bucket");
Map<String, String> headers = new HashMap<String, String>(){{
put(null, "foo");
}};
conf.setHeaders(headers);

BuildCacheService service = subject.createBuildCacheService(conf, buildCacheDescriber);

assertNotNull(service);
}

@Test
public void testNullHeaderValue() {
AwsS3BuildCache conf = new AwsS3BuildCache();
conf.setRegion("us-west-1");
conf.setBucket("my-bucket");
Map<String, String> headers = new HashMap<String, String>(){{
put("x-foo", null);
}};
conf.setHeaders(headers);

BuildCacheService service = subject.createBuildCacheService(conf, buildCacheDescriber);

assertNotNull(service);
}

@Test(expected = IllegalStateException.class)
public void testIllegalConfigWithoutRegion() throws Exception {
AwsS3BuildCache conf = new AwsS3BuildCache();
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Currently building version
version=0.7.1
version=0.8.0

#Previous version used to generate release notes delta
previousVersion=0.7.0

0 comments on commit aa4b0d2

Please sign in to comment.