Skip to content

Commit 740c86e

Browse files
authored
Default S3 endpoint scheme to HTTPS when not specified (#127704)
(cherry picked from commit 8dc6bf8)
1 parent 0861046 commit 740c86e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,21 @@ protected S3ClientBuilder buildClientBuilder(S3ClientSettings clientSettings, Sd
254254
}
255255

256256
if (Strings.hasLength(clientSettings.endpoint)) {
257-
s3clientBuilder.endpointOverride(URI.create(clientSettings.endpoint));
257+
String endpoint = clientSettings.endpoint;
258+
if ((endpoint.startsWith("http://") || endpoint.startsWith("https://")) == false) {
259+
// The SDK does not know how to interpret endpoints without a scheme prefix and will error. Therefore, when the scheme is
260+
// absent, we'll supply HTTPS as a default to avoid errors.
261+
// See https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/client-configuration.html#client-config-other-diffs
262+
endpoint = "https://" + endpoint;
263+
LOGGER.warn(
264+
"""
265+
found S3 client with endpoint [{}] that is missing a scheme, guessing it should use 'https://'; \
266+
to suppress this warning, add a scheme prefix to the [{}] setting on this node""",
267+
clientSettings.endpoint,
268+
S3ClientSettings.ENDPOINT_SETTING.getConcreteSettingForNamespace("CLIENT_NAME").getKey()
269+
);
270+
}
271+
s3clientBuilder.endpointOverride(URI.create(endpoint));
258272
}
259273

260274
return s3clientBuilder;

modules/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ServiceTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*/
99
package org.elasticsearch.repositories.s3;
1010

11+
import software.amazon.awssdk.http.SdkHttpClient;
1112
import software.amazon.awssdk.regions.Region;
13+
import software.amazon.awssdk.services.s3.S3Client;
1214
import software.amazon.awssdk.services.s3.endpoints.S3EndpointParams;
1315
import software.amazon.awssdk.services.s3.endpoints.internal.DefaultS3EndpointProvider;
1416

@@ -23,8 +25,10 @@
2325
import org.elasticsearch.watcher.ResourceWatcherService;
2426

2527
import java.io.IOException;
28+
import java.net.URI;
2629
import java.util.concurrent.atomic.AtomicBoolean;
2730

31+
import static org.hamcrest.Matchers.equalTo;
2832
import static org.mockito.Mockito.mock;
2933

3034
public class S3ServiceTests extends ESTestCase {
@@ -184,4 +188,22 @@ public void testGetClientRegionFallbackToUsEast1() {
184188
);
185189
}
186190
}
191+
192+
public void testEndpointOverrideSchemeDefaultsToHttpsWhenNotSpecified() {
193+
final S3Service s3Service = new S3Service(
194+
mock(Environment.class),
195+
Settings.EMPTY,
196+
mock(ResourceWatcherService.class),
197+
() -> Region.of("es-test-region")
198+
);
199+
final String endpointWithoutScheme = randomIdentifier() + ".ignore";
200+
S3Client s3Client = s3Service.buildClient(
201+
S3ClientSettings.getClientSettings(
202+
Settings.builder().put("s3.client.test-client.endpoint", endpointWithoutScheme).build(),
203+
"test-client"
204+
),
205+
mock(SdkHttpClient.class)
206+
);
207+
assertThat(s3Client.serviceClientConfiguration().endpointOverride().get(), equalTo(URI.create("https://" + endpointWithoutScheme)));
208+
}
187209
}

0 commit comments

Comments
 (0)