ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rle...@apache.org
Subject ambari git commit: AMBARI-14780. Make the property validator for File Browser more restrictive. (yusaku via rlevas)
Date Mon, 25 Jan 2016 12:02:56 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.2 9d413a29b -> b5ab41513


AMBARI-14780. Make the property validator for File Browser more restrictive. (yusaku via rlevas)


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

Branch: refs/heads/branch-2.2
Commit: b5ab41513bf316e3f770d59205942e5cf2a60222
Parents: 9d413a2
Author: Yusaku Sako <yusaku@hortonworks.com>
Authored: Mon Jan 25 07:02:07 2016 -0500
Committer: Robert Levas <rlevas@hortonworks.com>
Committed: Mon Jan 25 07:02:31 2016 -0500

----------------------------------------------------------------------
 .../view/filebrowser/PropertyValidator.java     | 10 +--
 .../view/filebrowser/PropertyValidatorTest.java | 75 ++++++++++++++++++++
 .../ambari/view/hive/PropertyValidator.java     | 28 +-------
 .../ambari/view/pig/PropertyValidator.java      | 26 +------
 contrib/views/utils/pom.xml                     |  5 ++
 .../view/utils/ambari/ValidatorUtils.java       | 46 ++++++++++++
 6 files changed, 133 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/b5ab4151/contrib/views/files/src/main/java/org/apache/ambari/view/filebrowser/PropertyValidator.java
----------------------------------------------------------------------
diff --git a/contrib/views/files/src/main/java/org/apache/ambari/view/filebrowser/PropertyValidator.java
b/contrib/views/files/src/main/java/org/apache/ambari/view/filebrowser/PropertyValidator.java
index 41b9b05..2ad779c 100644
--- a/contrib/views/files/src/main/java/org/apache/ambari/view/filebrowser/PropertyValidator.java
+++ b/contrib/views/files/src/main/java/org/apache/ambari/view/filebrowser/PropertyValidator.java
@@ -19,14 +19,10 @@
 package org.apache.ambari.view.filebrowser;
 
 import org.apache.ambari.view.ViewInstanceDefinition;
+import org.apache.ambari.view.utils.ambari.ValidatorUtils;
 import org.apache.ambari.view.validation.ValidationResult;
 import org.apache.ambari.view.validation.Validator;
 
