Skip to content

Commit 5580fa2

Browse files
author
Shintaro Onuma
committed
Parse S3 VPC endpoints for the region
1 parent 50d256e commit 5580fa2

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/DefaultS3ClientFactory.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.IOException;
2222
import java.net.URI;
2323
import java.net.URISyntaxException;
24+
import java.util.regex.Matcher;
25+
import java.util.regex.Pattern;
2426

2527
import org.apache.hadoop.classification.VisibleForTesting;
2628
import org.apache.hadoop.fs.s3a.impl.AWSClientConfig;
@@ -82,6 +84,9 @@ public class DefaultS3ClientFactory extends Configured
8284

8385
private static final String S3_SERVICE_NAME = "s3";
8486

87+
private static final Pattern VPC_ENDPOINT_PATTERN =
88+
Pattern.compile("^(?:.+\\.)?([a-z0-9-]+)\\.vpce\\.amazonaws\\.(?:com|com\\.cn)$");
89+
8590
/**
8691
* Subclasses refer to this.
8792
*/
@@ -379,10 +384,19 @@ private static URI getS3Endpoint(String endpoint, final Configuration conf) {
379384
* @param endpointEndsWithCentral true if the endpoint is configured as central.
380385
* @return the S3 region, null if unable to resolve from endpoint.
381386
*/
382-
private static Region getS3RegionFromEndpoint(final String endpoint,
387+
@VisibleForTesting
388+
static Region getS3RegionFromEndpoint(final String endpoint,
383389
final boolean endpointEndsWithCentral) {
384390

385391
if (!endpointEndsWithCentral) {
392+
// S3 VPC endpoint parsing
393+
Matcher matcher = VPC_ENDPOINT_PATTERN.matcher(endpoint);
394+
if (matcher.find()) {
395+
LOG.debug("Mapping to VPCE");
396+
LOG.debug("Endpoint {} is vpc endpoint; parsing region as {}", endpoint, matcher.group(1));
397+
return Region.of(matcher.group(1));
398+
}
399+
386400
LOG.debug("Endpoint {} is not the default; parsing", endpoint);
387401
return AwsHostNameUtils.parseSigningRegion(endpoint, S3_SERVICE_NAME).orElse(null);
388402
}

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3AEndpointRegion.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public class ITestS3AEndpointRegion extends AbstractS3ATestBase {
8989

9090
private static final String VPC_ENDPOINT = "vpce-1a2b3c4d-5e6f.s3.us-west-2.vpce.amazonaws.com";
9191

92+
private static final String CN_VPC_ENDPOINT = "vpce-1a2b3c4d-5e6f.s3.cn-northwest-1.vpce.amazonaws.com.cn";
93+
9294
public static final String EXCEPTION_THROWN_BY_INTERCEPTOR = "Exception thrown by interceptor";
9395

9496
/**
@@ -271,7 +273,6 @@ public void testWithGovCloudEndpoint() throws Throwable {
271273
}
272274

273275
@Test
274-
@Ignore("Pending HADOOP-18938. S3A region logic to handle vpce and non standard endpoints")
275276
public void testWithVPCE() throws Throwable {
276277
describe("Test with vpc endpoint");
277278
Configuration conf = getConfiguration();
@@ -281,6 +282,16 @@ public void testWithVPCE() throws Throwable {
281282
expectInterceptorException(client);
282283
}
283284

285+
@Test
286+
public void testWithChinaVPCE() throws Throwable {
287+
describe("Test with china vpc endpoint");
288+
Configuration conf = getConfiguration();
289+
290+
S3Client client = createS3Client(conf, CN_VPC_ENDPOINT, null, CN_NORTHWEST_1, false);
291+
292+
expectInterceptorException(client);
293+
}
294+
284295
@Test
285296
public void testCentralEndpointAndDifferentRegionThanBucket() throws Throwable {
286297
describe("Access public bucket using central endpoint and region "
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.hadoop.fs.s3a;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.Test;
5+
import software.amazon.awssdk.regions.Region;
6+
7+
public class TestS3AEndpointParsing extends AbstractS3AMockTest {
8+
9+
private static final String VPC_ENDPOINT = "vpce-1a2b3c4d-5e6f.s3.us-west-2.vpce.amazonaws.com";
10+
private static final String NON_VPC_ENDPOINT = "s3.eu-west-1.amazonaws.com";
11+
private static final String US_WEST_2 = "us-west-2";
12+
private static final String EU_WEST_1 = "eu-west-1";
13+
14+
@Test
15+
public void testVPCEndpoint() {
16+
Region region = DefaultS3ClientFactory.getS3RegionFromEndpoint(VPC_ENDPOINT, false);
17+
Assertions.assertThat(region).isEqualTo(Region.of(US_WEST_2));
18+
}
19+
20+
@Test
21+
public void testNonVPCEndpoint() {
22+
Region region = DefaultS3ClientFactory.getS3RegionFromEndpoint(NON_VPC_ENDPOINT, false);
23+
Assertions.assertThat(region).isEqualTo(Region.of(EU_WEST_1));
24+
}
25+
}

0 commit comments

Comments
 (0)