geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject geode git commit: GEODE-2955: Validating region names when creating lucene index
Date Wed, 24 May 2017 22:26:13 GMT
Repository: geode
Updated Branches:
  refs/heads/develop 662358fdb -> ee9ca4e81


GEODE-2955: Validating region names when creating lucene index

	This closes #531


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

Branch: refs/heads/develop
Commit: ee9ca4e8176183a6d0128322d21c47e5c4a0ffdf
Parents: 662358f
Author: David Anuta <david.r.anuta@gmail.com>
Authored: Wed May 24 11:34:39 2017 -0700
Committer: nabarun <nnag@pivotal.io>
Committed: Wed May 24 15:25:36 2017 -0700

----------------------------------------------------------------------
 .../geode/internal/cache/LocalRegion.java       |  3 +--
 .../lucene/internal/LuceneServiceImpl.java      | 26 ++++++++++++++++++++
 .../internal/cli/LuceneIndexCommands.java       |  4 +++
 .../functions/LuceneCreateIndexFunction.java    |  4 +++
 .../cli/LuceneIndexCommandsDUnitTest.java       | 26 +++++++++++++++++++-
 5 files changed, 60 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/ee9ca4e8/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java b/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java
index 4446d48..f581856 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java
@@ -231,8 +231,6 @@ public class LocalRegion extends AbstractRegion implements LoaderHelperFactory,
   // package-private to avoid synthetic accessor
   static final Logger logger = LogService.getLogger();
 
