[Checkins] SVN: GenericSetup/ Fixed an issue where if you ran the import steps with a context id that was neither 'profile-' nor 'snapshot-' it would default to try to run a snapshot (actually trying to do a string manipulation as if the context id had started with 'snapshot-'). Quite harmful.

Malthe Borch mborch at gmail.com
Fri Nov 23 13:15:53 EST 2007


Log message for revision 81968:
  Fixed an issue where if you ran the import steps with a context id that was neither 'profile-' nor 'snapshot-' it would default to try to run a snapshot (actually trying to do a string manipulation as if the context id had started with 'snapshot-'). Quite harmful.

Changed:
  U   GenericSetup/branches/1.2/tests/test_tool.py
  U   GenericSetup/branches/1.2/tool.py
  U   GenericSetup/branches/1.3/tests/test_tool.py
  U   GenericSetup/branches/1.3/tool.py
  U   GenericSetup/trunk/tests/test_tool.py
  U   GenericSetup/trunk/tool.py

-=-
Modified: GenericSetup/branches/1.2/tests/test_tool.py
===================================================================
--- GenericSetup/branches/1.2/tests/test_tool.py	2007-11-23 18:03:11 UTC (rev 81967)
+++ GenericSetup/branches/1.2/tests/test_tool.py	2007-11-23 18:15:52 UTC (rev 81968)
@@ -132,6 +132,15 @@
                          , 'profile-foo'
                          )
 
+    def test_setBaselineContext_invalidFormat( self ):
+
+        tool = self._makeOne('setup_tool')
+
+        self.assertRaises( KeyError
+                         , tool.setImportContext
+                         , 'profile/default'
+                         )
+
     def test_setImportContext( self ):
 
         from Products.GenericSetup.tool import IMPORT_STEPS_XML

Modified: GenericSetup/branches/1.2/tool.py
===================================================================
--- GenericSetup/branches/1.2/tool.py	2007-11-23 18:03:11 UTC (rev 81967)
+++ GenericSetup/branches/1.2/tool.py	2007-11-23 18:15:52 UTC (rev 81968)
@@ -31,12 +31,14 @@
 from interfaces import EXTENSION
 from interfaces import ISetupTool
 from interfaces import SKIPPED_FILES
+from interfaces import IImportContext
 from permissions import ManagePortal
 from context import DirectoryImportContext
 from context import SnapshotImportContext
 from context import TarballExportContext
 from context import TarballImportContext
 from context import SnapshotExportContext
+from context import BaseContext
 from differ import ConfigDiff
 from registry import ImportStepRegistry
 from registry import ExportStepRegistry
@@ -639,8 +641,16 @@
         """
         encoding = self.getEncoding()
 
+        if context_id == '':
+            context = BaseContext(self, encoding)
+
+            # default is purge
+            if should_purge == False:
+                context._should_purge = False
+
+            return context
+
         if context_id.startswith('profile-'):
-
             context_id = context_id[len('profile-'):]
             info = _profile_registry.getProfileInfo(context_id)
 
@@ -653,12 +663,14 @@
                 should_purge = (info.get('type') != EXTENSION)
             return DirectoryImportContext(self, path, should_purge, encoding)
 
-        # else snapshot
-        context_id = context_id[len('snapshot-'):]
-        if should_purge is None:
-            should_purge = True
-        return SnapshotImportContext(self, context_id, should_purge, encoding)
+        if context_id.startswith('snapshot-'):
+            context_id = context_id[len('snapshot-'):]
+            if should_purge is None:
+                should_purge = True
+            return SnapshotImportContext(self, context_id, should_purge, encoding)
 
+        raise KeyError, "Context id must be either profile or snapshot (%s)." % context_id
+
     security.declarePrivate('_updateImportStepsRegistry')
     def _updateImportStepsRegistry(self, context, encoding):
 
@@ -666,6 +678,11 @@
         """
         if context is None:
             context = self._getImportContext(self._import_context_id)
+
+        # assert that this context provides the import interface
+        if not IImportContext.providedBy(context):
+            return
+
         xml = context.readDataFile(IMPORT_STEPS_XML)
         if xml is None:
             return
