From 9df1c84be0f86f6c26030ba1b98e9a2b93dc743c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Elek?= Date: Wed, 17 Oct 2018 11:34:03 +0200 Subject: [PATCH] HDDS-563. Support hybrid VirtualHost style URL. Contributed by Bharat Viswanadham. --- .../ozone/s3/VirtualHostStyleFilter.java | 19 ++++++-------- .../ozone/s3/TestVirtualHostStyleFilter.java | 25 +++++++++++-------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java index 3bd690beabf..4cf78b6ff74 100644 --- a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java +++ b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java @@ -49,8 +49,6 @@ public class VirtualHostStyleFilter implements ContainerRequestFilter { private static final Logger LOG = LoggerFactory.getLogger( VirtualHostStyleFilter.class); - private static final Pattern URL_SCHEME_PATTERN = Pattern.compile("" + - "(?(.+))\\.(?(.+))\\."); @Inject private OzoneConfiguration conf; @@ -83,24 +81,21 @@ public class VirtualHostStyleFilter implements ContainerRequestFilter { // address length means it is virtual host style, we need to convert to // path style. if (host.length() > domain.length()) { - String bothNames = host.substring(0, host.length() - domain.length()); - LOG.debug("Both volume name and bucket name is {}", bothNames); - Matcher matcher = URL_SCHEME_PATTERN.matcher(bothNames); + String bucketName = host.substring(0, host.length() - domain.length()); - if (!matcher.matches()) { + if(!bucketName.endsWith(".")) { + //Checking this as the virtual host style pattern is http://bucket.host/ throw getException("Invalid S3 Gateway request {" + requestContext .getUriInfo().getRequestUri().toString() +"}:" +" Host: {" + host + " is in invalid format"); + } else { + bucketName = bucketName.substring(0, bucketName.length() - 1); } - - String bucketStr = matcher.group("bucket"); - String volumeStr = matcher.group("volume"); - - LOG.debug("bucket {}, volumeStr {}", bucketStr, volumeStr); + LOG.debug("Bucket name is {}", bucketName); URI baseURI = requestContext.getUriInfo().getBaseUri(); String currentPath = requestContext.getUriInfo().getPath(); - String newPath = String.format("%s/%s", volumeStr, bucketStr); + String newPath = bucketName; if (currentPath != null) { newPath += String.format("%s", currentPath); } diff --git a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java index ac8fa87f409..5548c7783bd 100644 --- a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java +++ b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java @@ -31,6 +31,8 @@ import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.SecurityContext; import java.net.URI; +import static org.junit.Assert.fail; + /** * This class test virtual host style mapping conversion to path style. */ @@ -87,10 +89,10 @@ public class TestVirtualHostStyleFilter { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - ".myvolume.localhost:9878", "/myfile", true); + ".localhost:9878", "/myfile", true); virtualHostStyleFilter.filter(containerRequest); URI expected = new URI("http://" + s3HttpAddr + - "/myvolume/mybucket/myfile"); + "/mybucket/myfile"); Assert.assertEquals(expected, containerRequest.getRequestUri()); } @@ -102,10 +104,10 @@ public class TestVirtualHostStyleFilter { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest(s3HttpAddr, - "/myvolume/mybucket/myfile", false); + "/mybucket/myfile", false); virtualHostStyleFilter.filter(containerRequest); URI expected = new URI("http://" + s3HttpAddr + - "/myvolume/mybucket/myfile"); + "/mybucket/myfile"); Assert.assertEquals(expected, containerRequest.getRequestUri()); } @@ -118,10 +120,9 @@ public class TestVirtualHostStyleFilter { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - ".myvolume.localhost:9878", null, true); + ".localhost:9878", null, true); virtualHostStyleFilter.filter(containerRequest); - URI expected = new URI("http://" + s3HttpAddr + - "/myvolume/mybucket"); + URI expected = new URI("http://" + s3HttpAddr + "/mybucket"); Assert.assertEquals(expected, containerRequest.getRequestUri()); } @@ -134,7 +135,7 @@ public class TestVirtualHostStyleFilter { virtualHostStyleFilter.setConfiguration(conf); ContainerRequest containerRequest = createContainerRequest("mybucket" + - ".myvolume.localhost:9999", null, true); + ".localhost:9999", null, true); try { virtualHostStyleFilter.filter(containerRequest); } catch (InvalidRequestException ex) { @@ -144,16 +145,18 @@ public class TestVirtualHostStyleFilter { } @Test - public void testVirtualHostStyleWithoutVolumeName() throws Exception { + public void testIncorrectVirtualHostStyle() throws + Exception { VirtualHostStyleFilter virtualHostStyleFilter = new VirtualHostStyleFilter(); virtualHostStyleFilter.setConfiguration(conf); - ContainerRequest containerRequest = createContainerRequest("mybucket." + - ".localhost:9878", null, true); + ContainerRequest containerRequest = createContainerRequest("mybucket" + + "localhost:9878", null, true); try { virtualHostStyleFilter.filter(containerRequest); + fail("testIncorrectVirtualHostStyle failed"); } catch (InvalidRequestException ex) { GenericTestUtils.assertExceptionContains("invalid format", ex); }