Skip to content

Commit c32d613

Browse files
authored
Merge pull request #633 from olamy/enforce-region
Enforce region for aws sdk calls
2 parents 8d8e261 + 66db82f commit c32d613

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

src/main/java/io/jenkins/plugins/artifact_manager_jclouds/s3/S3BlobStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public String getContainer() {
100100
}
101101

102102
public String getRegion() {
103-
return CredentialsAwsGlobalConfiguration.get().getRegion();
103+
return getConfiguration().getRegion().id();
104104
}
105105

106106
public S3BlobStoreConfig getConfiguration(){
@@ -215,6 +215,7 @@ public S3Presigner getS3Presigner(S3Client s3Client) {
215215
String customEndpoint = getConfiguration().getResolvedCustomEndpoint();
216216
S3Presigner.Builder presignerBuilder = S3Presigner.builder()
217217
.fipsEnabled(FIPS140.useCompliantAlgorithms())
218+
.region(getConfiguration().getRegion())
218219
.credentialsProvider(CredentialsAwsGlobalConfiguration.get().getCredentials())
219220
.s3Client(s3Client);
220221
if (StringUtils.isNotBlank(customEndpoint)) {
@@ -223,7 +224,7 @@ public S3Presigner getS3Presigner(S3Client s3Client) {
223224

224225
String customRegion = getConfiguration().getCustomSigningRegion();
225226
if(StringUtils.isBlank(customRegion)) {
226-
customRegion = CredentialsAwsGlobalConfiguration.get().getRegion();
227+
customRegion = getConfiguration().getRegion().id();
227228
}
228229
if(StringUtils.isNotBlank(customRegion)) {
229230
presignerBuilder.region(Region.of(customRegion));

src/main/java/io/jenkins/plugins/artifact_manager_jclouds/s3/S3BlobStoreConfig.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.net.URI;
2929
import java.net.URISyntaxException;
30+
import java.util.logging.Logger;
3031
import java.util.regex.Pattern;
3132

3233
import jenkins.util.SystemProperties;
@@ -55,8 +56,10 @@
5556
import org.jenkinsci.Symbol;
5657
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
5758
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
59+
import software.amazon.awssdk.core.exception.SdkClientException;
5860
import software.amazon.awssdk.core.SdkSystemSetting;
5961
import software.amazon.awssdk.regions.Region;
62+
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
6063
import software.amazon.awssdk.services.s3.S3Client;
6164
import software.amazon.awssdk.services.s3.S3ClientBuilder;
6265
import software.amazon.awssdk.services.s3.model.Bucket;
@@ -73,6 +76,8 @@
7376
@Extension
7477
public final class S3BlobStoreConfig extends AbstractAwsGlobalConfiguration {
7578

79+
private static final Logger LOGGER = Logger.getLogger(S3BlobStoreConfig.class.getName());
80+
7681
private static final String BUCKET_REGEXP = "^([a-z]|(\\d(?!\\d{0,2}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})))([a-z\\d]|(\\.(?!(\\.|-)))|(-(?!\\.))){1,61}[a-z\\d\\.]$";
7782
private static final Pattern bucketPattern = Pattern.compile(BUCKET_REGEXP);
7883

@@ -291,13 +296,15 @@ S3ClientBuilder getAmazonS3ClientBuilder() throws URISyntaxException {
291296
if (StringUtils.isNotBlank(getResolvedCustomEndpoint())) {
292297
String resolvedCustomSigningRegion = customSigningRegion;
293298
if (StringUtils.isBlank(resolvedCustomSigningRegion)) {
294-
resolvedCustomSigningRegion = "us-east-1";
299+
// we must revert to a region if no custom defined
300+
resolvedCustomSigningRegion = getRegion().id();
295301
}
296302
ret = ret.endpointOverride(new URI(getResolvedCustomEndpoint())).region(Region.of(resolvedCustomSigningRegion));
297-
} else if (StringUtils.isNotBlank(CredentialsAwsGlobalConfiguration.get().getRegion())) {
298-
ret = ret.region(Region.of(CredentialsAwsGlobalConfiguration.get().getRegion()));
299303
} else {
300-
ret = ret.useArnRegion(true);
304+
// not really sure of why this was used.. this should have a dedicated parameter
305+
//ret = ret.useArnRegion(true);
306+
// use same default algorithm as signer
307+
ret = ret.region(getRegion());
301308
}
302309
ret = ret.accelerate(useTransferAcceleration);
303310

@@ -316,20 +323,34 @@ public S3ClientBuilder getAmazonS3ClientBuilderWithCredentials() throws IOExcept
316323
}
317324
}
318325

326+
public Region getRegion() {
327+
String regionStr = CredentialsAwsGlobalConfiguration.get().getRegion();
328+
if (regionStr == null) {
329+
try {
330+
return new DefaultAwsRegionProviderChain().getRegion();
331+
} catch (SdkClientException e) {
332+
// need to revert to a default one.
333+
LOGGER.warning("There is no region configured for this S3 bucket and aws sdk couldn't discover one so using default: " + Region.US_EAST_1);
334+
return Region.US_EAST_1;
335+
}
336+
}
337+
return Region.of(regionStr);
338+
}
339+
319340
private S3ClientBuilder getAmazonS3ClientBuilderWithCredentials(boolean disableSessionToken) throws IOException, URISyntaxException {
320341
S3ClientBuilder builder = getAmazonS3ClientBuilder();
321342
if (disableSessionToken) {
322343
builder = builder.credentialsProvider(CredentialsAwsGlobalConfiguration.get().getCredentials());
323344
} else {
324345
AwsSessionCredentials awsSessionCredentials = CredentialsAwsGlobalConfiguration.get()
325-
.sessionCredentials(CredentialsAwsGlobalConfiguration.get().getRegion(),
326-
CredentialsAwsGlobalConfiguration.get().getCredentialsId());
346+
.sessionCredentials(getRegion().id(), CredentialsAwsGlobalConfiguration.get().getCredentialsId());
327347
if(awsSessionCredentials != null ) {
328348
builder.credentialsProvider(StaticCredentialsProvider.create(awsSessionCredentials));
329349
} else {
330350
throw new IOException("No session AWS credentials found");
331351
}
332352
}
353+
builder = builder.region(getRegion());
333354
return builder;
334355
}
335356

src/test/java/io/jenkins/plugins/artifact_manager_jclouds/s3/JCloudsVirtualFileTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import io.jenkins.plugins.aws.global_configuration.CredentialsAwsGlobalConfiguration;
2929
import static org.hamcrest.Matchers.*;
3030
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.*;
31-
import static org.junit.Assert.*;
3231

3332
import java.io.File;
3433
import java.io.FileNotFoundException;
@@ -56,7 +55,14 @@
5655
import java.net.ProtocolException;
5756

5857
import jenkins.util.VirtualFile;
58+
import static org.hamcrest.MatcherAssert.assertThat;
5959
import org.jclouds.http.HttpResponseException;
60+
import static org.junit.Assert.assertArrayEquals;
61+
import static org.junit.Assert.assertEquals;
62+
import static org.junit.Assert.assertFalse;
63+
import static org.junit.Assert.assertNotEquals;
64+
import static org.junit.Assert.assertTrue;
65+
import static org.junit.Assert.fail;
6066
import software.amazon.awssdk.services.s3.S3Client;
6167
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
6268
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
@@ -248,7 +254,7 @@ public void pagedListing() throws Exception {
248254
});
249255
// Default list page size for S3 is 1000 blobs; we have 1010 plus the two created for all tests, so should hit a second page.
250256
assertThat(subdir.list("sprawling/**/k3", null, true), iterableWithSize(100));
251-
assertEquals("calls GetBucketLocation then ListBucket, advance to …/sprawling/i9/j8/k8, ListBucket again", 3, httpLogging.getRecords().size());
257+
assertThat("calls GetBucketLocation (perhaps) then ListBucket, advance to …/sprawling/i9/j8/k8, ListBucket again", httpLogging.getRecords().size(), lessThanOrEqualTo(3));
252258
}
253259

254260
@Test

0 commit comments

Comments
 (0)