[CalendarServer-changes] [7327] CalendarServer/trunk/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Tue Apr 19 15:23:16 PDT 2011


Revision: 7327
          http://trac.macosforge.org/projects/calendarserver/changeset/7327
Author:   sagen at apple.com
Date:     2011-04-19 15:23:15 -0700 (Tue, 19 Apr 2011)
Log Message:
-----------
Preserve passwords within config across reloads because they can't be re-fetched from Keychain after the processes have shed privileges.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/config.py
    CalendarServer/trunk/twistedcaldav/stdconfig.py
    CalendarServer/trunk/twistedcaldav/test/test_config.py

Modified: CalendarServer/trunk/twistedcaldav/config.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/config.py	2011-04-19 20:43:41 UTC (rev 7326)
+++ CalendarServer/trunk/twistedcaldav/config.py	2011-04-19 22:23:15 UTC (rev 7327)
@@ -135,6 +135,8 @@
         else:
             self._provider = provider
         self._updating = False
+        self._beforeResetHook = None
+        self._afterResetHook = None
         self._preUpdateHooks = []
         self._postUpdateHooks = []
         self.reset()
@@ -179,15 +181,25 @@
             lastDict[configItem] = defaultValue
             return defaultValue
 
+    def addResetHooks(self, before, after):
+        """
+        Hooks for preserving config across reload( ) + reset( )
+
+        Each hook will be passed the config data; whatever the before hook
+        returns will be passed as the second arg to the after hook.
+        """
+        self._beforeResetHook = before
+        self._afterResetHook = after
+
     def addPreUpdateHooks(self, hooks):
         self._preUpdateHooks.extend(hooks)
-        
+
     def addPostUpdateHooks(self, hooks):
         self._postUpdateHooks.extend(hooks)
 
     def getProvider(self):
         return self._provider
-    
+
     def setProvider(self, provider):
         self._provider = provider
         self.reset()
@@ -231,7 +243,16 @@
         configDict = ConfigDict(self._provider.loadConfig())
         configDict._reloading = True
         if not self._provider.hasErrors():
+            if self._beforeResetHook:
+                # Give the beforeResetHook a chance to stash away values we want
+                # to preserve across the reload( )
+                preserved = self._beforeResetHook(self._data)
+            else:
+                preserved = None
             self.reset()
+            if preserved and self._afterResetHook:
+                # Pass the preserved data back to the afterResetHook
+                self._afterResetHook(self._data, preserved)
             self.update(configDict)
         else:
             raise ConfigurationError("Invalid configuration in %s"

Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/stdconfig.py	2011-04-19 20:43:41 UTC (rev 7326)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py	2011-04-19 22:23:15 UTC (rev 7327)
@@ -1217,3 +1217,34 @@
 config.setProvider(PListConfigProvider(DEFAULT_CONFIG))
 config.addPreUpdateHooks(PRE_UPDATE_HOOKS)
 config.addPostUpdateHooks(POST_UPDATE_HOOKS)
+
+
+def _preserveConfig(configDict):
+    """
+    Preserve certain config keys across reset( ) because these can't be
+    re-fetched after the process has shed privileges
+    """
+    iMIP = configDict.Scheduling.iMIP
+    XMPP = configDict.Notifications.Services.XMPPNotifier
+    preserved = {
+        "iMIPPassword" : iMIP.Password,
+        "MailSendingPassword" : iMIP.Sending.Password,
+        "MailReceivingPassword" : iMIP.Receiving.Password,
+        "XMPPPassword" : XMPP.Password,
+    }
+    return preserved
+
+def _restoreConfig(configDict, preserved):
+    """
+    Restore certain config keys across reset( ) because these can't be
+    re-fetched after the process has shed privileges
+    """
+    iMIP = configDict.Scheduling.iMIP
+    XMPP = configDict.Notifications.Services.XMPPNotifier
+    iMIP.Password = preserved["iMIPPassword"]
+    iMIP.Sending.Password = preserved["MailSendingPassword"]
+    iMIP.Receiving.Password = preserved["MailReceivingPassword"]
+    XMPP.Password = preserved["XMPPPassword"]
+
+
+config.addResetHooks(_preserveConfig, _restoreConfig)

Modified: CalendarServer/trunk/twistedcaldav/test/test_config.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_config.py	2011-04-19 20:43:41 UTC (rev 7326)
+++ CalendarServer/trunk/twistedcaldav/test/test_config.py	2011-04-19 22:23:15 UTC (rev 7327)
@@ -42,6 +42,37 @@
     <string>debug</string>
   </dict>
 
+  <key>Notifications</key>
+  <dict>
+    <key>Services</key>
+    <dict>
+      <key>XMPPNotifier</key>
+      <dict>
+          <key>Password</key>
+          <string>xmpp</string>
+      </dict>
+    </dict>
+  </dict>
+
+  <key>Scheduling</key>
+  <dict>
+    <key>iMIP</key>
+    <dict>
+      <key>Password</key>
+      <string>imip</string>
+      <key>Sending</key>
+      <dict>
+          <key>Password</key>
+          <string>sending</string>
+      </dict>
+      <key>Receiving</key>
+      <dict>
+          <key>Password</key>
+          <string>receiving</string>
+      </dict>
+    </dict>
+  </dict>
+
 </dict>
 </plist>
 """
@@ -126,6 +157,28 @@
 
         self.assertEquals(config.HTTPPort, 8008)
 
+    def testPreserveAcrossReload(self):
+        self.assertEquals(config.Scheduling.iMIP.Password, "")
+        self.assertEquals(config.Scheduling.iMIP.Sending.Password, "")
+        self.assertEquals(config.Scheduling.iMIP.Receiving.Password, "")
+        self.assertEquals(config.Notifications.Services.XMPPNotifier.Password, "")
+
+        config.load(self.testConfig)
+
+        self.assertEquals(config.Scheduling.iMIP.Password, "imip")
+        self.assertEquals(config.Scheduling.iMIP.Sending.Password, "sending")
+        self.assertEquals(config.Scheduling.iMIP.Receiving.Password, "receiving")
+        self.assertEquals(config.Notifications.Services.XMPPNotifier.Password, "xmpp")
+
+        writePlist({}, self.testConfig)
+
+        config.reload()
+
+        self.assertEquals(config.Scheduling.iMIP.Password, "imip")
+        self.assertEquals(config.Scheduling.iMIP.Sending.Password, "sending")
+        self.assertEquals(config.Scheduling.iMIP.Receiving.Password, "receiving")
+        self.assertEquals(config.Notifications.Services.XMPPNotifier.Password, "xmpp")
+
     def testSetAttr(self):
         self.assertNotIn("BindAddresses", config.__dict__)
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110419/1310aa5b/attachment.html>


More information about the calendarserver-changes mailing list