ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jai...@apache.org
Subject ambari git commit: AMBARI-13965. Ambari CS View "Ambari Cluster URL" setting regex is too restrictive. (DIPAYAN BHOWMICK via Jaimin)
Date Mon, 23 Nov 2015 07:29:07 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk d750b960a -> 0b950d306


AMBARI-13965. Ambari CS View "Ambari Cluster URL" setting regex is too restrictive. (DIPAYAN
BHOWMICK via Jaimin)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/0b950d30
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/0b950d30
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/0b950d30

Branch: refs/heads/trunk
Commit: 0b950d306628349b5600939893ef869db8615f89
Parents: d750b96
Author: Jaimin Jetly <jaimin@hortonworks.com>
Authored: Sun Nov 22 23:26:44 2015 -0800
Committer: Jaimin Jetly <jaimin@hortonworks.com>
Committed: Sun Nov 22 23:27:54 2015 -0800

----------------------------------------------------------------------
 contrib/views/capacity-scheduler/pom.xml        |  5 +++
 .../capacityscheduler/PropertyValidator.java    | 28 ++++++++++++++--
 .../PropertyValidatorTest.java                  | 34 ++++++++++++++++++++
 3 files changed, 64 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/0b950d30/contrib/views/capacity-scheduler/pom.xml
----------------------------------------------------------------------
diff --git a/contrib/views/capacity-scheduler/pom.xml b/contrib/views/capacity-scheduler/pom.xml
index 4c66430..8fe6e6d 100644
--- a/contrib/views/capacity-scheduler/pom.xml
+++ b/contrib/views/capacity-scheduler/pom.xml
@@ -69,6 +69,11 @@
             <version>1.9</version>
         </dependency>
         <dependency>
+          <groupId>commons-validator</groupId>
+          <artifactId>commons-validator</artifactId>
+          <version>1.4.1</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.ambari</groupId>
             <artifactId>ambari-views</artifactId>
             <scope>provided</scope>

http://git-wip-us.apache.org/repos/asf/ambari/blob/0b950d30/contrib/views/capacity-scheduler/src/main/java/org/apache/ambari/view/capacityscheduler/PropertyValidator.java
----------------------------------------------------------------------
diff --git a/contrib/views/capacity-scheduler/src/main/java/org/apache/ambari/view/capacityscheduler/PropertyValidator.java
b/contrib/views/capacity-scheduler/src/main/java/org/apache/ambari/view/capacityscheduler/PropertyValidator.java
index c9b8a86..a4976c7 100644
--- a/contrib/views/capacity-scheduler/src/main/java/org/apache/ambari/view/capacityscheduler/PropertyValidator.java
+++ b/contrib/views/capacity-scheduler/src/main/java/org/apache/ambari/view/capacityscheduler/PropertyValidator.java
@@ -21,14 +21,17 @@ package org.apache.ambari.view.capacityscheduler;
 import org.apache.ambari.view.ViewInstanceDefinition;
 import org.apache.ambari.view.validation.ValidationResult;
 import org.apache.ambari.view.validation.Validator;
+import org.apache.commons.validator.routines.RegexValidator;
+import org.apache.commons.validator.routines.UrlValidator;
 
 import java.net.MalformedURLException;
