Default S3 endpoint scheme to HTTPS when not specified (#127489)

This commit is contained in:
Nick Tindall 2025-05-05 18:47:23 +10:00 committed by GitHub
parent 4e4d59cc17
commit 8dc6bf8893
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View file

@ -254,7 +254,21 @@ class S3Service extends AbstractLifecycleComponent {
}
if (Strings.hasLength(clientSettings.endpoint)) {
s3clientBuilder.endpointOverride(URI.create(clientSettings.endpoint));
String endpoint = clientSettings.endpoint;
if ((endpoint.startsWith("http://") || endpoint.startsWith("https://")) == false) {
// The SDK does not know how to interpret endpoints without a scheme prefix and will error. Therefore, when the scheme is
// absent, we'll supply HTTPS as a default to avoid errors.
// See https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/client-configuration.html#client-config-other-diffs
endpoint = "https://" + endpoint;
LOGGER.warn(
"""
found S3 client with endpoint [{}] that is missing a scheme, guessing it should use 'https://'; \
to suppress this warning, add a scheme prefix to the [{}] setting on this node""",
clientSettings.endpoint,
S3ClientSettings.ENDPOINT_SETTING.getConcreteSettingForNamespace("CLIENT_NAME").getKey()
);
}
s3clientBuilder.endpointOverride(URI.create(endpoint));
}
return s3clientBuilder;

View file

@ -12,7 +12,9 @@ import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.retry.RetryPolicyContext;
import software.amazon.awssdk.core.retry.conditions.RetryCondition;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.endpoints.S3EndpointParams;
import software.amazon.awssdk.services.s3.endpoints.internal.DefaultS3EndpointProvider;
import software.amazon.awssdk.services.s3.model.S3Exception;
@ -29,8 +31,10 @@ import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.watcher.ResourceWatcherService;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
public class S3ServiceTests extends ESTestCase {
@ -217,4 +221,22 @@ public class S3ServiceTests extends ESTestCase {
);
}
}
public void testEndpointOverrideSchemeDefaultsToHttpsWhenNotSpecified() {
final S3Service s3Service = new S3Service(
mock(Environment.class),
Settings.EMPTY,
mock(ResourceWatcherService.class),
() -> Region.of("es-test-region")
);
final String endpointWithoutScheme = randomIdentifier() + ".ignore";
S3Client s3Client = s3Service.buildClient(
S3ClientSettings.getClientSettings(
Settings.builder().put("s3.client.test-client.endpoint", endpointWithoutScheme).build(),
"test-client"
),
mock(SdkHttpClient.class)
);
assertThat(s3Client.serviceClientConfiguration().endpointOverride().get(), equalTo(URI.create("https://" + endpointWithoutScheme)));
}
}