Changeset 102

Show
Ignore:
Timestamp:
11/19/07 22:34:45 (1 year ago)
Author:
edsuom
Message:

Module-level function 'parallow' can be exposed to remote PB peers to set instances (of just Parameterized or subclasses, ideally) to let past PB's security gate

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/params.py

    r101 r102  
    2525""" 
    2626 
    27 from twisted.spread import pb 
    28 from twisted.spread.jelly import Jellyable, Unjellyable 
     27from twisted.spread import pb, jelly 
     28from twisted.python.reflect import namedObject 
    2929 
    3030 
    31 class Parameterized(Jellyable, Unjellyable, object): 
     31def parallow(instances): 
    3232    """ 
     33    Call this function with one or more arguments containing standard string 
     34    representations of L{Parameterized} subclasses, and instances of those will 
     35    be allowed past PB security. Use judiciously! 
     36    """ 
     37    for stringRep in instances: 
     38        cls = namedObject(stringRep) 
     39        pb.globalSecurity.allowInstancesOf(cls) 
     40 
     41 
     42class Parameterized(jelly.Jellyable, jelly.Unjellyable, object): 
     43    """ 
     44    @cvar instances: A list of standard string representations of all instances 
     45      of me or my subclasses. Your remote PB peer should expose L{parallow} to 
     46      you so you can call it remotely with this list to allow those instances. 
     47     
    3348    @cvar keyAttrs: A dict of attributes on which cache keying will be 
    3449      partially based. Any entry having a value of C{None} has no default, and 
     
    4055 
    4156    """ 
     57    instances = [] 
    4258    paramNames, keyAttrs = [], {} 
    4359 
     
    5672        for name, value in kw.iteritems(): 
    5773            setattr(self, name, value) 
     74        myClass = self.__class__ 
     75        stringRep = "%s.%s" % (myClass.__module__, myClass.__name__) 
     76        if stringRep not in self.instances: 
     77            self.instances.append(stringRep) 
     78            # Make sure you can accept copies of me coming back, too. 
     79            pb.globalSecurity.allowInstancesOf(myClass) 
    5880 
    5981    def __getattr__(self, name): 
     
    128150                if hasattr(self, name): 
    129151                    state[name] = getattr(self, name) 
    130         myClass = self.__class__ 
    131         pb.globalSecurity.allowInstancesOf(myClass) 
    132         pb.globalSecurity.allowModules(myClass.__module__) 
    133152        return state 
    134153 
    135154 
     155 
     156