cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gdusba...@apache.org
Subject svn commit: r984995 - in /cassandra/trunk: src/java/org/apache/cassandra/config/ src/java/org/apache/cassandra/db/migration/ src/java/org/apache/cassandra/service/ test/system/ test/unit/org/apache/cassandra/db/
Date Thu, 12 Aug 2010 21:39:26 GMT
Author: gdusbabek
Date: Thu Aug 12 21:39:26 2010
New Revision: 984995

URL: http://svn.apache.org/viewvc?rev=984995&view=rev
Log:
disallow invalid ks+cf names. patch by gdusbabek, reviewed by jbellis. CASSANDRA-1377

Modified:
    cassandra/trunk/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
    cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddColumnFamily.java
    cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddKeyspace.java
    cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java
    cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameColumnFamily.java
    cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameKeyspace.java
    cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java
    cassandra/trunk/test/system/test_thrift_server.py
    cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java

Modified: cassandra/trunk/src/java/org/apache/cassandra/config/DatabaseDescriptor.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/config/DatabaseDescriptor.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/config/DatabaseDescriptor.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/config/DatabaseDescriptor.java Thu Aug 12
21:39:26 2010
@@ -476,11 +476,11 @@ public class DatabaseDescriptor
             Collection<KSMetaData> tableDefs = DefsTable.loadFromStorage(uuid);   
             for (KSMetaData def : tableDefs)
             {
-                if (!def.name.matches("\\w+"))
+                if (!def.name.matches(Migration.NAME_VALIDATOR_REGEX))
                     throw new RuntimeException("invalid keyspace name: " + def.name);
                 for (CFMetaData cfm : def.cfMetaData().values())
                 {
-                    if (!cfm.cfName.matches("\\w+"))
+                    if (!cfm.cfName.matches(Migration.NAME_VALIDATOR_REGEX))
                         throw new RuntimeException("invalid column family name: " + cfm.cfName);
                     try
                     {
@@ -563,7 +563,7 @@ public class DatabaseDescriptor
                 {
                     throw new ConfigurationException("ColumnFamily name attribute is required");
                 }
-                if (!cf.name.matches("\\w+"))
+                if (!cf.name.matches(Migration.NAME_VALIDATOR_REGEX))
                 {
                     throw new ConfigurationException("ColumnFamily name contains invalid
characters.");
                 }

Modified: cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddColumnFamily.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddColumnFamily.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddColumnFamily.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddColumnFamily.java Thu Aug
12 21:39:26 2010
@@ -52,6 +52,8 @@ public class AddColumnFamily extends Mig
             throw new ConfigurationException("Keyspace does not already exist.");
         else if (ksm.cfMetaData().containsKey(cfm.cfName))
             throw new ConfigurationException("CF is already defined in that keyspace.");
+        else if (!Migration.isLegalName(cfm.cfName))
+            throw new ConfigurationException("Invalid column family name: " + cfm.cfName);
         
         // clone ksm but include the new cf def.
         KSMetaData newKsm = makeNewKeyspaceDefinition(ksm);

Modified: cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddKeyspace.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddKeyspace.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddKeyspace.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/migration/AddKeyspace.java Thu Aug 12
21:39:26 2010
@@ -45,6 +45,11 @@ public class AddKeyspace extends Migrati
         
         if (DatabaseDescriptor.getTableDefinition(ksm.name) != null)
             throw new ConfigurationException("Keyspace already exists.");
+        if (!Migration.isLegalName(ksm.name))
+            throw new ConfigurationException("Invalid keyspace name: " + ksm.name);
+        for (CFMetaData cfm : ksm.cfMetaData().values())
+            if (!Migration.isLegalName(cfm.cfName))
+                throw new ConfigurationException("Invalid column family name: " + cfm.cfName);
         
         this.ksm = ksm;
         rm = makeDefinitionMutation(ksm, null, newVersion);

Modified: cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/migration/Migration.java Thu Aug 12 21:39:26
2010
@@ -68,6 +68,7 @@ public abstract class Migration
 {
     private static final Logger logger = LoggerFactory.getLogger(Migration.class);
     
+    public static final String NAME_VALIDATOR_REGEX = "\\w+";
     public static final String MIGRATIONS_CF = "Migrations";
     public static final String SCHEMA_CF = "Schema";
     public static final byte[] MIGRATIONS_KEY = "Migrations Key".getBytes(UTF_8);
@@ -326,4 +327,9 @@ public abstract class Migration
     {
         return version.toString().getBytes(UTF_8);
     }
+    
+    public static boolean isLegalName(String s)
+    {
+        return s.matches(Migration.NAME_VALIDATOR_REGEX);
+    }
 }

Modified: cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameColumnFamily.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameColumnFamily.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameColumnFamily.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameColumnFamily.java Thu
Aug 12 21:39:26 2010
@@ -61,6 +61,8 @@ public class RenameColumnFamily extends 
             throw new ConfigurationException("CF is not defined in that keyspace.");
         if (ksm.cfMetaData().containsKey(newName))
             throw new ConfigurationException("CF is already defined in that keyspace.");
+        if (!Migration.isLegalName(newName))
+            throw new ConfigurationException("Invalid column family name: " + newName);
         
         cfId = ksm.cfMetaData().get(oldName).cfId;
         

Modified: cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameKeyspace.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameKeyspace.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameKeyspace.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/migration/RenameKeyspace.java Thu Aug
12 21:39:26 2010
@@ -55,6 +55,8 @@ public class RenameKeyspace extends Migr
             throw new ConfigurationException("Keyspace either does not exist or does not
match the one currently defined.");
         if (DatabaseDescriptor.getTableDefinition(newName) != null)
             throw new ConfigurationException("Keyspace already exists.");
+        if (!Migration.isLegalName(newName))
+            throw new ConfigurationException("Invalid keyspace name: " + newName);
         
         // clone the ksm, replacing thename.
         KSMetaData newKsm = rename(oldKsm, newName, false); 

Modified: cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/service/StorageService.java Thu Aug 12 21:39:26
2010
@@ -1669,6 +1669,17 @@ public class StorageService implements I
      */
     public void loadSchemaFromYAML() throws ConfigurationException, IOException
     { 
+        // validate
+        final Collection<KSMetaData> tables = DatabaseDescriptor.readTablesFromYaml();
+        for (KSMetaData table : tables)
+        {
+            if (!table.name.matches(Migration.NAME_VALIDATOR_REGEX))
+                throw new ConfigurationException("Invalid table name: " + table.name);
+            for (CFMetaData cfm : table.cfMetaData().values())
+                if (!Migration.isLegalName(cfm.cfName))
+                    throw new ConfigurationException("Invalid column family name: " + cfm.cfName);
+        }
+        
         Callable<Migration> call = new Callable<Migration>()
         {
             public Migration call() throws Exception
@@ -1676,8 +1687,6 @@ public class StorageService implements I
                 // blow up if there is a schema saved.
                 if (DatabaseDescriptor.getDefsVersion().timestamp() > 0 || Migration.getLastMigrationId()
!= null)
                     throw new ConfigurationException("Cannot load from XML on top of pre-existing
schemas.");
-                
-                Collection<KSMetaData> tables = DatabaseDescriptor.readTablesFromYaml();
              
                 // cycle through first to make sure we can satisfy live nodes constraint.
                 int liveNodeCount = getLiveNodes().size();

Modified: cassandra/trunk/test/system/test_thrift_server.py
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/system/test_thrift_server.py?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/test/system/test_thrift_server.py (original)
+++ cassandra/trunk/test/system/test_thrift_server.py Thu Aug 12 21:39:26 2010
@@ -1124,6 +1124,33 @@ class TestMutations(ThriftTester):
 
     def test_describe_ring(self):
         assert list(client.describe_ring('Keyspace1'))[0].endpoints == ['127.0.0.1']
+        
+    def test_invalid_ks_names(self):
+        def invalid_keyspace():
+            client.system_add_keyspace(KsDef('in-valid', 'org.apache.cassandra.locator.RackUnawareStrategy',
{}, 1, []))
+        _expect_exception(invalid_keyspace, InvalidRequestException)
+        
+        def invalid_rename():
+            client.system_rename_keyspace('Keyspace1', 'in-valid')
+        _expect_exception(invalid_rename, InvalidRequestException)
+        
+    def test_invalid_cf_names(self):
+        def invalid_cf():
+            _set_keyspace('Keyspace1')
+            newcf = CfDef('Keyspace1', 'in-valid')
+            client.system_add_column_family(newcf)
+        _expect_exception(invalid_cf, InvalidRequestException)
+        
+        def invalid_cf_inside_new_ks():
+            cf = CfDef('ValidKsName_invalid_cf', 'in-valid')
+            _set_keyspace('system')
+            client.system_add_keyspace(KsDef('ValidKsName_invalid_cf', 'org.apache.cassandra.locator.RackUnawareStrategy',
{}, 1, [cf]))
+        _expect_exception(invalid_cf_inside_new_ks, InvalidRequestException)
+        
+        def invalid_rename():
+            _set_keyspace('Keyspace1')
+            client.system_rename_column_family('Standard1', 'in-validcf')
+        _expect_exception(invalid_rename, InvalidRequestException)
     
     def test_system_keyspace_operations(self):
         """ Test keyspace (add, drop, rename) operations """

Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java?rev=984995&r1=984994&r2=984995&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/db/DefsTest.java Thu Aug 12 21:39:26 2010
@@ -50,6 +50,18 @@ public class DefsTest extends CleanupHel
     }
     
     @Test
+    public void testInvalidNames() throws IOException
+    {
+        String[] valid = {"1", "a", "_1", "b_", "__", "1_a"};
+        for (String s : valid)
+            assert Migration.isLegalName(s);
+        
+        String[] invalid = {"b@t", "dash-y", "", " ", "dot.s", ".hidden"};
+        for (String s : invalid)
+            assert !Migration.isLegalName(s);
+    }
+    
+    @Test
     public void saveAndRestore() throws IOException
     {
         // verify dump and reload.



Mime
View raw message