-  private static final Pattern NAME_PATTERN = Pattern.compile("[aA-zZ0-9-_.]+");
-
   /**
    * Internal interface used to simulate failures when performing entry operations
    * 
@@ -7389,6 +7387,7 @@ public class LocalRegion extends AbstractRegion implements LoaderHelperFactory,
           "Region names may not begin with a double-underscore: " + name);
     }
 
+    final Pattern NAME_PATTERN = Pattern.compile("[aA-zZ0-9-_.]+");
     // Ensure the region only contains valid characters
     Matcher matcher = NAME_PATTERN.matcher(name);
     if (!matcher.matches()) {

http://git-wip-us.apache.org/repos/asf/geode/blob/ee9ca4e8/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java
b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java
index afbcc40..8ce7028 100644
--- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java
+++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/LuceneServiceImpl.java
@@ -17,6 +17,8 @@ package org.apache.geode.cache.lucene.internal;
 
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.geode.cache.lucene.LuceneIndexExistsException;
 import org.apache.geode.cache.lucene.internal.distributed.LuceneQueryFunction;
@@ -126,6 +128,30 @@ public class LuceneServiceImpl implements InternalLuceneService {
     return getUniqueIndexName(indexName, regionPath) + regionSuffix;
   }
 
+  public static void validateRegionName(String name) {
+    if (name == null) {
+      throw new IllegalArgumentException(
+          LocalizedStrings.LocalRegion_NAME_CANNOT_BE_NULL.toLocalizedString());
+    }
+    if (name.isEmpty()) {
+      throw new IllegalArgumentException(
+          LocalizedStrings.LocalRegion_NAME_CANNOT_BE_EMPTY.toLocalizedString());
+    }
+
+    if (name.startsWith("__")) {
+      throw new IllegalArgumentException(
+          "Region names may not begin with a double-underscore: " + name);
+    }
+
+    final Pattern NAME_PATTERN = Pattern.compile("[aA-zZ0-9-_.]+");
+    // Ensure the region only contains valid characters
+    Matcher matcher = NAME_PATTERN.matcher(name);
+    if (!matcher.matches()) {
+      throw new IllegalArgumentException(
+          "Region names may only be alphanumeric and may contain hyphens or underscores:
" + name);
+    }
+  }
+
   public void createIndex(String indexName, String regionPath,
       Map<String, Analyzer> fieldAnalyzers) {
     if (fieldAnalyzers == null || fieldAnalyzers.isEmpty()) {

http://git-wip-us.apache.org/repos/asf/geode/blob/ee9ca4e8/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommands.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommands.java
b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommands.java
index 5e17f6e..9317e2e 100755
--- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommands.java
+++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommands.java
@@ -35,6 +35,7 @@ import org.apache.geode.management.cli.CliMetaData;
 import org.apache.geode.management.cli.ConverterHint;
 import org.apache.geode.management.cli.Result;
 import org.apache.geode.management.internal.cli.CliUtil;
+import org.apache.geode.management.internal.cli.LogWrapper;
 import org.apache.geode.management.internal.cli.commands.AbstractCommandsSupport;
 import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
 import org.apache.geode.management.internal.cli.i18n.CliStrings;
@@ -202,6 +203,9 @@ public class LuceneIndexCommands extends AbstractCommandsSupport {
         }
       }
       result = ResultBuilder.buildResult(tabularResult);
+    } catch (IllegalArgumentException iae) {
+      LogWrapper.getInstance().info(iae.getMessage());
+      result = ResultBuilder.createUserErrorResult(iae.getMessage());
     } catch (CommandResultException crex) {
       result = crex.getResult();
     } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/geode/blob/ee9ca4e8/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/functions/LuceneCreateIndexFunction.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/functions/LuceneCreateIndexFunction.java
b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/functions/LuceneCreateIndexFunction.java
index 9f938a5..422b1ef 100644
--- a/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/functions/LuceneCreateIndexFunction.java
+++ b/geode-lucene/src/main/java/org/apache/geode/cache/lucene/internal/cli/functions/LuceneCreateIndexFunction.java
@@ -15,6 +15,8 @@
 
 package org.apache.geode.cache.lucene.internal.cli.functions;
 
+import static org.apache.geode.cache.lucene.internal.LuceneServiceImpl.validateRegionName;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.geode.cache.Cache;
 import org.apache.geode.cache.CacheFactory;
@@ -81,6 +83,8 @@ public class LuceneCreateIndexFunction extends FunctionAdapter implements
Intern
           indexFactory.addField(fields[i], analyzer);
         }
       }
+
+      validateRegionName(indexInfo.getRegionPath());
       indexFactory.create(indexInfo.getIndexName(), indexInfo.getRegionPath());
 
       // TODO - update cluster configuration by returning a valid XmlEntity

http://git-wip-us.apache.org/repos/asf/geode/blob/ee9ca4e8/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
index bc4c68e..04359a3 100755
--- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
+++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/internal/cli/LuceneIndexCommandsDUnitTest.java
@@ -198,6 +198,31 @@ public class LuceneIndexCommandsDUnitTest extends CliCommandTestBase
{
   }
 
   @Test
+  public void createIndexShouldNotAcceptEmptyRegionNames() {
+    final VM vm1 = Host.getHost(0).getVM(-1);
+    vm1.invoke(() -> {
+      getCache();
+    });
+
+    CommandStringBuilder csb = new CommandStringBuilder(LuceneCliStrings.LUCENE_CREATE_INDEX);
+    csb.addOption(LuceneCliStrings.LUCENE__INDEX_NAME, INDEX_NAME);
+    csb.addOption(LuceneCliStrings.LUCENE__REGION_PATH, "\'__\'");
+    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, "field1,field2,field3");
+
+    String resultAsString = executeCommandAndLogResult(csb);
+    assertTrue(resultAsString.contains("Region names may not begin with a double-underscore:"));
+
+    csb = new CommandStringBuilder(LuceneCliStrings.LUCENE_CREATE_INDEX);
+    csb.addOption(LuceneCliStrings.LUCENE__INDEX_NAME, INDEX_NAME);
+    csb.addOption(LuceneCliStrings.LUCENE__REGION_PATH, "\' @@@*%\'");
+    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, "field1,field2,field3");
+
+    resultAsString = executeCommandAndLogResult(csb);
+    assertTrue(resultAsString
+        .contains("Region names may only be alphanumeric and may contain hyphens or underscores:"));
+  }
+
+  @Test
   public void createIndexShouldTrimAnalyzerNames() throws Exception {
     final VM vm1 = Host.getHost(0).getVM(-1);
     vm1.invoke(() -> {
@@ -497,7 +522,6 @@ public class LuceneIndexCommandsDUnitTest extends CliCommandTestBase {
 
     TabularResultData data = (TabularResultData) executeCommandAndGetResult(csb).getResultData();
     assertEquals(4, data.retrieveAllValues("key").size());
-
   }
 
   @Test


Mime
View raw message