Skip to content

Commit

Permalink
Automatically add scheme to discovery.ec2.endpoint (opensearch-projec…
Browse files Browse the repository at this point in the history
…t#11512)

Signed-off-by: Yanlong He <[email protected]>
Signed-off-by: 何延龙 <[email protected]>
  • Loading branch information
heyanlong authored Dec 12, 2023
1 parent b5700b4 commit a8da66c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Improve boolean parsing performance ([#11308](https://github.com/opensearch-project/OpenSearch/pull/11308))
- Interpret byte array as primitive using VarHandles ([#11362](https://github.com/opensearch-project/OpenSearch/pull/11362))
- Change error message when per shard document limit is breached ([#11312](https://github.com/opensearch-project/OpenSearch/pull/11312))
- Automatically add scheme to discovery.ec2.endpoint ([#11512](https://github.com/opensearch-project/OpenSearch/pull/11512))
- Restore support for Java 8 for RestClient ([#11562](https://github.com/opensearch-project/OpenSearch/pull/11562))

### Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected Ec2Client buildClient(

if (Strings.hasText(endpoint)) {
logger.debug("using explicit ec2 endpoint [{}]", endpoint);
builder.endpointOverride(URI.create(endpoint));
builder.endpointOverride(URI.create(getFullEndpoint(endpoint)));
}

if (Strings.hasText(region)) {
Expand All @@ -110,6 +110,19 @@ protected Ec2Client buildClient(
return SocketAccess.doPrivileged(builder::build);
}

protected String getFullEndpoint(String endpoint) {
if (!Strings.hasText(endpoint)) {
return null;
}
if (endpoint.startsWith("http")) {
return endpoint;
}

// if no scheme is provided, default to https
logger.debug("no scheme found in endpoint [{}], defaulting to https", endpoint);
return "https://" + endpoint;
}

static ProxyConfiguration buildProxyConfiguration(Logger logger, Ec2ClientSettings clientSettings) {
if (Strings.hasText(clientSettings.proxyHost)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,26 @@ public void testAWSConfigurationWithAwsSettings() {
assertTrue(clientOverrideConfiguration.retryPolicy().isPresent());
assertThat(clientOverrideConfiguration.retryPolicy().get().numRetries(), is(10));
}

public void testGetFullEndpointWithScheme() {
final Settings settings = Settings.builder().put("discovery.ec2.endpoint", "http://ec2.us-west-2.amazonaws.com").build();
Ec2ClientSettings clientSettings = Ec2ClientSettings.getClientSettings(settings);

AwsEc2ServiceImpl awsEc2ServiceImpl = new AwsEc2ServiceImpl();

String endpoint = awsEc2ServiceImpl.getFullEndpoint(clientSettings.endpoint);
assertEquals("http://ec2.us-west-2.amazonaws.com", endpoint);
}

public void testGetFullEndpointWithoutScheme() {
final Settings settings = Settings.builder().put("discovery.ec2.endpoint", "ec2.us-west-2.amazonaws.com").build();
Ec2ClientSettings clientSettings = Ec2ClientSettings.getClientSettings(settings);

AwsEc2ServiceImpl awsEc2ServiceImpl = new AwsEc2ServiceImpl();

String endpoint = awsEc2ServiceImpl.getFullEndpoint(clientSettings.endpoint);
assertEquals("https://ec2.us-west-2.amazonaws.com", endpoint);

assertNull(awsEc2ServiceImpl.getFullEndpoint(""));
}
}

0 comments on commit a8da66c

Please sign in to comment.