Browse Source

HDDS-573. Make VirtualHostStyleFilter port agnostic. Contributed by Danilo Perez.

Márton Elek 6 years ago
parent
commit
095e8f61b7

+ 9 - 0
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/VirtualHostStyleFilter.java

@@ -74,6 +74,7 @@ public class VirtualHostStyleFilter implements ContainerRequestFilter {
     }
     }
     //Get the value of the host
     //Get the value of the host
     String host = requestContext.getHeaderString(HttpHeaders.HOST);
     String host = requestContext.getHeaderString(HttpHeaders.HOST);
+    host = checkHostWithoutPort(host);
     String domain = getDomainName(host);
     String domain = getDomainName(host);
 
 
     if (domain == null) {
     if (domain == null) {
@@ -148,6 +149,14 @@ public class VirtualHostStyleFilter implements ContainerRequestFilter {
     return match;
     return match;
   }
   }
 
 
+  private String checkHostWithoutPort(String host) {
+    if (host.contains(":")){
+      return host.substring(0, host.lastIndexOf(":"));
+    } else {
+      return host;
+    }
+  }
+
   @VisibleForTesting
   @VisibleForTesting
   public void setAuthenticationHeaderParser(AuthenticationHeaderParser parser) {
   public void setAuthenticationHeaderParser(AuthenticationHeaderParser parser) {
     this.authenticationHeaderParser = parser;
     this.authenticationHeaderParser = parser;

+ 3 - 1
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestVirtualHostStyleFilter.java

@@ -49,6 +49,7 @@ public class TestVirtualHostStyleFilter {
     conf = new OzoneConfiguration();
     conf = new OzoneConfiguration();
     s3HttpAddr = "localhost:9878";
     s3HttpAddr = "localhost:9878";
     conf.set(S3GatewayConfigKeys.OZONE_S3G_HTTP_ADDRESS_KEY, s3HttpAddr);
     conf.set(S3GatewayConfigKeys.OZONE_S3G_HTTP_ADDRESS_KEY, s3HttpAddr);
+    s3HttpAddr = s3HttpAddr.substring(0, s3HttpAddr.lastIndexOf(":"));
     conf.set(S3GatewayConfigKeys.OZONE_S3G_DOMAIN_NAME, s3HttpAddr);
     conf.set(S3GatewayConfigKeys.OZONE_S3G_DOMAIN_NAME, s3HttpAddr);
     authenticationHeaderParser = new AuthenticationHeaderParser();
     authenticationHeaderParser = new AuthenticationHeaderParser();
     authenticationHeaderParser.setAuthHeader("AWS ozone:scret");
     authenticationHeaderParser.setAuthHeader("AWS ozone:scret");
@@ -185,9 +186,10 @@ public class TestVirtualHostStyleFilter {
         authenticationHeaderParser);
         authenticationHeaderParser);
 
 
     ContainerRequest containerRequest = createContainerRequest("mybucket" +
     ContainerRequest containerRequest = createContainerRequest("mybucket" +
-        ".localhost:9999", null, null, true);
+        ".myhost:9999", null, null, true);
     try {
     try {
       virtualHostStyleFilter.filter(containerRequest);
       virtualHostStyleFilter.filter(containerRequest);
+      fail("testVirtualHostStyleWithNoMatchingDomain");
     } catch (InvalidRequestException ex) {
     } catch (InvalidRequestException ex) {
       GenericTestUtils.assertExceptionContains("No matching domain", ex);
       GenericTestUtils.assertExceptionContains("No matching domain", ex);
     }
     }