@@ -697,6 +714,11 @@
         """
         if context is None:
             context = self._getImportContext(self._import_context_id)
+
+        # assert that this context provides the import interface
+        if not IImportContext.providedBy(context):
+            return
+
         xml = context.readDataFile(EXPORT_STEPS_XML)
         if xml is None:
             return

Modified: GenericSetup/branches/1.3/tests/test_tool.py
===================================================================
--- GenericSetup/branches/1.3/tests/test_tool.py	2007-11-23 18:03:11 UTC (rev 81967)
+++ GenericSetup/branches/1.3/tests/test_tool.py	2007-11-23 18:15:52 UTC (rev 81968)
@@ -119,6 +119,15 @@
                          , 'profile-foo'
                          )
 
+    def test_setBaselineContext_invalidFormat( self ):
+
+        tool = self._makeOne('setup_tool')
+
+        self.assertRaises( KeyError
+                         , tool.setBaselineContext
+                         , 'profile/default'
+                         )
+
     def test_setBaselineContext( self ):
 
         from Products.GenericSetup.tool import IMPORT_STEPS_XML
@@ -317,7 +326,7 @@
     def test_runAllImportSteps_sorted_default_purge( self ):
 
         TITLE = 'original title'
-        PROFILE_ID = 'testing'
+        PROFILE_ID = 'snapshot-testing'
         site = self._makeSite( TITLE )
         tool = self._makeOne('setup_tool').__of__( site )
 
@@ -355,7 +364,7 @@
     def test_runAllImportSteps_unicode_profile_id_creates_reports( self ):
 
         TITLE = 'original title'
-        PROFILE_ID = u'testing'
+        PROFILE_ID = u'snapshot-testing'
         site = self._makeSite( TITLE )
         tool = self._makeOne('setup_tool').__of__( site )
 

Modified: GenericSetup/branches/1.3/tool.py
===================================================================
--- GenericSetup/branches/1.3/tool.py	2007-11-23 18:03:11 UTC (rev 81967)
+++ GenericSetup/branches/1.3/tool.py	2007-11-23 18:15:52 UTC (rev 81968)
@@ -35,12 +35,14 @@
 from interfaces import EXTENSION
 from interfaces import ISetupTool
 from interfaces import SKIPPED_FILES
+from interfaces import IImportContext
 from permissions import ManagePortal
 from context import DirectoryImportContext
 from context import SnapshotImportContext
 from context import TarballExportContext
 from context import TarballImportContext
 from context import SnapshotExportContext
+from context import BaseContext
 from differ import ConfigDiff
 from registry import ImportStepRegistry
 from registry import ExportStepRegistry
@@ -887,8 +889,16 @@
         """
         encoding = self.getEncoding()
 
+        if context_id == '':
+            context = BaseContext(self, encoding)
+
+            # default is purge
+            if should_purge == False:
+                context._should_purge = False
+
+            return context
+
         if context_id.startswith('profile-'):
-
             context_id = context_id[len('profile-'):]
             info = _profile_registry.getProfileInfo(context_id)
 
@@ -901,12 +911,14 @@
                 should_purge = (info.get('type') != EXTENSION)
             return DirectoryImportContext(self, path, should_purge, encoding)
 
-        # else snapshot
-        context_id = context_id[len('snapshot-'):]
-        if should_purge is None:
-            should_purge = True
-        return SnapshotImportContext(self, context_id, should_purge, encoding)
+        if context_id.startswith('snapshot-'):
+            context_id = context_id[len('snapshot-'):]
+            if should_purge is None:
+                should_purge = True
+            return SnapshotImportContext(self, context_id, should_purge, encoding)
 
+        raise KeyError, "Context id must be either profile or snapshot (%s)." % context_id
+
     security.declarePrivate('_updateImportStepsRegistry')
     def _updateImportStepsRegistry(self, context, encoding):
 
@@ -914,6 +926,11 @@
         """
         if context is None:
             context = self._getImportContext(self._import_context_id)
+
+        # assert that this context provides the import interface
+        if not IImportContext.providedBy(context):
+            return
+
         xml = context.readDataFile(IMPORT_STEPS_XML)
         if xml is None:
             return
@@ -944,6 +961,11 @@
         """
         if context is None:
             context = self._getImportContext(self._import_context_id)
+
+        # assert that this context provides the import interface
+        if not IImportContext.providedBy(context):
+            return
+
         xml = context.readDataFile(EXPORT_STEPS_XML)
         if xml is None:
             return

