You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by av...@apache.org on 2020/07/28 13:07:01 UTC

[ignite] branch master updated: IGNITE-13291 Add path validation to Ignite codebase. Remove unnecessary dependency to Apache Curator. (#8080)

This is an automated email from the ASF dual-hosted git repository.

av pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new da22909  IGNITE-13291 Add path validation to Ignite codebase. Remove unnecessary dependency to Apache Curator. (#8080)
da22909 is described below

commit da22909747a9205b7097b2ab0b2b802a7b986a4f
Author: Ivan Daschinskiy <iv...@gmail.com>
AuthorDate: Tue Jul 28 16:06:36 2020 +0300

    IGNITE-13291 Add path validation to Ignite codebase. Remove unnecessary dependency to Apache Curator. (#8080)
---
 .../spi/discovery/zk/ZookeeperDiscoverySpi.java    |   4 +-
 .../spi/discovery/zk/internal/ZkIgnitePaths.java   |  63 ++++++++++-
 .../zk/ZookeeperDiscoverySpiTestSuite1.java        |   2 +
 .../zk/internal/ZookeeperValidatePathsTest.java    | 126 +++++++++++++++++++++
 4 files changed, 192 insertions(+), 3 deletions(-)

diff --git a/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java b/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java
index 44ce423..5cdfa58 100644
--- a/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java
+++ b/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java
@@ -26,7 +26,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-import org.apache.curator.utils.PathUtils;
 import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.internal.IgniteFeatures;
@@ -55,6 +54,7 @@ import org.apache.ignite.spi.discovery.DiscoverySpiListener;
 import org.apache.ignite.spi.discovery.DiscoverySpiMutableCustomMessageSupport;
 import org.apache.ignite.spi.discovery.DiscoverySpiNodeAuthenticator;
 import org.apache.ignite.spi.discovery.DiscoverySpiOrderSupport;
+import org.apache.ignite.spi.discovery.zk.internal.ZkIgnitePaths;
 import org.apache.ignite.spi.discovery.zk.internal.ZookeeperClusterNode;
 import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoveryImpl;
 import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoveryStatistics;
@@ -452,7 +452,7 @@ public class ZookeeperDiscoverySpi extends IgniteSpiAdapter implements IgniteDis
             zkRootPath = zkRootPath.substring(0, zkRootPath.length() - 1);
 
         try {
-            PathUtils.validatePath(zkRootPath);
+            ZkIgnitePaths.validatePath(zkRootPath);
         }
         catch (IllegalArgumentException e) {
             throw new IgniteSpiException("zkRootPath is invalid: " + zkRootPath, e);
diff --git a/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/internal/ZkIgnitePaths.java b/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/internal/ZkIgnitePaths.java
index 88b35d8..4e54254 100644
--- a/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/internal/ZkIgnitePaths.java
+++ b/modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/internal/ZkIgnitePaths.java
@@ -22,7 +22,7 @@ import java.util.UUID;
 /**
  *
  */
-class ZkIgnitePaths {
+public class ZkIgnitePaths {
     /** */
     static final String PATH_SEPARATOR = "/";
 
@@ -304,4 +304,65 @@ class ZkIgnitePaths {
 
         return (byte)(Integer.parseInt(flagsStr, 16) - 128);
     }
+
+    /**
+     * Validate the provided znode path string.
+     *
+     * @param path znode path string.
+     * @return The given path if it was valid, for fluent chaining.
+     * @throws IllegalArgumentException if the path is invalid/
+     */
+    public static String validatePath(String path) throws IllegalArgumentException {
+        if (path == null)
+            throw new IllegalArgumentException("Path cannot be null");
+
+        if (path.isEmpty())
+            throw new IllegalArgumentException("Path length must be > 0");
+
+        if (path.charAt(0) != '/')
+            throw new IllegalArgumentException("Path must start with / character");
+
+        if (path.length() == 1)
+            return path;
+
+        if (path.charAt(path.length() - 1) == '/')
+            throw new IllegalArgumentException("Path must not end with / character");
+
+        String reason = null;
+        char prev = '/';
+        char chars[] = path.toCharArray();
+        char c;
+
+        for (int i = 1; i < chars.length; prev = chars[i], i++) {
+            c = chars[i];
+
+            if (c == 0) {
+                reason = "null character not allowed @" + i;
+
+                break;
+            }
+            else if (c == '/' && prev == '/') {
+                reason = "empty node name specified @" + i;
+
+                break;
+            }
+            else if (c == '.' && (i + 1 == chars.length || chars[i + 1] == '/')
+                && ((chars[i - 2] == '/' && prev == '.') || chars[i - 1] == '/')) {
+                reason = "relative paths not allowed @" + i;
+
+                break;
+            }
+            else if (c > '\u0000' && c < '\u001f' || c > '\u007f' && c < '\u009f' || c > '\ud800' && c < '\uf8ff'
+                || c > '\ufff0' && c < '\uffff') {
+                reason = "invalid charater @" + i;
+
+                break;
+            }
+        }
+
+        if (reason != null)
+            throw new IllegalArgumentException("Invalid path string \"" + path + "\" caused by " + reason);
+
+        return path;
+    }
 }
diff --git a/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpiTestSuite1.java b/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpiTestSuite1.java
index d51c369..d5be881 100644
--- a/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpiTestSuite1.java
+++ b/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpiTestSuite1.java
@@ -30,6 +30,7 @@ import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoverySpiSaslSucc
 import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoverySpiSslTest;
 import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoverySplitBrainTest;
 import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoveryTopologyChangeAndReconnectTest;
+import org.apache.ignite.spi.discovery.zk.internal.ZookeeperValidatePathsTest;
 import org.junit.BeforeClass;
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
@@ -39,6 +40,7 @@ import org.junit.runners.Suite;
  */
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
+    ZookeeperValidatePathsTest.class,
     ZookeeperDiscoverySegmentationAndConnectionRestoreTest.class,
     ZookeeperDiscoveryConcurrentStartAndStartStopTest.class,
     ZookeeperDiscoveryTopologyChangeAndReconnectTest.class,
diff --git a/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperValidatePathsTest.java b/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperValidatePathsTest.java
new file mode 100644
index 0000000..5438beb
--- /dev/null
+++ b/modules/zookeeper/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperValidatePathsTest.java
@@ -0,0 +1,126 @@
+/*
+ * 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.ignite.spi.discovery.zk.internal;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/**
+ * Test {@link ZkIgnitePaths#validatePath} implementation.
+ */
+@RunWith(Enclosed.class)
+public class ZookeeperValidatePathsTest {
+    /**
+     *  Parameterized test for testing common cases, excluding unprintable characters.
+     */
+    @RunWith(Parameterized.class)
+    public static class ZoookeperCommonValidatePathsTest extends GridCommonAbstractTest {
+        /** */
+        @Parameterized.Parameters(name = "input string = {0}, expected error = {1}")
+        public static Collection<Object[]> parameters() {
+            return Arrays.asList(
+                new Object[] {"/apacheIgnite", null},
+                new Object[] {null, "Path cannot be null"},
+                new Object[] {"", "Path length must be > 0"},
+                new Object[] {"/apacheIgnite/../root", "relative paths not allowed @15"},
+                new Object[] {"/apacheIgnite/./root", "relative paths not allowed @14"},
+                new Object[] {"/apacheIgnite//root", "empty node name specified @14"},
+                new Object[] {"/apacheIgnite/", "Path must not end with / character"}
+            );
+        }
+
+        /** Zookeeper path to validate. */
+        @Parameterized.Parameter
+        public String zkPath;
+
+        /** Expected error. If {@code null}, path is correct. */
+        @Parameterized.Parameter(1)
+        public String errMsg;
+
+        /** */
+        @Test
+        public void testValidatePath() {
+            validatePath(zkPath, errMsg);
+        }
+    }
+
+    /**
+     * Test validate path with unprintable characters. Should move it to separate test because of
+     * surefire report problems on TC.
+     */
+    public static class ZookeeperUnprintableCharactersValidatePathTest extends GridCommonAbstractTest {
+        /** */
+        @Test
+        public void testValidatePathWithUnprintableCharacters() {
+            for (Character c: unprintables())
+                validatePath(String.format("/apacheIgnite%s", c), "invalid charater @13");
+
+            validatePath(String.format("/apacheIgnite%s", '\u0000'), "null character not allowed @13");
+        }
+    }
+
+    /** */
+    private static void validatePath(String path, String msg) {
+        try {
+            ZkIgnitePaths.validatePath(path);
+
+            if (msg != null)
+                Assert.fail(String.format("Expected failure containing \"%s\" in message did't occur", msg));
+        }
+        catch (IllegalArgumentException e) {
+            if (msg == null) {
+                Assert.fail(String.format("Path \"%s\" should be considering as valid, but failing with \"%s\"",
+                    path, e.getMessage()));
+            }
+            else {
+                Assert.assertTrue(String.format("Error messages \"%s\" does't contains \"%s\"", e.getMessage(), msg),
+                    e.getMessage().contains(msg));
+            }
+        }
+    }
+
+    /**
+     * @return List of unprintables characters.
+     */
+    private static List<Character> unprintables() {
+        List<Character> ret = new ArrayList<>();
+
+        List<IgnitePair<Character>> intervals = Arrays.asList(
+            new IgnitePair<>('\u0000', '\u001f'),
+            new IgnitePair<>('\u007f', '\u009f'),
+            new IgnitePair<>('\ud800', '\uf8ff'),
+            new IgnitePair<>('\ufff0', '\uffff')
+        );
+
+        for (IgnitePair<Character> interval: intervals) {
+            for (char c = (char)(interval.get1() + 1); c < interval.get2(); c++)
+                ret.add(c);
+        }
+
+        return ret;
+    }
+}