HDDS-563. Support hybrid VirtualHost style URL. Contributed by Bharat Viswanadham.

This commit is contained in:
Márton Elek 2018-10-17 11:34:03 +02:00
parent 5085e5fa9e
commit 9df1c84be0
2 changed files with 21 additions and 23 deletions

View File

@ -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("" +
"(?<bucket>(.+))\\.(?<volume>(.+))\\.");
@Inject
private OzoneConfiguration conf;
@ -83,24 +81,21 @@ public void filter(ContainerRequestContext requestContext) throws
// 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);
}

View File

@ -31,6 +31,8 @@
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 void testVirtualHostStyle() throws Exception {
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 void testPathStyle() throws Exception {
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 void testVirtualHostStyleWithCreateBucketRequest() throws Exception {
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 void testVirtualHostStyleWithNoMatchingDomain() throws Exception {
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 void testVirtualHostStyleWithNoMatchingDomain() throws Exception {
}
@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);
}