diff --git a/common/src/main/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfiguration.java b/common/src/main/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfiguration.java index 4df13f50..ce7d870d 100644 --- a/common/src/main/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfiguration.java +++ b/common/src/main/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfiguration.java @@ -25,6 +25,8 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.EnumUtils; +import software.amazon.awssdk.core.exception.SdkClientException; +import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; import software.amazon.awssdk.services.glue.model.Compatibility; import java.net.URI; @@ -150,7 +152,15 @@ private void validateAndSetAWSRegion(Map configs) { if (isPresent(configs, AWSSchemaRegistryConstants.AWS_REGION)) { this.region = String.valueOf(configs.get(AWSSchemaRegistryConstants.AWS_REGION)); } else { - throw new AWSSchemaRegistryException("Region is not defined in the properties"); + try { + this.region = DefaultAwsRegionProviderChain + .builder() + .build() + .getRegion() + .id(); + } catch (SdkClientException ex) { + throw new AWSSchemaRegistryException("Region is not defined in the properties", ex); + } } } diff --git a/common/src/test/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfigurationTest.java b/common/src/test/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfigurationTest.java index 9ae2bb09..249c4605 100644 --- a/common/src/test/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfigurationTest.java +++ b/common/src/test/java/com/amazonaws/services/schemaregistry/common/configs/GlueSchemaRegistryConfigurationTest.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Properties; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -103,12 +104,28 @@ public void testBuildConfig_noRegionConfigsSupplied_throwsException() { assertEquals("Region is not defined in the properties", exception.getMessage()); } + /** + * Tests configuration for region value via default AWS region provider chain + */ + @Test + public void testBuildConfig_regionConfigsSuppliedUsingAwsProvider_thenUseDefaultAwsRegionProviderChain() { + Map configWithoutRegion = new HashMap<>(); + configWithoutRegion.put(AWSSchemaRegistryConstants.AWS_ENDPOINT, "https://test/"); + System.setProperty("aws.region", "us-west-2"); + + GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = new GlueSchemaRegistryConfiguration(configWithoutRegion); + + assertEquals("us-west-2", glueSchemaRegistryConfiguration.getRegion()); + + System.clearProperty("aws.region"); + } + /** * Tests configuration for region value */ @Test public void testBuildConfig_withRegionConfig_Instantiates() { - assertNotNull(new GlueSchemaRegistryConfiguration("us-west-1")); + assertDoesNotThrow(() -> new GlueSchemaRegistryConfiguration("us-west-1")); } /**