-import java.net.URISyntaxException;
 import java.net.URL;
 
 public class PropertyValidator implements Validator {
 
   public static final String AMBARI_SERVER_URL = "ambari.server.url";
+  public static final String PATH_REGEX = "/api/v1/clusters/\\w+";
+  public static final String AUTHORITY_REGEX = "^[a-zA-Z0-9]+([\\-\\.]{1}[a-zA-Z0-9]+)*(:[0-9]{1,5}){1}$";
 
   @Override
   public ValidationResult validateInstance(ViewInstanceDefinition viewInstanceDefinition,
ValidationContext validationContext) {
@@ -44,14 +47,33 @@ public class PropertyValidator implements Validator {
     if (property.equals(AMBARI_SERVER_URL)) {
       String ambariServerUrl = viewInstanceDefinition.getPropertyMap().get(AMBARI_SERVER_URL);
 
-      if (!ambariServerUrl.matches("^https?://[\\w\\d\\.]+:\\d+/api/v1/clusters/\\w+$"))
{
+      if (!(validateUrl(new String[] {"http", "https"}, ambariServerUrl)
+        && validatePortAndPath(PATH_REGEX, ambariServerUrl))) {
         return new InvalidPropertyValidationResult(false,
-            "URL should contain protocol, hostname, port, cluster name, e.g. http://ambari.server:8080/api/v1/clusters/MyCluster");
+          "URL should contain protocol, hostname, port, cluster name, e.g. http://ambari.server:8080/api/v1/clusters/MyCluster");
       }
     }
     return ValidationResult.SUCCESS;
   }
 
+  private boolean validatePortAndPath(String pathRegex, String urlString) {
+    try {
+      URL url = new URL(urlString);
+      String path = url.getPath();
+      int port = url.getPort();
+      return path.matches(pathRegex) && (port != -1);
+    } catch (MalformedURLException e) {
+      // Unreachable as this will not be called if the URL is not valid
+    }
+    return false;
+  }
+
+  private boolean validateUrl(String[] schemas, String urlString) {
+    RegexValidator authorityValidator = new RegexValidator(AUTHORITY_REGEX);
+    UrlValidator validator = new UrlValidator(schemas, authorityValidator, UrlValidator.ALLOW_LOCAL_URLS);
+    return validator.isValid(urlString);
+  }
+
   public static class InvalidPropertyValidationResult implements ValidationResult {
     private boolean valid;
     private String detail;

http://git-wip-us.apache.org/repos/asf/ambari/blob/0b950d30/contrib/views/capacity-scheduler/src/test/java/org/apache/ambari/view/capacityscheduler/PropertyValidatorTest.java
----------------------------------------------------------------------
diff --git a/contrib/views/capacity-scheduler/src/test/java/org/apache/ambari/view/capacityscheduler/PropertyValidatorTest.java
b/contrib/views/capacity-scheduler/src/test/java/org/apache/ambari/view/capacityscheduler/PropertyValidatorTest.java
index bfa4f06..decc967 100644
--- a/contrib/views/capacity-scheduler/src/test/java/org/apache/ambari/view/capacityscheduler/PropertyValidatorTest.java
+++ b/contrib/views/capacity-scheduler/src/test/java/org/apache/ambari/view/capacityscheduler/PropertyValidatorTest.java
@@ -119,6 +119,40 @@ public class PropertyValidatorTest {
         Validator.ValidationContext.PRE_CREATE).isValid());
   }
 
+  @Test
+  public void shouldValidateUrlsWithHyphenInHostName() throws Exception {
+    ViewInstanceDefinition instanceDefinition =
+      getViewInstanceDefinition("http://sub-domain.hostname.com:8080/api/v1/clusters/Cluster");
+    PropertyValidator propertyValidator = new PropertyValidator();
+
+    assertTrue(propertyValidator.validateProperty(
+      PropertyValidator.AMBARI_SERVER_URL, instanceDefinition,
+      Validator.ValidationContext.PRE_CREATE).isValid());
+  }
+
+  @Test
+  public void shouldValidateUrlWithNonStandardTLDs() throws Exception {
+    ViewInstanceDefinition instanceDefinition =
+      getViewInstanceDefinition("http://cl1-node.nova:8080/api/v1/clusters/Cluster");
+    PropertyValidator propertyValidator = new PropertyValidator();
+
+    assertTrue(propertyValidator.validateProperty(
+      PropertyValidator.AMBARI_SERVER_URL, instanceDefinition,
+      Validator.ValidationContext.PRE_CREATE).isValid());
+  }
+
+  @Test
+  public void shouldValidateLocalhost() throws Exception {
+    ViewInstanceDefinition instanceDefinition =
+      getViewInstanceDefinition("http://localhost:8080/api/v1/clusters/Cluster");
+    PropertyValidator propertyValidator = new PropertyValidator();
+
+    assertTrue(propertyValidator.validateProperty(
+      PropertyValidator.AMBARI_SERVER_URL, instanceDefinition,
+      Validator.ValidationContext.PRE_CREATE).isValid());
+
+  }
+
   private ViewInstanceDefinition getViewInstanceDefinition(String ambariServerUrl) {
     ViewInstanceDefinition instanceDefinition = createNiceMock(ViewInstanceDefinition.class);
     Map<String, String> map = new HashMap<String, String>();


Mime
View raw message