-import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-
 public class PropertyValidator implements Validator {
 
   public static final String WEBHDFS_URL = "webhdfs.url";
@@ -41,9 +37,7 @@ public class PropertyValidator implements Validator {
     if (property.equals(WEBHDFS_URL)) {
       String webhdfsUrl = viewInstanceDefinition.getPropertyMap().get(WEBHDFS_URL);
       if (webhdfsUrl != null) {
-        try {
-          new URI(webhdfsUrl);
-        } catch (URISyntaxException e) {
+        if (!ValidatorUtils.validateHdfsURL(webhdfsUrl)) {
           return new InvalidPropertyValidationResult(false, "Must be valid URL");
         }
       }

http://git-wip-us.apache.org/repos/asf/ambari/blob/b5ab4151/contrib/views/files/src/test/java/org/apache/ambari/view/filebrowser/PropertyValidatorTest.java
----------------------------------------------------------------------
diff --git a/contrib/views/files/src/test/java/org/apache/ambari/view/filebrowser/PropertyValidatorTest.java
b/contrib/views/files/src/test/java/org/apache/ambari/view/filebrowser/PropertyValidatorTest.java
new file mode 100644
index 0000000..5af65ab
--- /dev/null
+++ b/contrib/views/files/src/test/java/org/apache/ambari/view/filebrowser/PropertyValidatorTest.java
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.filebrowser;
+
+import org.apache.ambari.view.ViewInstanceDefinition;
+import org.apache.ambari.view.validation.ValidationResult;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+
+public class PropertyValidatorTest {
+
+  @Test
+  public void testValidatePropertyWithValidWebhdfsURI(){
+    Map<String,String> propertyMap = new HashMap<String, String>();
+    propertyMap.put(PropertyValidator.WEBHDFS_URL,"webhdfs://host:1234/");
+    ViewInstanceDefinition instanceDefinition = getInstanceDef(propertyMap);
+
+    ValidationResult result =  new PropertyValidator().validateProperty(PropertyValidator.WEBHDFS_URL,instanceDefinition,null);
+    assertEquals(result, ValidationResult.SUCCESS);
+    assertEquals(result.isValid(),true);
+  }
+
+  @Test
+  public void testValidatePropertyWithValidHdfsURI(){
+    Map<String,String> propertyMap = new HashMap<String, String>();
+    propertyMap.put(PropertyValidator.WEBHDFS_URL,"hdfs://host:1234/");
+    ViewInstanceDefinition instanceDefinition = getInstanceDef(propertyMap);
+
+    ValidationResult result =  new PropertyValidator().validateProperty(PropertyValidator.WEBHDFS_URL,instanceDefinition,null);
+    assertEquals(result, ValidationResult.SUCCESS);
+    assertEquals(result.isValid(),true);
+  }
+
+  @Test
+  public void testValidatePropertyWithLocalFileURI(){
+    Map<String,String> propertyMap = new HashMap<String, String>();
+    propertyMap.put(PropertyValidator.WEBHDFS_URL,"file:///");
+    ViewInstanceDefinition instanceDefinition = getInstanceDef(propertyMap);
+
+    ValidationResult result =  new PropertyValidator().validateProperty(PropertyValidator.WEBHDFS_URL,instanceDefinition,null);
+    assertEquals(result.getClass(), PropertyValidator.InvalidPropertyValidationResult.class);
+    assertEquals(result.isValid(),false);
+    assertEquals(result.getDetail(),"Must be valid URL");
+  }
+
+  private ViewInstanceDefinition getInstanceDef(Map<String,String> propertyMap){
+    ViewInstanceDefinition instanceDefinition = createNiceMock(ViewInstanceDefinition.class);
+    expect(instanceDefinition.getPropertyMap()).andReturn(propertyMap);
+    replay(instanceDefinition);
+    return  instanceDefinition;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/b5ab4151/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
index e97196e..ae73bc0 100644
--- a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
+++ b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
@@ -19,10 +19,9 @@
 package org.apache.ambari.view.hive;
 
 import org.apache.ambari.view.ViewInstanceDefinition;
+import org.apache.ambari.view.utils.ambari.ValidatorUtils;
 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;
 
 public class PropertyValidator implements Validator {
 
@@ -61,7 +60,7 @@ public class PropertyValidator implements Validator {
     // Cluster associated properties
     if (property.equals(WEBHDFS_URL)) {
       String webhdfsUrl = viewInstanceDefinition.getPropertyMap().get(WEBHDFS_URL);
-      if (!validateHdfsURL(webhdfsUrl)) {
+      if (!ValidatorUtils.validateHdfsURL(webhdfsUrl)) {
         return new InvalidPropertyValidationResult(false, "Must be valid URL");
       }
     }
@@ -82,7 +81,7 @@ public class PropertyValidator implements Validator {
 
     if (property.equals(YARN_ATS_URL)) {
       String atsUrl = viewInstanceDefinition.getPropertyMap().get(YARN_ATS_URL);
-      if (!validateHttpURL(atsUrl)) {
+      if (!ValidatorUtils.validateHttpURL(atsUrl)) {
         return new InvalidPropertyValidationResult(false, "Must be valid URL");
       }
     }
@@ -90,27 +89,6 @@ public class PropertyValidator implements Validator {
     return ValidationResult.SUCCESS;
   }
 
-  /**
-   * Validates filesystem URL
-   * @param webhdfsUrl url
-   * @return is url valid
-   */
-  private boolean validateHdfsURL(String webhdfsUrl) {
-    String[] schemes = {"webhdfs", "hdfs", "s3", "file"};
-    return validateURL(webhdfsUrl, schemes);
-  }
-
-  private boolean validateHttpURL(String webhdfsUrl) {
-    String[] schemes = {"http", "https"};
-    return validateURL(webhdfsUrl, schemes);
-  }
-
-  private boolean validateURL(String webhdfsUrl, String[] schemes) {
-    RegexValidator authority = new RegexValidator(".*");
-    UrlValidator urlValidator = new UrlValidator(schemes, authority, UrlValidator.ALLOW_LOCAL_URLS);
-    return urlValidator.isValid(webhdfsUrl);
-  }
-
   public static class InvalidPropertyValidationResult implements ValidationResult {
     private boolean valid;
     private String detail;

http://git-wip-us.apache.org/repos/asf/ambari/blob/b5ab4151/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/PropertyValidator.java
----------------------------------------------------------------------
diff --git a/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/PropertyValidator.java
b/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/PropertyValidator.java
index c36561f..d3c9866 100644
--- a/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/PropertyValidator.java
+++ b/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/PropertyValidator.java
@@ -19,10 +19,9 @@
 package org.apache.ambari.view.pig;
 
 import org.apache.ambari.view.ViewInstanceDefinition;
+import org.apache.ambari.view.utils.ambari.ValidatorUtils;
 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;
 
 public class PropertyValidator implements Validator {
 
@@ -48,7 +47,7 @@ public class PropertyValidator implements Validator {
     // 3. Cluster associated properties
     if (property.equals(WEBHDFS_URL)) {
       String webhdfsUrl = viewInstanceDefinition.getPropertyMap().get(WEBHDFS_URL);
-      if (!validateHdfsURL(webhdfsUrl)) {
+      if (!ValidatorUtils.validateHdfsURL(webhdfsUrl)) {
         return new InvalidPropertyValidationResult(false, "Must be valid URL");
       }
     }
@@ -70,27 +69,6 @@ public class PropertyValidator implements Validator {
     return ValidationResult.SUCCESS;
   }
 
-  /**
-   * Validates filesystem URL
-   * @param webhdfsUrl url
-   * @return is url valid
-   */
-  private boolean validateHdfsURL(String webhdfsUrl) {
-    String[] schemes = {"webhdfs", "hdfs", "s3", "file"};
-    return validateURL(webhdfsUrl, schemes);
-  }
-
-  private boolean validateHttpURL(String webhdfsUrl) {
-    String[] schemes = {"http", "https"};
-    return validateURL(webhdfsUrl, schemes);
-  }
-
-  private boolean validateURL(String webhdfsUrl, String[] schemes) {
-    RegexValidator authority = new RegexValidator(".*");
-    UrlValidator urlValidator = new UrlValidator(schemes, authority, UrlValidator.ALLOW_LOCAL_URLS);
-    return urlValidator.isValid(webhdfsUrl);
-  }
-
   public static class InvalidPropertyValidationResult implements ValidationResult {
     private boolean valid;
     private String detail;

http://git-wip-us.apache.org/repos/asf/ambari/blob/b5ab4151/contrib/views/utils/pom.xml
----------------------------------------------------------------------
diff --git a/contrib/views/utils/pom.xml b/contrib/views/utils/pom.xml
index eaf6b49..89ecc68 100644
--- a/contrib/views/utils/pom.xml
+++ b/contrib/views/utils/pom.xml
@@ -138,6 +138,11 @@
       <artifactId>hadoop-azure</artifactId>
       <version>${hadoop.version}</version>
     </dependency>
+    <dependency>
+      <groupId>commons-validator</groupId>
+      <artifactId>commons-validator</artifactId>
+      <version>1.4.0</version>
+    </dependency>
   </dependencies>
 
   <properties>

http://git-wip-us.apache.org/repos/asf/ambari/blob/b5ab4151/contrib/views/utils/src/main/java/org/apache/ambari/view/utils/ambari/ValidatorUtils.java
----------------------------------------------------------------------
diff --git a/contrib/views/utils/src/main/java/org/apache/ambari/view/utils/ambari/ValidatorUtils.java
b/contrib/views/utils/src/main/java/org/apache/ambari/view/utils/ambari/ValidatorUtils.java
new file mode 100644
index 0000000..f5654f2
--- /dev/null
+++ b/contrib/views/utils/src/main/java/org/apache/ambari/view/utils/ambari/ValidatorUtils.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.utils.ambari;
+
+
+import org.apache.commons.validator.routines.RegexValidator;
+import org.apache.commons.validator.routines.UrlValidator;
+
+public class ValidatorUtils {
+  /**
+   * Validates filesystem URL
+   * @param webhdfsUrl url
+   * @return is url valid
+   */
+  public static boolean validateHdfsURL(String webhdfsUrl) {
+    String[] schemes = {"webhdfs", "hdfs", "s3","wasb"};
+    return validateURL(webhdfsUrl, schemes);
+  }
+
+  public static boolean validateHttpURL(String webhdfsUrl) {
+    String[] schemes = {"http", "https"};
+    return validateURL(webhdfsUrl, schemes);
+  }
+
+  public static boolean validateURL(String webhdfsUrl, String[] schemes) {
+    RegexValidator authority = new RegexValidator(".*");
+    UrlValidator urlValidator = new UrlValidator(schemes, authority, UrlValidator.ALLOW_LOCAL_URLS);
+    return urlValidator.isValid(webhdfsUrl);
+  }
+}


Mime
View raw message