2727import java .io .IOException ;
2828import java .net .URI ;
2929import java .net .URISyntaxException ;
30+ import java .util .logging .Logger ;
3031import java .util .regex .Pattern ;
3132
3233import jenkins .util .SystemProperties ;
5556import org .jenkinsci .Symbol ;
5657import software .amazon .awssdk .auth .credentials .AwsSessionCredentials ;
5758import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
59+ import software .amazon .awssdk .core .exception .SdkClientException ;
5860import software .amazon .awssdk .core .SdkSystemSetting ;
5961import software .amazon .awssdk .regions .Region ;
62+ import software .amazon .awssdk .regions .providers .DefaultAwsRegionProviderChain ;
6063import software .amazon .awssdk .services .s3 .S3Client ;
6164import software .amazon .awssdk .services .s3 .S3ClientBuilder ;
6265import software .amazon .awssdk .services .s3 .model .Bucket ;
7376@ Extension
7477public 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
0 commit comments