Skip to content

Commit

Permalink
[JENKINS-40654] Load the list of Amazon S3 regions from {{c.a.r.Regio…
Browse files Browse the repository at this point in the history
…nMetadataProvider}}, stop relying on a static lists and constants.

* Adapt the http proxy logic of the plugin to discover the S3 endpoint hostname with `com.amazonaws.regions.Region#getServiceEndpoint("s3")` instead of the hardcoded constant `s3.amazonaws.com`
* Replace
   * `com.amazonaws.regions.Regions` by `com.amazonaws.regions.RegionUtils#getRegionsForService("s3")`
   * `com.amazonaws.services.s3.model.Region` by `com.amazonaws.regions.Region`
* For the default region used in some places of the plugin, introduce the system property `hudson.plugins.s3.DEFAULT_AMAZON_S3_REGION` to override the default `us-east-1`. Note that it would be better to no longer rely on a default AWS Region and to ask the user to specify the desired AWS Region.

The list of AWS regions can be overridden specifying a file `classpath://com/amazonaws/partitions/override/endpoints.json` matching the format defined in https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/resources/com/amazonaws/partitions/endpoints.json .

A solution to add this file in the classpath of Jenkins is to use the `java` command line parameter `-Xbootclasspath/a:/path/to/boot/classpath/folder/` and to locate `com/amazonaws/partitions/override/endpoints.json` in `/path/to/boot/classpath/folder/`.
  • Loading branch information
cyrille-leclerc committed Dec 23, 2016
1 parent 92247d2 commit ff7d349
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
39 changes: 29 additions & 10 deletions src/main/java/hudson/plugins/s3/ClientHelper.java
Expand Up @@ -5,35 +5,49 @@
import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import hudson.ProxyConfiguration;

import java.util.regex.Pattern;

public class ClientHelper {

public static String DEFAULT_AMAZON_S3_REGION_NAME = System.getProperty(
"hudson.plugins.s3.DEFAULT_AMAZON_S3_REGION",
com.amazonaws.services.s3.model.Region.US_Standard.toAWSRegion().getName());

/**
* This method should be deprecated to always use an AWS region with {@link #createClient(String, String, boolean, String, ProxyConfiguration)}
*/
public static AmazonS3Client createClient(String accessKey, String secretKey, boolean useRole, ProxyConfiguration proxy)
{
return createClient(accessKey, secretKey, useRole, null, proxy);
return createClient(accessKey, secretKey, useRole, DEFAULT_AMAZON_S3_REGION_NAME, proxy);
}

public static AmazonS3Client createClient(String accessKey, String secretKey, boolean useRole, String region, ProxyConfiguration proxy)
{
final AmazonS3Client client;
Region awsRegion = getRegionFromString(region);

ClientConfiguration clientConfiguration = getClientConfiguration(proxy, awsRegion);

final AmazonS3Client client;
if (useRole) {
client = new AmazonS3Client(getClientConfiguration(proxy));
client = new AmazonS3Client(clientConfiguration);
} else {
client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), getClientConfiguration(proxy));
client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), clientConfiguration);
}

if (region != null)
{
client.setRegion(getRegionFromString(region));
}
client.setRegion(awsRegion);

return client;
}

/**
*
* @param regionName nullable region name
* @return AWS region, never {@code null}, defaults to {@link com.amazonaws.services.s3.model.Region#US_Standard}
*/
private static Region getRegionFromString(String regionName) {
// In 0.7, selregion comes from Regions#name
Region region = RegionUtils.getRegion(regionName);
Expand All @@ -43,13 +57,18 @@ private static Region getRegionFromString(String regionName) {
region = RegionUtils.getRegion(Regions.valueOf(regionName).getName());
}

if (region == null) {
region = RegionUtils.getRegion(DEFAULT_AMAZON_S3_REGION_NAME);
}
return region;
}

public static ClientConfiguration getClientConfiguration(ProxyConfiguration proxy) {
public static ClientConfiguration getClientConfiguration(ProxyConfiguration proxy, Region region) {
final ClientConfiguration clientConfiguration = new ClientConfiguration();

if (shouldUseProxy(proxy, "s3.amazonaws.com")) {
String s3Endpoint = region.getServiceEndpoint(AmazonS3.ENDPOINT_PREFIX);

if (shouldUseProxy(proxy, s3Endpoint)) {
clientConfiguration.setProxyHost(proxy.name);
clientConfiguration.setProxyPort(proxy.port);
if (proxy.getUserName() != null) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/hudson/plugins/s3/Entry.java
@@ -1,6 +1,9 @@
package hudson.plugins.s3;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
Expand Down Expand Up @@ -36,7 +39,7 @@ public final class Entry implements Describable<Entry> {
/**
* Regions Values
*/
public static final Regions[] regions = Regions.values();
public static final List<Region> regions = RegionUtils.getRegionsForService(AmazonS3.ENDPOINT_PREFIX);
/**
* Stores the Region Value
*/
Expand Down Expand Up @@ -134,7 +137,7 @@ public ListBoxModel doFillStorageClassItems() {

public ListBoxModel doFillSelectedRegionItems() {
final ListBoxModel model = new ListBoxModel();
for (Regions r : regions) {
for (Region r : regions) {
model.add(r.getName(), r.getName());
}
return model;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/s3/S3BucketPublisher.java
@@ -1,7 +1,7 @@
package hudson.plugins.s3;

import com.amazonaws.AmazonClientException;
import com.amazonaws.regions.Regions;
import com.amazonaws.regions.Region;
import com.amazonaws.services.s3.AmazonS3Client;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -365,7 +365,7 @@ public DescriptorImpl(Class<? extends Publisher> clazz) {
load();
}

public Regions[] regions = Entry.regions;
public List<Region> regions = Entry.regions;

public String[] storageClasses = Entry.storageClasses;

Expand Down

0 comments on commit ff7d349

Please sign in to comment.