From 833db1d79c5ec58942eb42b19904f2674c840d36 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Thu, 8 Jun 2023 13:12:52 -0400 Subject: [PATCH] Default discovery-ec2 region to none. (#7969) Signed-off-by: dblock Signed-off-by: Rishab Nahata --- .../discovery/ec2/AwsEc2ServiceImpl.java | 10 +++++++--- .../discovery/ec2/Ec2ClientSettings.java | 11 +++++------ .../discovery/ec2/Ec2DiscoveryPluginTests.java | 14 +++++++------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/AwsEc2ServiceImpl.java b/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/AwsEc2ServiceImpl.java index 058b38f9aa17d..2a142b841b713 100644 --- a/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/AwsEc2ServiceImpl.java +++ b/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/AwsEc2ServiceImpl.java @@ -84,7 +84,7 @@ protected Ec2Client buildClient( ProxyConfiguration proxyConfiguration, ClientOverrideConfiguration overrideConfiguration, String endpoint, - Region region, + String region, long readTimeoutMillis ) { ApacheHttpClient.Builder clientBuilder = ApacheHttpClient.builder() @@ -94,14 +94,18 @@ protected Ec2Client buildClient( Ec2ClientBuilder builder = Ec2Client.builder() .overrideConfiguration(overrideConfiguration) .httpClientBuilder(clientBuilder) - .credentialsProvider(awsCredentialsProvider) - .region(region); + .credentialsProvider(awsCredentialsProvider); if (Strings.hasText(endpoint)) { logger.debug("using explicit ec2 endpoint [{}]", endpoint); builder.endpointOverride(URI.create(endpoint)); } + if (Strings.hasText(region)) { + logger.debug("using explicit ec2 region [{}]", region); + builder.region(Region.of(region)); + } + return SocketAccess.doPrivileged(builder::build); } diff --git a/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/Ec2ClientSettings.java b/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/Ec2ClientSettings.java index a5bcfe5d4151c..8f09276e6a627 100644 --- a/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/Ec2ClientSettings.java +++ b/plugins/discovery-ec2/src/main/java/org/opensearch/discovery/ec2/Ec2ClientSettings.java @@ -46,7 +46,6 @@ import software.amazon.awssdk.auth.credentials.AwsCredentials; import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; import software.amazon.awssdk.core.Protocol; -import software.amazon.awssdk.regions.Region; import java.util.Locale; @@ -79,10 +78,10 @@ final class Ec2ClientSettings { ); /** An override for the scoping region for authentication. */ - static final Setting REGION_SETTING = new Setting<>( + static final Setting REGION_SETTING = new Setting<>( "discovery.ec2.region", - "us-west-2", - s -> Region.of(s.toLowerCase(Locale.ROOT)), + "", + s -> s.toLowerCase(Locale.ROOT), Property.NodeScope ); @@ -124,7 +123,7 @@ final class Ec2ClientSettings { /** * The ec2 signing region. */ - final Region region; + final String region; /** The protocol to use to talk to ec2. Defaults to https. */ final Protocol protocol; @@ -150,7 +149,7 @@ final class Ec2ClientSettings { protected Ec2ClientSettings( AwsCredentials credentials, String endpoint, - Region region, + String region, Protocol protocol, String proxyHost, int proxyPort, diff --git a/plugins/discovery-ec2/src/test/java/org/opensearch/discovery/ec2/Ec2DiscoveryPluginTests.java b/plugins/discovery-ec2/src/test/java/org/opensearch/discovery/ec2/Ec2DiscoveryPluginTests.java index 0203397498b47..35df2bdbfdfb8 100644 --- a/plugins/discovery-ec2/src/test/java/org/opensearch/discovery/ec2/Ec2DiscoveryPluginTests.java +++ b/plugins/discovery-ec2/src/test/java/org/opensearch/discovery/ec2/Ec2DiscoveryPluginTests.java @@ -109,16 +109,16 @@ public void testDefaultEndpoint() throws IOException { public void testDefaultRegion() throws IOException { final Settings settings = Settings.builder().build(); try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(settings)) { - final Region region = ((MockEc2Client) plugin.ec2Service.client().get()).region; - assertEquals(region, Region.US_WEST_2); + final String region = ((MockEc2Client) plugin.ec2Service.client().get()).region; + assertEquals(region, ""); } } public void testSpecificRegion() throws IOException { final Settings settings = Settings.builder().put(Ec2ClientSettings.REGION_SETTING.getKey(), "us-west-2").build(); try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(settings)) { - final Region region = ((MockEc2Client) plugin.ec2Service.client().get()).region; - assertEquals(region, Region.US_WEST_2); + final String region = ((MockEc2Client) plugin.ec2Service.client().get()).region; + assertEquals(region, Region.US_WEST_2.toString()); } } @@ -249,7 +249,7 @@ protected Ec2Client buildClient( ProxyConfiguration proxyConfiguration, ClientOverrideConfiguration overrideConfiguration, String endpoint, - Region region, + String region, long readTimeoutMillis ) { return new MockEc2Client(credentials, proxyConfiguration, overrideConfiguration, endpoint, region, readTimeoutMillis); @@ -261,7 +261,7 @@ protected Ec2Client buildClient( private static class MockEc2Client implements Ec2Client { String endpoint; - final Region region; + final String region; final AwsCredentialsProvider credentials; final ClientOverrideConfiguration clientOverrideConfiguration; final ProxyConfiguration proxyConfiguration; @@ -272,7 +272,7 @@ private static class MockEc2Client implements Ec2Client { ProxyConfiguration proxyConfiguration, ClientOverrideConfiguration clientOverrideConfiguration, String endpoint, - Region region, + String region, long readTimeoutMillis ) { this.credentials = credentials;