Modified: GenericSetup/trunk/tests/test_tool.py
===================================================================
--- GenericSetup/trunk/tests/test_tool.py	2007-11-23 18:03:11 UTC (rev 81967)
+++ GenericSetup/trunk/tests/test_tool.py	2007-11-23 18:15:52 UTC (rev 81968)
@@ -141,6 +141,15 @@
                          , 'profile-foo'
                          )
 
+    def test_setBaselineContext_invalidFormat( self ):
+
+        tool = self._makeOne('setup_tool')
+
+        self.assertRaises( KeyError
+                         , tool.setBaselineContext
+                         , 'profile/default'
+                         )
+
     def test_setBaselineContext( self ):
 
         from Products.GenericSetup.tool import IMPORT_STEPS_XML
@@ -376,7 +385,7 @@
     def test_runAllImportSteps_sorted_default_purge( self ):
 
         TITLE = 'original title'
-        PROFILE_ID = 'testing'
+        PROFILE_ID = 'snapshot-testing'
         site = self._makeSite( TITLE )
         tool = self._makeOne('setup_tool').__of__( site )
 
@@ -414,7 +423,7 @@
     def test_runAllImportSteps_unicode_profile_id_creates_reports( self ):
 
         TITLE = 'original title'
-        PROFILE_ID = u'testing'
+        PROFILE_ID = u'snapshot-testing'
         site = self._makeSite( TITLE )
         tool = self._makeOne('setup_tool').__of__( site )
 

Modified: GenericSetup/trunk/tool.py
===================================================================
--- GenericSetup/trunk/tool.py	2007-11-23 18:03:11 UTC (rev 81967)
+++ GenericSetup/trunk/tool.py	2007-11-23 18:15:52 UTC (rev 81968)
@@ -36,6 +36,7 @@
 from interfaces import EXTENSION
 from interfaces import ISetupTool
 from interfaces import SKIPPED_FILES
+from interfaces import IImportContext
 from permissions import ManagePortal
 from events import BeforeProfileImportEvent
 from events import ProfileImportedEvent
@@ -44,6 +45,7 @@
 from context import TarballExportContext
 from context import TarballImportContext
 from context import SnapshotExportContext
+from context import BaseContext
 from differ import ConfigDiff
 from registry import ImportStepRegistry
 from registry import ExportStepRegistry
@@ -309,10 +311,11 @@
                                  run_dependencies=True, purge_old=None):
         """ See ISetupTool.
         """
+
         old_context = self._import_context_id
         context = self._getImportContext(profile_id, purge_old)
 
-        self.applyContext(context)
+        #self.applyContext(context)
 
         info = self.getImportStepMetadata(step_id)
 
@@ -940,8 +943,16 @@
         """
         encoding = self.getEncoding()
 
+        if context_id == '':
+            context = BaseContext(self, encoding)
+
+            # default is purge
+            if should_purge == False:
+                context._should_purge = False
+
+            return context
+        
         if context_id.startswith('profile-'):
-
             context_id = context_id[len('profile-'):]
             info = _profile_registry.getProfileInfo(context_id)
 
@@ -954,12 +965,14 @@
                 should_purge = (info.get('type') != EXTENSION)
             return DirectoryImportContext(self, path, should_purge, encoding)
 
-        # else snapshot
-        context_id = context_id[len('snapshot-'):]
-        if should_purge is None:
-            should_purge = True
-        return SnapshotImportContext(self, context_id, should_purge, encoding)
+        if context_id.startswith('snapshot-'):
+            context_id = context_id[len('snapshot-'):]
+            if should_purge is None:
+                should_purge = True
+            return SnapshotImportContext(self, context_id, should_purge, encoding)
 
+        raise KeyError, "Context id must be either profile or snapshot (%s)." % context_id
+
     security.declarePrivate('_updateImportStepsRegistry')
     def _updateImportStepsRegistry(self, context, encoding):
 
@@ -967,6 +980,11 @@
         """
         if context is None:
             context = self._getImportContext(self._import_context_id)
+
+        # assert that this context provides the import interface
+        if not IImportContext.providedBy(context):
+            return
+
         xml = context.readDataFile(IMPORT_STEPS_XML)
         if xml is None:
             return
@@ -997,6 +1015,11 @@
         """
         if context is None:
             context = self._getImportContext(self._import_context_id)
+
+        # assert that this context provides the import interface
+        if not IImportContext.providedBy(context):
+            return
+
         xml = context.readDataFile(EXPORT_STEPS_XML)
         if xml is None:
             return



More information about the Checkins mailing list